Skip to content

Commit

Permalink
Support setting path with global config. Using the priority order in h…
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDing1995 committed Jun 3, 2019
1 parent 8250776 commit e3aab54
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
12 changes: 6 additions & 6 deletions apluslms_roman/backends/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def _client(self):
timeout = env.get('DOCKER_TIMEOUT', None)
if timeout:
kwargs['timeout'] = timeout
host_path = env.get('DOCKER_HOST_PATH', None)
if host_path:
print("Host source file path is", host_path)
# host_path = env.get('DOCKER_HOST_PATH', None)
# if host_path:
# print("Host source file path is", host_path)
return docker.from_env(environment=env, **kwargs)

def _run_opts(self, task, step):
Expand All @@ -46,14 +46,14 @@ def _run_opts(self, task, step):

# mounts and workdir
if step.mnt:
opts['mounts'] = [Mount(step.mnt, env.environ.get('DOCKER_HOST_PATH', task.path), type='bind', read_only=False)]
opts['mounts'] = [Mount(step.mnt, task.path, type='bind', read_only=False)]
opts['working_dir'] = step.mnt
else:
wpath = self.WORK_PATH
opts['mounts'] = [
Mount(wpath, None, type='tmpfs', read_only=False, tmpfs_size=self.WORK_SIZE),
Mount(join(wpath, 'src'), env.environ.get('DOCKER_HOST_PATH', task.path), type='bind', read_only=True),
Mount(join(wpath, 'build'), join(env.environ.get('DOCKER_HOST_PATH', task.path), '_build'), type='bind', read_only=False),
Mount(join(wpath, 'src'), task.path, type='bind', read_only=True),
Mount(join(wpath, 'build'), join(task.path, '_build'), type='bind', read_only=False),
]
opts['working_dir'] = wpath

Expand Down
25 changes: 24 additions & 1 deletion apluslms_roman/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from apluslms_yamlidator.utils.decorator import cached_property
from apluslms_yamlidator.utils.collections import OrderedDict

from apluslms_roman.utils.path_mapping import get_host_path
from .backends import BACKENDS, BuildTask, BuildStep, Environment
from .observer import StreamObserver
from .utils.importing import import_string
Expand Down Expand Up @@ -36,7 +37,27 @@ def build(self, step_refs: list = None, host_path=None):
backend = self._engine.backend
observer = self._observer
steps = self.get_steps(step_refs)
task = BuildTask(self.path if host_path is None else host_path, steps)
# Check if has global config set.
# Using path in global config file
print("dir mapping set in config:", self._engine._dir_mapping if hasattr(self._engine, '_dir_mapping') else None)
print("env set:", self._engine._environment.environ.get('DOCKER_HOST_PATH', None))
print("-p paramater set:", host_path)
if hasattr(self._engine, '_dir_mapping'):
print("Using roman config")
path = get_host_path(self.path, self._engine._dir_mapping)
elif self._engine._environment.environ.get('DOCKER_HOST_PATH', None) is not None:
print("Using DOCKER_HOST_PATH")
path = self._engine._environment.environ.get('DOCKER_HOST_PATH', None)
elif host_path is not None:
print("Using -p parameter")
path = host_path
else:
path = self.path
print("No config find")
task = BuildTask(path, steps)
# Using config file
# Using -p flag or global environment variable
# task = BuildTask(self.path if host_path is None else host_path, steps)
observer.enter_prepare()
backend.prepare(task, observer)
observer.enter_build()
Expand All @@ -50,6 +71,8 @@ def __init__(self, backend_class=None, settings=None):
if backend_class is None:
if settings and 'backend' in settings:
backend_class = settings['backend']
if 'docker' in settings and 'directory_map' in settings['docker']:
self._dir_mapping = dict(settings['docker']['directory_map'])
else:
from .backends.docker import DockerBackend as backend_class
if isinstance(backend_class, str):
Expand Down
7 changes: 7 additions & 0 deletions apluslms_roman/roman_config,yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

version: '1.0'
backend: docker

docker:
directory_map:
/data/: /var/lib/docker/volumes/aplus_data/_data/
4 changes: 4 additions & 0 deletions apluslms_roman/schemas/roman_settings-v1.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ properties:
type: object
additionalProperties: false
properties:
directory_map:
title: docker conatiner-host machine path mapping
description: The dictornary mapping between docker and it's host
type: object
host:
title: docker host
description: the URL to the Docker host
Expand Down
16 changes: 16 additions & 0 deletions apluslms_roman/utils/path_mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pathlib import PurePosixPath


def get_host_path(original, mapping):
ret = original
orig_path = PurePosixPath(original)
for k, v in mapping.items():
print(k, v)
try:
relative_path = orig_path.relative_to(k)
ret = PurePosixPath(v).joinpath(relative_path)
print("Get new path:", ret)
return str(ret)
except ValueError:
pass
return str(ret)

0 comments on commit e3aab54

Please sign in to comment.