From 19ddc57a517d84f59c3c24da232fd98e7e85efd9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20Nussbaumer?= <rn@google.com>
Date: Fri, 23 Jul 2010 13:48:50 +0200
Subject: [PATCH] Add RPC calls to update /etc/hosts
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: RenΓ© Nussbaumer <rn@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 daemons/ganeti-noded |  9 +++++++++
 lib/backend.py       | 22 ++++++++++++++++++++++
 lib/constants.py     |  4 ++++
 lib/rpc.py           | 16 ++++++++++++++++
 4 files changed, 51 insertions(+)

diff --git a/daemons/ganeti-noded b/daemons/ganeti-noded
index 147d88db2..1148445e8 100755
--- a/daemons/ganeti-noded
+++ b/daemons/ganeti-noded
@@ -608,6 +608,15 @@ class NodeHttpServer(http.server.HttpServer):
     vgname, hypervisor_type = params
     return backend.GetNodeInfo(vgname, hypervisor_type)
 
+  @staticmethod
+  def perspective_etc_hosts_modify(params):
+    """Modify a node entry in /etc/hosts.
+
+    """
+    backend.EtcHostsModify(params[0], params[1], params[2])
+
+    return True
+
   @staticmethod
   def perspective_node_verify(params):
     """Run a verify sequence on this node.
diff --git a/lib/backend.py b/lib/backend.py
index a76826eef..2e9e34a2c 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -350,6 +350,28 @@ def StopMaster(stop_daemons):
                     result.cmd, result.exit_code, result.output)
 
 
+def EtcHostsModify(mode, host, ip):
+  """Modify a host entry in /etc/hosts.
+
+  @param mode: The mode to operate. Either add or remove entry
+  @param host: The host to operate on
+  @param ip: The ip associated with the entry
+
+  """
+  if mode == constants.ETC_HOSTS_ADD:
+    if not ip:
+      RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
+              " present")
+    utils.AddHostToEtcHosts(host, ip)
+  elif mode == constants.ETC_HOSTS_REMOVE:
+    if ip:
+      RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
+              " parameter is present")
+    utils.RemoveHostFromEtcHosts(host)
+  else:
+    RPCFail("Mode not supported")
+
+
 def LeaveCluster(modify_ssh_setup):
   """Cleans up and remove the current node.
 
diff --git a/lib/constants.py b/lib/constants.py
index 07e27b452..ca76eef63 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -841,6 +841,10 @@ ELOG_PROGRESS = "progress"
 ELOG_REMOTE_IMPORT = "remote-import"
 ELOG_JQUEUE_TEST = "jqueue-test"
 
+# /etc/hosts modification
+ETC_HOSTS_ADD = "add"
+ETC_HOSTS_REMOVE = "remove"
+
 # Job queue test
 JQT_MSGPREFIX = "TESTMSG="
 JQT_EXPANDNAMES = "expandnames"
diff --git a/lib/rpc.py b/lib/rpc.py
index 9f7cf0f2d..b2fb7cbd0 100644
--- a/lib/rpc.py
+++ b/lib/rpc.py
@@ -873,6 +873,22 @@ class RpcRunner(object):
     return self._MultiNodeCall(node_list, "node_info",
                                [vg_name, hypervisor_type])
 
+  @_RpcTimeout(_TMO_NORMAL)
+  def call_etc_hosts_modify(self, node, mode, name, ip):
+    """Modify hosts file with name
+
+    @type node: string
+    @param node: The node to call
+    @type mode: string
+    @param mode: The mode to operate. Currently "add" or "remove"
+    @type name: string
+    @param name: The host name to be modified
+    @type ip: string
+    @param ip: The ip of the entry (just valid if mode is "add")
+
+    """
+    return self._SingleNodeCall(node, "etc_hosts_modify", [mode, name, ip])
+
   @_RpcTimeout(_TMO_NORMAL)
   def call_node_verify(self, node_list, checkdict, cluster_name):
     """Request verification of given parameters.
-- 
GitLab