Validate the hostnames at creation time
[ganeti-local] / lib / bootstrap.py
index b7a677f..7d96712 100644 (file)
@@ -107,7 +107,7 @@ def GenerateHmacKey(file_name):
   @param file_name: Path to output file
 
   """
-  utils.WriteFile(file_name, data=utils.GenerateSecret(), mode=0400)
+  utils.WriteFile(file_name, data="%s\n" % utils.GenerateSecret(), mode=0400)
 
 
 def _InitGanetiServerSetup(master_name):
@@ -126,25 +126,29 @@ def _InitGanetiServerSetup(master_name):
   if not os.path.exists(constants.HMAC_CLUSTER_KEY):
     GenerateHmacKey(constants.HMAC_CLUSTER_KEY)
 
-  result = utils.RunCmd([constants.NODE_INITD_SCRIPT, "restart"])
-
+  result = utils.RunCmd([constants.DAEMON_UTIL, "start", constants.NODED])
   if result.failed:
     raise errors.OpExecError("Could not start the node daemon, command %s"
                              " had exitcode %s and error %s" %
                              (result.cmd, result.exit_code, result.output))
 
-  # Wait for node daemon to become responsive
-  end_time = time.time() + 10.0
-  while True:
-    result = rpc.RpcRunner.call_version([master_name])[master_name]
-    if not result.fail_msg:
-      break
+  _WaitForNodeDaemon(master_name)
+
 
-    if time.time() > end_time:
-      raise errors.OpExecError("Node daemon didn't answer queries within"
-                               " 10 seconds")
+def _WaitForNodeDaemon(node_name):
+  """Wait for node daemon to become responsive.
+
+  """
+  def _CheckNodeDaemon():
+    result = rpc.RpcRunner.call_version([node_name])[node_name]
+    if result.fail_msg:
+      raise utils.RetryAgain()
 
-    time.sleep(1)
+  try:
+    utils.Retry(_CheckNodeDaemon, 1.0, 10.0)
+  except utils.RetryTimeout:
+    raise errors.OpExecError("Node daemon on %s didn't answer queries within"
+                             " 10 seconds" % node_name)
 
 
 def InitCluster(cluster_name, mac_prefix,
@@ -172,7 +176,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"
@@ -186,7 +190,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(utils.HostInfo.NormalizeName(cluster_name))
 
   if utils.TcpPing(clustername.ip, constants.DEFAULT_NODED_PORT,
                    timeout=5):
@@ -243,12 +247,6 @@ def InitCluster(cluster_name, mac_prefix,
                                (master_netdev,
                                 result.output.strip()), errors.ECODE_INVAL)
 
-  if not (os.path.isfile(constants.NODE_INITD_SCRIPT) and
-          os.access(constants.NODE_INITD_SCRIPT, os.X_OK)):
-    raise errors.OpPrereqError("Init.d script '%s' missing or not"
-                               " executable." % constants.NODE_INITD_SCRIPT,
-                               errors.ECODE_ENVIRON)
-
   dirs = [(constants.RUN_GANETI_DIR, constants.RUN_DIRS_MODE)]
   utils.EnsureDirs(dirs)
 
@@ -364,7 +362,7 @@ def FinalizeClusterDestroy(master):
   result = rpc.RpcRunner.call_node_stop_master(master, True)
   msg = result.fail_msg
   if msg:
-    logging.warning("Could not disable the master role: %s" % msg)
+    logging.warning("Could not disable the master role: %s", msg)
   result = rpc.RpcRunner.call_node_leave_cluster(master, modify_ssh_setup)
   msg = result.fail_msg
   if msg:
@@ -418,13 +416,13 @@ def SetupNodeDaemon(cluster_name, node, ssh_key_check):
                "cat > '%s' << '!EOF.' && \n"
                "%s!EOF.\n"
                "chmod 0400 %s %s %s && "
-               "%s restart" %
+               "%s start %s" %
                (constants.SSL_CERT_FILE, noded_cert,
                 constants.RAPI_CERT_FILE, rapi_cert,
                 constants.HMAC_CLUSTER_KEY, hmac_key,
                 constants.SSL_CERT_FILE, constants.RAPI_CERT_FILE,
                 constants.HMAC_CLUSTER_KEY,
-                constants.NODE_INITD_SCRIPT))
+                constants.DAEMON_UTIL, constants.NODED))
 
   result = sshrunner.Run(node, 'root', mycommand, batch=False,
                          ask_key=ssh_key_check,
@@ -435,6 +433,8 @@ def SetupNodeDaemon(cluster_name, node, ssh_key_check):
                              " output: %s" %
                              (node, result.fail_reason, result.output))
 
+  _WaitForNodeDaemon(node)
+
 
 def MasterFailover(no_voting=False):
   """Failover the master node.