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

support (nested) interpolation of env vars #361

Merged
merged 3 commits into from
Sep 5, 2015
Merged
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
30 changes: 22 additions & 8 deletions airflow/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ def generate_fernet_key():
return FERNET_KEY


def expand_env_var(env_var):
"""
Expands (potentially nested) env vars by repeatedly applying
`expandvars` and `expanduser` until interpolation stops having
any effect.
"""
while True:
interpolated = os.path.expanduser(os.path.expandvars(str(env_var)))
if interpolated == env_var:
return interpolated
else:
env_var = interpolated


class AirflowConfigException(Exception):
pass

Expand Down Expand Up @@ -233,15 +247,15 @@ def get(self, section, key):
# must have format AIRFLOW__{SESTION}__{KEY} (note double underscore)
env_var = 'AIRFLOW__{S}__{K}'.format(S=section.upper(), K=key.upper())
if env_var in os.environ:
return os.environ[env_var]
return expand_env_var(os.environ[env_var])

# ...then the config file
elif self.has_option(section, key):
return ConfigParser.get(self, section, key)
return expand_env_var(ConfigParser.get(self, section, key))

# ...then the defaults
elif section in d and key in d[section]:
return d[section][key]
return expand_env_var(d[section][key])

else:
raise AirflowConfigException(
Expand Down Expand Up @@ -278,19 +292,19 @@ def mkdir_p(path):
"""

if 'AIRFLOW_HOME' not in os.environ:
AIRFLOW_HOME = os.path.expanduser('~/airflow')
AIRFLOW_HOME = expand_env_var('~/airflow')
else:
AIRFLOW_HOME = os.path.expanduser(os.environ['AIRFLOW_HOME'])
AIRFLOW_HOME = expand_env_var(os.environ['AIRFLOW_HOME'])

mkdir_p(AIRFLOW_HOME)

if 'AIRFLOW_CONFIG' not in os.environ:
if os.path.isfile(os.path.expanduser('~/airflow.cfg')):
AIRFLOW_CONFIG = os.path.expanduser('~/airflow.cfg')
if os.path.isfile(expand_env_var('~/airflow.cfg')):
AIRFLOW_CONFIG = expand_env_var('~/airflow.cfg')
else:
AIRFLOW_CONFIG = AIRFLOW_HOME + '/airflow.cfg'
else:
AIRFLOW_CONFIG = os.environ['AIRFLOW_CONFIG']
AIRFLOW_CONFIG = expand_env_var(os.environ['AIRFLOW_CONFIG'])

if not os.path.isfile(AIRFLOW_CONFIG):
"""
Expand Down