Resolves #927 - fix merging command line environment with a list in the config

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
Daniel Nephin 2015-02-14 14:09:55 -05:00
commit f47431d591
5 changed files with 103 additions and 45 deletions

View file

@ -1,26 +1,25 @@
from __future__ import print_function
from __future__ import unicode_literals
from inspect import getdoc
from operator import attrgetter
import logging
import sys
import re
import signal
from operator import attrgetter
import sys
from inspect import getdoc
from docker.errors import APIError
import dockerpty
from .. import __version__
from ..project import NoSuchService, ConfigurationError
from ..service import BuildError, CannotBeScaledError
from ..service import BuildError, CannotBeScaledError, parse_environment
from .command import Command
from .docopt_command import NoSuchCommand
from .errors import UserError
from .formatter import Formatter
from .log_printer import LogPrinter
from .utils import yesno
from docker.errors import APIError
from .errors import UserError
from .docopt_command import NoSuchCommand
log = logging.getLogger(__name__)
@ -316,11 +315,10 @@ class TopLevelCommand(Command):
}
if options['-e']:
for option in options['-e']:
if 'environment' not in service.options:
service.options['environment'] = {}
k, v = option.split('=', 1)
service.options['environment'][k] = v
# Merge environment from config with -e command line
container_options['environment'] = dict(
parse_environment(service.options.get('environment')),
**parse_environment(options['-e']))
if options['--entrypoint']:
container_options['entrypoint'] = options.get('--entrypoint')

View file

@ -8,6 +8,7 @@ from operator import attrgetter
import sys
from docker.errors import APIError
import six
from .container import Container, get_container_name
from .progress_stream import stream_output, StreamOutputError
@ -450,7 +451,7 @@ class Service(object):
(parse_volume_spec(v).internal, {})
for v in container_options['volumes'])
container_options['environment'] = merge_environment(container_options)
container_options['environment'] = build_environment(container_options)
if self.can_be_built():
container_options['image'] = self.full_name
@ -629,19 +630,28 @@ def get_env_files(options):
return env_files
def merge_environment(options):
def build_environment(options):
env = {}
for f in get_env_files(options):
env.update(env_vars_from_file(f))
if 'environment' in options:
if isinstance(options['environment'], list):
env.update(dict(split_env(e) for e in options['environment']))
else:
env.update(options['environment'])
env.update(parse_environment(options.get('environment')))
return dict(resolve_env(k, v) for k, v in six.iteritems(env))
return dict(resolve_env(k, v) for k, v in env.items())
def parse_environment(environment):
if not environment:
return {}
if isinstance(environment, list):
return dict(split_env(e) for e in environment)
if isinstance(environment, dict):
return environment
raise ConfigError("environment \"%s\" must be a list or mapping," %
environment)
def split_env(env):