no real change, just made prettier with a more standard layout - half of steps
[bootmanager.git] / source / steps / ValidateNodeInstall.py
index 4ae1ff8..a39ee30 100644 (file)
@@ -14,7 +14,7 @@ import systeminfo
 import ModelOptions
 
 
-def Run( vars, log ):
+def Run(vars, log):
     """
     See if a node installation is valid. More checks should certainly be
     done in the future, but for now, make sure that the sym links kernel-boot
@@ -31,37 +31,37 @@ def Run( vars, log ):
     ROOT_MOUNTED             the node root file system is mounted
     """
 
-    log.write( "\n\nStep: Validating node installation.\n" )
+    log.write("\n\nStep: Validating node installation.\n")
 
     # make sure we have the variables we need
     try:
-        SYSIMG_PATH= vars["SYSIMG_PATH"]
+        SYSIMG_PATH = vars["SYSIMG_PATH"]
         if SYSIMG_PATH == "":
-            raise ValueError, "SYSIMG_PATH"
+            raise ValueError("SYSIMG_PATH")
 
-        NODE_ID= vars["NODE_ID"]
+        NODE_ID = vars["NODE_ID"]
         if NODE_ID == "":
-            raise ValueError, "NODE_ID"
+            raise ValueError("NODE_ID")
 
-        PLCONF_DIR= vars["PLCONF_DIR"]
+        PLCONF_DIR = vars["PLCONF_DIR"]
         if PLCONF_DIR == "":
-            raise ValueError, "PLCONF_DIR"
+            raise ValueError("PLCONF_DIR")
         
-        NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
+        NODE_MODEL_OPTIONS = vars["NODE_MODEL_OPTIONS"]
 
-        PARTITIONS= vars["PARTITIONS"]
+        PARTITIONS = vars["PARTITIONS"]
         if PARTITIONS == None:
-            raise ValueError, "PARTITIONS"
+            raise ValueError("PARTITIONS")
 
-    except KeyError, var:
-        raise BootManagerException, "Missing variable in vars: %s\n" % var
-    except ValueError, var:
-        raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
+    except KeyError as var:
+        raise BootManagerException("Missing variable in vars: {}\n".format(var))
+    except ValueError as var:
+        raise BootManagerException("Variable in vars, shouldn't be: {}\n".format(var))
 
 
-    ROOT_MOUNTED= 0
+    ROOT_MOUNTED = 0
     if vars.has_key('ROOT_MOUNTED'):
-        ROOT_MOUNTED= vars['ROOT_MOUNTED']
+        ROOT_MOUNTED = vars['ROOT_MOUNTED']
 
     # mount the root system image if we haven't already.
     # capture BootManagerExceptions during the vgscan/change and mount
@@ -74,14 +74,14 @@ def Run( vars, log ):
         systeminfo.get_block_device_list(vars, log)
 
         try:
-            utils.sysexec( "vgscan", log )
-            utils.sysexec( "vgchange -ay planetlab", log )
-        except BootManagerException, e:
-            log.write( "BootManagerException during vgscan/vgchange: %s\n" %
-                       str(e) )
+            utils.sysexec("vgscan", log)
+            utils.sysexec("vgchange -ay planetlab", log)
+        except BootManagerException as e:
+            log.write("BootManagerException during vgscan/vgchange: {}\n"\
+                      .format(str(e)))
             return 0
             
-        utils.makedirs( SYSIMG_PATH )
+        utils.makedirs(SYSIMG_PATH)
 
         # xxx - TODO - need to fsck the btrfs partition
         if vars['virt'] == 'vs':
@@ -92,37 +92,38 @@ def Run( vars, log ):
         for filesystem in filesystems_tocheck:
             try:
                 # first run fsck to prevent fs corruption from hanging mount...
