import optparse
import platform
import yaml
+import time
from UcsSdk import *
from collections import defaultdict
if server.Type == 'instance' and "POD-2" in server.Dn:
yield server
+
+def ack_pending(handle=None, server=None):
+ """
+ Acknowledge pending state of server
+ """
+ handle.AddManagedObject(server, LsmaintAck.ClassId(), {LsmaintAck.DN: server.Dn + "/ack", LsmaintAck.DESCR:"", LsmaintAck.ADMIN_STATE:"trigger-immediate", LsmaintAck.SCHEDULER:"", LsmaintAck.POLICY_OWNER:"local"}, True)
+
+
def get_vnics(handle=None, server=None):
"""
Return list of vnics for given server
Print current network config
"""
print "\nCURRENT NETWORK CONFIG:"
+ print " d - default, t - tagged"
for server in get_servers(handle):
print ' {}'.format(server.Name)
for vnic in get_vnics(handle, server):
print ' {}'.format(vnic.Addr)
vnicIfs = handle.ConfigResolveChildren(VnicEtherIf.ClassId(), vnic.Dn, None, YesOrNo.TRUE)
for vnicIf in vnicIfs.OutConfigs.GetChild():
- print ' Vlan: {}'.format(vnicIf.Vnet)
+ if vnicIf.DefaultNet == 'yes':
+ print ' Vlan: {}d'.format(vnicIf.Vnet)
+ else:
+ print ' Vlan: {}t'.format(vnicIf.Vnet)
-def add_interface(handle=None, lsServerDn=None, vnicEther=None, templName=None, order=None):
+def add_interface(handle=None, lsServerDn=None, vnicEther=None, templName=None, order=None, macAddr=None):
"""
Add interface to server specified by server.DN name
"""
VnicEther.ORDER: order,
"adminHostPort": "ANY",
VnicEther.ADMIN_VCON: "any",
+ VnicEther.ADDR: macAddr,
VnicEther.NW_TEMPL_NAME: templName,
VnicEther.MTU: "1500"}
handle.AddManagedObject(obj, VnicEther.ClassId(), params, True)
Configure VLANs on POD according specified network
"""
# add interfaces and bind them with vNIC templates
- # TODO: make sure MAC address for admin is still same
print "\nRECONFIGURING VNICs..."
network = read_yaml_file(yamlFile)
- for server in get_servers(handle):
+ for index, server in enumerate(get_servers(handle)):
for iface, data in network.iteritems():
- add_interface(handle, server.Dn, iface, data['template'], data['order'])
+ add_interface(handle, server.Dn, iface, data['template'], data['order'], data['mac-list'][index])
# Remove other interfaces which have not assigned required vnic template
vnics = get_vnics(handle, server)
for vnic in vnics:
handle.Login(options.ip, options.userName, options.password)
+ # Change vnic template if specified in cli option
if (options.yamlFile != None):
- print options.yamlFile
set_network(handle, options.yamlFile)
+
+ time.sleep(3)
+
+ print "\nCheck if acknowledge is needed..."
+ for server in get_servers(handle):
+ print " {}: {}".format(server.Dn, server.OperState)
+ if server.OperState == "pending-reboot":
+ ack_pending(handle,server)
+ print " Acknowledged."
+
+ print "\nWait until Overall Status of all nodes is OK..."
+ timeout = time.time() + 60*5 #5 minutes timeout
+ while True:
+ list_of_states = []
+ for server in get_servers(handle):
+ list_of_states.append(server.OperState)
+ print " {}".format(list_of_states)
+ if all(state == "ok" for state in list_of_states) or time.time() > timeout:
+ break
+ time.sleep(2)
+
+ # Show current vnic MACs and VLANs
get_network_config(handle)
handle.Logout()