Giter Club home page Giter Club logo

python-envconsul's Introduction

EnvConsul

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/name

EnvConsul 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')

Features

  • 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

Install

pip install envconsul

Example Usage

Initialization

import 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,
)

Retrieval of Environment Variables

Example Django settings file

Full example here Django settings file here

Bool type
curl -x PUT -d 'True' http://localhost:8500/v1/kv/web00.django.test/debug
DEBUG = ENV_CONSUL.get_bool('/debug', True)

Return list type

curl -X PUT -d "example.com, example.com." http://localhost:8500/v1/kv/web00.django.test/allowed_hosts
ALLOWED_HOSTS = []
ALLOWED_HOSTS += ENV_CONSUL.get_list('/allowed_hosts', [])

Return tuple type

curl -X PUT -d 'django.contrib.admin, env' http://localhost:8500/v1/kv/web00.django.test/installed_apps
INSTALLED_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',])

Nested path example

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/name
DATABASES = {
    'default': {
        'ENGINE': ENV_CONSUL.get_str('/databases/default/engine'),
        'NAME': os.path.join(BASE_DIR, ENV_CONSUL.get_str('/databases/default/name', 'db.sqlite3')),
    }
}

Simple no-default, no type retrieval; not recommend

REDIS_HOST = ENV_CONSUL.get('/redis_host')

Full example here Django settings file here

For Development

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/

python-envconsul's People

Contributors

cevaris avatar

Watchers

Dave Foster avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.