Commit 00003458 authored by Guido Trotter's avatar Guido Trotter
Browse files

Make import/export use the auxiliary ssh library to build the remote commands.

This avoids forgetting some parameters, as it's happening right now 
(the correct known host file is not being passed)

In order to do so we split SSHCall into an auxiliary BuildSSHCmd which builds
the command but doesn't actually call it, and SSHCall itself which runs RunCmd
on top of BuildSSHCmd's result. BuildSSHCmd is then explicitely called by 
import/export who has to build a more complex command to be run later.
parent 017778ac
...@@ -1022,13 +1022,14 @@ def ExportSnapshot(disk, dest_node, instance): ...@@ -1022,13 +1022,14 @@ def ExportSnapshot(disk, dest_node, instance):
comprcmd = "gzip" comprcmd = "gzip"
remotecmd = utils.BuildShellCmd("ssh -q -oStrictHostKeyChecking=yes" destcmd = utils.BuildShellCmd("mkdir -p %s; cat > %s/%s",
" -oBatchMode=yes -oEscapeChar=none" destdir, destdir, destfile)
" %s 'mkdir -p %s; cat > %s/%s'", remotecmd = ssh.BuildSSHCmd(dest_node, 'root', destcmd)
dest_node, destdir, destdir, destfile)
# all commands have been checked, so we're safe to combine them # all commands have been checked, so we're safe to combine them
command = '|'.join([expcmd, comprcmd, remotecmd]) command = '|'.join([expcmd, comprcmd, ' '.join(remotecmd)])
result = utils.RunCmd(command) result = utils.RunCmd(command)
...@@ -1168,9 +1169,8 @@ def ImportOSIntoInstance(instance, os_disk, swap_disk, src_node, src_image): ...@@ -1168,9 +1169,8 @@ def ImportOSIntoInstance(instance, os_disk, swap_disk, src_node, src_image):
if not os.path.exists(constants.LOG_OS_DIR): if not os.path.exists(constants.LOG_OS_DIR):
os.mkdir(constants.LOG_OS_DIR, 0750) os.mkdir(constants.LOG_OS_DIR, 0750)
remotecmd = utils.BuildShellCmd("ssh -q -oStrictHostKeyChecking=yes" destcmd = utils.BuildShellCmd('cat %s', src_image)
" -oBatchMode=yes -oEscapeChar=none" remotecmd = ssh.BuildSSHCmd(src_node, 'root', destcmd)
" %s 'cat %s'", src_node, src_image)
comprcmd = "gunzip" comprcmd = "gunzip"
impcmd = utils.BuildShellCmd("(cd %s; %s -i %s -b %s -s %s &>%s)", impcmd = utils.BuildShellCmd("(cd %s; %s -i %s -b %s -s %s &>%s)",
...@@ -1178,7 +1178,7 @@ def ImportOSIntoInstance(instance, os_disk, swap_disk, src_node, src_image): ...@@ -1178,7 +1178,7 @@ def ImportOSIntoInstance(instance, os_disk, swap_disk, src_node, src_image):
real_os_dev.dev_path, real_swap_dev.dev_path, real_os_dev.dev_path, real_swap_dev.dev_path,
logfile) logfile)
command = '|'.join([remotecmd, comprcmd, impcmd]) command = '|'.join([' '.join(remotecmd), comprcmd, impcmd])
result = utils.RunCmd(command) result = utils.RunCmd(command)
......
...@@ -54,12 +54,8 @@ ASK_KEY_OPTS = [ ...@@ -54,12 +54,8 @@ ASK_KEY_OPTS = [
"-oHashKnownHosts=no", "-oHashKnownHosts=no",
] ]
def BuildSSHCmd(hostname, user, command, batch=True, ask_key=False):
def SSHCall(hostname, user, command, batch=True, ask_key=False): """Build an ssh string to execute a command on a remote node.
"""Execute a command on a remote node.
This method has the same return value as `utils.RunCmd()`, which it
uses to launch ssh.
Args: Args:
hostname: the target host, string hostname: the target host, string
...@@ -70,7 +66,7 @@ def SSHCall(hostname, user, command, batch=True, ask_key=False): ...@@ -70,7 +66,7 @@ def SSHCall(hostname, user, command, batch=True, ask_key=False):
we can connect to an unknown host (not valid in batch mode) we can connect to an unknown host (not valid in batch mode)
Returns: Returns:
`utils.RunResult` as for `utils.RunCmd()` The ssh call to run 'command' on the remote host.
""" """
argv = ["ssh", "-q"] argv = ["ssh", "-q"]
...@@ -82,8 +78,29 @@ def SSHCall(hostname, user, command, batch=True, ask_key=False): ...@@ -82,8 +78,29 @@ def SSHCall(hostname, user, command, batch=True, ask_key=False):
argv.extend(BATCH_MODE_OPTS) argv.extend(BATCH_MODE_OPTS)
elif ask_key: elif ask_key:
argv.extend(ASK_KEY_OPTS) argv.extend(ASK_KEY_OPTS)
argv.extend(["%s@%s" % (user, hostname), command]) argv.extend(["%s@%s" % (user, hostname), "'%s'" % command])
return utils.RunCmd(argv) 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()`
"""
return utils.RunCmd(BuildSSHCmd(hostname, user, command, batch=batch, ask_key=ask_key))
def CopyFileToNode(node, filename): def CopyFileToNode(node, filename):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment