Fix a broken commandline switch option
[ganeti-local] / scripts / gnt-debug
index 91f2e10..bf9d98d 100755 (executable)
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301, USA.
 
+"""Debugging commands"""
 
-# pylint: disable-msg=W0401,W0614
+# pylint: disable-msg=W0401,W0614,C0103
 # W0401: Wildcard import ganeti.cli
 # W0614: Unused import %s from wildcard import (since we need cli)
+# C0103: Invalid name gnt-backup
 
 import sys
 import simplejson
 import time
 
-from optparse import make_option
-
 from ganeti.cli import *
 from ganeti import cli
 from ganeti import opcodes
-from ganeti import constants
 from ganeti import utils
 from ganeti import errors
 
@@ -51,8 +50,9 @@ def Delay(opts, args):
   delay = float(args[0])
   op = opcodes.OpTestDelay(duration=delay,
                            on_master=opts.on_master,
-                           on_nodes=opts.on_nodes)
-  SubmitOpCode(op)
+                           on_nodes=opts.on_nodes,
+                           repeat=opts.repeat)
+  SubmitOpCode(op, opts=opts)
 
   return 0
 
@@ -60,8 +60,6 @@ def Delay(opts, args):
 def GenericOpCodes(opts, args):
   """Send any opcode to the master.
 
-  @todo: The function is broken and needs to be converted to the
-      current job queue API
   @param opts: the command line options selected by the user
   @type args: list
   @param args: should contain only one element, the path of
@@ -71,14 +69,40 @@ def GenericOpCodes(opts, args):
 
   """
   cl = cli.GetClient()
-  jex = cli.JobExecutor(cl=cl)
-
-  for fname in args:
-    op_data = simplejson.loads(open(fname).read())
-    op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
-    jex.QueueJob("file %s" % fname, *op_list)
+  jex = cli.JobExecutor(cl=cl, verbose=opts.verbose, opts=opts)
+
+  job_cnt = 0
+  op_cnt = 0
+  if opts.timing_stats:
+    ToStdout("Loading...")
+  for job_idx in range(opts.rep_job):
+    for fname in args:
+      # pylint: disable-msg=W0142
+      op_data = simplejson.loads(utils.ReadFile(fname))
+      op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
+      op_list = op_list * opts.rep_op
+      jex.QueueJob("file %s/%d" % (fname, job_idx), *op_list)
+      op_cnt += len(op_list)
+      job_cnt += 1
+
+  if opts.timing_stats:
+    t1 = time.time()
+    ToStdout("Submitting...")
+
+  jex.SubmitPending(each=opts.each)
+
+  if opts.timing_stats:
+    t2 = time.time()
+    ToStdout("Executing...")
 
   jex.GetResults()
+  if opts.timing_stats:
+    t3 = time.time()
+    ToStdout("C:op     %4d" % op_cnt)
+    ToStdout("C:job    %4d" % job_cnt)
+    ToStdout("T:submit %4.4f" % (t2-t1))
+    ToStdout("T:exec   %4.4f" % (t3-t2))
+    ToStdout("T:total  %4.4f" % (t3-t1))
   return 0
 
 
@@ -115,66 +139,73 @@ def TestAllocator(opts, args):
 
   op = opcodes.OpTestAllocator(mode=opts.mode,
                                name=args[0],
+                               evac_nodes=args,
                                mem_size=opts.mem,
                                disks=disks,
                                disk_template=opts.disk_template,
                                nics=nic_dict,
-                               os=opts.os_type,
+                               os=opts.os,
                                vcpus=opts.vcpus,
                                tags=opts.tags,
                                direction=opts.direction,
-                               allocator=opts.allocator,
+                               allocator=opts.iallocator,
                                )
