Skip to content

Commit 8f14440

Browse files
committed
Support setting path with global config.
1 parent 8250776 commit 8f14440

5 files changed

Lines changed: 58 additions & 7 deletions

File tree

apluslms_roman/backends/docker.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def _client(self):
2929
timeout = env.get('DOCKER_TIMEOUT', None)
3030
if timeout:
3131
kwargs['timeout'] = timeout
32-
host_path = env.get('DOCKER_HOST_PATH', None)
33-
if host_path:
34-
print("Host source file path is", host_path)
32+
# host_path = env.get('DOCKER_HOST_PATH', None)
33+
# if host_path:
34+
# print("Host source file path is", host_path)
3535
return docker.from_env(environment=env, **kwargs)
3636

3737
def _run_opts(self, task, step):
@@ -46,14 +46,14 @@ def _run_opts(self, task, step):
4646

4747
# mounts and workdir
4848
if step.mnt:
49-
opts['mounts'] = [Mount(step.mnt, env.environ.get('DOCKER_HOST_PATH', task.path), type='bind', read_only=False)]
49+
opts['mounts'] = [Mount(step.mnt, task.path, type='bind', read_only=False)]
5050
opts['working_dir'] = step.mnt
5151
else:
5252
wpath = self.WORK_PATH
5353
opts['mounts'] = [
5454
Mount(wpath, None, type='tmpfs', read_only=False, tmpfs_size=self.WORK_SIZE),
55-
Mount(join(wpath, 'src'), env.environ.get('DOCKER_HOST_PATH', task.path), type='bind', read_only=True),
56-
Mount(join(wpath, 'build'), join(env.environ.get('DOCKER_HOST_PATH', task.path), '_build'), type='bind', read_only=False),
55+
Mount(join(wpath, 'src'), task.path, type='bind', read_only=True),
56+
Mount(join(wpath, 'build'), join(task.path, '_build'), type='bind', read_only=False),
5757
]
5858
opts['working_dir'] = wpath
5959

apluslms_roman/builder.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from apluslms_yamlidator.utils.decorator import cached_property
55
from apluslms_yamlidator.utils.collections import OrderedDict
66

7+
from apluslms_roman.utils.path_mapping import get_host_path
78
from .backends import BACKENDS, BuildTask, BuildStep, Environment
89
from .observer import StreamObserver
910
from .utils.importing import import_string
@@ -36,7 +37,27 @@ def build(self, step_refs: list = None, host_path=None):
3637
backend = self._engine.backend
3738
observer = self._observer
3839
steps = self.get_steps(step_refs)
39-
task = BuildTask(self.path if host_path is None else host_path, steps)
40+
# Check if has global config set.
41+
# Using path in global config file
42+
print("dir mapping set in config:", self._engine._dir_mapping if hasattr(self._engine, '_dir_mapping') else None)
43+
print("env set:", self._engine._environment.environ.get('DOCKER_HOST_PATH', None))
44+
print("-p paramater set:", host_path)
45+
if hasattr(self._engine, '_dir_mapping'):
46+
print("Using roman config")
47+
path = get_host_path(self.path, self._engine._dir_mapping)
48+
elif self._engine._environment.environ.get('DOCKER_HOST_PATH', None) is not None:
49+
print("Using DOCKER_HOST_PATH")
50+
path = self._engine._environment.environ.get('DOCKER_HOST_PATH', None)
51+
elif host_path is not None:
52+
print("Using -p parameter")
53+
path = host_path
54+
else:
55+
path = self.path
56+
print("No config find")
57+
task = BuildTask(path, steps)
58+
# Using config file
59+
# Using -p flag or global environment variable
60+
# task = BuildTask(self.path if host_path is None else host_path, steps)
4061
observer.enter_prepare()
4162
backend.prepare(task, observer)
4263
observer.enter_build()
@@ -50,6 +71,9 @@ def __init__(self, backend_class=None, settings=None):
5071
if backend_class is None:
5172
if settings and 'backend' in settings:
5273
backend_class = settings['backend']
74+
mapping = settings['docker']['directory_map']
75+
if mapping is not None:
76+
self._dir_mapping = dict(mapping)
5377
else:
5478
from .backends.docker import DockerBackend as backend_class
5579
if isinstance(backend_class, str):

apluslms_roman/roman_config,yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
version: '1.0'
3+
backend: docker
4+
5+
docker:
6+
directory_map:
7+
/data/: /var/lib/docker/volumes/aplus_data/_data/

apluslms_roman/schemas/roman_settings-v1.0.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ properties:
2424
type: object
2525
additionalProperties: false
2626
properties:
27+
directory_map:
28+
title: docker conatiner-host machine path mapping
29+
description: The dictornary mapping between docker and it's host
30+
type: object
2731
host:
2832
title: docker host
2933
description: the URL to the Docker host
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from pathlib import PurePosixPath
2+
3+
4+
def get_host_path(original, mapping):
5+
ret = original
6+
orig_path = PurePosixPath(original)
7+
for k, v in mapping.items():
8+
print(k, v)
9+
try:
10+
relative_path = orig_path.relative_to(k)
11+
ret = PurePosixPath(v).joinpath(relative_path)
12+
print("Get new path:", ret)
13+
return str(ret)
14+
except ValueError:
15+
pass
16+
return str(ret)

0 commit comments

Comments
 (0)