Skip to content

Commit

Permalink
Merge pull request aws#76 in VFS/bamboos from feature/new-docker-py-a…
Browse files Browse the repository at this point in the history
…rgs to develop

* commit '4c7d1c51e4676d068823cd5c48460e610003efbd':
  Add new args to docker.py functions.
  • Loading branch information
kzemek committed Jan 27, 2016
2 parents 691875c + 4c7d1c5 commit fccea0e
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions docker/environment/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
# noinspection PyDefaultArgument
def run(image, docker_host=None, detach=False, dns_list=[], add_host={},
envs={}, hostname=None, interactive=False, link={}, tty=False, rm=False,
reflect=[], volumes=[], name=None, workdir=None, user=None,
run_params=[], command=None, stdin=None, stdout=None, stderr=None):
reflect=[], volumes=[], name=None, workdir=None, user=None, group=None,
group_add=[], privileged=False, run_params=[], command=None,
stdin=None, stdout=None, stderr=None):
cmd = ['docker']

if docker_host:
Expand Down Expand Up @@ -70,15 +71,24 @@ def run(image, docker_host=None, detach=False, dns_list=[], add_host={},
cmd.extend(['-w', os.path.abspath(workdir)])

if user:
cmd.extend(['-u', user])
user_group = '{0}:{1}'.format(user, group) if group else user
cmd.extend(['-u', user_group])

for g in group_add:
cmd.extend(['--group-add', g])

if privileged:
cmd.append('--privileged')

cmd.extend(run_params)
cmd.append(image)

if isinstance(command, str):
if isinstance(command, basestring):
cmd.extend(['sh', '-c', command])
elif isinstance(command, list):
cmd.extend(command)
else:
raise '{0} is not a string nor list'.format(command)

if detach:
return subprocess.check_output(cmd, stdin=stdin, stderr=stderr).decode(
Expand All @@ -87,15 +97,20 @@ def run(image, docker_host=None, detach=False, dns_list=[], add_host={},
return subprocess.call(cmd, stdin=stdin, stderr=stderr, stdout=stdout)


def exec_(container, command, docker_host=None, detach=False, interactive=False,
tty=False, output=False, stdin=None, stdout=None, stderr=None):
def exec_(container, command, docker_host=None, user=None, group=None,
detach=False, interactive=False, tty=False, privileged=False,
output=False, stdin=None, stdout=None, stderr=None):
cmd = ['docker']

if docker_host:
cmd.extend(['-H', docker_host])

cmd.append('exec')

if user:
user_group = '{0}:{1}'.format(user, group) if group else user
cmd.extend(['-u', user_group])

if detach:
cmd.append('-d')

Expand All @@ -105,12 +120,17 @@ def exec_(container, command, docker_host=None, detach=False, interactive=False,
if tty:
cmd.append('-t')

if privileged:
cmd.append('--privileged')

cmd.append(container)

if isinstance(command, str):
if isinstance(command, basestring):
cmd.extend(['sh', '-c', command])
elif isinstance(command, list):
cmd.extend(command)
else:
raise '{0} is not a string nor list'.format(command)

if detach or output:
return subprocess.check_output(cmd, stdin=stdin, stderr=stderr).decode(
Expand Down

0 comments on commit fccea0e

Please sign in to comment.