+ def __init__(self, cluster_name, ipv6=False):
+ """Initializes this class.
+
+ @type cluster_name: str
+ @param cluster_name: name of the cluster
+ @type ipv6: bool
+ @param ipv6: If true, force ssh to use IPv6 addresses only
+
+ """
+ self.cluster_name = cluster_name
+ self.ipv6 = ipv6
+
+ def _BuildSshOptions(self, batch, ask_key, use_cluster_key,
+ strict_host_check, private_key=None, quiet=True):
+ """Builds a list with needed SSH options.
+
+ @param batch: same as ssh's batch option
+ @param ask_key: allows ssh to ask for key confirmation; this
+ parameter conflicts with the batch one
+ @param use_cluster_key: if True, use the cluster name as the
+ HostKeyAlias name
+ @param strict_host_check: this makes the host key checking strict
+ @param private_key: use this private key instead of the default
+ @param quiet: whether to enable -q to ssh
+
+ @rtype: list
+ @return: the list of options ready to use in L{utils.RunCmd}
+
+ """
+ options = [
+ "-oEscapeChar=none",
+ "-oHashKnownHosts=no",
+ "-oGlobalKnownHostsFile=%s" % constants.SSH_KNOWN_HOSTS_FILE,
+ "-oUserKnownHostsFile=/dev/null",
+ "-oCheckHostIp=no",
+ ]
+
+ if use_cluster_key:
+ options.append("-oHostKeyAlias=%s" % self.cluster_name)
+
+ if quiet:
+ options.append("-q")
+
+ if private_key:
+ options.append("-i%s" % private_key)
+
+ # TODO: Too many boolean options, maybe convert them to more descriptive
+ # constants.
+
+ # Note: ask_key conflicts with batch mode
+ if batch:
+ if ask_key:
+ raise errors.ProgrammerError("SSH call requested conflicting options")
+
+ options.append("-oBatchMode=yes")
+
+ if strict_host_check:
+ options.append("-oStrictHostKeyChecking=yes")
+ else:
+ options.append("-oStrictHostKeyChecking=no")
+
+ else:
+ # non-batch mode
+
+ if ask_key:
+ options.append("-oStrictHostKeyChecking=ask")
+ elif strict_host_check:
+ options.append("-oStrictHostKeyChecking=yes")
+ else:
+ options.append("-oStrictHostKeyChecking=no")
+
+ if self.ipv6:
+ options.append("-6")
+
+ return options
+
+ def BuildCmd(self, hostname, user, command, batch=True, ask_key=False,
+ tty=False, use_cluster_key=True, strict_host_check=True,
+ private_key=None, quiet=True):
+ """Build an ssh command to execute a command on a remote node.
+
+ @param hostname: the target host, string
+ @param user: user to auth as
+ @param command: the command
+ @param batch: if true, ssh will run in batch mode with no prompting
+ @param ask_key: if true, ssh will run with
+ StrictHostKeyChecking=ask, so that we can connect to an
+ unknown host (not valid in batch mode)
+ @param use_cluster_key: whether to expect and use the
+ cluster-global SSH key
+ @param strict_host_check: whether to check the host's SSH key at all
+ @param private_key: use this private key instead of the default
+ @param quiet: whether to enable -q to ssh
+
+ @return: the ssh call to run 'command' on the remote host.
+
+ """
+ argv = [constants.SSH]
+ argv.extend(self._BuildSshOptions(batch, ask_key, use_cluster_key,
+ strict_host_check, private_key,
+ quiet=quiet))
+ if tty:
+ argv.extend(["-t", "-t"])
+ argv.extend(["%s@%s" % (user, hostname), command])
+ return argv
+
+ def Run(self, *args, **kwargs):
+ """Runs a command on a remote node.
+
+ This method has the same return value as `utils.RunCmd()`, which it
+ uses to launch ssh.