-  result = SubmitOpCode(op)
+  result = SubmitOpCode(op, opts=opts)
   ToStdout("%s" % result)
   return 0
 
 
 commands = {
-  'delay': (Delay, [ArgUnknown(min=1, max=1)],
-            [DEBUG_OPT,
-             make_option("--no-master", dest="on_master", default=True,
-                         action="store_false",
-                         help="Do not sleep in the master code"),
-             make_option("-n", dest="on_nodes", default=[],
-                         action="append",
-                         help="Select nodes to sleep on"),
-             ],
-            "[opts...] <duration>", "Executes a TestDelay OpCode"),
-  'submit-job': (GenericOpCodes, [ArgFile(min=1)], [DEBUG_OPT],
-                 "<op_list_file...>", "Submits jobs built from json files"
-                 " containing a list of serialized opcodes"),
-  'allocator': (TestAllocator, [ArgInstance(min=1, max=1)],
-                [DEBUG_OPT,
-                 make_option("--dir", dest="direction",
-                             default="in", choices=["in", "out"],
-                             help="Show allocator input (in) or allocator"
-                             " results (out)"),
-                 make_option("--algorithm", dest="allocator",
-                             default=None,
-                             help="Allocator algorithm name"),
-                 make_option("-m", "--mode", default="relocate",
-                             choices=["relocate", "allocate"],
-                             help="Request mode, either allocate or"
-                             " relocate"),
-                 cli_option("--mem", default=128, type="unit",
-                            help="Memory size for the instance (MiB)"),
-                 make_option("--disks", default="4096,4096",
-                             help="Comma separated list of disk sizes (MiB)"),
-                 make_option("-t", "--disk-template", default="drbd",
-                             help="Select the disk template"),
-                 make_option("--nics", default="00:11:22:33:44:55",
-                             help="Comma separated list of nics, each nic"
-                             " definition is of form mac/ip/bridge, if"
-                             " missing values are replace by None"),
-                 make_option("-o", "--os-type", default=None,
-                             help="Select os for the instance"),
-                 make_option("-p", "--vcpus", default=1, type="int",
-                             help="Select number of VCPUs for the instance"),
-                 make_option("--tags", default=None,
-                             help="Comma separated list of tags"),
-                 ],
-                "{opts...} <instance>", "Executes a TestAllocator OpCode"),
+  'delay': (
+    Delay, [ArgUnknown(min=1, max=1)],
+    [cli_option("--no-master", dest="on_master", default=True,
+                action="store_false", help="Do not sleep in the master code"),
+     cli_option("-n", dest="on_nodes", default=[],
+                action="append", help="Select nodes to sleep on"),
+     cli_option("-r", "--repeat", type="int", default="0", dest="repeat",
+                help="Number of times to repeat the sleep"),
+     ],
+    "[opts...] <duration>", "Executes a TestDelay OpCode"),
+  'submit-job': (
+    GenericOpCodes, [ArgFile(min=1)],
+    [VERBOSE_OPT,
+     cli_option("--op-repeat", type="int", default="1", dest="rep_op",
+                help="Repeat the opcode sequence this number of times"),
+     cli_option("--job-repeat", type="int", default="1", dest="rep_job",
+                help="Repeat the job this number of times"),
+     cli_option("--timing-stats", default=False,
+                action="store_true", help="Show timing stats"),
+     cli_option("--each", default=False, action="store_true",
+                help="Submit each job separately"),
+     ],
+    "<op_list_file...>", "Submits jobs built from json files"
+    " containing a list of serialized opcodes"),
+  'allocator': (
+    TestAllocator, [ArgUnknown(min=1)],
+    [cli_option("--dir", dest="direction",
+                default="in", choices=["in", "out"],
+                help="Show allocator input (in) or allocator"
+                " results (out)"),
+     IALLOCATOR_OPT,
+     cli_option("-m", "--mode", default="relocate",
+                choices=["relocate", "allocate", "multi-evacuate"],
+                help="Request mode, either allocate or relocate"),
+     cli_option("--mem", default=128, type="unit",
+                help="Memory size for the instance (MiB)"),
+     cli_option("--disks", default="4096,4096",
+                help="Comma separated list of disk sizes (MiB)"),
+     DISK_TEMPLATE_OPT,
+     cli_option("--nics", default="00:11:22:33:44:55",
+                help="Comma separated list of nics, each nic"
+                " definition is of form mac/ip/bridge, if"
+                " missing values are replace by None"),
+     OS_OPT,
+     cli_option("-p", "--vcpus", default=1, type="int",
+                help="Select number of VCPUs for the instance"),
+     cli_option("--tags", default=None,
+                help="Comma separated list of tags"),
+     ],
+    "{opts...} <instance>", "Executes a TestAllocator OpCode"),
   }