Merge branch 'stable-2.12' into stable-2.13
[ganeti-github.git] / lib / bootstrap.py
index 4f0aecf..66fddb9 100644 (file)
@@ -37,7 +37,6 @@ import os.path
 import re
 import logging
 import time
-import tempfile
 
 from ganeti.cmdlib import cluster
 import ganeti.rpc.node as rpc
@@ -67,30 +66,6 @@ _INITCONF_ECID = "initconfig-ecid"
 _DAEMON_READY_TIMEOUT = 10.0
 
 
-def _InitSSHSetup():
-  """Setup the SSH configuration for the cluster.
-
-  This generates a dsa keypair for root, adds the pub key to the
-  permitted hosts and adds the hostkey to its own known hosts.
-
-  """
-  priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.SSH_LOGIN_USER)
-
-  for name in priv_key, pub_key:
-    if os.path.exists(name):
-      utils.CreateBackup(name)
-    utils.RemoveFile(name)
-
-  result = utils.RunCmd(["ssh-keygen", "-t", "dsa",
-                         "-f", priv_key,
-                         "-q", "-N", ""])
-  if result.failed:
-    raise errors.OpExecError("Could not generate ssh keypair, error %s" %
-                             result.output)
-
-  utils.AddAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
-
-
 def GenerateHmacKey(file_name):
   """Writes a new HMAC key.
 
@@ -320,93 +295,6 @@ def _WaitForSshDaemon(hostname, port):
                              (hostname, port, hostip, _DAEMON_READY_TIMEOUT))
 
 
-def RunNodeSetupCmd(cluster_name, node, basecmd, debug, verbose,
-                    use_cluster_key, ask_key, strict_host_check,
-                    port, data):
-  """Runs a command to configure something on a remote machine.
-
-  @type cluster_name: string
-  @param cluster_name: Cluster name
-  @type node: string
-  @param node: Node name
-  @type basecmd: string
-  @param basecmd: Base command (path on the remote machine)
-  @type debug: bool
-  @param debug: Enable debug output
-  @type verbose: bool
-  @param verbose: Enable verbose output
-  @type use_cluster_key: bool
-  @param use_cluster_key: See L{ssh.SshRunner.BuildCmd}
-  @type ask_key: bool
-  @param ask_key: See L{ssh.SshRunner.BuildCmd}
-  @type strict_host_check: bool
-  @param strict_host_check: See L{ssh.SshRunner.BuildCmd}
-  @type port: int
-  @param port: The SSH port of the remote machine or None for the default
-  @param data: JSON-serializable input data for script (passed to stdin)
-
-  """
-  cmd = [basecmd]
-
-  # Pass --debug/--verbose to the external script if set on our invocation
-  if debug:
-    cmd.append("--debug")
-
-  if verbose:
-    cmd.append("--verbose")
-
-  logging.debug("Node setup command: %s", cmd)
-
-  version = constants.DIR_VERSION
-  all_cmds = [["test", "-d", os.path.join(pathutils.PKGLIBDIR, version)]]
-  if constants.HAS_GNU_LN:
-    all_cmds.extend([["ln", "-s", "-f", "-T",
-                      os.path.join(pathutils.PKGLIBDIR, version),
-                      os.path.join(pathutils.SYSCONFDIR, "ganeti/lib")],
-                     ["ln", "-s", "-f", "-T",
-                      os.path.join(pathutils.SHAREDIR, version),
-                      os.path.join(pathutils.SYSCONFDIR, "ganeti/share")]])
-  else:
-    all_cmds.extend([["rm", "-f",
-                      os.path.join(pathutils.SYSCONFDIR, "ganeti/lib")],
-                     ["ln", "-s", "-f",
-                      os.path.join(pathutils.PKGLIBDIR, version),
-                      os.path.join(pathutils.SYSCONFDIR, "ganeti/lib")],
-                     ["rm", "-f",
-                      os.path.join(pathutils.SYSCONFDIR, "ganeti/share")],
-                     ["ln", "-s", "-f",
-                      os.path.join(pathutils.SHAREDIR, version),
-                      os.path.join(pathutils.SYSCONFDIR, "ganeti/share")]])
-  all_cmds.append(cmd)
-
-  if port is None:
-    port = netutils.GetDaemonPort(constants.SSH)
-
-  srun = ssh.SshRunner(cluster_name)
-  scmd = srun.BuildCmd(node, constants.SSH_LOGIN_USER,
-                       utils.ShellQuoteArgs(
-                           utils.ShellCombineCommands(all_cmds)),
-                       batch=False, ask_key=ask_key, quiet=False,
-                       strict_host_check=strict_host_check,
-                       use_cluster_key=use_cluster_key,
-                       port=port)
-
-  tempfh = tempfile.TemporaryFile()
-  try:
-    tempfh.write(serializer.DumpJson(data))
-    tempfh.seek(0)
-
-    result = utils.RunCmd(scmd, interactive=True, input_fd=tempfh)
-  finally:
-    tempfh.close()
-
-  if result.failed:
-    raise errors.OpExecError("Command '%s' failed: %s" %
-                             (result.cmd, result.fail_reason))
-
-  _WaitForSshDaemon(node, port)
-
-
 def _InitFileStorageDir(file_storage_dir):
   """Initialize if needed the file storage.
 
@@ -825,7 +713,7 @@ def InitCluster(cluster_name, mac_prefix, # pylint: disable=R0913, R0914
     utils.AddHostToEtcHosts(hostname.name, hostname.ip)
 
   if modify_ssh_setup:
-    _InitSSHSetup()
+    ssh.InitSSHSetup()
 
   if default_iallocator is not None:
     alloc_script = utils.FindFile(default_iallocator,
@@ -856,6 +744,12 @@ def InitCluster(cluster_name, mac_prefix, # pylint: disable=R0913, R0914
   if compression_tools is not None:
     cluster.CheckCompressionTools(compression_tools)
 
+  initial_dc_config = dict(active=True,
+                           interval=int(constants.MOND_TIME_INTERVAL * 1e6))
+  data_collectors = dict(
+      (name, initial_dc_config.copy())
+      for name in constants.DATA_COLLECTOR_NAMES)
+
   # init of cluster config file
   cluster_config = objects.Cluster(
     serial_no=1,
@@ -885,6 +779,7 @@ def InitCluster(cluster_name, mac_prefix, # pylint: disable=R0913, R0914
     ctime=now,
     mtime=now,
     maintain_node_health=maintain_node_health,
+    data_collectors=data_collectors,
     drbd_usermode_helper=drbd_helper,
     default_iallocator=default_iallocator,
     default_iallocator_params=default_iallocator_params,
@@ -917,6 +812,9 @@ def InitCluster(cluster_name, mac_prefix, # pylint: disable=R0913, R0914
   cfg.Update(cfg.GetClusterInfo(), logging.error)
   ssconf.WriteSsconfFiles(cfg.GetSsconfValues())
 
+  master_uuid = cfg.GetMasterNode()
+  if modify_ssh_setup:
+    ssh.InitPubKeyFile(master_uuid)
   # set up the inter-node password and certificate
   _InitGanetiServerSetup(hostname.name, cfg)
 
@@ -973,6 +871,7 @@ def InitConfig(version, cluster_config, master_node_config,
                                    instances={},
                                    networks={},
                                    disks={},
+                                   filters={},
                                    serial_no=1,
                                    ctime=now, mtime=now)
   utils.WriteFile(cfg_file,
@@ -1037,11 +936,14 @@ def SetupNodeDaemon(opts, cluster_name, node, ssh_port):
     constants.NDS_NODE_NAME: node,
     }
 
-  RunNodeSetupCmd(cluster_name, node, pathutils.NODE_DAEMON_SETUP,
-                  opts.debug, opts.verbose,
-                  True, opts.ssh_key_check, opts.ssh_key_check,
-                  ssh_port, data)
+  ssh.RunSshCmdWithStdin(cluster_name, node, pathutils.NODE_DAEMON_SETUP,
+                         ssh_port, data,
+                         debug=opts.debug, verbose=opts.verbose,
+                         use_cluster_key=True, ask_key=opts.ssh_key_check,
+                         strict_host_check=opts.ssh_key_check,
+                         ensure_version=True)
 
+  _WaitForSshDaemon(node, ssh_port)
   _WaitForNodeDaemon(node)