Commit a51b19de authored by Helga Velroyen's avatar Helga Velroyen
Browse files

Move GetClient() to runtime module



Soon both, the cli and the masterd, will use a luxi
client to run queries. The method to obtain and configure
the luxi client with the right address can be reused from
cli to masterd here. Therefore, this patch moves the
function to the common runtime module.
Signed-off-by: default avatarHelga Velroyen <helgav@google.com>
Reviewed-by: default avatarHrvoje Ribicic <riba@google.com>
parent 89352544
......@@ -37,7 +37,6 @@ from ganeti import errors
from ganeti import constants
from ganeti import opcodes
from ganeti import luxi
from ganeti import ssconf
from ganeti import rpc
from ganeti import ssh
from ganeti import compat
......@@ -46,6 +45,8 @@ from ganeti import qlang
from ganeti import objects
from ganeti import pathutils
from ganeti.runtime import (GetClient)
from optparse import (OptionParser, TitledHelpFormatter,
Option, OptionValueError)
......@@ -2361,51 +2362,6 @@ def SetGenericOpcodeOpts(opcode_list, options):
_InitReasonTrail(op, options)
def GetClient(query=False):
"""Connects to the a luxi socket and returns a client.
@type query: boolean
@param query: this signifies that the client will only be
used for queries; if the build-time parameter
enable-split-queries is enabled, then the client will be
connected to the query socket instead of the masterd socket
"""
override_socket = os.getenv(constants.LUXI_OVERRIDE, "")
if override_socket:
if override_socket == constants.LUXI_OVERRIDE_MASTER:
address = pathutils.MASTER_SOCKET
elif override_socket == constants.LUXI_OVERRIDE_QUERY:
address = pathutils.QUERY_SOCKET
else:
address = override_socket
elif query:
address = pathutils.QUERY_SOCKET
else:
address = None
# TODO: Cache object?
try:
client = luxi.Client(address=address)
except luxi.NoMasterError:
ss = ssconf.SimpleStore()
# Try to read ssconf file
try:
ss.GetMasterNode()
except errors.ConfigurationError:
raise errors.OpPrereqError("Cluster not initialized or this machine is"
" not part of a cluster",
errors.ECODE_INVAL)
master, myself = ssconf.GetMasterAndMyself(ss=ss)
if master != myself:
raise errors.OpPrereqError("This is not the master node, please connect"
" to node '%s' and rerun the command" %
master, errors.ECODE_INVAL)
raise
return client
def FormatError(err):
"""Return a formatted error message for a given error.
......
......@@ -26,10 +26,14 @@
import grp
import pwd
import threading
import os
import platform
from ganeti import constants
from ganeti import errors
from ganeti import luxi
from ganeti import pathutils
from ganeti import ssconf
from ganeti import utils
......@@ -235,3 +239,48 @@ def GetArchInfo():
" initialized")
return _arch
def GetClient(query=False):
"""Connects to the a luxi socket and returns a client.
@type query: boolean
@param query: this signifies that the client will only be
used for queries; if the build-time parameter
enable-split-queries is enabled, then the client will be
connected to the query socket instead of the masterd socket
"""
override_socket = os.getenv(constants.LUXI_OVERRIDE, "")
if override_socket:
if override_socket == constants.LUXI_OVERRIDE_MASTER:
address = pathutils.MASTER_SOCKET
elif override_socket == constants.LUXI_OVERRIDE_QUERY:
address = pathutils.QUERY_SOCKET
else:
address = override_socket
elif query:
address = pathutils.QUERY_SOCKET
else:
address = None
# TODO: Cache object?
try:
client = luxi.Client(address=address)
except luxi.NoMasterError:
ss = ssconf.SimpleStore()
# Try to read ssconf file
try:
ss.GetMasterNode()
except errors.ConfigurationError:
raise errors.OpPrereqError("Cluster not initialized or this machine is"
" not part of a cluster",
errors.ECODE_INVAL)
master, myself = ssconf.GetMasterAndMyself(ss=ss)
if master != myself:
raise errors.OpPrereqError("This is not the master node, please connect"
" to node '%s' and rerun the command" %
master, errors.ECODE_INVAL)
raise
return client
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment