Create pecan conf from config file
By default we should start adpater from config If there is no config defined - we should use cli.parse functional Change-Id: I476caac72c45cab21dd902d455573a265f7d36f3 Implements: blueprint ostf-config-file
This commit is contained in:
parent
4f5946d99f
commit
5d3aa80da6
|
@ -0,0 +1,9 @@
|
|||
[adapter]
|
||||
server_host = 127.0.0.1
|
||||
server_port = 8777
|
||||
dbpath = postgresql+psycopg2://ostf:ostf@localhost/ostf
|
||||
lock_dir = /var/lock
|
||||
nailgun_host = 127.0.0.1
|
||||
nailgun_port = 8000
|
||||
log_file = /var/log/ostf.log
|
||||
after_init_hook = False
|
|
@ -17,9 +17,12 @@
|
|||
import os
|
||||
import logging
|
||||
import signal
|
||||
import sys
|
||||
import pecan
|
||||
from gevent import pywsgi
|
||||
|
||||
from oslo.config import cfg
|
||||
|
||||
from fuel_plugin.ostf_adapter import cli_config
|
||||
from fuel_plugin.ostf_adapter import nailgun_hooks
|
||||
from fuel_plugin.ostf_adapter import logger
|
||||
|
@ -28,33 +31,112 @@ from fuel_plugin.ostf_adapter.nose_plugin import nose_discovery
|
|||
from fuel_plugin.ostf_adapter.storage import engine
|
||||
from fuel_plugin.ostf_adapter import mixins
|
||||
|
||||
adapter_group = cfg.OptGroup(name='adapter',
|
||||
title='Adapter Options')
|
||||
AdapterGroup = [
|
||||
cfg.StrOpt('server_host',
|
||||
default='127.0.0.1',
|
||||
help="adapter host"),
|
||||
cfg.StrOpt('server_port',
|
||||
default='8777',
|
||||
help="Port number"),
|
||||
cfg.StrOpt('dbpath',
|
||||
default='postgresql+psycopg2://ostf:ostf@localhost/ostf',
|
||||
help=""),
|
||||
cfg.StrOpt('lock_dir',
|
||||
default='/var/lock',
|
||||
help=""),
|
||||
cfg.StrOpt('nailgun_host',
|
||||
default='127.0.0.1',
|
||||
help=""),
|
||||
cfg.StrOpt('nailgun_port',
|
||||
default='8000',
|
||||
help=""),
|
||||
cfg.StrOpt('log_file',
|
||||
default='/var/log/ostf.log',
|
||||
help=""),
|
||||
cfg.BoolOpt('after_init_hook',
|
||||
default='False',
|
||||
help='Should be true when we need migrate data to db')
|
||||
]
|
||||
|
||||
|
||||
def register_adapter_opts(conf):
|
||||
conf.register_group(adapter_group)
|
||||
for opt in AdapterGroup:
|
||||
conf.register_opt(opt, group='adapter')
|
||||
|
||||
|
||||
class Ostf_Config(object):
|
||||
|
||||
DEFAULT_CONFIG_DIR = os.path.join(os.path.abspath(
|
||||
os.path.dirname(__file__)), '/etc')
|
||||
|
||||
DEFAULT_CONFIG_FILE = "ostf.conf"
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize a configuration from a conf directory and conf file."""
|
||||
config_files = []
|
||||
|
||||
failsafe_path = "/etc/ostf/" + self.DEFAULT_CONFIG_FILE
|
||||
|
||||
# Environment variables override defaults...
|
||||
custom_config = os.environ.get('CUSTOM_OSTF_CONFIG')
|
||||
if custom_config:
|
||||
path = custom_config
|
||||
else:
|
||||
conf_dir = os.environ.get('OSTF_CONFIG_DIR',
|
||||
self.DEFAULT_CONFIG_DIR)
|
||||
conf_file = os.environ.get('OSTF_CONFIG', self.DEFAULT_CONFIG_FILE)
|
||||
|
||||
path = os.path.join(conf_dir, conf_file)
|
||||
|
||||
if not (os.path.isfile(path)
|
||||
or 'OSTF_CONFIG_DIR' in os.environ
|
||||
or 'OSTF_CONFIG' in os.environ):
|
||||
path = failsafe_path
|
||||
|
||||
if not os.path.exists(path):
|
||||
msg = "Config file %(path)s not found" % locals()
|
||||
print >> sys.stderr, RuntimeError(msg)
|
||||
else:
|
||||
config_files.append(path)
|
||||
|
||||
cfg.CONF([], project='ostf', default_config_files=config_files)
|
||||
|
||||
register_adapter_opts(cfg.CONF)
|
||||
self.adapter = cfg.CONF.adapter
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
settings = Ostf_Config()
|
||||
|
||||
cli_args = cli_config.parse_cli()
|
||||
|
||||
config = {
|
||||
'server': {
|
||||
'host': cli_args.host,
|
||||
'port': cli_args.port
|
||||
'host': settings.adapter.server_host or cli_args.host,
|
||||
'port': settings.adapter.server_port or cli_args.port
|
||||
},
|
||||
'dbpath': cli_args.dbpath,
|
||||
'dbpath': settings.adapter.dbpath or cli_args.dbpath,
|
||||
'debug': cli_args.debug,
|
||||
'debug_tests': cli_args.debug_tests,
|
||||
'lock_dir': cli_args.lock_dir,
|
||||
'lock_dir': settings.adapter.lock_dir or cli_args.lock_dir,
|
||||
'nailgun': {
|
||||
'host': cli_args.nailgun_host,
|
||||
'port': cli_args.nailgun_port
|
||||
'host': settings.adapter.nailgun_host or cli_args.nailgun_host,
|
||||
'port': settings.adapter.nailgun_port or cli_args.nailgun_port
|
||||
}
|
||||
}
|
||||
|
||||
logger.setup(log_file=cli_args.log_file)
|
||||
logger.setup(log_file=(
|
||||
settings.adapter.log_file or cli_args.log_file))
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
root = app.setup_app(config=config)
|
||||
|
||||
if getattr(cli_args, 'after_init_hook'):
|
||||
if settings.adapter.after_init_hook or\
|
||||
getattr(cli_args, 'after_init_hook'):
|
||||
return nailgun_hooks.after_initialization_environment_hook()
|
||||
|
||||
with engine.contexted_session(pecan.conf.dbpath) as session:
|
||||
|
|
Loading…
Reference in New Issue