Skip to content

Commit

Permalink
Add withdocker option to job
Browse files Browse the repository at this point in the history
  • Loading branch information
avirshup committed Jun 4, 2018
1 parent aca38b2 commit 46e1859
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
49 changes: 24 additions & 25 deletions pyccc/engines/dockerengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,30 @@ def _generate_container_args(self, job):
if job.env:
container_args['environment'].update(job.env)

if job.engine_options:
volumes = []
binds = []

# mount the docker socket into the container
if job.engine_options.get('mount_docker_socket', False):
volumes.append('/var/run/docker.sock')
binds.append('/var/run/docker.sock:/var/run/docker.sock:rw')

# handle other mounted volumes
for volume, mount in job.engine_options.get('volumes', {}).items():
if isinstance(mount, (list, tuple)):
mountpoint, mode = mount
bind = '%s:%s:%s' % (volume, mountpoint, mode)
else:
mountpoint = mount
mode = None
bind = '%s:%s' % (volume, mountpoint)

volumes.append(mountpoint)
binds.append(bind)

if volumes or binds:
container_args['volumes'] = volumes
container_args['host_config'] = self.client.create_host_config(binds=binds)
volumes = []
binds = []

# mount the docker socket into the container (two ways to do this for backwards compat.)
if job.withdocker or job.engine_options.get('mount_docker_socket', False):
volumes.append('/var/run/docker.sock')
binds.append('/var/run/docker.sock:/var/run/docker.sock:rw')

# handle other mounted volumes
for volume, mount in job.engine_options.get('volumes', {}).items():
if isinstance(mount, (list, tuple)):
mountpoint, mode = mount
bind = '%s:%s:%s' % (volume, mountpoint, mode)
else:
mountpoint = mount
mode = None
bind = '%s:%s' % (volume, mountpoint)

volumes.append(mountpoint)
binds.append(bind)

if volumes or binds:
container_args['volumes'] = volumes
container_args['host_config'] = self.client.create_host_config(binds=binds)

return container_args

Expand Down
7 changes: 4 additions & 3 deletions pyccc/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Job(object):
contents (which can be either a FileContainer or just a string)
on_status_update (callable): function that can be called as ``func(job)``; will be called
locally whenever the job's status field is updated
withdocker (bool): whether this job needs access to a docker daemon
when_finished (callable): function that can be called as ``func(job)``; will be called
locally once, when this job completes
numcpus (int): number of CPUs required (default:1)
Expand All @@ -79,7 +80,7 @@ def __init__(self, engine=None,
name='untitled',
submit=True,
inputs=None,
requirements=None,
withdocker=False,
numcpus=1,
runtime=3600,
on_status_update=None,
Expand All @@ -92,7 +93,7 @@ def __init__(self, engine=None,
self.engine = engine
self.image = image
self.command = if_not_none(command, '')
self.engine_options = engine_options
self.engine_options = if_not_none(engine_options, {})
self.workingdir = workingdir
self.env = env

Expand All @@ -104,11 +105,11 @@ def __init__(self, engine=None,
else:
self.inputs[filename] = fileobj

self.requirements = if_not_none(requirements, {})
self.on_status_update = on_status_update
self.when_finished = when_finished
self.numcpus = numcpus
self.runtime = runtime
self.withdocker = withdocker

self._reset()

Expand Down
16 changes: 15 additions & 1 deletion pyccc/tests/test_job_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,10 @@ def _runcall(fixture, request, function, *args, **kwargs):
job.wait()
return job.result


@pytest.mark.skipif('CI_PROJECT_ID' in os.environ,
reason="Can't mount docker socket in codeship")
def test_docker_socket_mount(local_docker_engine):
def test_docker_socket_mount_with_engine_option(local_docker_engine):
engine = local_docker_engine

job = engine.launch(image='docker',
Expand All @@ -360,6 +361,19 @@ def test_docker_socket_mount(local_docker_engine):
assert job.jobid in running


@pytest.mark.skipif('CI_PROJECT_ID' in os.environ,
reason="Can't mount docker socket in codeship")
def test_docker_socket_mount_withdocker_option(local_docker_engine):
engine = local_docker_engine

job = engine.launch(image='docker',
command='docker ps -q --no-trunc',
withdocker=True)
job.wait()
running = job.stdout.strip().splitlines()
assert job.jobid in running


def test_docker_volume_mount(local_docker_engine):
"""
Note:
Expand Down

0 comments on commit 46e1859

Please sign in to comment.