mirror of
https://github.com/docker/compose.git
synced 2025-07-23 21:54:40 +02:00
Fix env_file and environment when used with extends.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
83760d0e9e
commit
8fb6fb7b19
@ -257,16 +257,11 @@ class ServiceExtendsResolver(object):
|
|||||||
def run(self):
|
def run(self):
|
||||||
self.detect_cycle()
|
self.detect_cycle()
|
||||||
|
|
||||||
service_dict = dict(self.service_config.config)
|
if 'extends' in self.service_config.config:
|
||||||
env = resolve_environment(self.working_dir, self.service_config.config)
|
|
||||||
if env:
|
|
||||||
service_dict['environment'] = env
|
|
||||||
service_dict.pop('env_file', None)
|
|
||||||
|
|
||||||
if 'extends' in service_dict:
|
|
||||||
service_dict = self.resolve_extends(*self.validate_and_construct_extends())
|
service_dict = self.resolve_extends(*self.validate_and_construct_extends())
|
||||||
|
return self.service_config._replace(config=service_dict)
|
||||||
|
|
||||||
return self.service_config._replace(config=service_dict)
|
return self.service_config
|
||||||
|
|
||||||
def validate_and_construct_extends(self):
|
def validate_and_construct_extends(self):
|
||||||
extends = self.service_config.config['extends']
|
extends = self.service_config.config['extends']
|
||||||
@ -316,16 +311,15 @@ class ServiceExtendsResolver(object):
|
|||||||
return filename
|
return filename
|
||||||
|
|
||||||
|
|
||||||
def resolve_environment(working_dir, service_dict):
|
def resolve_environment(service_config):
|
||||||
"""Unpack any environment variables from an env_file, if set.
|
"""Unpack any environment variables from an env_file, if set.
|
||||||
Interpolate environment values if set.
|
Interpolate environment values if set.
|
||||||
"""
|
"""
|
||||||
if 'environment' not in service_dict and 'env_file' not in service_dict:
|
service_dict = service_config.config
|
||||||
return {}
|
|
||||||
|
|
||||||
env = {}
|
env = {}
|
||||||
if 'env_file' in service_dict:
|
if 'env_file' in service_dict:
|
||||||
for env_file in get_env_files(working_dir, service_dict):
|
for env_file in get_env_files(service_config.working_dir, service_dict):
|
||||||
env.update(env_vars_from_file(env_file))
|
env.update(env_vars_from_file(env_file))
|
||||||
|
|
||||||
env.update(parse_environment(service_dict.get('environment')))
|
env.update(parse_environment(service_dict.get('environment')))
|
||||||
@ -362,6 +356,10 @@ def process_service(service_config):
|
|||||||
working_dir = service_config.working_dir
|
working_dir = service_config.working_dir
|
||||||
service_dict = dict(service_config.config)
|
service_dict = dict(service_config.config)
|
||||||
|
|
||||||
|
if 'environment' in service_dict or 'env_file' in service_dict:
|
||||||
|
service_dict['environment'] = resolve_environment(service_config)
|
||||||
|
service_dict.pop('env_file', None)
|
||||||
|
|
||||||
if 'volumes' in service_dict and service_dict.get('volume_driver') is None:
|
if 'volumes' in service_dict and service_dict.get('volume_driver') is None:
|
||||||
service_dict['volumes'] = resolve_volume_paths(working_dir, service_dict)
|
service_dict['volumes'] = resolve_volume_paths(working_dir, service_dict)
|
||||||
|
|
||||||
|
@ -46,7 +46,8 @@ class DockerClientTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
service_config = ServiceConfig('.', None, name, kwargs)
|
service_config = ServiceConfig('.', None, name, kwargs)
|
||||||
options = process_service(service_config)
|
options = process_service(service_config)
|
||||||
options['environment'] = resolve_environment('.', kwargs)
|
options['environment'] = resolve_environment(
|
||||||
|
service_config._replace(config=options))
|
||||||
labels = options.setdefault('labels', {})
|
labels = options.setdefault('labels', {})
|
||||||
labels['com.docker.compose.test-name'] = self.id()
|
labels['com.docker.compose.test-name'] = self.id()
|
||||||
|
|
||||||
|
@ -1317,6 +1317,54 @@ class ExtendsTest(unittest.TestCase):
|
|||||||
},
|
},
|
||||||
]))
|
]))
|
||||||
|
|
||||||
|
def test_extends_with_environment_and_env_files(self):
|
||||||
|
tmpdir = py.test.ensuretemp('test_extends_with_environment')
|
||||||
|
self.addCleanup(tmpdir.remove)
|
||||||
|
commondir = tmpdir.mkdir('common')
|
||||||
|
commondir.join('base.yml').write("""
|
||||||
|
app:
|
||||||
|
image: 'example/app'
|
||||||
|
env_file:
|
||||||
|
- 'envs'
|
||||||
|
environment:
|
||||||
|
- SECRET
|
||||||
|
""")
|
||||||
|
tmpdir.join('docker-compose.yml').write("""
|
||||||
|
ext:
|
||||||
|
extends:
|
||||||
|
file: common/base.yml
|
||||||
|
service: app
|
||||||
|
env_file:
|
||||||
|
- 'envs'
|
||||||
|
environment:
|
||||||
|
- THING
|
||||||
|
""")
|
||||||
|
commondir.join('envs').write("""
|
||||||
|
COMMON_ENV_FILE=1
|
||||||
|
""")
|
||||||
|
tmpdir.join('envs').write("""
|
||||||
|
FROM_ENV_FILE=1
|
||||||
|
""")
|
||||||
|
|
||||||
|
expected = [
|
||||||
|
{
|
||||||
|
'name': 'ext',
|
||||||
|
'image': 'example/app',
|
||||||
|
'environment': {
|
||||||
|
'SECRET': 'secret',
|
||||||
|
'FROM_ENV_FILE': '1',
|
||||||
|
'COMMON_ENV_FILE': '1',
|
||||||
|
'THING': 'thing',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
with mock.patch.dict(os.environ):
|
||||||
|
os.environ['SECRET'] = 'secret'
|
||||||
|
os.environ['THING'] = 'thing'
|
||||||
|
config = load_from_filename(str(tmpdir.join('docker-compose.yml')))
|
||||||
|
|
||||||
|
assert config == expected
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
|
@pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
|
||||||
class ExpandPathTest(unittest.TestCase):
|
class ExpandPathTest(unittest.TestCase):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user