import logging import os from airflow.models import Variable logger = logging.getLogger(__name__) DEV = 'dev' PROD = 'prod' QA = 'qa' ALL_ENVIRONMENTS = (DEV, QA, PROD) def var(name, default=None): """ Returns value for given variable either from system environments or from airflow Variable It detects airflow context by existence of AIRFLOW_HOME environment variable. In Airflow UI Variables section, Airflow will automatically hide any values if the variable name contains "secret" or "password" :param name: airflow Variable name or os.environ key :return: """ assert name # detect if os.environ.get('AIRFLOW_HOME'): value = Variable.get(name, default) else: value = os.environ.get(name, default) return value def env_default(env, dev, prod, qa=None): """ Returns value for given variable either from system environments or from airflow Variable It detects airflow context by existence of AIRFLOW_HOME environment variable. In Airflow UI Variables section, Airflow will automatically hide any values if the variable name contains "secret" or "password" :param env: name of environment (dev,qa,prod) :param dev: default value on DEV environment :param prod: default value on PROD environment :param qa: default value on QA environment (if omitted then prod value used) :return: """ assert env in ALL_ENVIRONMENTS if env == DEV: return dev elif env == QA and qa is not None: return qa return prod