X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/9fbfbb7b1a1bd958a04bf482b180861862b06c26..87e439883e8d2542596e71ec7d2fa5faafbc3dd2:/scripts/gnt-job diff --git a/scripts/gnt-job b/scripts/gnt-job index e1ee560..1402583 100755 --- a/scripts/gnt-job +++ b/scripts/gnt-job @@ -28,6 +28,8 @@ import sys from ganeti.cli import * from ganeti import constants from ganeti import errors +from ganeti import utils +from ganeti import cli #: default list of fields for L{ListJobs} @@ -38,6 +40,7 @@ _LIST_DEF_FIELDS = ["id", "status", "summary"] _USER_JOB_STATUS = { constants.JOB_STATUS_QUEUED: "queued", constants.JOB_STATUS_WAITLOCK: "waiting", + constants.JOB_STATUS_CANCELING: "canceling", constants.JOB_STATUS_RUNNING: "running", constants.JOB_STATUS_CANCELED: "canceled", constants.JOB_STATUS_SUCCESS: "success", @@ -62,7 +65,7 @@ def ListJobs(opts, args): else: selected_fields = opts.output.split(",") - output = GetClient().QueryJobs(None, selected_fields) + output = GetClient().QueryJobs(args, selected_fields) if not opts.no_headers: # TODO: Implement more fields headers = { @@ -82,12 +85,12 @@ def ListJobs(opts, args): else: headers = None - # we don't have yet unitfields here - unitfields = None - numfields = None - # change raw values to nicer strings - for row in output: + for row_id, row in enumerate(output): + if row is None: + ToStderr("No such job: %s" % args[row_id]) + continue + for idx, field in enumerate(selected_fields): val = row[idx] if field == "status": @@ -105,8 +108,7 @@ def ListJobs(opts, args): row[idx] = str(val) data = GenerateTable(separator=opts.separator, headers=headers, - fields=selected_fields, unitfields=unitfields, - numfields=numfields, data=output, units=opts.units) + fields=selected_fields, data=output) for line in data: ToStdout(line) @@ -140,8 +142,8 @@ def AutoArchiveJobs(opts, args): @param opts: the command line options selected by the user @type args: list @param args: should contain only one element, the age as a time spec - that can be parsed by L{cli.ParseTimespec} or the keyword I{all}, - which will cause all jobs to be archived + that can be parsed by L{ganeti.cli.ParseTimespec} or the + keyword I{all}, which will cause all jobs to be archived @rtype: int @return: the desired exit code @@ -155,7 +157,9 @@ def AutoArchiveJobs(opts, args): else: age = ParseTimespec(age) - client.AutoArchiveJobs(age) + (archived_count, jobs_left) = client.AutoArchiveJobs(age) + ToStdout("Archived %s jobs, %s unchecked left", archived_count, jobs_left) + return 0 @@ -172,8 +176,10 @@ def CancelJobs(opts, args): client = GetClient() for job_id in args: - client.CancelJob(job_id) + (success, msg) = client.CancelJob(job_id) + ToStdout(msg) + # TODO: Different exit value if not all jobs were canceled? return 0 @@ -302,15 +308,42 @@ def ShowJobs(opts, args): format(3, "Execution log:") for serial, log_ts, log_type, log_msg in log: time_txt = FormatTimestamp(log_ts) - encoded = str(log_msg).encode('string_escape') + encoded = utils.SafeEncode(log_msg) format(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded)) return 0 +def WatchJob(opts, args): + """Follow a job and print its output as it arrives. + + @param opts: the command line options selected by the user + @type args: list + @param args: Contains the job ID + @rtype: int + @return: the desired exit code + + """ + job_id = args[0] + + msg = ("Output from job %s follows" % job_id) + ToStdout(msg) + ToStdout("-" * len(msg)) + + retcode = 0 + try: + cli.PollJob(job_id) + except errors.GenericError, err: + (retcode, job_result) = cli.FormatError(err) + ToStderr("Job %s failed: %s", job_id, job_result) + + return retcode + + commands = { - 'list': (ListJobs, ARGS_NONE, - [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], - "", "List the jobs and their status. The available fields are" + 'list': (ListJobs, ARGS_ANY, + [DEBUG_OPT, NOHDR_OPT, SEP_OPT, FIELDS_OPT], + "[job_id ...]", + "List the jobs and their status. The available fields are" " (see the man page for details): id, status, op_list," " op_status, op_result." " The default field" @@ -330,6 +363,9 @@ commands = { 'info': (ShowJobs, ARGS_ANY, [DEBUG_OPT], " [ ...]", "Show detailed information about the specified jobs"), + 'watch': (WatchJob, ARGS_ONE, [DEBUG_OPT], + "", + "Follows a job and prints its output as it arrives"), }