Skip to content

feat: Pass group id (GID) in --user flag when calling docker run #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions cwltool/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
from .stdfsaccess import StdFsAccess
from .utils import aslist, get_feature, docker_windows_path_adjust, onWindows

# if six.PY3:
# AvroSchemaFromJSONData = avro.schema.SchemaFromJSONData
# else:
AvroSchemaFromJSONData = avro.schema.make_avsc_object

CONTENT_LIMIT = 64 * 1024
Expand Down
35 changes: 19 additions & 16 deletions cwltool/docker_uid.py → cwltool/docker_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
from __future__ import absolute_import

import subprocess
from typing import List, Text
from typing import List, Text, Tuple


def docker_vm_uid(): # type: () -> int
def docker_vm_id(): # type: () -> Tuple[int, int]
"""
Returns the UID of the default docker user inside the VM
Returns the User ID and Group ID of the default docker user inside the VM

When a host is using boot2docker or docker-machine to run docker with
boot2docker.iso (As on Mac OS X), the UID that mounts the shared filesystem
inside the VirtualBox VM is likely different than the user's UID on the host.
:return: The numeric UID (as a string) of the docker account inside
:return: A tuple containing numeric User ID and Group ID of the docker account inside
the boot2docker VM
"""
if boot2docker_running():
return boot2docker_uid()
return boot2docker_id()
elif docker_machine_running():
return docker_machine_uid()
return docker_machine_id()
else:
return None
return (None, None)


def check_output_and_strip(cmd): # type: (List[Text]) -> Text
Expand Down Expand Up @@ -95,23 +95,26 @@ def cmd_output_to_int(cmd): # type: (List[Text]) -> int
return None


def boot2docker_uid(): # type: () -> int
def boot2docker_id(): # type: () -> Tuple[int, int]
"""
Gets the UID of the docker user inside a running boot2docker vm
:return: the UID, or None if error (e.g. boot2docker not present or stopped)
Gets the UID and GID of the docker user inside a running boot2docker vm
:return: Tuple (UID, GID), or (None, None) if error (e.g. boot2docker not present or stopped)
"""
return cmd_output_to_int(['boot2docker', 'ssh', 'id', '-u'])

uid = cmd_output_to_int(['boot2docker', 'ssh', 'id', '-u'])
gid = cmd_output_to_int(['boot2docker', 'ssh', 'id', '-g'])
return (uid, gid)

def docker_machine_uid(): # type: () -> int
def docker_machine_id(): # type: () -> Tuple[int, int]
"""
Asks docker-machine for active machine and gets the UID of the docker user
inside the vm
:return: the UID, or None if error (e.g. docker-machine not present or stopped)
:return: tuple (UID, GID), or (None, None) if error (e.g. docker-machine not present or stopped)
"""
machine_name = docker_machine_name()
return cmd_output_to_int(['docker-machine', 'ssh', machine_name, "id -u"])
uid = cmd_output_to_int(['docker-machine', 'ssh', machine_name, "id -u"])
gid = cmd_output_to_int(['docker-machine', 'ssh', machine_name, "id -g"])
return (uid, gid)


if __name__ == '__main__':
print(docker_vm_uid())
print(docker_vm_id())
13 changes: 6 additions & 7 deletions cwltool/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .utils import copytree_with_merge, docker_windows_path_adjust, onWindows
from . import docker
from .builder import Builder
from .docker_uid import docker_vm_uid
from .docker_id import docker_vm_id
from .errors import WorkflowException
from .pathmapper import PathMapper
from .process import (UnsupportedRequirement, empty_subtree, get_feature,
Expand Down Expand Up @@ -391,13 +391,12 @@ def run(self, pull_image=True, rm_container=True,
if self.stdout:
runtime.append("--log-driver=none")

if onWindows(): # windows os dont have getuid or geteuid functions
euid = docker_vm_uid()
else:
euid = docker_vm_uid() or os.geteuid()
euid, egid = docker_vm_id()
if not onWindows(): # MS Windows does not have getuid() or geteuid() functions
euid, egid = euid or os.geteuid(), egid or os.getgid()

if kwargs.get("no_match_user", None) is False and euid is not None:
runtime.append(u"--user=%s" % (euid))
if kwargs.get("no_match_user", None) is False and (euid, egid) != (None, None):
runtime.append(u"--user=%d:%d" % (euid, egid))

if rm_container:
runtime.append(u"--rm")
Expand Down