Passed
Pull Request — master (#3212)
by Matěj
02:53
created

ssg_test_suite.common   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 5

3 Functions

Rating   Name   Duplication   Size   Complexity  
A run_cmd_remote() 0 6 1
A run_cmd_local() 0 5 1
A _run_cmd() 0 11 3
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