1
1
import os
2
- from distutils .spawn import find_executable
3
2
import subprocess
4
3
from contextlib import contextmanager
4
+
5
+ from defaults import default_username
5
6
from testgres .logger import log
6
7
7
8
import paramiko
8
9
9
10
10
11
class OsOperations :
11
12
12
- def __init__ (self , host , ssh_key = None , username = 'dev' ):
13
+ def __init__ (self , host , ssh_key = None , username = default_username () ):
13
14
self .host = host
14
15
self .ssh_key = ssh_key
15
16
self .username = username
@@ -49,9 +50,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False):
49
50
cmd = ' ' .join (cmd )
50
51
log .debug (f"os_ops.exec_command: `{ cmd } `; remote={ self .remote } " )
51
52
# Source global profile file + execute command
52
- cmd = f"source /etc/profile.d/custom.sh; { cmd } "
53
53
try :
54
54
if self .remote :
55
+ cmd = f"source /etc/profile.d/custom.sh; { cmd } "
55
56
with self .ssh_connect () as ssh :
56
57
stdin , stdout , stderr = ssh .exec_command (cmd )
57
58
exit_status = 0
@@ -60,15 +61,15 @@ def exec_command(self, cmd, wait_exit=False, verbose=False):
60
61
result = stdout .read ().decode ('utf-8' )
61
62
error = stderr .read ().decode ('utf-8' )
62
63
else :
63
- process = subprocess .run (cmd , shell = True , text = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
64
+ process = subprocess .run (cmd , shell = True , text = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE ,
65
+ timeout = 60 )
64
66
exit_status = process .returncode
65
67
result = process .stdout
66
68
error = process .stderr
67
69
68
- if exit_status != 0 or error != '' :
69
- log .error (f"Problem in executing command: `{ cmd } `;\n error: `{ error } `;\n exit_code: { exit_status } " )
70
- if 'error' in error .lower ():
71
- exit (1 )
70
+ if exit_status != 0 or 'error' in error .lower ():
71
+ log .error (f"Problem in executing command: `{ cmd } `\n error: { error } \n exit_code: { exit_status } " )
72
+ exit (1 )
72
73
73
74
if verbose :
74
75
return exit_status , result , error
@@ -131,20 +132,17 @@ def isfile(self, remote_file):
131
132
return os .path .isfile (remote_file )
132
133
133
134
def find_executable (self , executable ):
134
- if self .remote :
135
- search_paths = self .environ ('PATH' )
136
- if not search_paths :
137
- return None
135
+ search_paths = self .environ ('PATH' )
136
+ if not search_paths :
137
+ return None
138
138
139
- search_paths = search_paths .split (self .pathsep )
140
- for path in search_paths :
141
- remote_file = os .path .join (path , executable )
142
- if self .isfile (remote_file ):
143
- return remote_file
139
+ search_paths = search_paths .split (self .pathsep )
140
+ for path in search_paths :
141
+ remote_file = os .path .join (path , executable )
142
+ if self .isfile (remote_file ):
143
+ return remote_file
144
144
145
- return None
146
- else :
147
- return find_executable (executable )
145
+ return None
148
146
149
147
def is_executable (self , file ):
150
148
# Check if the file is executable
0 commit comments