Skip to content

Commit

Permalink
configuration backend simpler to choose different ones
Browse files Browse the repository at this point in the history
  • Loading branch information
adgaudio committed Feb 26, 2015
1 parent c3e4e5f commit e6ec707
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
17 changes: 9 additions & 8 deletions conf/stolos-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ if [ -z "$DIR" ] ; then DIR='.' ; fi
export STOLOS_JOB_ID_DEFAULT_TEMPLATE="{date}_{client_id}_{collection_name}"
export STOLOS_JOB_ID_VALIDATIONS="stolos.examples.job_id_validations"

# you can specify a different configuration backend. the default is json
unset STOLOS_CONFIGURATION_BACKEND
# Queue Backend:
export STOLOS_ZOOKEEPER_HOSTS="localhost:2181"

# Configuration backend. the default is json

# JSON configuration backend
# export STOLOS_CONFIGURATION_BACKEND="stolos.configuration_backend.json_config.JSONConfig"
# export STOLOS_CONFIGURATION_BACKEND="json" # enabled by default
export STOLOS_TASKS_JSON="$DIR/stolos/examples/tasks.json"

# Zookeeper configuration backend
export STOLOS_ZOOKEEPER_HOSTS="localhost:2181"


# Redis configuration backend
# export STOLOS_CONFIGURATION_BACKEND="stolos.configuration_backend.redis_config.RedisMapping"
# export STOLOS_CONFIGURATION_BACKEND="redis"
# export STOLOS_REDIS_DB=3
# export STOLOS_REDIS_PORT=6379
# export STOLOS_REDIS_HOST='localhost'

# You can define your own custom configuration backend
# export STOLOS_CONFIGURATION_BACKEND="mymodule.myconfiguration_backend"
46 changes: 30 additions & 16 deletions stolos/configuration_backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,44 @@
from stolos import argparse_shared as at
from stolos.util import load_obj_from_path as _load_obj_from_path

# expose the configuration backend base class for developers
# expose the base class to other configuration backend modules
from .tasks_config_base import TasksConfigBaseMapping, TasksConfigBaseSequence


_PREFIX = "stolos.configuration_backend"
# a lookup table that we can alias a particular
# configuration backend's full python import path to.
_KNOWN_DEFAULT_BACKENDS = {
"json": "%s.%s" % (_PREFIX, "json_config.JSONMapping"),
"redis": "%s.%s" % (_PREFIX, "redis_config.RedisMapping"),
}


def _load_backend(inpt):
_cb = _KNOWN_DEFAULT_BACKENDS.get(inpt, inpt)
try:
cb = _load_obj_from_path(
_cb, dict(key='configuration_backend', configuration_backend=_cb))
except:
log.error(
"Could not load configuration backend",
extra=dict(configuration_backend=_cb))
raise
return cb


build_arg_parser = at.build_arg_parser([at.group(
# TODO: inherit from the configuration backend choice somehow?
"Application Dependency Configuration",
at.add_argument(
'--configuration_backend',
default='stolos.configuration_backend.json_config.JSONMapping', help=(
default='json', type=_load_backend, help=(
"Where do you store the application dependency data?"
' This options defines which backend to use to access'
' This option defines which backend to use to access'
' application dependency configuration.'
' Stolos supports a couple options.'
' See conf/stolos-env.sh for an example')),
' See conf/stolos-env.sh for an example. '
' You can supply your own configuration backend or choose from the'
' following supported options: %s'
) % list(_KNOWN_DEFAULT_BACKENDS.keys())),
)])


Expand Down Expand Up @@ -55,13 +78,4 @@ def get_tasks_config():
configuration backend.
"""
ns = stolos.get_NS()
try:
cb = _load_obj_from_path(
ns.configuration_backend,
dict(key='configuration_backend',
configuration_backend=ns.configuration_backend)
)
except:
log.error("Could not load configuration backend")
raise
return cb()
return ns.configuration_backend()
4 changes: 2 additions & 2 deletions stolos/configuration_backend/redis_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ class RedisMapping(_RedisConfig, TasksConfigBaseMapping):
"""
def __init__(self, data=None):
NS = get_NS()
self.db = NS.db
self.db = NS.redis_db
self.redis_key_prefix = NS.redis_key_prefix
self.cli = redis.StrictRedis(
db=NS.db, port=NS.port, host=NS.host)
db=NS.redis_db, port=NS.redis_port, host=NS.redis_host)
if data is None:
self.cache = {}
elif isinstance(data, self.__class__):
Expand Down
6 changes: 2 additions & 4 deletions stolos/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ def _get_parent_parsers(objects):
yield p


def initialize_configuration_backend(choice, parser, add_help):
def initialize_configuration_backend(cbackend, parser, add_help):
"""
get options for the chosen configuration_backend.
ensure they don't conflict with previously defined ones
"""
cb = importlib.import_module(
util.load_obj_from_path(choice).__module__)\
.build_arg_parser()
cb = importlib.import_module(cbackend.__module__).build_arg_parser()
newparser = at.build_arg_parser(parents=[parser, cb], add_help=add_help)
return newparser

Expand Down

0 comments on commit e6ec707

Please sign in to comment.