Skip to content

Commit 33d19ce

Browse files
author
Robert Breker
committed
Don't pipe ncat communication - use stdin instead
Signed-off-by: Robert Breker <[email protected]>
1 parent d2ad863 commit 33d19ce

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

src/xscontainer/docker.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
"the VM that is reachable from Dom0.")
1515

1616

17-
def prepare_request_cmds(request_type, request):
18-
# @todo: can we do something smarter then piping?
19-
request_cmds = ['echo -e "%s %s HTTP/1.0\r\n"' % (request_type, request) +
20-
'| ncat -U %s' % (DOCKER_SOCKET_PATH)]
21-
return request_cmds
17+
def prepare_request_cmd():
18+
return ("ncat -U %s" % (DOCKER_SOCKET_PATH))
19+
20+
21+
def prepare_request_stdin(request_type, request):
22+
return ("%s %s HTTP/1.0\r\n\r\n" % (request_type, request))
2223

2324

2425
def _interact_with_api(session, vmuuid, request_type, request,
2526
message_error=False):
26-
request_cmds = prepare_request_cmds(request_type, request)
27-
stdout = ssh_helper.execute_ssh(session, vmuuid, request_cmds)
27+
provided_stdin = prepare_request_stdin(request_type, request)
28+
stdout = ssh_helper.execute_ssh(session, vmuuid, prepare_request_cmd(),
29+
stdin_input=provided_stdin)
2830
headerend = stdout.index('\r\n\r\n')
2931
header = stdout[:headerend]
3032
body = stdout[headerend + 4:]

src/xscontainer/docker_monitor/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ def __monitor_vm_events(self):
118118
vmuuid = self.get_uuid()
119119
ssh_client = ssh_helper.prepare_ssh_client(session, vmuuid)
120120
try:
121-
cmd = "ncat -U %s" % (docker.DOCKER_SOCKET_PATH)
121+
cmd = docker.prepare_request_cmd()
122122
log.info("__monitor_vm_events is running '%s' on VM '%s'"
123123
% (cmd, vmuuid ))
124124
stdin, stdout, _ = ssh_client.exec_command(cmd)
125-
stdin.write("GET /events HTTP/1.0\r\n\r\n")
125+
stdin.write(docker.prepare_request_stdin('GET', '/events'))
126+
stdin.channel.shutdown_write()
126127
self._ssh_client = ssh_client
127128
data = ""
128129
# set unblocking io for select.select

src/xscontainer/ssh_helper.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def prepare_ssh_client(session, vmuuid):
100100
return client
101101

102102

103-
def execute_ssh(session, vmuuid, cmd):
103+
def execute_ssh(session, vmuuid, cmd, stdin_input=None):
104104
# The heavy weight is docker ps with plenty of containers.
105105
# Assume 283 bytes per container.
106106
# 300KB should be enough for 1085 containers.
@@ -111,9 +111,15 @@ def execute_ssh(session, vmuuid, cmd):
111111
client = prepare_ssh_client(session, vmuuid)
112112
if isinstance(cmd, list):
113113
cmd = ' '.join(cmd)
114-
log.info("execute_ssh will run '%s' on vm %s"
115-
% (cmd, vmuuid))
116-
_, stdout, _ = client.exec_command(cmd)
114+
stripped_stdin_input = stdin_input
115+
if stripped_stdin_input:
116+
stripped_stdin_input = stripped_stdin_input.strip()
117+
log.info("execute_ssh will run '%s' with stdin '%s' on vm %s"
118+
% (cmd, stripped_stdin_input, vmuuid))
119+
stdin, stdout, _ = client.exec_command(cmd)
120+
if stdin_input:
121+
stdin.write(stdin_input)
122+
stdin.channel.shutdown_write()
117123
output = stdout.read(max_read_size)
118124
if stdout.read(1) != "":
119125
raise SshException("too much data was returned when executing"

0 commit comments

Comments
 (0)