Skip to content

Commit 0fd70b4

Browse files
committed
Properly implement exec API
1 parent 2b153a6 commit 0fd70b4

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

docker/client.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import re
1818
import shlex
1919
import struct
20+
import warnings
2021
from datetime import datetime
2122

2223
import requests
@@ -515,6 +516,18 @@ def events(self, since=None, until=None, filters=None, decode=None):
515516
@check_resource
516517
def execute(self, container, cmd, detach=False, stdout=True, stderr=True,
517518
stream=False, tty=False):
519+
warnings.warn(
520+
'Client.execute is being deprecated. Please use exec_create & '
521+
'exec_start instead', DeprecationWarning
522+
)
523+
create_res = self.exec_create(
524+
container, cmd, detach, stdout, stderr, tty
525+
)
526+
527+
return self.exec_start(create_res, detach, tty, stream)
528+
529+
def exec_create(self, container, cmd, detach=False, stdout=True,
530+
stderr=True, tty=False):
518531
if utils.compare_version('1.15', self._version) < 0:
519532
raise errors.InvalidVersion('Exec is not supported in API < 1.15')
520533
if isinstance(container, dict):
@@ -534,14 +547,44 @@ def execute(self, container, cmd, detach=False, stdout=True, stderr=True,
534547
'Cmd': cmd
535548
}
536549

537-
# create the command
538550
url = self._url('/containers/{0}/exec'.format(container))
539551
res = self._post_json(url, data=data)
540-
self._raise_for_status(res)
552+
return self._result(res, True)
553+
554+
def exec_inspect(self, exec_id):
555+
if utils.compare_version('1.15', self._version) < 0:
556+
raise errors.InvalidVersion('Exec is not supported in API < 1.15')
557+
if isinstance(exec_id, dict):
558+
exec_id = exec_id.get('Id')
559+
res = self._get(self._url("/exec/{0}/json".format(exec_id)))
560+
return self._result(res, True)
561+
562+
def exec_resize(self, exec_id, height=None, width=None):
563+
if utils.compare_version('1.15', self._version) < 0:
564+
raise errors.InvalidVersion('Exec is not supported in API < 1.15')
565+
if isinstance(exec_id, dict):
566+
exec_id = exec_id.get('Id')
567+
data = {
568+
'h': height,
569+
'w': width
570+
}
571+
res = self._post_json(
572+
self._url('exec/{0}/resize'.format(exec_id)), data
573+
)
574+
res.raise_for_status()
575+
576+
def exec_start(self, exec_id, detach=False, tty=False, stream=False):
577+
if utils.compare_version('1.15', self._version) < 0:
578+
raise errors.InvalidVersion('Exec is not supported in API < 1.15')
579+
if isinstance(exec_id, dict):
580+
exec_id = exec_id.get('Id')
581+
582+
data = {
583+
'Tty': tty,
584+
'Detach': detach
585+
}
541586

542-
# start the command
543-
cmd_id = res.json().get('Id')
544-
res = self._post_json(self._url('/exec/{0}/start'.format(cmd_id)),
587+
res = self._post_json(self._url('/exec/{0}/start'.format(exec_id)),
545588
data=data, stream=stream)
546589
self._raise_for_status(res)
547590
if stream:

0 commit comments

Comments
 (0)