- argv = ["ssh", "-q"]
- argv.extend(KNOWN_HOSTS_OPTS)
- if batch:
- # if we are in batch mode, we can't ask the key
- if ask_key:
- raise errors.ProgrammerError("SSH call requested conflicting options")
- argv.extend(BATCH_MODE_OPTS)
- elif ask_key:
- argv.extend(ASK_KEY_OPTS)
- argv.extend(["%s@%s" % (user, hostname), command])
- return argv
-
-
-def SSHCall(hostname, user, command, batch=True, ask_key=False):
- """Execute a command on a remote node.
-
- This method has the same return value as `utils.RunCmd()`, which it
- uses to launch ssh.
-
- Args:
- hostname: the target host, string
- user: user to auth as
- command: the command
- batch: if true, ssh will run in batch mode with no prompting
- ask_key: if true, ssh will run with StrictHostKeyChecking=ask, so that
- we can connect to an unknown host (not valid in batch mode)
-
- Returns:
- `utils.RunResult` as for `utils.RunCmd()`
+ def __init__(self, sstore=None):
+ if sstore is None:
+ self.sstore = ssconf.SimpleStore()
+ else:
+ self.sstore = sstore
+
+ def _GetHostKeyAliasOption(self):
+ return "-oHostKeyAlias=%s" % self.sstore.GetClusterName()
+
+ def BuildCmd(self, hostname, user, command, batch=True, ask_key=False,
+ tty=False):
+ """Build an ssh command to execute a command on a remote node.
+
+ Args:
+ hostname: the target host, string
+ user: user to auth as
+ command: the command
+ batch: if true, ssh will run in batch mode with no prompting
+ ask_key: if true, ssh will run with StrictHostKeyChecking=ask, so that
+ we can connect to an unknown host (not valid in batch mode)
+
+ Returns:
+ The ssh call to run 'command' on the remote host.
+
+ """
+ argv = [constants.SSH, "-q"]
+ argv.extend(KNOWN_HOSTS_OPTS)
+ argv.append(self._GetHostKeyAliasOption())
+ if batch:
+ # if we are in batch mode, we can't ask the key
+ if ask_key:
+ raise errors.ProgrammerError("SSH call requested conflicting options")
+ argv.extend(BATCH_MODE_OPTS)
+ elif ask_key:
+ argv.extend(ASK_KEY_OPTS)
+ if tty:
+ argv.append("-t")
+ argv.extend(["%s@%s" % (user, hostname), command])
+ return argv
+
+ def Run(self, hostname, user, command, batch=True, ask_key=False):
+ """Runs a command on a remote node.
+
+ This method has the same return value as `utils.RunCmd()`, which it
+ uses to launch ssh.
+
+ Args:
+ hostname: the target host, string
+ user: user to auth as
+ command: the command
+ batch: if true, ssh will run in batch mode with no prompting
+ ask_key: if true, ssh will run with StrictHostKeyChecking=ask, so that
+ we can connect to an unknown host (not valid in batch mode)
+
+ Returns:
+ `utils.RunResult` like `utils.RunCmd()`
+
+ """
+ return utils.RunCmd(self.BuildCmd(hostname, user, command, batch=batch,
+ ask_key=ask_key))
+
+ def CopyFileToNode(self, node, filename):
+ """Copy a file to another node with scp.
+
+ Args:
+ node: node in the cluster
+ filename: absolute pathname of a local file
+
+ Returns:
+ success: True/False