Commit b33e986b authored by Iustin Pop's avatar Iustin Pop
Browse files

Make gnt-* commands fail nicely on non-masters

This patch adds a check that we are on the master after failing to
connect to the socket, and log nicely the master name.

Reviewed-by: ultrotter
parent c9e5c064
...@@ -35,6 +35,7 @@ from ganeti import errors ...@@ -35,6 +35,7 @@ from ganeti import errors
from ganeti import constants from ganeti import constants
from ganeti import opcodes from ganeti import opcodes
from ganeti import luxi from ganeti import luxi
from ganeti import ssconf
from optparse import (OptionParser, make_option, TitledHelpFormatter, from optparse import (OptionParser, make_option, TitledHelpFormatter,
Option, OptionValueError) Option, OptionValueError)
...@@ -377,7 +378,7 @@ def SubmitOpCode(op, cl=None, feedback_fn=None): ...@@ -377,7 +378,7 @@ def SubmitOpCode(op, cl=None, feedback_fn=None):
""" """
if cl is None: if cl is None:
cl = luxi.Client() cl = GetClient()
job_id = cl.SubmitJob([op]) job_id = cl.SubmitJob([op])
...@@ -414,7 +415,17 @@ def SubmitOpCode(op, cl=None, feedback_fn=None): ...@@ -414,7 +415,17 @@ def SubmitOpCode(op, cl=None, feedback_fn=None):
def GetClient(): def GetClient():
# TODO: Cache object? # TODO: Cache object?
return luxi.Client() try:
client = luxi.Client()
except luxi.NoMasterError:
master, myself = ssconf.GetMasterAndMyself()
if master != myself:
raise errors.OpPrereqError("This is not the master node, please connect"
" to node '%s' and rerun the command" %
master)
else:
raise
return client
def FormatError(err): def FormatError(err):
......
...@@ -201,7 +201,21 @@ class WritableSimpleStore(SimpleStore): ...@@ -201,7 +201,21 @@ class WritableSimpleStore(SimpleStore):
uid=0, gid=0, mode=0400) uid=0, gid=0, mode=0400)
def CheckMaster(debug): def GetMasterAndMyself(ss=None):
"""Get the master node and my own hostname.
This can be either used for a 'soft' check (compared to CheckMaster,
which exits) or just for computing both at the same time.
The function does not handle any errors, these should be handled in
the caller (errors.ConfigurationError, errors.ResolverError).
"""
if ss is None:
ss = SimpleStore()
return ss.GetMasterNode(), utils.HostInfo().name
def CheckMaster(debug, ss=None):
"""Checks the node setup. """Checks the node setup.
If this is the master, the function will return. Otherwise it will If this is the master, the function will return. Otherwise it will
...@@ -209,19 +223,15 @@ def CheckMaster(debug): ...@@ -209,19 +223,15 @@ def CheckMaster(debug):
""" """
try: try:
ss = SimpleStore() master_name, myself = GetMasterAndMyself(ss)
master_name = ss.GetMasterNode()
except errors.ConfigurationError, err: except errors.ConfigurationError, err:
print "Cluster configuration incomplete: '%s'" % str(err) print "Cluster configuration incomplete: '%s'" % str(err)
sys.exit(constants.EXIT_NODESETUP_ERROR) sys.exit(constants.EXIT_NODESETUP_ERROR)
try:
myself = utils.HostInfo()
except errors.ResolverError, err: except errors.ResolverError, err:
sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0]) sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
sys.exit(constants.EXIT_NODESETUP_ERROR) sys.exit(constants.EXIT_NODESETUP_ERROR)
if myself.name != master_name: if myself != master_name:
if debug: if debug:
sys.stderr.write("Not master, exiting.\n") sys.stderr.write("Not master, exiting.\n")
sys.exit(constants.EXIT_NOTMASTER) sys.exit(constants.EXIT_NOTMASTER)
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