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.utils as utils
|
||||
|
||||
import websocket
|
||||
|
||||
|
||||
class APIError(requests.exceptions.HTTPError):
|
||||
def __init__(self, message, response, explanation=None):
|
||||
|
@ -142,24 +144,32 @@ class Client(requests.Session):
|
|||
kwargs['headers']['Content-Type'] = 'application/json'
|
||||
return self.post(url, json.dumps(data2), **kwargs)
|
||||
|
||||
def attach_socket(self, container, params=None):
|
||||
if params is None:
|
||||
params = {
|
||||
'stdout': 1,
|
||||
'stderr': 1,
|
||||
'stream': 1
|
||||
}
|
||||
def attach_socket(self, container, params=None, ws=False):
|
||||
if ws:
|
||||
return self._attach_websocket(container, params)
|
||||
|
||||
if isinstance(container, dict):
|
||||
container = container.get('Id')
|
||||
|
||||
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)
|
||||
# hijack the underlying socket from requests, icky
|
||||
# but for some reason requests.iter_contents and ilk
|
||||
# eventually block
|
||||
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):
|
||||
socket = self.attach_socket(container)
|
||||
|
||||
|
@ -170,6 +180,13 @@ class Client(requests.Session):
|
|||
else:
|
||||
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,
|
||||
nocache=False, rm=False):
|
||||
remote = context = headers = None
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
mock==1.0.1
|
||||
requests==1.2.3
|
||||
six==1.3.0
|
||||
websocket-client==0.11.0
|
||||
|
|
Loading…
Reference in New Issue