From ceab32dd60647e49d01a00eefb64ec4ceca99ccf Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 1 Apr 2008 13:05:48 +0000
Subject: [PATCH] Add submit function to lib/cli.py

This patch adds function that submit jobs or queries over the unix socket
interface to lib/cli.py. The will be used by the scripts instead of the
SubmitOpCode function.

Reviewed-by: ultrotter
---
 lib/cli.py       | 20 +++++++++++++++++++-
 lib/constants.py |  1 +
 lib/luxi.py      |  5 ++++-
 3 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/lib/cli.py b/lib/cli.py
index a58f5505b..c3780380b 100644
--- a/lib/cli.py
+++ b/lib/cli.py
@@ -34,11 +34,13 @@ from ganeti import errors
 from ganeti import mcpu
 from ganeti import constants
 from ganeti import opcodes
+from ganeti import luxi
 
 from optparse import (OptionParser, make_option, TitledHelpFormatter,
                       Option, OptionValueError, SUPPRESS_HELP)
 
-__all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain", "SubmitOpCode",
+__all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain",
+           "SubmitOpCode", "SubmitJob", "SubmitQuery",
            "cli_option", "GenerateTable", "AskUser",
            "ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE",
            "USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT",
@@ -386,6 +388,22 @@ def SubmitOpCode(op, proc=None, feedback_fn=None):
   return proc.ExecOpCode(op)
 
 
+def SubmitJob(job, cl=None):
+  if cl is None:
+    cl = luxi.Client()
+  jid = cl.SubmitJob(job)
+  return jid
+
+
+def SubmitQuery(data, cl=None):
+  if cl is None:
+    cl = luxi.Client()
+  result = cl.Query(data)
+  if not result['success']:
+    raise ValueError(result)
+  return result['result']
+
+
 def FormatError(err):
   """Return a formatted error message for a given error.
 
diff --git a/lib/constants.py b/lib/constants.py
index 4e04a18fe..1b9cb32de 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -42,6 +42,7 @@ WATCHER_STATEFILE = DATA_DIR + "/watcher.data"
 SSH_KNOWN_HOSTS_FILE = DATA_DIR + "/known_hosts"
 ETC_HOSTS = "/etc/hosts"
 DEFAULT_FILE_STORAGE_DIR = _autoconf.FILE_STORAGE_DIR
+MASTER_SOCKET = RUN_DIR + "/master.sock"
 
 NODE_INITD_SCRIPT = _autoconf.SYSCONFDIR + "/init.d/ganeti"
 DEFAULT_NODED_PORT = 1811
diff --git a/lib/luxi.py b/lib/luxi.py
index 9c27ad7a5..785543467 100644
--- a/lib/luxi.py
+++ b/lib/luxi.py
@@ -35,6 +35,7 @@ import simplejson
 import time
 
 from ganeti import opcodes
+from ganeti import constants
 
 
 KEY_REQUEST = 'request'
@@ -216,7 +217,7 @@ class Client(object):
   implements data serialization/deserialization.
 
   """
-  def __init__(self, address, timeouts=None, transport=Transport):
+  def __init__(self, address=None, timeouts=None, transport=Transport):
     """Constructor for the Client class.
 
     Arguments:
@@ -229,6 +230,8 @@ class Client(object):
     class are used.
 
     """
+    if address is None:
+      address = constants.MASTER_SOCKET
     self.transport = transport(address, timeouts=timeouts)
 
   def SendRequest(self, request, data):
-- 
GitLab