Skip to content

Commit cbfcd2e

Browse files
authored
Merge pull request #287 from xcp-ng/ssh-background
Ssh background cleanup
2 parents f9bb219 + 0ec67e8 commit cbfcd2e

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

lib/commands.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _ellide_log_lines(log):
6161
return "\n{}".format("\n".join(reduced_message))
6262

6363
def _ssh(hostname_or_ip, cmd, check, simple_output, suppress_fingerprint_warnings,
64-
background, target_os, decode, options):
64+
background, decode, options):
6565
opts = list(options)
6666
opts.append('-o "BatchMode yes"')
6767
if suppress_fingerprint_warnings:
@@ -76,17 +76,11 @@ def _ssh(hostname_or_ip, cmd, check, simple_output, suppress_fingerprint_warning
7676
command = cmd
7777
else:
7878
command = " ".join(cmd)
79-
if background and target_os != "windows":
80-
# https://stackoverflow.com/questions/29142/getting-ssh-to-execute-a-command-in-the-background-on-target-machine
81-
# ... and run the command through a bash shell so that output redirection both works on Linux and FreeBSD.
82-
# Bash being available on VMs is a documented requirement.
83-
command = "nohup bash -c \"%s &>/dev/null &\"" % command
8479

8580
ssh_cmd = f"ssh root@{hostname_or_ip} {' '.join(opts)} {shlex.quote(command)}"
8681

87-
windows_background = background and target_os == "windows"
8882
# Fetch banner and remove it to avoid stdout/stderr pollution.
89-
if config.ignore_ssh_banner and not windows_background:
83+
if config.ignore_ssh_banner:
9084
banner_res = subprocess.run(
9185
"ssh root@%s %s '%s'" % (hostname_or_ip, ' '.join(opts), '\n'),
9286
shell=True,
@@ -95,15 +89,15 @@ def _ssh(hostname_or_ip, cmd, check, simple_output, suppress_fingerprint_warning
9589
check=False
9690
)
9791

92+
logging.debug(f"[{hostname_or_ip}] {command}")
9893
process = subprocess.Popen(
9994
ssh_cmd,
10095
shell=True,
10196
stdout=subprocess.PIPE,
10297
stderr=subprocess.STDOUT
10398
)
104-
logging.debug(f"[{hostname_or_ip}] {command}")
105-
if windows_background:
106-
return True, process
99+
if background:
100+
return True, None
107101

108102
stdout = []
109103
for line in iter(process.stdout.readline, b''):
@@ -140,9 +134,9 @@ def _ssh(hostname_or_ip, cmd, check, simple_output, suppress_fingerprint_warning
140134
# This function is kept short for shorter pytest traces upon SSH failures, which are common,
141135
# as pytest prints the whole function definition that raised the SSHCommandFailed exception
142136
def ssh(hostname_or_ip, cmd, check=True, simple_output=True, suppress_fingerprint_warnings=True,
143-
background=False, target_os='linux', decode=True, options=[]):
137+
background=False, decode=True, options=[]):
144138
success, result_or_exc = _ssh(hostname_or_ip, cmd, check, simple_output, suppress_fingerprint_warnings,
145-
background, target_os, decode, options)
139+
background, decode, options)
146140
if not success:
147141
raise result_or_exc
148142
return result_or_exc

lib/vm.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ def try_get_and_store_ip(self):
8484

8585
def ssh(self, cmd, check=True, simple_output=True, background=False, decode=True):
8686
# raises by default for any nonzero return code
87-
target_os = "windows" if self.is_windows else "linux"
8887
return commands.ssh(self.ip, cmd, check=check, simple_output=simple_output, background=background,
89-
target_os=target_os, decode=decode)
88+
decode=decode)
9089

9190
def ssh_with_result(self, cmd):
9291
# doesn't raise if the command's return is nonzero, unless there's a SSH error
@@ -271,9 +270,15 @@ def start_background_process(self, cmd):
271270
self.sftp_put(f.name, script.replace("/tmp/", "/Users/root/AppData/Local/Temp/"))
272271
else:
273272
self.scp(f.name, script)
274-
# Use bash to run the script, to avoid being hit by differences between shells, for example on FreeBSD
273+
274+
# https://stackoverflow.com/questions/29142/getting-ssh-to-execute-a-command-in-the-background-on-target-machine
275+
# ... and run the command through a bash shell so that output redirection both works on Linux and FreeBSD.
275276
# It is a documented requirement that bash is present on all test VMs.
276-
self.ssh(['bash', script], background=True)
277+
remote_cmd = f"bash {script}"
278+
if not self.is_windows:
279+
remote_cmd = f'nohup bash -c "{remote_cmd} &>/dev/null &"'
280+
self.ssh(remote_cmd, background=True)
281+
277282
wait_for(lambda: self.ssh_with_result(['test', '-f', pidfile]),
278283
"wait for pid file %s to exist" % pidfile)
279284
pid = self.ssh(['cat', pidfile])

0 commit comments

Comments
 (0)