#
#
-# Copyright (C) 2006, 2007 Google Inc.
+# Copyright (C) 2006, 2007, 2011, 2012 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
import errno
import logging
+from ganeti import compat
from ganeti import serializer
from ganeti import constants
from ganeti import errors
from ganeti import utils
+from ganeti import objects
+from ganeti import pathutils
KEY_METHOD = "method"
REQ_WAIT_FOR_JOB_CHANGE = "WaitForJobChange"
REQ_CANCEL_JOB = "CancelJob"
REQ_ARCHIVE_JOB = "ArchiveJob"
-REQ_AUTOARCHIVE_JOBS = "AutoArchiveJobs"
+REQ_CHANGE_JOB_PRIORITY = "ChangeJobPriority"
+REQ_AUTO_ARCHIVE_JOBS = "AutoArchiveJobs"
+REQ_QUERY = "Query"
+REQ_QUERY_FIELDS = "QueryFields"
REQ_QUERY_JOBS = "QueryJobs"
REQ_QUERY_INSTANCES = "QueryInstances"
REQ_QUERY_NODES = "QueryNodes"
+REQ_QUERY_GROUPS = "QueryGroups"
+REQ_QUERY_NETWORKS = "QueryNetworks"
REQ_QUERY_EXPORTS = "QueryExports"
REQ_QUERY_CONFIG_VALUES = "QueryConfigValues"
REQ_QUERY_CLUSTER_INFO = "QueryClusterInfo"
REQ_QUERY_TAGS = "QueryTags"
-REQ_QUERY_LOCKS = "QueryLocks"
-REQ_QUEUE_SET_DRAIN_FLAG = "SetDrainFlag"
+REQ_SET_DRAIN_FLAG = "SetDrainFlag"
REQ_SET_WATCHER_PAUSE = "SetWatcherPause"
+#: List of all LUXI requests
+REQ_ALL = compat.UniqueFrozenset([
+ REQ_ARCHIVE_JOB,
+ REQ_AUTO_ARCHIVE_JOBS,
+ REQ_CANCEL_JOB,
+ REQ_CHANGE_JOB_PRIORITY,
+ REQ_QUERY,
+ REQ_QUERY_CLUSTER_INFO,
+ REQ_QUERY_CONFIG_VALUES,
+ REQ_QUERY_EXPORTS,
+ REQ_QUERY_FIELDS,
+ REQ_QUERY_GROUPS,
+ REQ_QUERY_INSTANCES,
+ REQ_QUERY_JOBS,
+ REQ_QUERY_NODES,
+ REQ_QUERY_TAGS,
+ REQ_SET_DRAIN_FLAG,
+ REQ_SET_WATCHER_PAUSE,
+ REQ_SUBMIT_JOB,
+ REQ_SUBMIT_MANY_JOBS,
+ REQ_WAIT_FOR_JOB_CHANGE,
+ ])
+
DEF_CTMO = 10
DEF_RWTO = 60
while True:
try:
data = self.socket.recv(4096)
+ except socket.timeout, err:
+ raise TimeoutError("Receive timeout: %s" % str(err))
except socket.error, err:
if err.args and err.args[0] == errno.EAGAIN:
continue
raise
- except socket.timeout, err:
- raise TimeoutError("Receive timeout: %s" % str(err))
break
if not data:
raise ConnectionClosedError("Connection closed while reading")
logging.error("LUXI request not a dict: %r", msg)
raise ProtocolError("Invalid LUXI request (not a dict)")
- method = request.get(KEY_METHOD, None) # pylint: disable-msg=E1103
- args = request.get(KEY_ARGS, None) # pylint: disable-msg=E1103
- version = request.get(KEY_VERSION, None) # pylint: disable-msg=E1103
+ method = request.get(KEY_METHOD, None) # pylint: disable=E1103
+ args = request.get(KEY_ARGS, None) # pylint: disable=E1103
+ version = request.get(KEY_VERSION, None) # pylint: disable=E1103
if method is None or args is None:
logging.error("LUXI request missing method or arguments: %r", msg)
# Parse the result
try:
data = serializer.LoadJson(msg)
+ except KeyboardInterrupt:
+ raise
except Exception, err:
raise ProtocolError("Error while deserializing response: %s" % str(err))
raise ProtocolError("Invalid response from server: %r" % data)
return (data[KEY_SUCCESS], data[KEY_RESULT],
- data.get(KEY_VERSION, None)) # pylint: disable-msg=E1103
+ data.get(KEY_VERSION, None)) # pylint: disable=E1103
def FormatResponse(success, result, version=None):
request[KEY_VERSION] = version
# Serialize the request
- return serializer.DumpJson(request, indent=False)
+ return serializer.DumpJson(request)
def CallLuxiMethod(transport_cb, method, args, version=None):
"""
if address is None:
- address = constants.MASTER_SOCKET
+ address = pathutils.MASTER_SOCKET
self.address = address
self.timeouts = timeouts
self.transport_class = transport
old_transp = self.transport
self.transport = None
old_transp.Close()
- except Exception: # pylint: disable-msg=W0703
+ except Exception: # pylint: disable=W0703
pass
def _SendMethodCall(self, data):
self._CloseTransport()
raise
+ def Close(self):
+ """Close the underlying connection.
+
+ """
+ self._CloseTransport()
+
def CallMethod(self, method, args):
"""Send a generic request and return the response.
"""
+ if not isinstance(args, (list, tuple)):
+ raise errors.ProgrammerError("Invalid parameter passed to CallMethod:"
+ " expected list, got %s" % type(args))
return CallLuxiMethod(self._SendMethodCall, method, args,
version=constants.LUXI_VERSION)
def SetQueueDrainFlag(self, drain_flag):
- return self.CallMethod(REQ_QUEUE_SET_DRAIN_FLAG, drain_flag)
+ return self.CallMethod(REQ_SET_DRAIN_FLAG, (drain_flag, ))
def SetWatcherPause(self, until):
- return self.CallMethod(REQ_SET_WATCHER_PAUSE, [until])
+ return self.CallMethod(REQ_SET_WATCHER_PAUSE, (until, ))
def SubmitJob(self, ops):
ops_state = map(lambda op: op.__getstate__(), ops)
- return self.CallMethod(REQ_SUBMIT_JOB, ops_state)
+ return self.CallMethod(REQ_SUBMIT_JOB, (ops_state, ))
def SubmitManyJobs(self, jobs):
jobs_state = []
for ops in jobs:
jobs_state.append([op.__getstate__() for op in ops])
- return self.CallMethod(REQ_SUBMIT_MANY_JOBS, jobs_state)
+ return self.CallMethod(REQ_SUBMIT_MANY_JOBS, (jobs_state, ))
def CancelJob(self, job_id):
- return self.CallMethod(REQ_CANCEL_JOB, job_id)
+ return self.CallMethod(REQ_CANCEL_JOB, (job_id, ))
def ArchiveJob(self, job_id):
- return self.CallMethod(REQ_ARCHIVE_JOB, job_id)
+ return self.CallMethod(REQ_ARCHIVE_JOB, (job_id, ))
+
+ def ChangeJobPriority(self, job_id, priority):
+ return self.CallMethod(REQ_CHANGE_JOB_PRIORITY, (job_id, priority))
def AutoArchiveJobs(self, age):
timeout = (DEF_RWTO - 1) / 2
- return self.CallMethod(REQ_AUTOARCHIVE_JOBS, (age, timeout))
+ return self.CallMethod(REQ_AUTO_ARCHIVE_JOBS, (age, timeout))
def WaitForJobChangeOnce(self, job_id, fields,
prev_job_info, prev_log_serial,
break
return result
+ def Query(self, what, fields, qfilter):
+ """Query for resources/items.
+
+ @param what: One of L{constants.QR_VIA_LUXI}
+ @type fields: List of strings
+ @param fields: List of requested fields
+ @type qfilter: None or list
+ @param qfilter: Query filter
+ @rtype: L{objects.QueryResponse}
+
+ """
+ result = self.CallMethod(REQ_QUERY, (what, fields, qfilter))
+ return objects.QueryResponse.FromDict(result)
+
+ def QueryFields(self, what, fields):
+ """Query for available fields.
+
+ @param what: One of L{constants.QR_VIA_LUXI}
+ @type fields: None or list of strings
+ @param fields: List of requested fields
+ @rtype: L{objects.QueryFieldsResponse}
+
+ """
+ result = self.CallMethod(REQ_QUERY_FIELDS, (what, fields))
+ return objects.QueryFieldsResponse.FromDict(result)
+
def QueryJobs(self, job_ids, fields):
return self.CallMethod(REQ_QUERY_JOBS, (job_ids, fields))
def QueryNodes(self, names, fields, use_locking):
return self.CallMethod(REQ_QUERY_NODES, (names, fields, use_locking))
+ def QueryGroups(self, names, fields, use_locking):
+ return self.CallMethod(REQ_QUERY_GROUPS, (names, fields, use_locking))
+
+ def QueryNetworks(self, names, fields, use_locking):
+ return self.CallMethod(REQ_QUERY_NETWORKS, (names, fields, use_locking))
+
def QueryExports(self, nodes, use_locking):
return self.CallMethod(REQ_QUERY_EXPORTS, (nodes, use_locking))
return self.CallMethod(REQ_QUERY_CLUSTER_INFO, ())
def QueryConfigValues(self, fields):
- return self.CallMethod(REQ_QUERY_CONFIG_VALUES, fields)
+ return self.CallMethod(REQ_QUERY_CONFIG_VALUES, (fields, ))
def QueryTags(self, kind, name):
return self.CallMethod(REQ_QUERY_TAGS, (kind, name))
-
- def QueryLocks(self, fields, sync):
- return self.CallMethod(REQ_QUERY_LOCKS, (fields, sync))