Commit 104f4ca1 authored by Iustin Pop's avatar Iustin Pop
Browse files

Introduce a wrapper for hostname resolving



Currently a few of the LU's CheckPrereq use utils.HostInfo which raises
a resolver error in case of failure. This is an exception from the
standard that CheckPrereq should raise an OpPrereqError if the error is
in the 'pre' phase (so that it can be retried).

This patch adds a new error code (resolver_error) and a wrapper over
utils.HostInfo that just converts the ResolverError into
OpPrereqError(…, errors.ECODE_RESOLVER). It then uses this wrapper in
cmdlib, bootstrap and some scripts.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 0ce8f948
...@@ -170,7 +170,7 @@ def InitCluster(cluster_name, mac_prefix, ...@@ -170,7 +170,7 @@ def InitCluster(cluster_name, mac_prefix,
" entries: %s" % invalid_hvs, " entries: %s" % invalid_hvs,
errors.ECODE_INVAL) errors.ECODE_INVAL)
hostname = utils.HostInfo() hostname = utils.GetHostInfo()
if hostname.ip.startswith("127."): if hostname.ip.startswith("127."):
raise errors.OpPrereqError("This host's IP resolves to the private" raise errors.OpPrereqError("This host's IP resolves to the private"
...@@ -184,7 +184,7 @@ def InitCluster(cluster_name, mac_prefix, ...@@ -184,7 +184,7 @@ def InitCluster(cluster_name, mac_prefix,
" belong to this host. Aborting." % " belong to this host. Aborting." %
hostname.ip, errors.ECODE_ENVIRON) hostname.ip, errors.ECODE_ENVIRON)
clustername = utils.HostInfo(cluster_name) clustername = utils.GetHostInfo(cluster_name)
if utils.TcpPing(clustername.ip, constants.DEFAULT_NODED_PORT, if utils.TcpPing(clustername.ip, constants.DEFAULT_NODED_PORT,
timeout=5): timeout=5):
......
...@@ -1788,7 +1788,7 @@ class LURenameCluster(LogicalUnit): ...@@ -1788,7 +1788,7 @@ class LURenameCluster(LogicalUnit):
"""Verify that the passed name is a valid one. """Verify that the passed name is a valid one.
""" """
hostname = utils.HostInfo(self.op.name) hostname = utils.GetHostInfo(self.op.name)
new_name = hostname.name new_name = hostname.name
self.ip = new_ip = hostname.ip self.ip = new_ip = hostname.ip
...@@ -2843,7 +2843,7 @@ class LUAddNode(LogicalUnit): ...@@ -2843,7 +2843,7 @@ class LUAddNode(LogicalUnit):
node_name = self.op.node_name node_name = self.op.node_name
cfg = self.cfg cfg = self.cfg
dns_data = utils.HostInfo(node_name) dns_data = utils.GetHostInfo(node_name)
node = dns_data.name node = dns_data.name
primary_ip = self.op.primary_ip = dns_data.ip primary_ip = self.op.primary_ip = dns_data.ip
...@@ -4041,7 +4041,7 @@ class LURenameInstance(LogicalUnit): ...@@ -4041,7 +4041,7 @@ class LURenameInstance(LogicalUnit):
self.instance = instance self.instance = instance
# new name verification # new name verification
name_info = utils.HostInfo(self.op.new_name) name_info = utils.GetHostInfo(self.op.new_name)
self.op.new_name = new_name = name_info.name self.op.new_name = new_name = name_info.name
instance_list = self.cfg.GetInstanceList() instance_list = self.cfg.GetInstanceList()
...@@ -5606,7 +5606,7 @@ class LUCreateInstance(LogicalUnit): ...@@ -5606,7 +5606,7 @@ class LUCreateInstance(LogicalUnit):
#### instance parameters check #### instance parameters check
# instance name verification # instance name verification
hostname1 = utils.HostInfo(self.op.instance_name) hostname1 = utils.GetHostInfo(self.op.instance_name)
self.op.instance_name = instance_name = hostname1.name self.op.instance_name = instance_name = hostname1.name
# this is just a preventive check, but someone might still add this # this is just a preventive check, but someone might still add this
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
# OpPrereqError failure types # OpPrereqError failure types
# resolver errors
ECODE_RESOLVER = "resolver_error"
# not enough resources (iallocator failure, disk space, memory, etc.) # not enough resources (iallocator failure, disk space, memory, etc.)
ECODE_NORES = "insufficient_resources" ECODE_NORES = "insufficient_resources"
# wrong arguments (at syntax level) # wrong arguments (at syntax level)
......
...@@ -590,6 +590,16 @@ class HostInfo: ...@@ -590,6 +590,16 @@ class HostInfo:
return result return result
def GetHostInfo(name=None):
"""Lookup host name and raise an OpPrereqError for failures"""
try:
return HostInfo(name)
except errors.ResolverError, err:
raise errors.OpPrereqError("The given name (%s) does not resolve: %s" %
(err[0], err[2]), errors.ECODE_RESOLVER)
def ListVolumeGroups(): def ListVolumeGroups():
"""List volume groups and their size """List volume groups and their size
......
...@@ -285,7 +285,7 @@ def ClusterCopyFile(opts, args): ...@@ -285,7 +285,7 @@ def ClusterCopyFile(opts, args):
cl = GetClient() cl = GetClient()
myname = utils.HostInfo().name myname = utils.GetHostInfo().name
cluster_name = cl.QueryConfigValues(["cluster_name"])[0] cluster_name = cl.QueryConfigValues(["cluster_name"])[0]
......
...@@ -130,7 +130,7 @@ def AddNode(opts, args): ...@@ -130,7 +130,7 @@ def AddNode(opts, args):
""" """
cl = GetClient() cl = GetClient()
dns_data = utils.HostInfo(args[0]) dns_data = utils.GetHostInfo(args[0])
node = dns_data.name node = dns_data.name
readd = opts.readd readd = opts.readd
......
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