From 8f14440828332d52b75e1fbf341c9ca82583293c Mon Sep 17 00:00:00 2001 From: Ruiyang Ding Date: Mon, 3 Jun 2019 11:53:08 +0300 Subject: [PATCH] Support setting path with global config. --- apluslms_roman/backends/docker.py | 12 ++++----- apluslms_roman/builder.py | 26 ++++++++++++++++++- apluslms_roman/roman_config,yml | 7 +++++ .../schemas/roman_settings-v1.0.yaml | 4 +++ apluslms_roman/utils/path_mapping.py | 16 ++++++++++++ 5 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 apluslms_roman/roman_config,yml create mode 100644 apluslms_roman/utils/path_mapping.py diff --git a/apluslms_roman/backends/docker.py b/apluslms_roman/backends/docker.py index 61c4b68..fc96d32 100644 --- a/apluslms_roman/backends/docker.py +++ b/apluslms_roman/backends/docker.py @@ -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): @@ -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 diff --git a/apluslms_roman/builder.py b/apluslms_roman/builder.py index 4174f64..3cd677a 100644 --- a/apluslms_roman/builder.py +++ b/apluslms_roman/builder.py @@ -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 @@ -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() @@ -50,6 +71,9 @@ def __init__(self, backend_class=None, settings=None): if backend_class is None: if settings and 'backend' in settings: backend_class = settings['backend'] + mapping = settings['docker']['directory_map'] + if mapping is not None: + self._dir_mapping = dict(mapping) else: from .backends.docker import DockerBackend as backend_class if isinstance(backend_class, str): diff --git a/apluslms_roman/roman_config,yml b/apluslms_roman/roman_config,yml new file mode 100644 index 0000000..a1fb940 --- /dev/null +++ b/apluslms_roman/roman_config,yml @@ -0,0 +1,7 @@ + +version: '1.0' +backend: docker + +docker: + directory_map: + /data/: /var/lib/docker/volumes/aplus_data/_data/ \ No newline at end of file diff --git a/apluslms_roman/schemas/roman_settings-v1.0.yaml b/apluslms_roman/schemas/roman_settings-v1.0.yaml index a972077..7a5865b 100644 --- a/apluslms_roman/schemas/roman_settings-v1.0.yaml +++ b/apluslms_roman/schemas/roman_settings-v1.0.yaml @@ -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 diff --git a/apluslms_roman/utils/path_mapping.py b/apluslms_roman/utils/path_mapping.py new file mode 100644 index 0000000..fbbac3f --- /dev/null +++ b/apluslms_roman/utils/path_mapping.py @@ -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)