|
1 | | -import subprocess |
2 | | - |
3 | | -from binnacle.core import binnacle_task |
4 | | - |
5 | | - |
6 | | -def escaped_cmd(cmd): |
7 | | - return cmd.replace("'", """'"'"'""") |
8 | | - |
9 | | - |
10 | | -def escaped_ssh_cmd(cmd): |
11 | | - cmd = f"/bin/bash -c '{escaped_cmd(cmd)}'" |
12 | | - cmd = f"sudo {cmd}" if _command_config.sudo else cmd |
13 | | - |
14 | | - return escaped_cmd(cmd) |
15 | | - |
16 | | - |
17 | | -def full_cmd(cmd): |
18 | | - if _command_config.mode in ["", "local"] or _command_config.node in ["", "local"]: |
19 | | - return cmd |
20 | | - |
21 | | - if _command_config.mode == "ssh": |
22 | | - return f"""ssh {_command_config.ssh_user}@{_command_config.node} \ |
23 | | - -i {_command_config.ssh_pem_file} -p {_command_config.ssh_port} \ |
24 | | - '{escaped_ssh_cmd(cmd)}' |
25 | | - """ |
26 | | - elif _command_config.mode == 'docker': |
27 | | - return f"""docker exec -i {_command_config.node} /bin/bash -c '{escaped_cmd(cmd)}'""" |
28 | | - |
29 | | - return cmd |
30 | | - |
31 | | - |
32 | | -@binnacle_task |
33 | | -def command_run(cmd, ret = 0, task=None): |
34 | | - result = subprocess.run(cmd, shell=True, capture_output=True, universal_newlines=True) |
35 | | - task.ok = result.returncode == ret |
36 | | - if not task.ok: |
37 | | - task.info(f"Return code: {result.returncode}\n" + result.stderr.strip()) |
38 | | - return result.stdout |
39 | | - |
40 | | - |
41 | | -class CommandConfig: |
42 | | - def __init__(self): |
43 | | - self.node = "" |
44 | | - self.mode = "" |
45 | | - self.ssh_user = "root" |
46 | | - self.ssh_pem_file = "" |
47 | | - |
48 | | - |
49 | | -_command_config = CommandConfig() |
50 | | - |
51 | | - |
52 | | -def command_node(name): |
53 | | - _command_config.node = name |
54 | | - |
55 | | - |
56 | | -def command_container(name): |
57 | | - _command_config.node = name |
58 | | - |
59 | | - |
60 | | -def command_mode(name): |
61 | | - _command_config.mode = name |
62 | | - |
| 1 | +import subprocess |
| 2 | + |
| 3 | +from binnacle.core import binnacle_task |
| 4 | + |
| 5 | + |
| 6 | +def escaped_cmd(cmd): |
| 7 | + return cmd.replace("'", """'"'"'""") |
| 8 | + |
| 9 | + |
| 10 | +def escaped_ssh_cmd(cmd): |
| 11 | + cmd = f"/bin/bash -c '{escaped_cmd(cmd)}'" |
| 12 | + cmd = f"sudo {cmd}" if _command_config.sudo else cmd |
| 13 | + |
| 14 | + return escaped_cmd(cmd) |
| 15 | + |
| 16 | + |
| 17 | +def full_cmd(cmd): |
| 18 | + if _command_config.mode in ["", "local"] or _command_config.node in ["", "local"]: |
| 19 | + return cmd |
| 20 | + |
| 21 | + if _command_config.mode == "ssh": |
| 22 | + return f"""ssh {_command_config.ssh_user}@{_command_config.node} \ |
| 23 | + -i {_command_config.ssh_pem_file} -p {_command_config.ssh_port} \ |
| 24 | + '{escaped_ssh_cmd(cmd)}' |
| 25 | + """ |
| 26 | + |
| 27 | + elif _command_config.mode == 'docker': |
| 28 | + return f"""docker exec -i {_command_config.node} /bin/bash -c '{escaped_cmd(cmd)}'""" |
| 29 | + |
| 30 | + return cmd |
| 31 | + |
| 32 | + |
| 33 | +@binnacle_task |
| 34 | +def command_run(cmd, ret = 0, task=None): |
| 35 | + cmd = full_cmd(cmd) |
| 36 | + result = subprocess.run(cmd, shell=True, capture_output=True, universal_newlines=True) |
| 37 | + task.ok = result.returncode == ret |
| 38 | + if not task.ok: |
| 39 | + task.info(f"Return code: {result.returncode}\n" + result.stderr.strip()) |
| 40 | + return result.stdout |
| 41 | + |
| 42 | + |
| 43 | +class CommandConfig: |
| 44 | + def __init__(self): |
| 45 | + self.node = "" |
| 46 | + self.mode = "" |
| 47 | + self.ssh_user = "root" |
| 48 | + self.ssh_pem_file = "" |
| 49 | + self.ssh_port = "" |
| 50 | + |
| 51 | + |
| 52 | +_command_config = CommandConfig() |
| 53 | + |
| 54 | + |
| 55 | +def command_node(name): |
| 56 | + _command_config.node = name |
| 57 | + |
| 58 | + |
| 59 | +def command_container(name): |
| 60 | + _command_config.node = name |
| 61 | + |
| 62 | + |
| 63 | +def command_mode(name): |
| 64 | + _command_config.mode = name |
| 65 | + |
| 66 | + |
| 67 | +def command_ssh_user(name): |
| 68 | + _command_config.ssh_user = name |
| 69 | + |
| 70 | +def command_ssh_sudo(name): |
| 71 | + _command_config.sudo = name |
| 72 | + |
| 73 | +def command_ssh_pem_file(name): |
| 74 | + _command_config.ssh_pem_file = name |
| 75 | + |
| 76 | +def command_port(name): |
| 77 | + _command_config.ssh_port = name |
| 78 | + |
0 commit comments