This patch does two main changes to burnin:

- import the opcodes used explicitly, so that (in this file which uses
  opcodes explicitly) we can use a shorter name (alternative would be to
  from ganeti import opcodes as op)
- remove all parameters which are built in the function call to be
  initialized outside the loop, e.g.:

  for instance in …:
    op = Op…(disks=[non-trivial but constant expression])

  to:
  disks = [non-trivial…]
  for instance in…:
    op = Op…(disks=disks)
---
 tools/burnin |  181 +++++++++++++++++++++++++---------------------------------
 1 files changed, 78 insertions(+), 103 deletions(-)

diff --git a/tools/burnin b/tools/burnin
index c442957..99e215c 100755
--- a/tools/burnin
+++ b/tools/burnin
@@ -38,6 +38,16 @@ from ganeti import cli
 from ganeti import errors
 from ganeti import utils
 
+from ganeti.opcodes import (OpActivateInstanceDisks, OpCreateInstance,
+                            OpDeactivateInstanceDisks, OpDiagnoseOS,
+                            OpExportInstance, OpFailoverInstance,
+                            OpGrowDisk, OpMigrateInstance, OpMoveInstance,
+                            OpQueryInstances, OpQueryNodes, OpRebootInstance,
+                            OpReinstallInstance, OpRemoveExport,
+                            OpRemoveInstance, OpRenameInstance,
+                            OpReplaceDisks, OpSetInstanceParams,
+                            OpShutdownInstance, OpStartupInstance)
+
 
 USAGE = ("\tburnin -o OS_NAME [options...] instance_name ...")
 
@@ -439,16 +449,16 @@ class Burner(object):
     else:
       names = []
     try:
-      op = opcodes.OpQueryNodes(output_fields=["name", "offline", "drained"],
-                                names=names, use_locking=True)
+      op = OpQueryNodes(output_fields=["name", "offline", "drained"],
+                        names=names, use_locking=True)
       result = self.ExecOp(True, op)
     except errors.GenericError, err:
       err_code, msg = cli.FormatError(err)
       Err(msg, exit_code=err_code)
     self.nodes = [data[0] for data in result if not (data[1] or data[2])]
 
-    op_diagnose = opcodes.OpDiagnoseOS(output_fields=["name", "valid",
-                                                      "variants"], names=[])
+    op_diagnose = OpDiagnoseOS(output_fields=["name", "valid", "variants"],
+                               names=[])
     result = self.ExecOp(True, op_diagnose)
 
     if not result:
@@ -470,6 +480,7 @@ class Burner(object):
 
     """
     self.to_rem = []
+    disks = [{"size": size} for size in self.disk_size]
     mytor = izip(cycle(self.nodes),
                  islice(cycle(self.nodes), 1, None),
                  self.instances)
@@ -488,24 +499,16 @@ class Burner(object):
 
       Log(msg, indent=2)
 
-      op = opcodes.OpCreateInstance(instance_name=instance,
-                                    disks = [ {"size": size}
-                                              for size in self.disk_size],
-                                    disk_template=self.opts.disk_template,
-                                    nics=self.opts.nics,
-                                    mode=constants.INSTANCE_CREATE,
-                                    os_type=self.opts.os,
-                                    pnode=pnode,
-                                    snode=snode,
-                                    start=True,
-                                    ip_check=True,
-                                    wait_for_sync=True,
-                                    file_driver="loop",
-                                    file_storage_dir=None,
-                                    iallocator=self.opts.iallocator,
-                                    beparams=self.bep,
-                                    hvparams=self.hvp,
-                                    )
+      op = OpCreateInstance(instance_name=instance, disks=disks,
+                            disk_template=self.opts.disk_template,
+                            nics=self.opts.nics,
+                            mode=constants.INSTANCE_CREATE,
+                            os_type=self.opts.os, pnode=pnode, snode=snode,
+                            start=True, ip_check=True, wait_for_sync=True,
+                            file_driver="loop", file_storage_dir=None,
+                            iallocator=self.opts.iallocator,
+                            beparams=self.bep, hvparams=self.hvp,
+                            )
 
       self.ExecOrQueue(instance, op)
       self.to_rem.append(instance)
@@ -518,8 +521,8 @@ class Burner(object):
       Log("instance %s" % instance, indent=1)
       for idx, growth in enumerate(self.disk_growth):
         if growth > 0:
-          op = opcodes.OpGrowDisk(instance_name=instance, disk=idx,
-                                  amount=growth, wait_for_sync=True)
+          op = OpGrowDisk(instance_name=instance, disk=idx,
+                          amount=growth, wait_for_sync=True)
           Log("increase disk/%s by %s MB" % (idx, growth), indent=2)
           self.ExecOrQueue(instance, op)
 
@@ -527,13 +530,12 @@ class Burner(object):
   def BurnReplaceDisks1D8(self):
     """Replace disks on primary and secondary for drbd8."""
     Log("Replacing disks on the same nodes")
+    disks = range(self.disk_count)
     for instance in self.instances:
       Log("instance %s" % instance, indent=1)
       ops = []
       for mode in constants.REPLACE_DISK_SEC, constants.REPLACE_DISK_PRI:
-        op = opcodes.OpReplaceDisks(instance_name=instance,
-                                    mode=mode,
-                                    disks=[i for i in range(self.disk_count)])
+        op = OpReplaceDisks(instance_name=instance, mode=mode, disks=disks)
         Log("run %s" % mode, indent=2)
         ops.append(op)
       self.ExecOrQueue(instance, *ops)
@@ -544,8 +546,7 @@ class Burner(object):
     Log("Changing the secondary node")
     mode = constants.REPLACE_DISK_CHG
 
-    mytor = izip(islice(cycle(self.nodes), 2, None),
-                 self.instances)
+    mytor = izip(islice(cycle(self.nodes), 2, None), self.instances)
     for tnode, instance in mytor:
       Log("instance %s" % instance, indent=1)
       if self.opts.iallocator:
@@ -553,11 +554,8 @@ class Burner(object):
         msg = "with iallocator %s" % self.opts.iallocator
       else:
         msg = tnode
-      op = opcodes.OpReplaceDisks(instance_name=instance,
-                                  mode=mode,
-                                  remote_node=tnode,
-                                  iallocator=self.opts.iallocator,
-                                  disks=[])
+      op = OpReplaceDisks(instance_name=instance, mode=mode, remote_node=tnode,
+                          iallocator=self.opts.iallocator, disks=[])
       Log("run %s %s" % (mode, msg), indent=2)
       self.ExecOrQueue(instance, op)
 
@@ -568,8 +566,7 @@ class Burner(object):
     Log("Failing over instances")
     for instance in self.instances:
       Log("instance %s" % instance, indent=1)
-      op = opcodes.OpFailoverInstance(instance_name=instance,
-                                      ignore_consistency=False)
+      op = OpFailoverInstance(instance_name=instance, ignore_consistency=False)
       self.ExecOrQueue(instance, op)
 
   @_DoCheckInstances
@@ -577,12 +574,10 @@ class Burner(object):
   def BurnMove(self):
     """Move the instances."""
     Log("Moving instances")
-    mytor = izip(islice(cycle(self.nodes), 1, None),
-                 self.instances)
+    mytor = izip(islice(cycle(self.nodes), 1, None), self.instances)
     for tnode, instance in mytor:
       Log("instance %s" % instance, indent=1)
-      op = opcodes.OpMoveInstance(instance_name=instance,
-                                  target_node=tnode)
+      op = OpMoveInstance(instance_name=instance, target_node=tnode)
       self.ExecOrQueue(instance, op)
 
   @_DoBatch(False)
@@ -591,11 +586,8 @@ class Burner(object):
     Log("Migrating instances")
     for instance in self.instances:
       Log("instance %s" % instance, indent=1)
-      op1 = opcodes.OpMigrateInstance(instance_name=instance, live=True,
-                                      cleanup=False)
-
-      op2 = opcodes.OpMigrateInstance(instance_name=instance, live=True,
-                                      cleanup=True)
+      op1 = OpMigrateInstance(instance_name=instance, live=True, cleanup=False)
+      op2 = OpMigrateInstance(instance_name=instance, live=True, cleanup=True)
       Log("migration and migration cleanup", indent=2)
       self.ExecOrQueue(instance, op1, op2)
 
@@ -611,53 +603,41 @@ class Burner(object):
                  islice(cycle(self.nodes), 2, None),
                  self.instances)
 
+    disks = [{"size": i} for i in self.disk_size]
     for pnode, snode, enode, instance in mytor:
       Log("instance %s" % instance, indent=1)
       # read the full name of the instance
-      nam_op = opcodes.OpQueryInstances(output_fields=["name"],
-                                        names=[instance], use_locking=True)
+      nam_op = OpQueryInstances(output_fields=["name"],
+                                names=[instance], use_locking=True)
       full_name = self.ExecOp(False, nam_op)[0][0]
 
       if self.opts.iallocator:
         pnode = snode = None
-        import_log_msg = ("import from %s"
-                          " with iallocator %s" %
+        import_log_msg = ("import from %s with iallocator %s" %
                           (enode, self.opts.iallocator))
       elif self.opts.disk_template not in constants.DTS_NET_MIRROR:
         snode = None
-        import_log_msg = ("import from %s to %s" %
-                          (enode, pnode))
+        import_log_msg = ("import from %s to %s" % (enode, pnode))
       else:
-        import_log_msg = ("import from %s to %s, %s" %
-                          (enode, pnode, snode))
-
-      exp_op = opcodes.OpExportInstance(instance_name=instance,
-                                           target_node=enode,
-                                           shutdown=True)
-      rem_op = opcodes.OpRemoveInstance(instance_name=instance,
-                                        ignore_failures=True)
+        import_log_msg = ("import from %s to %s, %s" % (enode, pnode, snode))
+
+      exp_op = OpExportInstance(instance_name=instance,
+                                target_node=enode, shutdown=True)
+      rem_op = OpRemoveInstance(instance_name=instance, ignore_failures=True)
       imp_dir = os.path.join(constants.EXPORT_DIR, full_name)
-      imp_op = opcodes.OpCreateInstance(instance_name=instance,
-                                        disks = [ {"size": size}
-                                                  for size in self.disk_size],
-                                        disk_template=self.opts.disk_template,
-                                        nics=self.opts.nics,
-                                        mode=constants.INSTANCE_IMPORT,
-                                        src_node=enode,
-                                        src_path=imp_dir,
-                                        pnode=pnode,
-                                        snode=snode,
-                                        start=True,
-                                        ip_check=True,
-                                        wait_for_sync=True,
-                                        file_storage_dir=None,
-                                        file_driver="loop",
-                                        iallocator=self.opts.iallocator,
-                                        beparams=self.bep,
-                                        hvparams=self.hvp,
-                                        )
-
-      erem_op = opcodes.OpRemoveExport(instance_name=instance)
+      imp_op = OpCreateInstance(instance_name=instance, disks=disks,
+                                disk_template=self.opts.disk_template,
+                                nics=self.opts.nics,
+                                mode=constants.INSTANCE_IMPORT,
+                                src_node=enode, src_path=imp_dir,
+                                pnode=pnode, snode=snode,
+                                start=True, ip_check=True, wait_for_sync=True,
+                                file_storage_dir=None, file_driver="loop",
+                                iallocator=self.opts.iallocator,
+                                beparams=self.bep, hvparams=self.hvp,
+                                )
+
+      erem_op = OpRemoveExport(instance_name=instance)
 
       Log("export to node %s" % enode, indent=2)
       Log("remove instance", indent=2)
@@ -667,16 +647,15 @@ class Burner(object):
 
   def StopInstanceOp(self, instance):
     """Stop given instance."""
-    return opcodes.OpShutdownInstance(instance_name=instance)
+    return OpShutdownInstance(instance_name=instance)
 
   def StartInstanceOp(self, instance):
     """Start given instance."""
-    return opcodes.OpStartupInstance(instance_name=instance, force=False)
+    return OpStartupInstance(instance_name=instance, force=False)
 
   def RenameInstanceOp(self, instance, instance_new):
     """Rename instance."""
-    return opcodes.OpRenameInstance(instance_name=instance,
-                                    new_name=instance_new)
+    return OpRenameInstance(instance_name=instance, new_name=instance_new)
 
   @_DoCheckInstances
   @_DoBatch(True)
@@ -695,8 +674,7 @@ class Burner(object):
     Log("Removing instances")
     for instance in self.to_rem:
       Log("instance %s" % instance, indent=1)
-      op = opcodes.OpRemoveInstance(instance_name=instance,
-                                    ignore_failures=True)
+      op = OpRemoveInstance(instance_name=instance, ignore_failures=True)
       self.ExecOrQueue(instance, op)
 
   def BurnRename(self):
@@ -729,10 +707,9 @@ class Burner(object):
     for instance in self.instances:
       Log("instance %s" % instance, indent=1)
       op1 = self.StopInstanceOp(instance)
-      op2 = opcodes.OpReinstallInstance(instance_name=instance)
+      op2 = OpReinstallInstance(instance_name=instance)
       Log("reinstall without passing the OS", indent=2)
-      op3 = opcodes.OpReinstallInstance(instance_name=instance,
-                                        os_type=self.opts.os)
+      op3 = OpReinstallInstance(instance_name=instance, os_type=self.opts.os)
       Log("reinstall specifying the OS", indent=2)
       op4 = self.StartInstanceOp(instance)
       self.ExecOrQueue(instance, op1, op2, op3, op4)
@@ -746,9 +723,8 @@ class Burner(object):
       Log("instance %s" % instance, indent=1)
       ops = []
       for reboot_type in constants.REBOOT_TYPES:
-        op = opcodes.OpRebootInstance(instance_name=instance,
-                                      reboot_type=reboot_type,
-                                      ignore_secondaries=False)
+        op = OpRebootInstance(instance_name=instance, reboot_type=reboot_type,
+                              ignore_secondaries=False)
         Log("reboot with type '%s'" % reboot_type, indent=2)
         ops.append(op)
       self.ExecOrQueue(instance, *ops)
@@ -761,8 +737,8 @@ class Burner(object):
     for instance in self.instances:
       Log("instance %s" % instance, indent=1)
       op_start = self.StartInstanceOp(instance)
-      op_act = opcodes.OpActivateInstanceDisks(instance_name=instance)
-      op_deact = opcodes.OpDeactivateInstanceDisks(instance_name=instance)
+      op_act = OpActivateInstanceDisks(instance_name=instance)
+      op_deact = OpDeactivateInstanceDisks(instance_name=instance)
       op_stop = self.StopInstanceOp(instance)
       Log("activate disks when online", indent=2)
       Log("activate disks when offline", indent=2)
@@ -774,13 +750,12 @@ class Burner(object):
   def BurnAddRemoveDisks(self):
     """Add and remove an extra disk for the instances."""
     Log("Adding and removing disks")
+    add_disk = [(constants.DDM_ADD, {"size": self.disk_size[0]})]
+    del_disk = [(constants.DDM_REMOVE, {})]
     for instance in self.instances:
       Log("instance %s" % instance, indent=1)
-      op_add = opcodes.OpSetInstanceParams(\
-        instance_name=instance,
-        disks=[(constants.DDM_ADD, {"size": self.disk_size[0]})])
-      op_rem = opcodes.OpSetInstanceParams(\
-        instance_name=instance, disks=[(constants.DDM_REMOVE, {})])
+      op_add = OpSetInstanceParams(instance_name=instance, disks=add_disk)
+      op_rem = OpSetInstanceParams(instance_name=instance, disks=del_disk)
       op_stop = self.StopInstanceOp(instance)
       op_start = self.StartInstanceOp(instance)
       Log("adding a disk", indent=2)
@@ -791,12 +766,12 @@ class Burner(object):
   def BurnAddRemoveNICs(self):
     """Add and remove an extra NIC for the instances."""
     Log("Adding and removing NICs")
+    add_nic = [(constants.DDM_ADD, {})]
+    del_nic = [(constants.DDM_REMOVE, {})]
     for instance in self.instances:
       Log("instance %s" % instance, indent=1)
-      op_add = opcodes.OpSetInstanceParams(\
-        instance_name=instance, nics=[(constants.DDM_ADD, {})])
-      op_rem = opcodes.OpSetInstanceParams(\
-        instance_name=instance, nics=[(constants.DDM_REMOVE, {})])
+      op_add = OpSetInstanceParams(instance_name=instance, nics=add_nic)
+      op_rem = OpSetInstanceParams(instance_name=instance, nics=del_nic)
       Log("adding a NIC", indent=2)
       Log("removing last NIC", indent=2)
       self.ExecOrQueue(instance, op_add, op_rem)
-- 
1.6.5.3

Reply via email to