-                log.write( "fsck %s file system\n" % filesystem )
-                utils.sysexec("e2fsck -v -p %s" % (PARTITIONS[filesystem]), log, fsck=True)
-            except BootManagerException, e:
-                log.write( "BootManagerException during fsck of %s (%s) filesystem : %s\n" %
-                           (filesystem, PARTITIONS[filesystem], str(e)) )
+                log.write("fsck {} file system\n".format(filesystem))
+                utils.sysexec("e2fsck -v -p {}".format(PARTITIONS[filesystem]), log, fsck=True)
+            except BootManagerException as e:
+                log.write("BootManagerException during fsck of {} ({}) filesystem : {}\n"\
+                          .format(filesystem, PARTITIONS[filesystem], str(e)))
                 try:
-                    log.write( "Trying to recover filesystem errors on %s\n" % filesystem )
-                    utils.sysexec("e2fsck -v -y %s" % (PARTITIONS[filesystem]),log, fsck=True)
-                except BootManagerException, e:
-                    log.write( "BootManagerException during trying to recover filesystem errors on %s (%s) filesystem : %s\n" %
-                           (filesystem, PARTITIONS[filesystem], str(e)) )
+                    log.write("Trying to recover filesystem errors on {}\n".format(filesystem))
+                    utils.sysexec("e2fsck -v -y {}".format(PARTITIONS[filesystem]), log, fsck=True)
+                except BootManagerException as e:
+                    log.write("BootManagerException while trying to recover"
+                              "filesystem errors on {} ({}) filesystem : {}\n"
+                              .format(filesystem, PARTITIONS[filesystem], str(e)))
                     return -1
             else:
                 # disable time/count based filesystems checks
-                utils.sysexec_noerr( "tune2fs -c -1 -i 0 %s" % PARTITIONS[filesystem], log)
+                utils.sysexec_noerr("tune2fs -c -1 -i 0 {}".format(PARTITIONS[filesystem]), log)
 
         try:
             # then attempt to mount them
-            log.write( "mounting root file system\n" )
-            utils.sysexec("mount -t ext3 %s %s" % (PARTITIONS["root"],SYSIMG_PATH),log)
-        except BootManagerException, e:
-            log.write( "BootManagerException during mount of /root: %s\n" % str(e) )
+            log.write("mounting root file system\n")
+            utils.sysexec("mount -t ext3 {} {}".format(PARTITIONS["root"], SYSIMG_PATH),log)
+        except BootManagerException as e:
+            log.write("BootManagerException during mount of /root: {}\n".format(str(e)))
             return -2
             
         try:
-            PROC_PATH = "%s/proc" % SYSIMG_PATH
+            PROC_PATH = "{}/proc".format(SYSIMG_PATH)
             utils.makedirs(PROC_PATH)
-            log.write( "mounting /proc\n" )
-            utils.sysexec( "mount -t proc none %s" % PROC_PATH, log )
-        except BootManagerException, e:
-            log.write( "BootManagerException during mount of /proc: %s\n" % str(e) )
+            log.write("mounting /proc\n")
+            utils.sysexec("mount -t proc none {}".format(PROC_PATH), log)
+        except BootManagerException as e:
+            log.write("BootManagerException during mount of /proc: {}\n".format(str(e)))
             return -2
 
 
@@ -130,35 +131,37 @@ def Run( vars, log ):
 
         if (not one_partition):
             try:
-                VSERVERS_PATH = "%s/vservers" % SYSIMG_PATH
+                VSERVERS_PATH = "{}/vservers".format(SYSIMG_PATH)
                 utils.makedirs(VSERVERS_PATH)
-                log.write( "mounting vservers partition in root file system\n" )
-                if vars['virt']=='vs':
-                    utils.sysexec("mount -t ext3 %s %s" % (PARTITIONS["vservers"], VSERVERS_PATH), log)
+                log.write("mounting vservers partition in root file system\n")
+                if vars['virt'] == 'vs':
+                    utils.sysexec("mount -t ext3 {} {}".format(PARTITIONS["vservers"], VSERVERS_PATH), log)
                 else:
-                    utils.sysexec("mount -t btrfs %s %s" % (PARTITIONS["vservers"], VSERVERS_PATH), log)
-            except BootManagerException, e:
-                log.write( "BootManagerException during mount of /vservers: %s\n" % str(e) )
+                    utils.sysexec("mount -t btrfs {} {}".format(PARTITIONS["vservers"], VSERVERS_PATH), log)
+            except BootManagerException as e:
+                log.write("BootManagerException while mounting /vservers: {}\n".format(str(e)))
                 return -2
 
