Beispiel #1
0
def PrintExportList(opts, args):
    """Prints a list of all the exported system images.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: should be an empty list
  @rtype: int
  @return: the desired exit code

  """
    selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)

    qfilter = qlang.MakeSimpleFilter("node", opts.nodes)

    cl = GetClient()

    return GenericList(constants.QR_EXPORT,
                       selected_fields,
                       None,
                       opts.units,
                       opts.separator,
                       not opts.no_headers,
                       verbose=opts.verbose,
                       qfilter=qfilter,
                       cl=cl)
Beispiel #2
0
def ListJobs(opts, args):
    """List the jobs

  @param opts: the command line options selected by the user
  @type args: list
  @param args: should be an empty list
  @rtype: int
  @return: the desired exit code

  """
    selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)

    if opts.archived and "archived" not in selected_fields:
        selected_fields.append("archived")

    qfilter = qlang.MakeSimpleFilter("status", opts.status_filter)

    cl = GetClient(query=True)

    return GenericList(constants.QR_JOB,
                       selected_fields,
                       args,
                       None,
                       opts.separator,
                       not opts.no_headers,
                       format_override=_JOB_LIST_FORMAT,
                       verbose=opts.verbose,
                       force_filter=opts.force_filter,
                       namefield="id",
                       qfilter=qfilter,
                       isnumeric=True,
                       cl=cl)
Beispiel #3
0
    def _BuildFilter(fields, names):
        """Builds a filter for querying OSes.

    """
        name_filter = qlang.MakeSimpleFilter("name", names)

        # Legacy behaviour: Hide hidden, blacklisted or invalid OSes if the
        # respective field is not requested
        status_filter = [[qlang.OP_NOT, [qlang.OP_TRUE, fname]]
                         for fname in ["hidden", "blacklisted"]
                         if fname not in fields]
        if "valid" not in fields:
            status_filter.append([qlang.OP_TRUE, "valid"])

        if status_filter:
            status_filter.insert(0, qlang.OP_AND)
        else:
            status_filter = None

        if name_filter and status_filter:
            return [qlang.OP_AND, name_filter, status_filter]
        elif name_filter:
            return name_filter
        else:
            return status_filter
Beispiel #4
0
def _MultiJobAction(opts, args, cl, stdout_fn, ask_fn, question, action_fn):
    """Applies a function to multipe jobs.

  @param opts: Command line options
  @type args: list
  @param args: Job IDs
  @rtype: int
  @return: Exit code

  """
    if cl is None:
        cl = GetClient()

    if stdout_fn is None:
        stdout_fn = ToStdout

    if ask_fn is None:
        ask_fn = AskUser

    result = constants.EXIT_SUCCESS

    if bool(args) ^ (opts.status_filter is None):
        raise errors.OpPrereqError(
            "Either a status filter or job ID(s) must be"
            " specified and never both", errors.ECODE_INVAL)

    if opts.status_filter is not None:
        response = cl.Query(
            constants.QR_JOB, ["id", "status", "summary"],
            qlang.MakeSimpleFilter("status", opts.status_filter))

        jobs = [i for ((_, i), _, _) in response.data]
        if not jobs:
            raise errors.OpPrereqError(
                "No jobs with the requested status have been"
                " found", errors.ECODE_STATE)

        if not opts.force:
            (_, table) = FormatQueryResult(response,
                                           header=True,
                                           format_override=_JOB_LIST_FORMAT)
            for line in table:
                stdout_fn(line)

            if not ask_fn(question):
                return constants.EXIT_CONFIRMATION
    else:
        jobs = args

    for job_id in jobs:
        (success, msg) = action_fn(cl, job_id)

        if not success:
            result = constants.EXIT_FAILURE

        stdout_fn(msg)

    return result
Beispiel #5
0
        def _Query(qfilter):
            # Need to sort as constants.JOBS_PENDING has no stable order
            assert isinstance(constants.JOBS_PENDING, frozenset)
            self.assertEqual(
                sorted(qfilter),
                sorted(qlang.MakeSimpleFilter("status",
                                              constants.JOBS_PENDING)))

            return [
                [(constants.RS_UNAVAIL, None), (constants.RS_UNAVAIL, None),
                 (constants.RS_UNAVAIL, None)],
                [(constants.RS_NORMAL, 32532),
                 (constants.RS_NORMAL, constants.JOB_STATUS_QUEUED),
                 (constants.RS_NORMAL, ["op1", "op2", "op3"])],
            ]
