From 8709dc3c2477f59c270951a365dbfb53a2784c96 Mon Sep 17 00:00:00 2001 From: Jeff Kramer Date: Thu, 2 Jul 2015 12:51:40 -0500 Subject: [PATCH] Added env var parsing to volume container paths This commit adds environment variable parsing to the container side of the volume mapping in configs. The common use case for this is mounting SSH agent sockets in a container, using code like: volumes: - $SSH_AUTH_SOCK:$SSH_AUTH_SOCK environment: - SSH_AUTH_SOCK Signed-off-by: Jeff Kramer --- compose/config.py | 14 +++++++------- tests/unit/config_test.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/compose/config.py b/compose/config.py index cbdeca2d0..3e9065b16 100644 --- a/compose/config.py +++ b/compose/config.py @@ -184,7 +184,7 @@ def process_container_options(service_dict, working_dir=None): service_dict = service_dict.copy() if 'volumes' in service_dict: - service_dict['volumes'] = resolve_host_paths(service_dict['volumes'], working_dir=working_dir) + service_dict['volumes'] = resolve_volume_paths(service_dict['volumes'], working_dir=working_dir) if 'build' in service_dict: service_dict['build'] = resolve_build_path(service_dict['build'], working_dir=working_dir) @@ -345,18 +345,18 @@ def env_vars_from_file(filename): return env -def resolve_host_paths(volumes, working_dir=None): +def resolve_volume_paths(volumes, working_dir=None): if working_dir is None: - raise Exception("No working_dir passed to resolve_host_paths()") + raise Exception("No working_dir passed to resolve_volume_paths()") - return [resolve_host_path(v, working_dir) for v in volumes] + return [resolve_volume_path(v, working_dir) for v in volumes] -def resolve_host_path(volume, working_dir): +def resolve_volume_path(volume, working_dir): container_path, host_path = split_path_mapping(volume) + container_path = os.path.expanduser(os.path.expandvars(container_path)) if host_path is not None: - host_path = os.path.expanduser(host_path) - host_path = os.path.expandvars(host_path) + host_path = os.path.expanduser(os.path.expandvars(host_path)) return "%s:%s" % (expand_path(working_dir, host_path), container_path) else: return container_path diff --git a/tests/unit/config_test.py b/tests/unit/config_test.py index ebd2af7d5..af33421f5 100644 --- a/tests/unit/config_test.py +++ b/tests/unit/config_test.py @@ -334,6 +334,25 @@ class EnvTest(unittest.TestCase): {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''}, ) + @mock.patch.dict(os.environ) + def test_resolve_path(self): + os.environ['HOSTENV'] = '/tmp' + os.environ['CONTAINERENV'] = '/host/tmp' + + service_dict = config.make_service_dict( + 'foo', + {'volumes': ['$HOSTENV:$CONTAINERENV']}, + working_dir="tests/fixtures/env" + ) + self.assertEqual(set(service_dict['volumes']), set(['/tmp:/host/tmp'])) + + service_dict = config.make_service_dict( + 'foo', + {'volumes': ['/opt${HOSTENV}:/opt${CONTAINERENV}']}, + working_dir="tests/fixtures/env" + ) + self.assertEqual(set(service_dict['volumes']), set(['/opt/tmp:/opt/host/tmp'])) + class ExtendsTest(unittest.TestCase): def test_extends(self):