Skip to content

Commit 08d6b04

Browse files
committed
Make use of the default SSH user
1 parent b4268f8 commit 08d6b04

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

testgres/operations/remote_ops.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def __init__(self, conn_params: ConnectionParams):
5151
self.ssh_cmd = []
5252
self.remote = True
5353
self.username = conn_params.username or self.get_user()
54+
self.ssh_dest = f"{self.username}@{self.host}" if self.username else "{self.host}"
5455
self.add_known_host(self.host)
5556
self.tunnel_process = None
5657

@@ -95,9 +96,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
9596
"""
9697
ssh_cmd = []
9798
if isinstance(cmd, str):
98-
ssh_cmd = ['ssh', f"{self.username}@{self.host}"] + self.ssh_cmd + [cmd]
99+
ssh_cmd = ['ssh', self.ssh_dest] + self.ssh_cmd + [cmd]
99100
elif isinstance(cmd, list):
100-
ssh_cmd = ['ssh', f"{self.username}@{self.host}"] + self.ssh_cmd + cmd
101+
ssh_cmd = ['ssh', self.ssh_dest] + self.ssh_cmd + cmd
101102
process = subprocess.Popen(ssh_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
102103
if get_process:
103104
return process
@@ -246,9 +247,9 @@ def mkdtemp(self, prefix=None):
246247
- prefix (str): The prefix of the temporary directory name.
247248
"""
248249
if prefix:
249-
command = ["ssh"] + self.ssh_cmd + [f"{self.username}@{self.host}", f"mktemp -d {prefix}XXXXX"]
250+
command = ["ssh"] + self.ssh_cmd + [self.ssh_dest, f"mktemp -d {prefix}XXXXX"]
250251
else:
251-
command = ["ssh"] + self.ssh_cmd + [f"{self.username}@{self.host}", "mktemp -d"]
252+
command = ["ssh"] + self.ssh_cmd + [self.ssh_dest, "mktemp -d"]
252253

253254
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
254255

@@ -292,7 +293,7 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
292293

293294
with tempfile.NamedTemporaryFile(mode=mode, delete=False) as tmp_file:
294295
if not truncate:
295-
scp_cmd = ['scp'] + self.ssh_cmd + [f"{self.username}@{self.host}:{filename}", tmp_file.name]
296+
scp_cmd = ['scp'] + self.ssh_cmd + [f"{self.ssh_dest}:{filename}", tmp_file.name]
296297
subprocess.run(scp_cmd, check=False) # The file might not exist yet
297298
tmp_file.seek(0, os.SEEK_END)
298299

@@ -308,11 +309,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
308309
tmp_file.write(data)
309310

310311
tmp_file.flush()
311-
scp_cmd = ['scp'] + self.ssh_cmd + [tmp_file.name, f"{self.username}@{self.host}:{filename}"]
312+
scp_cmd = ['scp'] + self.ssh_cmd + [tmp_file.name, f"{self.ssh_dest}:{filename}"]
312313
subprocess.run(scp_cmd, check=True)
313314

314315
remote_directory = os.path.dirname(filename)
315-
mkdir_cmd = ['ssh'] + self.ssh_cmd + [f"{self.username}@{self.host}", f"mkdir -p {remote_directory}"]
316+
mkdir_cmd = ['ssh'] + self.ssh_cmd + [self.ssh_dest, f"mkdir -p {remote_directory}"]
316317
subprocess.run(mkdir_cmd, check=True)
317318

318319
os.remove(tmp_file.name)
@@ -377,7 +378,7 @@ def get_pid(self):
377378
return int(self.exec_command("echo $$", encoding=get_default_encoding()))
378379

379380
def get_process_children(self, pid):
380-
command = ["ssh"] + self.ssh_cmd + [f"{self.username}@{self.host}", f"pgrep -P {pid}"]
381+
command = ["ssh"] + self.ssh_cmd + [self.ssh_dest, f"pgrep -P {pid}"]
381382

382383
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
383384

0 commit comments

Comments
 (0)