solar/solar/config.py
Jedrzej Nowak 6a794156f5 Add /etc/solar/config.yaml as first file in config
Change-Id: Id11a82593b4ca488cef7113ed924bd4b1fb0e14b
Closes-bug: #1552629
2016-03-03 10:47:06 +01:00

102 lines
2.6 KiB
Python

#
# Copyright 2015 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
import os
from munch import Munch
import yaml
CWD = os.getcwd()
C = Munch(solar_db="")
C.riak_ensemble = False
C.lock_bucket_type = None
C.counter_bucket_type = None
C.log_file = 'solar.log'
C.system_log_address = 'ipc:///tmp/solar_system_log'
C.tasks_address = 'ipc:///tmp/solar_tasks'
C.scheduler_address = 'ipc:///tmp/solar_scheduler'
C.timewatcher_address = 'ipc:///tmp/solar_timewatcher'
C.executor = 'zerorpc'
C.tasks_driver = 'solar'
C.scheduler_driver = 'solar'
C.system_log_driver = 'solar'
C.runner = 'gevent'
def _lookup_vals(setter, config, prefix=None):
for key, val in config.iteritems():
if prefix is None:
sub = [key]
else:
sub = prefix + [key]
if isinstance(val, Munch):
_lookup_vals(setter, val, sub)
else:
setter(config, sub)
def from_configs():
paths = [
'/etc/solar/solar.yaml',
os.getenv('SOLAR_CONFIG', os.path.join(CWD, '.config')),
os.getenv('SOLAR_CONFIG_OVERRIDE', None),
os.path.join(CWD, '.config.override')
]
data = {}
def _load_from_path(data, path):
with open(path) as f:
loaded = yaml.load(f)
if loaded:
data.update(loaded)
for path in paths:
if not path:
continue
if not os.path.exists(path):
continue
if not os.path.isfile(path):
continue
with open(path) as f:
loaded = yaml.load(f)
if loaded:
data.update(loaded)
def _setter(config, path):
vals = data
for key in path:
if key not in vals:
return
vals = vals[key]
config[path[-1]] = vals
if data:
_lookup_vals(_setter, C)
def from_env():
def _setter(config, path):
env_key = '_'.join(path).upper()
if env_key in os.environ:
config[path[-1]] = os.environ[env_key]
_lookup_vals(_setter, C)
from_configs()
from_env()