# 02110-1301, USA.
+# pylint: disable-msg=W0401,W0614
+# W0401: Wildcard import ganeti.cli
+# W0614: Unused import %s from wildcard import (since we need cli)
+
import sys
-import os
-import itertools
import simplejson
import time
from optparse import make_option
-from cStringIO import StringIO
from ganeti.cli import *
+from ganeti import cli
from ganeti import opcodes
-from ganeti import logger
from ganeti import constants
from ganeti import utils
from ganeti import errors
def Delay(opts, args):
"""Sleeps for a while
+ @param opts: the command line options selected by the user
+ @type args: list
+ @param args: should contain only one element, the duration
+ the sleep
+ @rtype: int
+ @return: the desired exit code
+
"""
delay = float(args[0])
op = opcodes.OpTestDelay(duration=delay,
on_master=opts.on_master,
on_nodes=opts.on_nodes)
+ SubmitOpCode(op)
- job = opcodes.Job(op_list=[op])
- jid = SubmitJob(job)
- print "Job id", jid
return 0
def GenericOpCodes(opts, args):
- """Send any opcode to the master
+ """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
+ the file with the opcode definition
+ @rtype: int
+ @return: the desired exit code
"""
- fname = args[0]
- op_data = simplejson.loads(open(fname).read())
- op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
- job = opcodes.Job(op_list=op_list)
- jid = SubmitJob(job)
- print "Job id:", jid
- query = {
- "object": "jobs",
- "fields": ["status"],
- "names": [jid],
- }
-
- # wait for job to complete (either by success or failure)
- while True:
- jdata = SubmitQuery(query)
- if not jdata:
- # job not found, gone away!
- print "Job lost!"
- return 1
-
- status = jdata[0][0]
- print status
- if status in (opcodes.Job.STATUS_SUCCESS, opcodes.Job.STATUS_FAIL):
- break
-
- # sleep between checks
- time.sleep(0.5)
-
- # job has finished, get and process its results
- query["fields"].extend(["op_list", "op_status", "op_result"])
- jdata = SubmitQuery(query)
- if not jdata:
- # job not found, gone away!
- print "Job lost!"
- return 1
- print jdata[0]
- status, op_list, op_status, op_result = jdata[0]
- for idx, op in enumerate(op_list):
- print idx, op.OP_ID, op_status[idx], op_result[idx]
+ cl = cli.GetClient()
+ jex = cli.JobExecutor(cl=cl, verbose=opts.verbose)
+
+ job_cnt = 0
+ op_cnt = 0
+ if opts.timing_stats:
+ ToStdout("Loading...")
+ for job_idx in range(opts.rep_job):
+ for fname in args:
+ op_data = simplejson.loads(open(fname).read())
+ 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()
+
+ 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
def TestAllocator(opts, args):
- """Runs the test allocator opcode"""
+ """Runs the test allocator opcode.
+
+ @param opts: the command line options selected by the user
+ @type args: list
+ @param args: should contain only one element, the iallocator name
+ @rtype: int
+ @return: the desired exit code
+ """
try:
disks = [{"size": utils.ParseUnit(val), "mode": 'w'}
for val in opts.disks.split(",")]
except errors.UnitParseError, err:
- print >> sys.stderr, "Invalid disks parameter '%s': %s" % (opts.disks, err)
+ ToStderr("Invalid disks parameter '%s': %s", opts.disks, err)
return 1
nics = [val.split("/") for val in opts.nics.split(",")]
allocator=opts.allocator,
)
result = SubmitOpCode(op)
- print result
+ ToStdout("%s" % result)
return 0
help="Select nodes to sleep on"),
],
"[opts...] <duration>", "Executes a TestDelay OpCode"),
- 'submit-job': (GenericOpCodes, ARGS_ONE,
+ 'submit-job': (GenericOpCodes, ARGS_ATLEAST(1),
[DEBUG_OPT,
+ make_option("--op-repeat", type="int", default="1",
+ dest="rep_op",
+ help="Repeat the opcode sequence this number"
+ " of times"),
+ make_option("--job-repeat", type="int", default="1",
+ dest="rep_job",
+ help="Repeat the job this number"
+ " of times"),
+ make_option("-v", "--verbose", default=False,
+ action="store_true",
+ help="Make the operation more verbose"),
+ make_option("--timing-stats", default=False,
+ action="store_true",
+ help="Show timing stats"),
],
- "<op_list_file>", "Submits a job built from a json-file"
- " with a list of serialized opcodes"),
+ "<op_list_file...>", "Submits jobs built from json files"
+ " containing a list of serialized opcodes"),
'allocator': (TestAllocator, ARGS_ONE,
[DEBUG_OPT,
make_option("--dir", dest="direction",
make_option("-m", "--mode", default="relocate",
choices=["relocate", "allocate"],
help="Request mode, either allocate or"
- "relocate"),
+ " relocate"),
cli_option("--mem", default=128, type="unit",
help="Memory size for the instance (MiB)"),
make_option("--disks", default="4096,4096",