Interpolate environment variables
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
parent
31ac3ce22a
commit
8b5bd945d0
9 changed files with 235 additions and 35 deletions
|
|
@ -8,6 +8,7 @@ import six
|
|||
|
||||
from compose.cli.utils import find_candidates_in_parent_dirs
|
||||
|
||||
from .interpolation import interpolate_environment_variables
|
||||
from .errors import (
|
||||
ConfigurationError,
|
||||
CircularReference,
|
||||
|
|
@ -132,11 +133,11 @@ def get_config_path(base_dir):
|
|||
|
||||
def load(config_details):
|
||||
dictionary, working_dir, filename = config_details
|
||||
dictionary = interpolate_environment_variables(dictionary)
|
||||
|
||||
service_dicts = []
|
||||
|
||||
for service_name, service_dict in list(dictionary.items()):
|
||||
if not isinstance(service_dict, dict):
|
||||
raise ConfigurationError('Service "%s" doesn\'t have any configuration options. All top level keys in your docker-compose.yml must map to a dictionary of configuration options.' % service_name)
|
||||
loader = ServiceLoader(working_dir=working_dir, filename=filename)
|
||||
service_dict = loader.make_service_dict(service_name, service_dict)
|
||||
validate_paths(service_dict)
|
||||
|
|
@ -429,9 +430,9 @@ def resolve_volume_paths(volumes, working_dir=None):
|
|||
|
||||
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))
|
||||
container_path = os.path.expanduser(container_path)
|
||||
if host_path is not None:
|
||||
host_path = os.path.expanduser(os.path.expandvars(host_path))
|
||||
host_path = os.path.expanduser(host_path)
|
||||
return "%s:%s" % (expand_path(working_dir, host_path), container_path)
|
||||
else:
|
||||
return container_path
|
||||
|
|
|
|||
69
compose/config/interpolation.py
Normal file
69
compose/config/interpolation.py
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import os
|
||||
from string import Template
|
||||
from collections import defaultdict
|
||||
|
||||
import six
|
||||
|
||||
from .errors import ConfigurationError
|
||||
|
||||
|
||||
def interpolate_environment_variables(config):
|
||||
return dict(
|
||||
(service_name, process_service(service_name, service_dict))
|
||||
for (service_name, service_dict) in config.items()
|
||||
)
|
||||
|
||||
|
||||
def process_service(service_name, service_dict):
|
||||
if not isinstance(service_dict, dict):
|
||||
raise ConfigurationError(
|
||||
'Service "%s" doesn\'t have any configuration options. '
|
||||
'All top level keys in your docker-compose.yml must map '
|
||||
'to a dictionary of configuration options.' % service_name
|
||||
)
|
||||
|
||||
return dict(
|
||||
(key, interpolate_value(service_name, key, val))
|
||||
for (key, val) in service_dict.items()
|
||||
)
|
||||
|
||||
|
||||
def interpolate_value(service_name, config_key, value):
|
||||
try:
|
||||
return recursive_interpolate(value)
|
||||
except InvalidInterpolation as e:
|
||||
raise ConfigurationError(
|
||||
'Invalid interpolation format for "{config_key}" option '
|
||||
'in service "{service_name}": "{string}"'
|
||||
.format(
|
||||
config_key=config_key,
|
||||
service_name=service_name,
|
||||
string=e.string,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def recursive_interpolate(obj):
|
||||
if isinstance(obj, six.string_types):
|
||||
return interpolate(obj, os.environ)
|
||||
elif isinstance(obj, dict):
|
||||
return dict(
|
||||
(key, recursive_interpolate(val))
|
||||
for (key, val) in obj.items()
|
||||
)
|
||||
elif isinstance(obj, list):
|
||||
return map(recursive_interpolate, obj)
|
||||
else:
|
||||
return obj
|
||||
|
||||
|
||||
def interpolate(string, mapping):
|
||||
try:
|
||||
return Template(string).substitute(defaultdict(lambda: "", mapping))
|
||||
except ValueError:
|
||||
raise InvalidInterpolation(string)
|
||||
|
||||
|
||||
class InvalidInterpolation(Exception):
|
||||
def __init__(self, string):
|
||||
self.string = string
|
||||
Loading…
Add table
Add a link
Reference in a new issue