Skip to content

Commit 2601cb9

Browse files
authored
test the 3 run command modes- local,ssh and docker (#12)
* test the 3 run command modes- local,ssh and docker * Add emptylines at the end of file Signed-off-by: Divyashree Jayaram <divyashreej15@gmail.com>
1 parent c6d54e3 commit 2601cb9

File tree

5 files changed

+119
-66
lines changed

5 files changed

+119
-66
lines changed

binnacle/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from binnacle.core import debug, show_summary
2-
from binnacle.plugins.http import http_get, http_post, http_put, http_delete, validated_json, http_base_url, http_set_header
3-
from binnacle.plugins.commands import command_run
4-
from binnacle.plugins.compare import compare_equal, compare_not_equal
1+
from binnacle.core import debug, show_summary
2+
from binnacle.plugins.http import http_get, http_post, http_put, http_delete, validated_json, http_base_url, http_set_header
3+
from binnacle.plugins.commands import (
4+
command_run, command_mode, command_node, command_ssh_user,
5+
command_ssh_sudo, command_ssh_pem_file, command_port, command_container
6+
)
7+
from binnacle.plugins.compare import compare_equal, compare_not_equal

binnacle/plugins/commands.py

Lines changed: 78 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,78 @@
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+

test_commands_docker.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from binnacle import *
2+
3+
#mode 3: docker
4+
command_mode("docker")
5+
command_container("server1")
6+
7+
# Check if report is generated or a file exists in a container
8+
command_run("echo 'Hello World!' > /root/reports.txt")
9+
command_run("stat /root/reports.txt") #report exixts
10+
command_run("stat /root/reports1.txt 2>/dev/null && echo 'File exists' || echo 'File does not exist'") #report1 does not exixt
11+

test_commands_local.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from binnacle import *
2+
3+
#mode1: local
4+
validated_json('{"name": 100}')
5+
command_run("ls /tmp")
6+
command_run("stat /home/ubuntu/reports.pdf")
7+

test_commands_ssh.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from binnacle import *
2+
3+
#mode2: ssh
4+
command_mode("ssh")
5+
command_node("192.168.29.65")
6+
command_ssh_user("test1")
7+
command_ssh_sudo(False)
8+
command_ssh_pem_file("~/.ssh/id_rsa")
9+
command_port("22")
10+
11+
# All the future command_run will be executed as ssh command
12+
# unless command_node("local")
13+
command_run("cat /tmp/hello")
14+
show_summary()
15+
16+

0 commit comments

Comments
 (0)