Revision a51b19de lib/cli.py

b/lib/cli.py
37 37
from ganeti import constants
38 38
from ganeti import opcodes
39 39
from ganeti import luxi
40
from ganeti import ssconf
41 40
from ganeti import rpc
42 41
from ganeti import ssh
43 42
from ganeti import compat
......
46 45
from ganeti import objects
47 46
from ganeti import pathutils
48 47

  
48
from ganeti.runtime import (GetClient)
49

  
49 50
from optparse import (OptionParser, TitledHelpFormatter,
50 51
                      Option, OptionValueError)
51 52

  
......
2361 2362
    _InitReasonTrail(op, options)
2362 2363

  
2363 2364

  
2364
def GetClient(query=False):
2365
  """Connects to the a luxi socket and returns a client.
2366

  
2367
  @type query: boolean
2368
  @param query: this signifies that the client will only be
2369
      used for queries; if the build-time parameter
2370
      enable-split-queries is enabled, then the client will be
2371
      connected to the query socket instead of the masterd socket
2372

  
2373
  """
2374
  override_socket = os.getenv(constants.LUXI_OVERRIDE, "")
2375
  if override_socket:
2376
    if override_socket == constants.LUXI_OVERRIDE_MASTER:
2377
      address = pathutils.MASTER_SOCKET
2378
    elif override_socket == constants.LUXI_OVERRIDE_QUERY:
2379
      address = pathutils.QUERY_SOCKET
2380
    else:
2381
      address = override_socket
2382
  elif query:
2383
    address = pathutils.QUERY_SOCKET
2384
  else:
2385
    address = None
2386
  # TODO: Cache object?
2387
  try:
2388
    client = luxi.Client(address=address)
2389
  except luxi.NoMasterError:
2390
    ss = ssconf.SimpleStore()
2391

  
2392
    # Try to read ssconf file
2393
    try:
2394
      ss.GetMasterNode()
2395
    except errors.ConfigurationError:
2396
      raise errors.OpPrereqError("Cluster not initialized or this machine is"
2397
                                 " not part of a cluster",
2398
                                 errors.ECODE_INVAL)
2399

  
2400
    master, myself = ssconf.GetMasterAndMyself(ss=ss)
2401
    if master != myself:
2402
      raise errors.OpPrereqError("This is not the master node, please connect"
2403
                                 " to node '%s' and rerun the command" %
2404
                                 master, errors.ECODE_INVAL)
2405
    raise
2406
  return client
2407

  
2408

  
2409 2365
def FormatError(err):
2410 2366
  """Return a formatted error message for a given error.
2411 2367

  

Also available in: Unified diff