+ _WaitForNodeDaemon(master_name)
+
+
+def _WaitForNodeDaemon(node_name):
+ """Wait for node daemon to become responsive.
+
+ """
+ def _CheckNodeDaemon():
+ # Pylint bug <http://www.logilab.org/ticket/35642>
+ # pylint: disable=E1101
+ result = rpc.BootstrapRunner().call_version([node_name])[node_name]
+ if result.fail_msg:
+ raise utils.RetryAgain()
+
+ try:
+ utils.Retry(_CheckNodeDaemon, 1.0, _DAEMON_READY_TIMEOUT)
+ except utils.RetryTimeout:
+ raise errors.OpExecError("Node daemon on %s didn't answer queries within"
+ " %s seconds" % (node_name, _DAEMON_READY_TIMEOUT))
+
+
+def _WaitForMasterDaemon():
+ """Wait for master daemon to become responsive.
+
+ """
+ def _CheckMasterDaemon():
+ try:
+ cl = luxi.Client()
+ (cluster_name, ) = cl.QueryConfigValues(["cluster_name"])
+ except Exception:
+ raise utils.RetryAgain()
+
+ logging.debug("Received cluster name %s from master", cluster_name)
+
+ try:
+ utils.Retry(_CheckMasterDaemon, 1.0, _DAEMON_READY_TIMEOUT)
+ except utils.RetryTimeout:
+ raise errors.OpExecError("Master daemon didn't answer queries within"
+ " %s seconds" % _DAEMON_READY_TIMEOUT)
+
+
+def _WaitForSshDaemon(hostname, port, family):
+ """Wait for SSH daemon to become responsive.
+
+ """
+ hostip = netutils.GetHostname(name=hostname, family=family).ip
+
+ def _CheckSshDaemon():
+ if netutils.TcpPing(hostip, port, timeout=1.0, live_port_needed=True):
+ logging.debug("SSH daemon on %s:%s (IP address %s) has become"
+ " responsive", hostname, port, hostip)
+ else:
+ raise utils.RetryAgain()
+
+ try:
+ utils.Retry(_CheckSshDaemon, 1.0, _DAEMON_READY_TIMEOUT)
+ except utils.RetryTimeout:
+ raise errors.OpExecError("SSH daemon on %s:%s (IP address %s) didn't"
+ " become responsive within %s seconds" %
+ (hostname, port, hostip, _DAEMON_READY_TIMEOUT))
+
+
+def RunNodeSetupCmd(cluster_name, node, basecmd, debug, verbose,
+ use_cluster_key, ask_key, strict_host_check, 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}
+ @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")
+
+ family = ssconf.SimpleStore().GetPrimaryIPFamily()
+ srun = ssh.SshRunner(cluster_name,
+ ipv6=(family == netutils.IP6Address.family))
+ scmd = srun.BuildCmd(node, constants.SSH_LOGIN_USER,
+ utils.ShellQuoteArgs(cmd),
+ batch=False, ask_key=ask_key, quiet=False,
+ strict_host_check=strict_host_check,
+ use_cluster_key=use_cluster_key)
+
+ 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, netutils.GetDaemonPort(constants.SSH), family)
+
+
+def _InitFileStorageDir(file_storage_dir):
+ """Initialize if needed the file storage.
+
+ @param file_storage_dir: the user-supplied value
+ @return: either empty string (if file storage was disabled at build
+ time) or the normalized path to the storage directory
+
+ """
+ file_storage_dir = os.path.normpath(file_storage_dir)
+
+ if not os.path.isabs(file_storage_dir):
+ raise errors.OpPrereqError("File storage directory '%s' is not an absolute"
+ " path" % file_storage_dir, errors.ECODE_INVAL)
+
+ if not os.path.exists(file_storage_dir):
+ try:
+ os.makedirs(file_storage_dir, 0750)
+ except OSError, err:
+ raise errors.OpPrereqError("Cannot create file storage directory"
+ " '%s': %s" % (file_storage_dir, err),
+ errors.ECODE_ENVIRON)
+
+ if not os.path.isdir(file_storage_dir):
+ raise errors.OpPrereqError("The file storage directory '%s' is not"
+ " a directory." % file_storage_dir,
+ errors.ECODE_ENVIRON)
+
+ return file_storage_dir
+
+
+def _PrepareFileBasedStorage(
+ enabled_disk_templates, file_storage_dir,
+ default_dir, file_disk_template,
+ init_fn=_InitFileStorageDir, acceptance_fn=None):
+ """Checks if a file-base storage type is enabled and inits the dir.
+
+ @type enabled_disk_templates: list of string
+ @param enabled_disk_templates: list of enabled disk templates
+ @type file_storage_dir: string
+ @param file_storage_dir: the file storage directory
+ @type default_dir: string
+ @param default_dir: default file storage directory when C{file_storage_dir}
+ is 'None'
+ @type file_disk_template: string
+ @param file_disk_template: a disk template whose storage type is 'ST_FILE'
+ @rtype: string
+ @returns: the name of the actual file storage directory
+
+ """
+ assert (file_disk_template in
+ utils.storage.GetDiskTemplatesOfStorageType(constants.ST_FILE))
+ if file_storage_dir is None:
+ file_storage_dir = default_dir
+ if not acceptance_fn:
+ acceptance_fn = \
+ lambda path: filestorage.CheckFileStoragePathAcceptance(
+ path, exact_match_ok=True)
+
+ cluster.CheckFileStoragePathVsEnabledDiskTemplates(
+ logging.warning, file_storage_dir, enabled_disk_templates)
+
+ file_storage_enabled = file_disk_template in enabled_disk_templates
+ if file_storage_enabled:
+ try:
+ acceptance_fn(file_storage_dir)
+ except errors.FileStoragePathError as e:
+ raise errors.OpPrereqError(str(e))
+ result_file_storage_dir = init_fn(file_storage_dir)
+ else:
+ result_file_storage_dir = file_storage_dir
+ return result_file_storage_dir
+
+
+def _PrepareFileStorage(
+ enabled_disk_templates, file_storage_dir, init_fn=_InitFileStorageDir,
+ acceptance_fn=None):
+ """Checks if file storage is enabled and inits the dir.
+
+ @see: C{_PrepareFileBasedStorage}
+
+ """
+ return _PrepareFileBasedStorage(
+ enabled_disk_templates, file_storage_dir,
+ pathutils.DEFAULT_FILE_STORAGE_DIR, constants.DT_FILE,
+ init_fn=init_fn, acceptance_fn=acceptance_fn)
+
+
+def _PrepareSharedFileStorage(
+ enabled_disk_templates, file_storage_dir, init_fn=_InitFileStorageDir,
+ acceptance_fn=None):
+ """Checks if shared file storage is enabled and inits the dir.
+
+ @see: C{_PrepareFileBasedStorage}