-        ROOT_MOUNTED= 1
-        vars['ROOT_MOUNTED']= 1
+        ROOT_MOUNTED = 1
+        vars['ROOT_MOUNTED'] = 1
         
     # check if the base kernel is installed 
     # these 2 links are created by our kernel's post-install scriplet
     log.write("Checking for a custom kernel\n")
     try:
         if vars['virt'] == 'vs':
-            os.stat("%s/boot/kernel-boot" % SYSIMG_PATH)
+            os.stat("{}/boot/kernel-boot".format(SYSIMG_PATH))
         else:
             try:
-                kversion = os.popen("chroot %s rpm -qa kernel | tail -1 | cut -c 8-" % SYSIMG_PATH).read().rstrip()
-                os.stat("%s/boot/vmlinuz-%s" % (SYSIMG_PATH,kversion))
+                kversion = os.popen("chroot {} rpm -qa kernel | tail -1 | cut -c 8-"\
+                                    .format(SYSIMG_PATH)).read().rstrip()
+                os.stat("{}/boot/vmlinuz-{}".format(SYSIMG_PATH, kversion))
                 major_version = int(kversion[0]) # Check if the string looks like a kernel version
             except:
-                kversion = os.popen("ls -lrt %s/lib/modules | tail -1 | awk '{print $9;}'"%SYSIMG_PATH).read().rstrip()
-    except OSError, e:            
-        log.write( "Couldn't locate base kernel (you might be using the stock kernel).\n")
+                kversion = os.popen("ls -lrt {}/lib/modules | tail -1 | awk '{print $9;}'"\
+                                    .format(SYSIMG_PATH)).read().rstrip()
+    except OSError as e:            
+        log.write("Couldn't locate base kernel (you might be using the stock kernel).\n")
         return -3
 
     # check if the model specified kernel is installed
@@ -166,28 +169,28 @@ def Run( vars, log ):
     if NODE_MODEL_OPTIONS & ModelOptions.SMP:
         option = 'smp'
         try:
-            os.stat("%s/boot/kernel-boot%s" % (SYSIMG_PATH,option))
-        except OSError, e:
+            os.stat("{}/boot/kernel-boot{}".format(SYSIMG_PATH, option))
+        except OSError as e:
             # smp kernel is not there; remove option from modeloptions
             # such that the rest of the code base thinks we are just
             # using the base kernel.
             NODE_MODEL_OPTIONS = NODE_MODEL_OPTIONS & ~ModelOptions.SMP
             vars["NODE_MODEL_OPTIONS"] = NODE_MODEL_OPTIONS
-            log.write( "WARNING: Couldn't locate smp kernel.\n")
+            log.write("WARNING: Couldn't locate smp kernel.\n")
             
     # write out the node id to /etc/planetlab/node_id. if this fails, return
     # 0, indicating the node isn't a valid install.
     try:
-        node_id_file_path= "%s/%s/node_id" % (SYSIMG_PATH,PLCONF_DIR)
-        node_id_file= file( node_id_file_path, "w" )
-        node_id_file.write( str(NODE_ID) )
+        node_id_file_path = "{}/{}/node_id".format(SYSIMG_PATH, PLCONF_DIR)
+        node_id_file = file(node_id_file_path, "w")
+        node_id_file.write(str(NODE_ID))
         node_id_file.close()
-        node_id_file= None
-        log.write( "Updated /etc/planetlab/node_id\n" )
-    except IOError, e:
-        log.write( "Unable to write out /etc/planetlab/node_id\n" )
+        node_id_file = None
+        log.write("Updated /etc/planetlab/node_id\n")
+    except IOError as e:
+        log.write("Unable to write out /etc/planetlab/node_id\n")
         return 0
 
-    log.write( "Node installation appears to be ok\n" )
+    log.write("Node installation appears to be ok\n")
     
     return 1