From b38364e9c829539012ddf0e3f261e8a294e4e9d6 Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Mon, 23 Apr 2018 03:45:16 +0000 Subject: [PATCH] Connect to websocket proxy for exec In before, zunclient directly connects to docker daemon for doing interactive execute. This patch makes zunclient switches to connect to the websocket proxy server. Depends-On: I68e49b99eee9e6c22a9df2cc19a1d2ba5053489e Change-Id: I68d2b3daaaf8fb551d00774f01ad0ee88538f7ae Closes-Bug: #1735076 --- .../common/websocketclient/websocketclient.py | 53 ++++++++----------- zunclient/osc/v1/containers.py | 2 +- zunclient/v1/containers_shell.py | 2 +- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/zunclient/common/websocketclient/websocketclient.py b/zunclient/common/websocketclient/websocketclient.py index 58f24a70..b17f226d 100644 --- a/zunclient/common/websocketclient/websocketclient.py +++ b/zunclient/common/websocketclient/websocketclient.py @@ -31,8 +31,6 @@ import time import tty import websocket -import docker - from zunclient.common.apiclient import exceptions as acexceptions from zunclient.common.websocketclient import exceptions @@ -259,6 +257,9 @@ class WebSocketClient(BaseClient): def recv(self): return self.ws.recv() + +class AttachClient(WebSocketClient): + def tty_resize(self, height, width): """Resize the tty session @@ -272,33 +273,14 @@ class WebSocketClient(BaseClient): self.cs.containers.resize(self.id, width, height) -class HTTPClient(BaseClient): +class ExecClient(WebSocketClient): def __init__(self, zunclient, url, exec_id, id, escape='~', close_wait=0.5): - super(HTTPClient, self).__init__(zunclient, url, id, escape, + super(ExecClient, self).__init__(zunclient, url, id, escape, close_wait) 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): """Resize the tty session @@ -373,9 +355,9 @@ class WINCHHandler(object): def do_attach(zunclient, url, container_id, escape, close_wait): if url.startswith("ws://"): try: - wscls = WebSocketClient(zunclient=zunclient, url=url, - id=container_id, escape=escape, - close_wait=close_wait) + wscls = AttachClient(zunclient=zunclient, url=url, + id=container_id, escape=escape, + close_wait=close_wait) wscls.connect() wscls.handle_resize() wscls.start_loop() @@ -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): - httpcls = HTTPClient(zunclient=zunclient, url=url, exec_id=exec_id, - id=container_id, escape="~", close_wait=0.5) - httpcls.connect() - httpcls.handle_resize() - httpcls.start_loop() + if url.startswith("ws://"): + try: + wscls = ExecClient(zunclient=zunclient, url=url, + exec_id=exec_id, + 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) diff --git a/zunclient/osc/v1/containers.py b/zunclient/osc/v1/containers.py index 5e1cbb2c..56029e32 100644 --- a/zunclient/osc/v1/containers.py +++ b/zunclient/osc/v1/containers.py @@ -530,7 +530,7 @@ class ExecContainer(command.Command): response = client.containers.execute(container, **opts) if parsed_args.interactive: exec_id = response['exec_id'] - url = response['url'] + url = response['proxy_url'] websocketclient.do_exec(client, url, container, exec_id, "~", 0.5) else: output = response['output'] diff --git a/zunclient/v1/containers_shell.py b/zunclient/v1/containers_shell.py index 7de9d4fa..4cd383c6 100644 --- a/zunclient/v1/containers_shell.py +++ b/zunclient/v1/containers_shell.py @@ -493,7 +493,7 @@ def do_exec(cs, args): response = cs.containers.execute(args.container, **opts) if args.interactive: exec_id = response['exec_id'] - url = response['url'] + url = response['proxy_url'] websocketclient.do_exec(cs, url, args.container, exec_id, "~", 0.5) else: output = response['output']