|
1
|
|
|
import logging |
|
2
|
|
|
import subprocess |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
IGNORE_KNOWN_HOSTS_OPTIONS = ( |
|
6
|
|
|
"-o", "StrictHostKeyChecking=no", |
|
7
|
|
|
"-o", "UserKnownHostsFile=/dev/null", |
|
8
|
|
|
) |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def run_cmd_local(command, verbose_path, env=None): |
|
12
|
|
|
command_string = ' '.join(command) |
|
13
|
|
|
logging.debug('Running {}'.format(command_string)) |
|
14
|
|
|
returncode, output = _run_cmd(command, verbose_path, env) |
|
15
|
|
|
return returncode, output |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
def run_cmd_remote(command_string, domain_ip, verbose_path, env=None): |
|
19
|
|
|
machine = 'root@{0}'.format(domain_ip) |
|
20
|
|
|
remote_cmd = ['ssh'] + IGNORE_KNOWN_HOSTS_OPTIONS + [machine, command_string] |
|
21
|
|
|
logging.debug('Running {}'.format(command_string)) |
|
22
|
|
|
returncode, output = _run_cmd(remote_cmd, verbose_path, env) |
|
23
|
|
|
return returncode, output |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def _run_cmd(command_list, verbose_path, env=None): |
|
27
|
|
|
returncode = 0 |
|
28
|
|
|
output = b"" |
|
29
|
|
|
try: |
|
30
|
|
|
with open(verbose_path, 'w') as verbose_file: |
|
31
|
|
|
output = subprocess.check_output( |
|
32
|
|
|
command_list, stderr=verbose_file, env=env) |
|
33
|
|
|
except subprocess.CalledProcessError as e: |
|
34
|
|
|
returncode = e.returncode |
|
35
|
|
|
output = e.output |
|
36
|
|
|
return returncode, output.decode('utf-8') |
|
37
|
|
|
|