-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_client.py
More file actions
52 lines (44 loc) · 1.55 KB
/
command_client.py
File metadata and controls
52 lines (44 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import socket
from subprocess import Popen, PIPE
import uuid
import json
class CommandClient(object):
def __init__(self, ctlloc):
if ctlloc == "in":
print "Using unix socket client for commands"
self.client = UnixSocketClient()
else:
print "Using subprocess client for commands"
self.client = SubprocessClient()
def run_command(self, command):
return self.client.run_command(command)
def run_commands(self, commands):
for command in commands:
_, err = self.run_command(command)
if err:
return False
return True
class SubprocessClient(object):
def run_command(self, command):
process = Popen(command, shell=True, executable="/bin/bash", stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
if process.returncode != 0:
return (stdout, stderr)
else:
return (stdout, None)
class UnixSocketClient(object):
SOCKET = "/var/run/command.sock"
def _send(self, request):
unix_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
unix_socket.connect(UnixSocketClient.SOCKET)
unix_socket.sendall(json.dumps(request))
response_string = unix_socket.recv(4096)
unix_socket.close()
return json.loads(response_string)
def run_command(self, command):
request = {"id": str(uuid.uuid1()), "command": command}
response = self._send(request)
exit_code = response["exit_code"]
if exit_code != 0:
return (None, "Command failed with exit code %d, stderr: %s" % (exit_code, response["stderr"]))
return (response["stdout"], None)