Skip to content

Commit 04aa555

Browse files
lib/host: Avoid using identical source and destination paths in execute_script scp
Using the same path for both source and destination may cause scp to fail, especially when the source path (e.g. a temp path on macOS) does not exist on the remote system. Thus, resorting to use mktemp on destination. Signed-off-by: Rushikesh Jadhav <[email protected]>
1 parent 2f30ecc commit 04aa555

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lib/host.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,19 @@ def execute_script(self, script_contents, shebang='sh', simple_output=True):
231231
script.write('#!/usr/bin/env ' + shebang + '\n')
232232
script.write(script_contents)
233233
script.flush()
234-
self.scp(script.name, script.name)
234+
try:
235+
remote_path = self.ssh("mktemp").strip()
236+
self.scp(script.name, remote_path)
237+
self.ssh(['chmod', '0755', remote_path])
238+
except Exception as e:
239+
logging.error("Failed to create temporary file. %s", e)
240+
raise
235241

236242
try:
237243
logging.debug(f"[{self}] # Will execute this temporary script:\n{script_contents.strip()}")
238-
return self.ssh([script.name], simple_output=simple_output)
244+
return self.ssh([remote_path], simple_output=simple_output)
239245
finally:
240-
self.ssh(['rm', '-f', script.name])
246+
self.ssh(['rm', '-f', remote_path])
241247

242248
def _get_xensource_inventory(self) -> Dict[str, str]:
243249
output = self.ssh(['cat', '/etc/xensource-inventory'])

0 commit comments

Comments
 (0)