mirror of https://github.com/docker/docker-py.git
Merge branch 'attach-websocket' of github.com:aanand/docker-py into aanand-attach-websocket
Conflicts: docker/client.py
This commit is contained in:
commit
0901b28957
|
|
@ -24,6 +24,8 @@ import docker.auth as auth
|
||||||
import docker.unixconn as unixconn
|
import docker.unixconn as unixconn
|
||||||
import docker.utils as utils
|
import docker.utils as utils
|
||||||
|
|
||||||
|
import websocket
|
||||||
|
|
||||||
|
|
||||||
class APIError(requests.exceptions.HTTPError):
|
class APIError(requests.exceptions.HTTPError):
|
||||||
def __init__(self, message, response, explanation=None):
|
def __init__(self, message, response, explanation=None):
|
||||||
|
|
@ -142,24 +144,32 @@ class Client(requests.Session):
|
||||||
kwargs['headers']['Content-Type'] = 'application/json'
|
kwargs['headers']['Content-Type'] = 'application/json'
|
||||||
return self.post(url, json.dumps(data2), **kwargs)
|
return self.post(url, json.dumps(data2), **kwargs)
|
||||||
|
|
||||||
def attach_socket(self, container, params=None):
|
def attach_socket(self, container, params=None, ws=False):
|
||||||
if params is None:
|
if ws:
|
||||||
params = {
|
return self._attach_websocket(container, params)
|
||||||
'stdout': 1,
|
|
||||||
'stderr': 1,
|
|
||||||
'stream': 1
|
|
||||||
}
|
|
||||||
if isinstance(container, dict):
|
if isinstance(container, dict):
|
||||||
container = container.get('Id')
|
container = container.get('Id')
|
||||||
|
|
||||||
u = self._url("/containers/{0}/attach".format(container))
|
u = self._url("/containers/{0}/attach".format(container))
|
||||||
res = self.post(u, None, params=params, stream=True)
|
res = self.post(u, None, params=self._attach_params(params),
|
||||||
|
stream=True)
|
||||||
self._raise_for_status(res)
|
self._raise_for_status(res)
|
||||||
# hijack the underlying socket from requests, icky
|
# hijack the underlying socket from requests, icky
|
||||||
# but for some reason requests.iter_contents and ilk
|
# but for some reason requests.iter_contents and ilk
|
||||||
# eventually block
|
# eventually block
|
||||||
return res.raw._fp.fp._sock
|
return res.raw._fp.fp._sock
|
||||||
|
|
||||||
|
def _attach_websocket(self, container, params=None):
|
||||||
|
url = self._url("/containers/{0}/attach/ws".format(container))
|
||||||
|
req = requests.Request("POST", url, params=self._attach_params(params))
|
||||||
|
full_url = req.prepare().url
|
||||||
|
full_url = full_url.replace("http://", "ws://", 1)
|
||||||
|
full_url = full_url.replace("https://", "wss://", 1)
|
||||||
|
return self._create_websocket_connection(full_url)
|
||||||
|
|
||||||
|
def _create_websocket_connection(self, url):
|
||||||
|
return websocket.create_connection(url)
|
||||||
|
|
||||||
def attach(self, container):
|
def attach(self, container):
|
||||||
socket = self.attach_socket(container)
|
socket = self.attach_socket(container)
|
||||||
|
|
||||||
|
|
@ -170,6 +180,13 @@ class Client(requests.Session):
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
def _attach_params(self, override=None):
|
||||||
|
return override or {
|
||||||
|
'stdout': 1,
|
||||||
|
'stderr': 1,
|
||||||
|
'stream': 1
|
||||||
|
}
|
||||||
|
|
||||||
def build(self, path=None, tag=None, quiet=False, fileobj=None,
|
def build(self, path=None, tag=None, quiet=False, fileobj=None,
|
||||||
nocache=False, rm=False):
|
nocache=False, rm=False):
|
||||||
remote = context = headers = None
|
remote = context = headers = None
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
mock==1.0.1
|
mock==1.0.1
|
||||||
requests==1.2.3
|
requests==1.2.3
|
||||||
six==1.3.0
|
six==1.3.0
|
||||||
|
websocket-client==0.11.0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue