Test to skip lab-reconfig.
[releng.git] / utils / lab-reconfiguration / reconfigUcsNet.py
index 8adace0..45a72a3 100755 (executable)
@@ -30,9 +30,13 @@ import getpass
 import optparse
 import platform
 import yaml
+import time
+import sys
 from UcsSdk import *
 from collections import defaultdict
 
+POD_PREFIX = "POD-2"
+INSTALLER = "POD-21"
 
 def getpassword(prompt):
     if platform.system() == "Linux":
@@ -50,9 +54,45 @@ def get_servers(handle=None):
     orgObj = handle.GetManagedObject(None, OrgOrg.ClassId(), {OrgOrg.DN : "org-root"})[0]
     servers = handle.GetManagedObject(orgObj, LsServer.ClassId())
     for server in servers:
-        if server.Type == 'instance' and "POD-2" in server.Dn:
+        if server.Type == 'instance' and POD_PREFIX in server.Dn:
             yield server
 
+
+def set_boot_policy(handle=None, server=None, policy=None):
+    """
+    Modify Boot policy of server
+    """
+    obj = handle.GetManagedObject(None, LsServer.ClassId(), {
+            LsServer.DN: server.Dn})
+    handle.SetManagedObject(obj, LsServer.ClassId(), {
+            LsServer.BOOT_POLICY_NAME: policy} )
+    print " Configured boot policy: {}".format(policy)
+
+
+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)
+    print " Pending-reboot -> Acknowledged."
+
+
+def boot_server(handle=None, server=None):
+    """
+    Boot server (when is in power-off state)
+    """
+    obj = handle.GetManagedObject(None, LsServer.ClassId(), {LsServer.DN: server.Dn})
+    handle.AddManagedObject(obj, LsPower.ClassId(), {
+            LsPower.DN: server.Dn + "/power",
+            LsPower.STATE:"admin-up"}, True)
+    print " Booting."
+
+
 def get_vnics(handle=None, server=None):
     """
     Return list of vnics for given server
@@ -66,17 +106,22 @@ def get_network_config(handle=None):
     Print current network config
     """
     print "\nCURRENT NETWORK CONFIG:"
+    print " d - default, t - tagged"
     for server in get_servers(handle):
         print ' {}'.format(server.Name)
+        print '  Boot policy: {}'.format(server.OperBootPolicyName)
         for vnic in get_vnics(handle, server):
             print '  {}'.format(vnic.Name)
             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
     """
@@ -91,6 +136,7 @@ def add_interface(handle=None, lsServerDn=None, vnicEther=None, templName=None,
         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)
@@ -119,12 +165,15 @@ def set_network(handle=None, yamlFile=None):
     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):
+    pod_data = read_yaml_file(yamlFile)
+    network = pod_data['network']
+
+    for index, server in enumerate(get_servers(handle)):
+        # Assign template to interface
         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:
@@ -132,8 +181,14 @@ def set_network(handle=None, yamlFile=None):
                 remove_interface(handle, vnic.Dn)
                 print "  {} removed, template: {}".format(vnic.Name, vnic.OperNwTemplName)
 
+        # Set boot policy template
+        if not INSTALLER in server.Dn:
+            set_boot_policy(handle, server, pod_data['boot-policy'])
+
 
 if __name__ == "__main__":
+    print "\n*** SKIPING RECONFIGURATION.***\n"
+    sys.exit(0)
     # Latest urllib2 validate certs by default
     # The process wide "revert to the old behaviour" hook is to monkeypatch the ssl module
     # https://bugs.python.org/issue22417
@@ -164,9 +219,29 @@ if __name__ == "__main__":
 
         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(5)
+
+        print "\nWait until Overall Status of all nodes is OK..."
+        timeout = time.time() + 60*10   #10 minutes timeout
+        while True:
+            list_of_states = []
+            for server in get_servers(handle):
+                if server.OperState == "power-off":
+                    boot_server(handle,server)
+                if server.OperState == "pending-reboot":
+                    ack_pending(handle,server)
+                list_of_states.append(server.OperState)
+            print " {}, {} seconds remains.".format(list_of_states, round(timeout-time.time()))
+            if all(state == "ok" for state in list_of_states):
+                break
+            if time.time() > timeout:
+                raise Exception("Timeout reached while waiting for OK status.")
+            time.sleep(10)
+
+        # Show current vnic MACs and VLANs
         get_network_config(handle)
 
         handle.Logout()
@@ -178,3 +253,4 @@ if __name__ == "__main__":
         print '-'*60
         traceback.print_exc(file=sys.stdout)
         print '-'*60
+        sys.exit(1)