mirror of https://github.com/docker/docker-py.git
Merge pull request #662 from stevenewey/exec_create_user
Exec create user
This commit is contained in:
commit
db1a93fd27
|
@ -301,19 +301,23 @@ class Client(clientbase.ClientBase):
|
||||||
|
|
||||||
@check_resource
|
@check_resource
|
||||||
def exec_create(self, container, cmd, stdout=True, stderr=True, tty=False,
|
def exec_create(self, container, cmd, stdout=True, stderr=True, tty=False,
|
||||||
privileged=False):
|
privileged=False, user=''):
|
||||||
if utils.compare_version('1.15', self._version) < 0:
|
if utils.compare_version('1.15', self._version) < 0:
|
||||||
raise errors.InvalidVersion('Exec is not supported in API < 1.15')
|
raise errors.InvalidVersion('Exec is not supported in API < 1.15')
|
||||||
if privileged and utils.compare_version('1.19', self._version) < 0:
|
if privileged and utils.compare_version('1.19', self._version) < 0:
|
||||||
raise errors.InvalidVersion(
|
raise errors.InvalidVersion(
|
||||||
'Privileged exec is not supported in API < 1.19'
|
'Privileged exec is not supported in API < 1.19'
|
||||||
)
|
)
|
||||||
|
if user and utils.compare_version('1.19', self._version) < 0:
|
||||||
|
raise errors.InvalidVersion(
|
||||||
|
'User-specific exec is not supported in API < 1.19'
|
||||||
|
)
|
||||||
if isinstance(cmd, six.string_types):
|
if isinstance(cmd, six.string_types):
|
||||||
cmd = shlex.split(str(cmd))
|
cmd = shlex.split(str(cmd))
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'Container': container,
|
'Container': container,
|
||||||
'User': '',
|
'User': user,
|
||||||
'Privileged': privileged,
|
'Privileged': privileged,
|
||||||
'Tty': tty,
|
'Tty': tty,
|
||||||
'AttachStdin': False,
|
'AttachStdin': False,
|
||||||
|
|
|
@ -303,6 +303,7 @@ Sets up an exec instance in a running container.
|
||||||
* stdout (bool): Attach to stdout of the exec command if true. Default: True
|
* stdout (bool): Attach to stdout of the exec command if true. Default: True
|
||||||
* stderr (bool): Attach to stderr of the exec command if true. Default: True
|
* stderr (bool): Attach to stderr of the exec command if true. Default: True
|
||||||
* tty (bool): Allocate a pseudo-TTY. Default: False
|
* tty (bool): Allocate a pseudo-TTY. Default: False
|
||||||
|
* user (str): User to execute command as. Default: root
|
||||||
|
|
||||||
**Returns** (dict): A dictionary with an exec 'Id' key.
|
**Returns** (dict): A dictionary with an exec 'Id' key.
|
||||||
|
|
||||||
|
|
|
@ -895,6 +895,40 @@ class TestExecuteCommandString(BaseTestCase):
|
||||||
self.assertEqual(exec_log, expected)
|
self.assertEqual(exec_log, expected)
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
|
||||||
|
class TestExecuteCommandStringAsUser(BaseTestCase):
|
||||||
|
def runTest(self):
|
||||||
|
container = self.client.create_container('busybox', 'cat',
|
||||||
|
detach=True, stdin_open=True)
|
||||||
|
id = container['Id']
|
||||||
|
self.client.start(id)
|
||||||
|
self.tmp_containers.append(id)
|
||||||
|
|
||||||
|
res = self.client.exec_create(id, 'whoami', user='default')
|
||||||
|
self.assertIn('Id', res)
|
||||||
|
|
||||||
|
exec_log = self.client.exec_start(res)
|
||||||
|
expected = b'default' if six.PY3 else 'default\n'
|
||||||
|
self.assertEqual(exec_log, expected)
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
|
||||||
|
class TestExecuteCommandStringAsRoot(BaseTestCase):
|
||||||
|
def runTest(self):
|
||||||
|
container = self.client.create_container('busybox', 'cat',
|
||||||
|
detach=True, stdin_open=True)
|
||||||
|
id = container['Id']
|
||||||
|
self.client.start(id)
|
||||||
|
self.tmp_containers.append(id)
|
||||||
|
|
||||||
|
res = self.client.exec_create(id, 'whoami')
|
||||||
|
self.assertIn('Id', res)
|
||||||
|
|
||||||
|
exec_log = self.client.exec_start(res)
|
||||||
|
expected = b'root' if six.PY3 else 'root\n'
|
||||||
|
self.assertEqual(exec_log, expected)
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
|
@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
|
||||||
class TestExecuteCommandStreaming(BaseTestCase):
|
class TestExecuteCommandStreaming(BaseTestCase):
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
|
|
Loading…
Reference in New Issue