Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inject ENV variables to yaml #1338

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion compose/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,17 @@ def get_service_name_from_net(net_config):
def load_yaml(filename):
try:
with open(filename, 'r') as fh:
return yaml.safe_load(fh)
return yaml.safe_load(inject_env_variables(fh.read()))
except IOError as e:
raise ConfigurationError(six.text_type(e))


def inject_env_variables(content):
for k, v in os.environ.iteritems():
content = content.replace('$' + k, v)
return content


class ConfigurationError(Exception):
def __init__(self, msg):
self.msg = msg
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,9 @@ def test_absolute_path(self):
def test_from_file(self):
service_dict = config.load('tests/fixtures/build-path/docker-compose.yml')
self.assertEquals(service_dict, [{'name': 'foo', 'build': self.abs_context_path}])


class InjectEnvTest(unittest.TestCase):
def test_injection(self):
os.environ["BAR"] = 'rab'
self.assertEquals(config.inject_env_variables('foo $BAR oof'), 'foo rab oof')