From 104f4ca1b407a19c9ca508129ce73b2323388e22 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Wed, 4 Nov 2009 10:16:03 +0100
Subject: [PATCH] Introduce a wrapper for hostname resolving
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/bootstrap.py    |  4 ++--
 lib/cmdlib.py       |  8 ++++----
 lib/errors.py       |  2 ++
 lib/utils.py        | 10 ++++++++++
 scripts/gnt-cluster |  2 +-
 scripts/gnt-node    |  2 +-
 6 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/lib/bootstrap.py b/lib/bootstrap.py
index 1401aba93..6d53c2ee5 100644
--- a/lib/bootstrap.py
+++ b/lib/bootstrap.py
@@ -170,7 +170,7 @@ def InitCluster(cluster_name, mac_prefix,
                                " entries: %s" % invalid_hvs,
                                errors.ECODE_INVAL)
 
-  hostname = utils.HostInfo()
+  hostname = utils.GetHostInfo()
 
   if hostname.ip.startswith("127."):
     raise errors.OpPrereqError("This host's IP resolves to the private"
@@ -184,7 +184,7 @@ def InitCluster(cluster_name, mac_prefix,
                                " belong to this host. Aborting." %
                                hostname.ip, errors.ECODE_ENVIRON)
 
-  clustername = utils.HostInfo(cluster_name)
+  clustername = utils.GetHostInfo(cluster_name)
 
   if utils.TcpPing(clustername.ip, constants.DEFAULT_NODED_PORT,
                    timeout=5):
diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index c054e6b0f..9b11c98ca 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -1788,7 +1788,7 @@ class LURenameCluster(LogicalUnit):
     """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
     self.ip = new_ip = hostname.ip
@@ -2843,7 +2843,7 @@ class LUAddNode(LogicalUnit):
     node_name = self.op.node_name
     cfg = self.cfg
 
-    dns_data = utils.HostInfo(node_name)
+    dns_data = utils.GetHostInfo(node_name)
 
     node = dns_data.name
     primary_ip = self.op.primary_ip = dns_data.ip
@@ -4041,7 +4041,7 @@ class LURenameInstance(LogicalUnit):
     self.instance = instance
 
     # 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
     instance_list = self.cfg.GetInstanceList()
@@ -5606,7 +5606,7 @@ class LUCreateInstance(LogicalUnit):
     #### instance parameters check
 
     # 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
 
     # this is just a preventive check, but someone might still add this
diff --git a/lib/errors.py b/lib/errors.py
index 81fa565c9..d11959130 100644
--- a/lib/errors.py
+++ b/lib/errors.py
@@ -24,6 +24,8 @@
 
 # OpPrereqError failure types
 
+# resolver errors
+ECODE_RESOLVER = "resolver_error"
 # not enough resources (iallocator failure, disk space, memory, etc.)
 ECODE_NORES = "insufficient_resources"
 # wrong arguments (at syntax level)
diff --git a/lib/utils.py b/lib/utils.py
index 36ae6e562..ec7fec838 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -590,6 +590,16 @@ class HostInfo:
     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():
   """List volume groups and their size
 
diff --git a/scripts/gnt-cluster b/scripts/gnt-cluster
index f398cc51a..f6101ba37 100755
--- a/scripts/gnt-cluster
+++ b/scripts/gnt-cluster
@@ -285,7 +285,7 @@ def ClusterCopyFile(opts, args):
 
   cl = GetClient()
 
-  myname = utils.HostInfo().name
+  myname = utils.GetHostInfo().name
 
   cluster_name = cl.QueryConfigValues(["cluster_name"])[0]
 
diff --git a/scripts/gnt-node b/scripts/gnt-node
index 205365af9..e44211054 100755
--- a/scripts/gnt-node
+++ b/scripts/gnt-node
@@ -130,7 +130,7 @@ def AddNode(opts, args):
 
   """
   cl = GetClient()
-  dns_data = utils.HostInfo(args[0])
+  dns_data = utils.GetHostInfo(args[0])
   node = dns_data.name
   readd = opts.readd
 
-- 
GitLab