+ # TODO: Fix feedback_fn situation.
+ cl = luxi.Client()
+ job = opcodes.Job(op_list=[op])
+ jid = SubmitJob(job)
+
+ query = {
+ "object": "jobs",
+ "fields": ["status"],
+ "names": [jid],
+ }
+
+ while True:
+ jdata = SubmitQuery(query)
+ if not jdata:
+ # job not found, go away!
+ raise errors.JobLost("Job with id %s lost" % jid)
+
+ status = jdata[0][0]
+ if status in (opcodes.Job.STATUS_SUCCESS, opcodes.Job.STATUS_FAIL):
+ break
+ time.sleep(1)
+
+ query["fields"].extend(["op_list", "op_status", "op_result"])
+ jdata = SubmitQuery(query)
+ if not jdata:
+ raise errors.JobLost("Job with id %s lost" % jid)
+ status, op_list, op_status, op_result = jdata[0]
+ if status != opcodes.Job.STATUS_SUCCESS:
+ raise errors.OpExecError(op_result[0])
+ return op_result[0]
+
+
+def SubmitJob(job, cl=None):
+ if cl is None:
+ cl = luxi.Client()
+ return cl.SubmitJob(job)
+
+
+def SubmitQuery(data, cl=None):
+ if cl is None:
+ cl = luxi.Client()
+ return cl.Query(data)
+
+
+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)
+ else:
+ obuf.write("Unhandled exception: %s" % msg)
+ return retcode, obuf.getvalue().rstrip('\n')