Merge "Connect to websocket proxy for exec"

This commit is contained in:
Zuul
2018-06-27 02:14:00 +00:00
committed by Gerrit Code Review
3 changed files with 24 additions and 33 deletions

View File

@@ -31,8 +31,6 @@ import time
import tty import tty
import websocket import websocket
import docker
from zunclient.common.apiclient import exceptions as acexceptions from zunclient.common.apiclient import exceptions as acexceptions
from zunclient.common.websocketclient import exceptions from zunclient.common.websocketclient import exceptions
@@ -259,6 +257,9 @@ class WebSocketClient(BaseClient):
def recv(self): def recv(self):
return self.ws.recv() return self.ws.recv()
class AttachClient(WebSocketClient):
def tty_resize(self, height, width): def tty_resize(self, height, width):
"""Resize the tty session """Resize the tty session
@@ -272,33 +273,14 @@ class WebSocketClient(BaseClient):
self.cs.containers.resize(self.id, width, height) self.cs.containers.resize(self.id, width, height)
class HTTPClient(BaseClient): class ExecClient(WebSocketClient):
def __init__(self, zunclient, url, exec_id, id, escape='~', def __init__(self, zunclient, url, exec_id, id, escape='~',
close_wait=0.5): close_wait=0.5):
super(HTTPClient, self).__init__(zunclient, url, id, escape, super(ExecClient, self).__init__(zunclient, url, id, escape,
close_wait) close_wait)
self.exec_id = exec_id self.exec_id = exec_id
def connect(self):
try:
client = docker.APIClient(base_url=self.url)
self.socket = client.exec_start(self.exec_id, socket=True,
tty=True)
print('connected to container "%s"' % self.id)
print('type %s. to disconnect' % self.escape)
except docker.errors.APIError as e:
raise exceptions.ConnectionFailed(e)
def fileno(self):
return self.socket.fileno()
def send(self, data):
self.socket.send(data)
def recv(self):
return self.socket.recv(4096)
def tty_resize(self, height, width): def tty_resize(self, height, width):
"""Resize the tty session """Resize the tty session
@@ -373,7 +355,7 @@ class WINCHHandler(object):
def do_attach(zunclient, url, container_id, escape, close_wait): def do_attach(zunclient, url, container_id, escape, close_wait):
if url.startswith("ws://"): if url.startswith("ws://"):
try: try:
wscls = WebSocketClient(zunclient=zunclient, url=url, wscls = AttachClient(zunclient=zunclient, url=url,
id=container_id, escape=escape, id=container_id, escape=escape,
close_wait=close_wait) close_wait=close_wait)
wscls.connect() wscls.connect()
@@ -387,8 +369,17 @@ def do_attach(zunclient, url, container_id, escape, close_wait):
def do_exec(zunclient, url, container_id, exec_id, escape, close_wait): def do_exec(zunclient, url, container_id, exec_id, escape, close_wait):
httpcls = HTTPClient(zunclient=zunclient, url=url, exec_id=exec_id, if url.startswith("ws://"):
id=container_id, escape="~", close_wait=0.5) try:
httpcls.connect() wscls = ExecClient(zunclient=zunclient, url=url,
httpcls.handle_resize() exec_id=exec_id,
httpcls.start_loop() id=container_id, escape=escape,
close_wait=close_wait)
wscls.connect()
wscls.handle_resize()
wscls.start_loop()
except exceptions.ContainerWebSocketException as e:
print("%(e)s:%(container)s" %
{'e': e, 'container': container_id})
else:
raise exceptions.InvalidWebSocketLink(container_id)

View File

@@ -530,7 +530,7 @@ class ExecContainer(command.Command):
response = client.containers.execute(container, **opts) response = client.containers.execute(container, **opts)
if parsed_args.interactive: if parsed_args.interactive:
exec_id = response['exec_id'] exec_id = response['exec_id']
url = response['url'] url = response['proxy_url']
websocketclient.do_exec(client, url, container, exec_id, "~", 0.5) websocketclient.do_exec(client, url, container, exec_id, "~", 0.5)
else: else:
output = response['output'] output = response['output']

View File

@@ -493,7 +493,7 @@ def do_exec(cs, args):
response = cs.containers.execute(args.container, **opts) response = cs.containers.execute(args.container, **opts)
if args.interactive: if args.interactive:
exec_id = response['exec_id'] exec_id = response['exec_id']
url = response['url'] url = response['proxy_url']
websocketclient.do_exec(cs, url, args.container, exec_id, "~", 0.5) websocketclient.do_exec(cs, url, args.container, exec_id, "~", 0.5)
else: else:
output = response['output'] output = response['output']