Beispiel #6
0
def _GetPendingVerifyDisks(cl, uuid):
    """Checks if there are any currently running or pending group verify jobs and
  if so, returns their id.

  """
    qfilter = qlang.MakeSimpleFilter(
        "status",
        frozenset([
            constants.JOB_STATUS_RUNNING, constants.JOB_STATUS_QUEUED,
            constants.JOB_STATUS_WAITING
        ]))
    qresult = cl.Query(constants.QR_JOB, ["id", "summary"], qfilter)

    ids = [
        jobid for ((_, jobid), (_, (job, ))) in qresult.data
        if job == ("GROUP_VERIFY_DISKS(%s)" % uuid)
    ]
    return ids
Beispiel #7
0
def ShowJobs(opts, args):
    """Show detailed information about jobs.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain the job IDs to be queried
  @rtype: int
  @return: the desired exit code

  """
    def format_msg(level, text):
        """Display the text indented."""
        ToStdout("%s%s", "  " * level, text)

    def result_helper(value):
        """Format a result field in a nice way."""
        if isinstance(value, (tuple, list)):
            return "[%s]" % utils.CommaJoin(value)
        else:
            return str(value)

    selected_fields = [
        "id",
        "status",
        "ops",
        "opresult",
        "opstatus",
        "oplog",
        "opstart",
        "opexec",
        "opend",
        "received_ts",
        "start_ts",
        "end_ts",
    ]

    qfilter = qlang.MakeSimpleFilter("id", _ParseJobIds(args))
    cl = GetClient(query=True)
    result = cl.Query(constants.QR_JOB, selected_fields, qfilter).data

    first = True

    for entry in result:
        if not first:
            format_msg(0, "")
        else:
            first = False

        ((_, job_id), (rs_status, status), (_, ops), (_, opresult),
         (_, opstatus), (_, oplog), (_, opstart), (_, opexec), (_, opend),
         (_, recv_ts), (_, start_ts), (_, end_ts)) = entry

        # Detect non-normal results
        if rs_status != constants.RS_NORMAL:
            format_msg(0, "Job ID %s not found" % job_id)
            continue

        format_msg(0, "Job ID: %s" % job_id)
        if status in _USER_JOB_STATUS:
            status = _USER_JOB_STATUS[status]
        else:
            raise errors.ProgrammerError("Unknown job status code '%s'" %
                                         status)

        format_msg(1, "Status: %s" % status)

        if recv_ts is not None:
            format_msg(1, "Received:         %s" % FormatTimestamp(recv_ts))
        else:
            format_msg(1, "Missing received timestamp (%s)" % str(recv_ts))

        if start_ts is not None:
            if recv_ts is not None:
                d1 = start_ts[0] - recv_ts[0] + (start_ts[1] -
                                                 recv_ts[1]) / 1000000.0
                delta = " (delta %.6fs)" % d1
            else:
                delta = ""
            format_msg(
                1,
                "Processing start: %s%s" % (FormatTimestamp(start_ts), delta))
        else:
            format_msg(1, "Processing start: unknown (%s)" % str(start_ts))

        if end_ts is not None:
            if start_ts is not None:
                d2 = end_ts[0] - start_ts[0] + (end_ts[1] -
                                                start_ts[1]) / 1000000.0
                delta = " (delta %.6fs)" % d2
            else:
                delta = ""
            format_msg(
                1, "Processing end:   %s%s" % (FormatTimestamp(end_ts), delta))
        else:
            format_msg(1, "Processing end:   unknown (%s)" % str(end_ts))

        if end_ts is not None and recv_ts is not None:
            d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0
            format_msg(1, "Total processing time: %.6f seconds" % d3)
        else:
            format_msg(1, "Total processing time: N/A")
        format_msg(1, "Opcodes:")
        for (opcode, result, status, log, s_ts, x_ts, e_ts) in \
                zip(ops, opresult, opstatus, oplog, opstart, opexec, opend):
            format_msg(2, "%s" % opcode["OP_ID"])
            format_msg(3, "Status: %s" % status)
            if isinstance(s_ts, (tuple, list)):
                format_msg(3, "Processing start: %s" % FormatTimestamp(s_ts))
            else:
                format_msg(3, "No processing start time")
            if isinstance(x_ts, (tuple, list)):
                format_msg(3, "Execution start:  %s" % FormatTimestamp(x_ts))
            else:
                format_msg(3, "No execution start time")
            if isinstance(e_ts, (tuple, list)):
                format_msg(3, "Processing end:   %s" % FormatTimestamp(e_ts))
            else:
                format_msg(3, "No processing end time")
            format_msg(3, "Input fields:")
            for key in utils.NiceSort(opcode.keys()):
                if key == "OP_ID":
                    continue
                val = opcode[key]
                if isinstance(val, (tuple, list)):
                    val = ",".join([str(item) for item in val])
                format_msg(4, "%s: %s" % (key, val))
            if result is None:
                format_msg(3, "No output data")
            elif isinstance(result, (tuple, list)):
                if not result:
                    format_msg(3, "Result: empty sequence")
                else:
                    format_msg(3, "Result:")
                    for elem in result:
                        format_msg(4, result_helper(elem))
            elif isinstance(result, dict):
                if not result:
                    format_msg(3, "Result: empty dictionary")
                else:
                    format_msg(3, "Result:")
                    for key, val in result.iteritems():
                        format_msg(4, "%s: %s" % (key, result_helper(val)))
            else:
                format_msg(3, "Result: %s" % result)
            format_msg(3, "Execution log:")
            for serial, log_ts, log_type, log_msg in log:
                time_txt = FormatTimestamp(log_ts)
                encoded = FormatLogMessage(log_type, log_msg)
                format_msg(
                    4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded))
    return 0
