3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 # This script reconfigure UCSM vnics for varios OPNFV deployers
15 # Usage: reconfigUcsNet.py [options]
18 # -h, --help show this help message and exit
19 # -i IP, --ip=IP [Mandatory] UCSM IP Address
20 # -u USERNAME, --username=USERNAME
21 # [Mandatory] Account Username for UCSM Login
22 # -p PASSWORD, --password=PASSWORD
23 # [Mandatory] Account Password for UCSM Login
24 # -f FILE, --file=FILE
25 # [Optional] Yaml file with network config you want to set for POD
26 # If not present only current network config will be printed
35 from collections import defaultdict
38 def getpassword(prompt):
39 if platform.system() == "Linux":
40 return getpass.unix_getpass(prompt=prompt)
41 elif platform.system() == "Windows" or platform.system() == "Microsoft":
42 return getpass.win_getpass(prompt=prompt)
44 return getpass.getpass(prompt=prompt)
47 def get_servers(handle=None):
49 Return list of servers
51 orgObj = handle.GetManagedObject(None, OrgOrg.ClassId(), {OrgOrg.DN : "org-root"})[0]
52 servers = handle.GetManagedObject(orgObj, LsServer.ClassId())
53 for server in servers:
54 if server.Type == 'instance' and "POD-2" in server.Dn:
58 def ack_pending(handle=None, server=None):
60 Acknowledge pending state of server
62 handle.AddManagedObject(server, LsmaintAck.ClassId(), {LsmaintAck.DN: server.Dn + "/ack", LsmaintAck.DESCR:"", LsmaintAck.ADMIN_STATE:"trigger-immediate", LsmaintAck.SCHEDULER:"", LsmaintAck.POLICY_OWNER:"local"}, True)
65 def get_vnics(handle=None, server=None):
67 Return list of vnics for given server
69 vnics = handle.ConfigResolveChildren(VnicEther.ClassId(), server.Dn, None, YesOrNo.TRUE)
70 return vnics.OutConfigs.GetChild()
73 def get_network_config(handle=None):
75 Print current network config
77 print "\nCURRENT NETWORK CONFIG:"
78 print " d - default, t - tagged"
79 for server in get_servers(handle):
80 print ' {}'.format(server.Name)
81 for vnic in get_vnics(handle, server):
82 print ' {}'.format(vnic.Name)
83 print ' {}'.format(vnic.Addr)
84 vnicIfs = handle.ConfigResolveChildren(VnicEtherIf.ClassId(), vnic.Dn, None, YesOrNo.TRUE)
85 for vnicIf in vnicIfs.OutConfigs.GetChild():
86 if vnicIf.DefaultNet == 'yes':
87 print ' Vlan: {}d'.format(vnicIf.Vnet)
89 print ' Vlan: {}t'.format(vnicIf.Vnet)
92 def add_interface(handle=None, lsServerDn=None, vnicEther=None, templName=None, order=None, macAddr=None):
94 Add interface to server specified by server.DN name
96 print " Adding interface: {}, template: {}, server.Dn: {}".format(vnicEther, templName, lsServerDn)
97 obj = handle.GetManagedObject(None, LsServer.ClassId(), {LsServer.DN:lsServerDn})
98 vnicEtherDn = lsServerDn + "/ether-" + vnicEther
100 VnicEther.STATS_POLICY_NAME: "default",
101 VnicEther.NAME: vnicEther,
102 VnicEther.DN: vnicEtherDn,
103 VnicEther.SWITCH_ID: "A-B",
104 VnicEther.ORDER: order,
105 "adminHostPort": "ANY",
106 VnicEther.ADMIN_VCON: "any",
107 VnicEther.ADDR: macAddr,
108 VnicEther.NW_TEMPL_NAME: templName,
109 VnicEther.MTU: "1500"}
110 handle.AddManagedObject(obj, VnicEther.ClassId(), params, True)
113 def remove_interface(handle=None, vnicEtherDn=None):
115 Remove interface specified by Distinguished Name (vnicEtherDn)
117 print " Removing interface: {}".format(vnicEtherDn)
118 obj = handle.GetManagedObject(None, VnicEther.ClassId(), {VnicEther.DN:vnicEtherDn})
119 handle.RemoveManagedObject(obj)
122 def read_yaml_file(yamlFile):
124 Read vnic config from yaml file
126 # TODO: add check if vnic templates specified in file exist on UCS
127 with open(yamlFile, 'r') as stream:
128 return yaml.load(stream)
131 def set_network(handle=None, yamlFile=None):
133 Configure VLANs on POD according specified network
135 # add interfaces and bind them with vNIC templates
136 print "\nRECONFIGURING VNICs..."
137 network = read_yaml_file(yamlFile)
138 for index, server in enumerate(get_servers(handle)):
139 for iface, data in network.iteritems():
140 add_interface(handle, server.Dn, iface, data['template'], data['order'], data['mac-list'][index])
141 # Remove other interfaces which have not assigned required vnic template
142 vnics = get_vnics(handle, server)
144 if not any(data['template'] in vnic.OperNwTemplName for iface, data in network.iteritems()):
145 remove_interface(handle, vnic.Dn)
146 print " {} removed, template: {}".format(vnic.Name, vnic.OperNwTemplName)
149 if __name__ == "__main__":
150 # Latest urllib2 validate certs by default
151 # The process wide "revert to the old behaviour" hook is to monkeypatch the ssl module
152 # https://bugs.python.org/issue22417
154 if hasattr(ssl, '_create_unverified_context'):
155 ssl._create_default_https_context = ssl._create_unverified_context
158 parser = optparse.OptionParser()
159 parser.add_option('-i', '--ip',dest="ip",
160 help="[Mandatory] UCSM IP Address")
161 parser.add_option('-u', '--username',dest="userName",
162 help="[Mandatory] Account Username for UCSM Login")
163 parser.add_option('-p', '--password',dest="password",
164 help="[Mandatory] Account Password for UCSM Login")
165 parser.add_option('-f', '--file',dest="yamlFile",
166 help="[Optional] Yaml file contains network config you want to set on UCS POD1")
167 (options, args) = parser.parse_args()
171 parser.error("Provide UCSM IP Address")
172 if not options.userName:
174 parser.error("Provide UCSM UserName")
175 if not options.password:
176 options.password=getpassword("UCSM Password:")
178 handle.Login(options.ip, options.userName, options.password)
180 # Change vnic template if specified in cli option
181 if (options.yamlFile != None):
182 set_network(handle, options.yamlFile)
186 print "\nCheck if acknowledge is needed..."
187 for server in get_servers(handle):
188 print " {}: {}".format(server.Dn, server.OperState)
189 if server.OperState == "pending-reboot":
190 ack_pending(handle,server)
191 print " Acknowledged."
193 print "\nWait until Overall Status of all nodes is OK..."
194 timeout = time.time() + 60*5 #5 minutes timeout
197 for server in get_servers(handle):
198 list_of_states.append(server.OperState)
199 print " {}".format(list_of_states)
200 if all(state == "ok" for state in list_of_states) or time.time() > timeout:
204 # Show current vnic MACs and VLANs
205 get_network_config(handle)
209 except Exception, err:
211 print "Exception:", str(err)
212 import traceback, sys
214 traceback.print_exc(file=sys.stdout)