Python environment variable wrapper around Consul key/value storage. When instantiated, EnvConsol fetches the key value data for a defined service from Consul. These environment variables are retrievable via the path they were stored in Consul.
For example if we insert a database name for specific services in Consul
curl -X PUT -d 'db.sqlite3' \
http://localhost:8500/v1/kv/web00.django.test/databases/default/nameEnvConsul can fetch and be queried like so
import envconsul
ENV_CONSUL = envconsul.EnvConsul('web00.django.test')
DATABASE_NAME = ENV_CONSUL.get_str('/databases/default/name')- Retrieve Consul key/value environment variables service
- Typed retrieval of environment variables
- EnvConsol object implements a simple ReadOnly/Immutable dict
- Implemented using supported Python's Consul
consulate
pip install envconsulimport envconsul
# where service name is 'web00.django.test'
ENV_CONSUL = envconsul.EnvConsul('web00.django.test')
# Or if using remote Consul instance
ENV_CONSUL = envconsul.EnvConsul(
service_name='web00.django.test',
host="localhost",
port=8500,
)Full example here Django settings file here
curl -X PUT -d 'True' http://localhost:8500/v1/kv/web00.django.test/debugDEBUG = ENV_CONSUL.get_bool('/debug', True)curl -X PUT -d "example.com, example.com." http://localhost:8500/v1/kv/web00.django.test/allowed_hostsALLOWED_HOSTS = []
ALLOWED_HOSTS += ENV_CONSUL.get_list('/allowed_hosts', [])curl -X PUT -d 'django.contrib.admin, env' http://localhost:8500/v1/kv/web00.django.test/installed_appsINSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
INSTALLED_APPS += ENV_CONSUL.get_tuple('/installed_apps', ['django.contrib.admin',])curl -X PUT -d 'django.db.backends.sqlite3' http://localhost:8500/v1/kv/web00.django.test/databases/default/engine
curl -X PUT -d 'db.sqlite3' http://localhost:8500/v1/kv/web00.django.test/databases/default/nameDATABASES = {
'default': {
'ENGINE': ENV_CONSUL.get_str('/databases/default/engine'),
'NAME': os.path.join(BASE_DIR, ENV_CONSUL.get_str('/databases/default/name', 'db.sqlite3')),
}
}REDIS_HOST = ENV_CONSUL.get('/redis_host')Full example here Django settings file here
Git clone project
git clone https://github.com/cevaris/python-envconsul.git
Build and install project
pip install -r requirements-dev.txt
Run tests
py.test tests/