项目作者: sam-atkins

项目描述 :
Builds a config object based on environment variables, settings files and (optional) parameters stored in AWS System Manager Parameter Store.
高级语言: Python
项目地址: git://github.com/sam-atkins/manageconf.git
创建时间: 2019-03-29T09:17:53Z
项目社区:https://github.com/sam-atkins/manageconf

开源协议:MIT License

下载


Manage Conf

CircleCI
Code style: black

Description

Builds a config object based on environment variables, settings files and (optional) parameters stored in AWS System Manager Parameter Store.

The config object merges in config, overriding any previous key/value pairs, in the following order:

  • ENV
  • default config: default.json
  • stage config: {stage}.json
  • remote config: remote_settings (AWS param store)

Available to download as a package on PyPi.

Settings Files

Set an environment variable with the key name project_config_dir. It is important this is set before the package is imported. The value of project_config_dir should be the location of your /settings folder.

Set-up your settings folder, adding in configuration to the appropriate file.

  1. .
  2. ├── settings <-- Settings folder
  3. ├── default.json <-- default configuration
  4. ├── {stage}.json <-- stage specific configuration e.g. `local`
  5. └── {stage}.json <-- stage specific configuration e.g. `dev`

Example configuration:

default.json

  1. {
  2. "project_name": "example-project",
  3. "DEBUG": false
  4. }
local.json
  1. {
  2. "DEBUG": true,
  3. "use_remote_settings": false
  4. }

Local config object:

  1. {
  2. "project_name": "example-project",
  3. "DEBUG": True,
  4. "use_remote_settings": False
  5. }
dev.json
  1. {
  2. "use_remote_settings": true
  3. }

Dev config object:

  1. {
  2. "project_name": "example-project",
  3. "DEBUG": True,
  4. "use_remote_settings": True
  5. # and any remote settings from AWS param store
  6. }

AWS Param Store

Project config

Add parameters in your AWS account with paths that match this pattern:

/{project_name}/{stage}/

If you set "use_remote_settings": true in a remote {stage}.json config file, the package will attempt to fetch the parameters from the store that have the specified base path. Using the example configuration above, the path would be:

  1. /example-project/dev/

Global service directory config

Add parameters in your AWS account with paths that match this pattern:

/global/{stage}/service_directory/

Set "global_service_directory": true in a remote {stage}.json config file.

Global service directory config for the {stage} will be merged in.

Usage

Make sure project_config_dir is set before importing the library.

  1. from manageconf import Config, get_config
  2. SECRET_KEY = get_config("SECRET_KEY")
  3. DEBUG = get_config("DEBUG", True)
  4. # default values are an optional second arg and will
  5. # be used if the param cannot be found in the config object

Development

Install

Requires Poetry.

  1. # create a Python3 virtual environment
  2. virtualenv -p python3 env
  3. # activate the virtual env
  4. source env/bin/activate
  5. # install requirements
  6. poetry install

Tests

  1. # run tests
  2. pytest -vv
  3. # coverage report in the Terminal
  4. pytest --cov=manageconf tests/
  5. # coverage report in HTML
  6. pytest --cov-report html --cov=manageconf tests/