Revision a51b19de

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

  
b/lib/runtime.py
26 26
import grp
27 27
import pwd
28 28
import threading
29
import os
29 30
import platform
30 31

  
31 32
from ganeti import constants
32 33
from ganeti import errors
34
from ganeti import luxi
35
from ganeti import pathutils
36
from ganeti import ssconf
33 37
from ganeti import utils
34 38

  
35 39

  
......
235 239
                                 " initialized")
236 240

  
237 241
  return _arch
242

  
243

  
244
def GetClient(query=False):
245
  """Connects to the a luxi socket and returns a client.
246

  
247
  @type query: boolean
248
  @param query: this signifies that the client will only be
249
      used for queries; if the build-time parameter
250
      enable-split-queries is enabled, then the client will be
251
      connected to the query socket instead of the masterd socket
252

  
253
  """
254
  override_socket = os.getenv(constants.LUXI_OVERRIDE, "")
255
  if override_socket:
256
    if override_socket == constants.LUXI_OVERRIDE_MASTER:
257
      address = pathutils.MASTER_SOCKET
258
    elif override_socket == constants.LUXI_OVERRIDE_QUERY:
259
      address = pathutils.QUERY_SOCKET
260
    else:
261
      address = override_socket
262
  elif query:
263
    address = pathutils.QUERY_SOCKET
264
  else:
265
    address = None
266
  # TODO: Cache object?
267
  try:
268
    client = luxi.Client(address=address)
269
  except luxi.NoMasterError:
270
    ss = ssconf.SimpleStore()
271

  
272
    # Try to read ssconf file
273
    try:
274
      ss.GetMasterNode()
275
    except errors.ConfigurationError:
276
      raise errors.OpPrereqError("Cluster not initialized or this machine is"
277
                                 " not part of a cluster",
278
                                 errors.ECODE_INVAL)
279

  
280
    master, myself = ssconf.GetMasterAndMyself(ss=ss)
281
    if master != myself:
282
      raise errors.OpPrereqError("This is not the master node, please connect"
283
                                 " to node '%s' and rerun the command" %
284
                                 master, errors.ECODE_INVAL)
285
    raise
286
  return client

Also available in: Unified diff