Beispiel #8
0
 def CheckArguments(self):
     self.eq = ExtStorageQuery(
         qlang.MakeSimpleFilter("name", self.op.names),
         self.op.output_fields, False)
Beispiel #9
0
    def _Test(self, field, names, expected, parse_exp=None):
        if parse_exp is None:
            parse_exp = names

        qfilter = qlang.MakeSimpleFilter(field, names)
        self.assertEqual(qfilter, expected)
Beispiel #10
0
def ShowJobs(opts, args):
    """Show detailed information about jobs.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain the job IDs to be queried
  @rtype: int
  @return: the desired exit code

  """
    selected_fields = [
        "id",
        "status",
        "ops",
        "opresult",
        "opstatus",
        "oplog",
        "opstart",
        "opexec",
        "opend",
        "received_ts",
        "start_ts",
        "end_ts",
    ]

    qfilter = qlang.MakeSimpleFilter("id", _ParseJobIds(args))
    cl = GetClient()
    result = cl.Query(constants.QR_JOB, selected_fields, qfilter).data

    job_info_container = []

    for entry in result:
        ((_, job_id), (rs_status, status), (_, ops), (_, opresult),
         (_, opstatus), (_, oplog), (_, opstart), (_, opexec), (_, opend),
         (_, recv_ts), (_, start_ts), (_, end_ts)) = entry

        # Detect non-normal results
        if rs_status != constants.RS_NORMAL:
            job_info_container.append("Job ID %s not found" % job_id)
            continue

        # Container for produced data
        job_info = [("Job ID", job_id)]

        if status in _USER_JOB_STATUS:
            status = _USER_JOB_STATUS[status]
        else:
            raise errors.ProgrammerError("Unknown job status code '%s'" %
                                         status)

        job_info.append(("Status", status))

        _ListJobTimestamp("Received", recv_ts, job_info)
        _ListJobTimestamp("Processing start",
                          start_ts,
                          job_info,
                          prior_ts=recv_ts)
        _ListJobTimestamp("Processing end",
                          end_ts,
                          job_info,
                          prior_ts=start_ts)

        if end_ts is not None and recv_ts is not None:
            job_info.append(("Total processing time",
                             "%.6f seconds" % _CalcDelta(recv_ts, end_ts)))
        else:
            job_info.append(("Total processing time", "N/A"))

        opcode_container = []
        for (opcode, result, status, log, s_ts, x_ts, e_ts) in \
                zip(ops, opresult, opstatus, oplog, opstart, opexec, opend):
            opcode_info = []
            opcode_info.append(("Opcode", opcode["OP_ID"]))
            opcode_info.append(("Status", status))

            _ListOpcodeTimestamp("Processing start", s_ts, opcode_info)
            _ListOpcodeTimestamp("Execution start", x_ts, opcode_info)
            _ListOpcodeTimestamp("Processing end", e_ts, opcode_info)

            opcode_info.append(("Input fields", opcode))
            opcode_info.append(("Result", result))

            exec_log_container = []
            for serial, log_ts, log_type, log_msg in log:
                time_txt = FormatTimestamp(log_ts)
                encoded = FormatLogMessage(log_type, log_msg)

                # Arranged in this curious way to preserve the brevity for multiple
                # logs. This content cannot be exposed as a 4-tuple, as time contains
                # the colon, causing some YAML parsers to fail.
                exec_log_info = [
                    ("Time", time_txt),
                    ("Content", (
                        serial,
                        log_type,
                        encoded,
                    )),
                ]
                exec_log_container.append(exec_log_info)
            opcode_info.append(("Execution log", exec_log_container))

            opcode_container.append(opcode_info)

        job_info.append(("Opcodes", opcode_container))
        job_info_container.append(job_info)

    PrintGenericInfo(job_info_container)

    return 0