Skip to content

Commit

Permalink
added ability to concatenate several environment variables into one l…
Browse files Browse the repository at this point in the history
…ine (#73)

* added ability to concatenate several environment variables into one line
  • Loading branch information
shulutkov authored and dekhtyarev committed Jan 18, 2019
1 parent 801d703 commit 0a83144
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ Dockerfile*
.gitignore
.idea/
.tox/
.travis.yml
tox.ini
__pychache__
htmlcov/
tests/
*.png

20 changes: 8 additions & 12 deletions k8s_handle/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
log = logging.getLogger(__name__)

INCLUDE_RE = re.compile(r'{{\s?file\s?=\s?\'(?P<file>[^\']*)\'\s?}}')
CUSTOM_ENV_RE = re.compile(r'^(?P<prefix>.*){{\s*env\s*=\s*\'(?P<env>[^\']*)\'\s*}}(?P<postfix>.*)$') # noqa
CUSTOM_ENV_RE = r'{{\s*env\s*=\s*\'([^\']*)\'\s*}}'

KEY_USE_KUBECONFIG = 'use_kubeconfig'
KEY_K8S_MASTER_URI = 'k8s_master_uri'
Expand Down Expand Up @@ -113,19 +113,15 @@ def _process_variable(variable):
if matches:
return load_yaml(matches.groupdict().get('file'))

matches = CUSTOM_ENV_RE.match(variable)
try:
return re.sub(CUSTOM_ENV_RE, lambda m: os.environ[m.group(1)], variable)

if matches:
prefix = matches.groupdict().get('prefix')
env_var_name = matches.groupdict().get('env')
postfix = matches.groupdict().get('postfix')

if os.environ.get(env_var_name) is None and settings.GET_ENVIRON_STRICT:
raise RuntimeError('Environment variable "{}" is not set'.format(env_var_name))

return prefix + os.environ.get(env_var_name, '') + postfix
except KeyError as err:
log.debug('Environment variable "{}" is not set'.format(err.args[0]))
if settings.GET_ENVIRON_STRICT:
raise RuntimeError('Environment variable "{}" is not set'.format(err.args[0]))

return variable
return re.sub(CUSTOM_ENV_RE, lambda m: os.environ.get(m.group(1), ''), variable)


def _update_single_variable(value, include_history):
Expand Down
12 changes: 10 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,17 @@ def test_context_update_recursion(self):
'section1-key4': [0, 1, 2, 3],
'section1-key5': "{{ env='CUSTOM_ENV' }}",
'section1-key6': "{{ file='tests/fixtures/include.yaml' }}",
'section1-key7': "{{ env='CUSTOM_ENV'}} = {{ env='CUSTOM_ENV' }}",
'section1-key8': "{{ env='NULL_VAR' }}-{{ env='CUSTOM_ENV' }}"
}
},
'section2': [
{},
'var2',
'var3',
'{{ env=\'CUSTOM_ENV\' }}'
'{{ env=\'CUSTOM_ENV\' }}',
'{{ env=\'CUSTOM_ENV\' }} = {{ env=\'CUSTOM_ENV\' }}',
'{{ env=\'NULL_VAR\' }}-{{ env=\'CUSTOM_ENV\' }}'
],
'section3': [0, 1, 2, 3, 4]
}
Expand All @@ -137,13 +141,17 @@ def test_context_update_recursion(self):
'section1-key4': [0, 1, 2, 3],
'section1-key5': 'My value',
'section1-key6': {'ha_ha': 'included_var'},
'section1-key7': 'My value = My value',
'section1-key8': "-My value"
}
},
'section2': [
{},
'var2',
'var3',
'My value'
'My value',
'My value = My value',
'-My value'
],
'section3': [0, 1, 2, 3, 4]
}
Expand Down

0 comments on commit 0a83144

Please sign in to comment.