+ if cl is None:
+ cl = GetClient()
+
+ job_id = SendJob([op], cl)
+
+ return PollJob(job_id, cl=cl, feedback_fn=feedback_fn)
+
+
+def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
+ """Wrapper around SubmitOpCode or SendJob.
+
+ This function will decide, based on the 'opts' parameter, whether to
+ submit and wait for the result of the opcode (and return it), or
+ whether to just send the job and print its identifier. It is used in
+ order to simplify the implementation of the '--submit' option.
+
+ """
+ if opts and opts.submit_only:
+ job_id = SendJob([op], cl=cl)
+ raise JobSubmittedException(job_id)
+ else:
+ return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn)
+
+
+def GetClient():
+ # TODO: Cache object?
+ try:
+ client = luxi.Client()
+ except luxi.NoMasterError:
+ master, myself = ssconf.GetMasterAndMyself()
+ if master != myself:
+ raise errors.OpPrereqError("This is not the master node, please connect"
+ " to node '%s' and rerun the command" %
+ master)
+ else:
+ raise
+ return client
+
+
+def FormatError(err):
+ """Return a formatted error message for a given error.
+
+ This function takes an exception instance and returns a tuple
+ consisting of two values: first, the recommended exit code, and
+ second, a string describing the error message (not
+ newline-terminated).
+
+ """
+ retcode = 1
+ obuf = StringIO()
+ msg = str(err)
+ if isinstance(err, errors.ConfigurationError):
+ txt = "Corrupt configuration file: %s" % msg
+ logger.Error(txt)
+ obuf.write(txt + "\n")
+ obuf.write("Aborting.")
+ retcode = 2
+ elif isinstance(err, errors.HooksAbort):
+ obuf.write("Failure: hooks execution failed:\n")
+ for node, script, out in err.args[0]:
+ if out:
+ obuf.write(" node: %s, script: %s, output: %s\n" %
+ (node, script, out))
+ else:
+ obuf.write(" node: %s, script: %s (no output)\n" %
+ (node, script))
+ elif isinstance(err, errors.HooksFailure):
+ obuf.write("Failure: hooks general failure: %s" % msg)
+ elif isinstance(err, errors.ResolverError):
+ this_host = utils.HostInfo.SysName()
+ if err.args[0] == this_host:
+ msg = "Failure: can't resolve my own hostname ('%s')"
+ else:
+ msg = "Failure: can't resolve hostname '%s'"
+ obuf.write(msg % err.args[0])
+ elif isinstance(err, errors.OpPrereqError):
+ obuf.write("Failure: prerequisites not met for this"
+ " operation:\n%s" % msg)
+ elif isinstance(err, errors.OpExecError):
+ obuf.write("Failure: command execution error:\n%s" % msg)
+ elif isinstance(err, errors.TagError):
+ obuf.write("Failure: invalid tag(s) given:\n%s" % msg)
+ elif isinstance(err, errors.GenericError):
+ obuf.write("Unhandled Ganeti error: %s" % msg)
+ elif isinstance(err, luxi.NoMasterError):
+ obuf.write("Cannot communicate with the master daemon.\nIs it running"
+ " and listening for connections?")
+ elif isinstance(err, luxi.TimeoutError):
+ obuf.write("Timeout while talking to the master daemon. Error:\n"
+ "%s" % msg)
+ elif isinstance(err, luxi.ProtocolError):
+ obuf.write("Unhandled protocol error while talking to the master daemon:\n"
+ "%s" % msg)
+ elif isinstance(err, JobSubmittedException):
+ obuf.write("JobID: %s\n" % err.args[0])
+ retcode = 0
+ else:
+ obuf.write("Unhandled exception: %s" % msg)
+ return retcode, obuf.getvalue().rstrip('\n')