Move the master socket in the ganeti run dir
[ganeti-local] / lib / ssh.py
index 9af71f5..6f32e56 100644 (file)
@@ -30,6 +30,7 @@ from ganeti import logger
 from ganeti import utils
 from ganeti import errors
 from ganeti import constants
+from ganeti import ssconf
 
 
 KNOWN_HOSTS_OPTS = [
@@ -90,7 +91,17 @@ class SshRunner:
   """Wrapper for SSH commands.
 
   """
-  def BuildCmd(self, hostname, user, command, batch=True, ask_key=False):
+  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:
@@ -105,8 +116,9 @@ class SshRunner:
       The ssh call to run 'command' on the remote host.
 
     """
-    argv = ["ssh", "-q"]
+    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:
@@ -114,6 +126,8 @@ class SshRunner:
       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
 
@@ -149,17 +163,18 @@ class SshRunner:
       success: True/False
 
     """
-    if not os.path.isfile(filename):
-      logger.Error("file %s does not exist" % (filename))
-      return False
-
     if not os.path.isabs(filename):
       logger.Error("file %s must be an absolute path" % (filename))
       return False
 
-    command = ["scp", "-q", "-p"]
+    if not os.path.isfile(filename):
+      logger.Error("file %s does not exist" % (filename))
+      return False
+
+    command = [constants.SCP, "-q", "-p"]
     command.extend(KNOWN_HOSTS_OPTS)
     command.extend(BATCH_MODE_OPTS)
+    command.append(self._GetHostKeyAliasOption())
     command.append(filename)
     command.append("%s:%s" % (node, filename))