Skip to content

Commit

Permalink
Merge branch '2429-config-env-var' into 2429-remote-config
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed May 18, 2015
2 parents efffd01 + e9cd0c4 commit cdb2ef1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 8 deletions.
33 changes: 25 additions & 8 deletions ckan/config/environment.py
Expand Up @@ -232,6 +232,16 @@ def genshi_lookup_attr(cls, obj, key):
p.load_all(config)


# A list of config settings that can be overridden by environmental variable.
ENV_VAR_WHITELIST = {
'sqlalchemy.url': 'CKAN_SQLALCHEMY_URL',
'ckan.datastore.write_url': 'CKAN_DATASTORE_WRITE_URL',
'ckan.datastore.read_url': 'CKAN_DATASTORE_READ_URL',
'solr_url': 'CKAN_SOLR_URL',
'ckan.site_id': 'CKAN_SITE_ID'
}


def update_config():
''' This code needs to be run when the config is changed to take those
changes into account. '''
Expand All @@ -241,11 +251,22 @@ def update_config():
# config = plugin.update_config(config)
plugin.update_config(config)

root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Set whitelisted env vars on config object
# This is set up before globals are initialized
site_id = os.environ.get('CKAN_SITE_ID')
if site_id:
config['ckan.site_id'] = site_id

ckan_db = os.environ.get('CKAN_DB', None)
if ckan_db:
msg = 'Setting CKAN_DB as an env var is deprecated and will be' \
' removed in a future release. Use CKAN_SQLALCHEMY_URL instead.'
log.warn(msg)
config['sqlalchemy.url'] = ckan_db

for option in ENV_VAR_WHITELIST:
from_env = os.environ.get(ENV_VAR_WHITELIST[option], None)
if from_env:
config[option] = from_env

root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

site_url = config.get('ckan.site_url', '')
ckan_host = config['ckan.host'] = urlparse(site_url).netloc
Expand Down Expand Up @@ -341,10 +362,6 @@ def template_loaded(template):
# CONFIGURATION OPTIONS HERE (note: all config options will override
# any Pylons config options)

ckan_db = os.environ.get('CKAN_DB')
if ckan_db:
config['sqlalchemy.url'] = ckan_db

# for postgresql we want to enforce utf-8
sqlalchemy_url = config.get('sqlalchemy.url', '')
if sqlalchemy_url.startswith('postgresql://'):
Expand Down
65 changes: 65 additions & 0 deletions ckan/tests/config/test_environment.py
@@ -0,0 +1,65 @@
import os
from nose import tools as nosetools

from pylons import config

import ckan.tests.helpers as h
import ckan.plugins as p


class TestUpdateConfig(h.FunctionalTestBase):

'''
Tests for config settings from env vars, set in
config.environment.update_config().
'''

ENV_VAR_LIST = [
('CKAN_SQLALCHEMY_URL', 'postgresql://mynewsqlurl/'),
('CKAN_DATASTORE_WRITE_URL', 'http://mynewdbwriteurl/'),
('CKAN_DATASTORE_READ_URL', 'http://mynewdbreadurl/'),
('CKAN_SOLR_URL', 'http://mynewsolrurl/solr'),
('CKAN_SITE_ID', 'my-site'),
('CKAN_DB', 'postgresql://mydeprectatesqlurl/')
]

def _setup_env_vars(self):
for env_var, value in self.ENV_VAR_LIST:
os.environ.setdefault(env_var, value)
# plugin.load() will force the config to update
p.load()

def teardown(self):
for env_var, _ in self.ENV_VAR_LIST:
if os.environ.get(env_var, None):
del os.environ[env_var]
# plugin.load() will force the config to update
p.load()

def test_update_config_env_vars(self):
'''
Setting an env var from the whitelist will set the appropriate option
in config object.
'''
self._setup_env_vars()

nosetools.assert_equal(config['solr_url'],
'http://mynewsolrurl/solr')
nosetools.assert_equal(config['sqlalchemy.url'],
'postgresql://mynewsqlurl/')
nosetools.assert_equal(config['ckan.datastore.write_url'],
'http://mynewdbwriteurl/')
nosetools.assert_equal(config['ckan.datastore.read_url'],
'http://mynewdbreadurl/')
nosetools.assert_equal(config['ckan.site_id'],
'my-site')

def test_update_config_db_url_precidence(self):
'''CKAN_SQLALCHEMY_URL in the env takes precidence over CKAN_DB'''
os.environ.setdefault('CKAN_DB', 'postgresql://mydeprectatesqlurl/')
os.environ.setdefault('CKAN_SQLALCHEMY_URL',
'postgresql://mynewsqlurl/')
p.load()

nosetools.assert_equal(config['sqlalchemy.url'],
'postgresql://mynewsqlurl/')

0 comments on commit cdb2ef1

Please sign in to comment.