1
# Copyright (C) 2012 Patrick Hetu <patrick.hetu@gmail.com>
4
# Patrick Hetu <patrick.hetu@gmail.com>
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16
# See the License for the specific language governing permissions and
17
# limitations under the License.
25
from jrapi.openstack.common import cfg
28
class JRAPIConfigOpts(cfg.ConfigOpts):
29
def __init__(self, default_config_files=None, **kwargs):
30
super(JRAPIConfigOpts, self).__init__(
33
default_config_files=default_config_files,
36
# register debug and logging
37
self.register_cli_opts(cfg.CommonConfigOpts.common_cli_opts)
38
self.register_opts(cfg.CommonConfigOpts.logging_cli_opts)
41
default_config_files = cfg.find_config_files(project='jrapi',
44
FLAGS = JRAPIConfigOpts(default_config_files=default_config_files)
47
cfg.StrOpt("repository",
49
help = "Path to the charm repository",
50
default = "~/charms"),
51
cfg.IntOpt("bind_port",
55
cfg.StrOpt("bind_host",
60
FLAGS.register_cli_opts(common_opts)
62
ssl_group = cfg.OptGroup(name='ssl',
66
cfg.StrOpt("cert_file",
69
cfg.StrOpt("key_file",
73
FLAGS.register_group(ssl_group)
74
FLAGS.register_opts(ssl_opts, group=ssl_group)
78
def setup_logging(conf):
80
Sets up the logging options for a log with supplied name
82
:param conf: a cfg.ConfOpts object
86
# Use a logging configuration file for all settings...
87
if os.path.exists(conf.log_config):
88
logging.config.fileConfig(conf.log_config)
91
raise RuntimeError("Unable to locate specified logging "
92
"config file: %s" % conf.log_config)
94
# hook twisted.python.log with standard logging
95
from twisted.python import log as twisted_log
96
observer = twisted_log.PythonLoggingObserver('twisted')
99
twisted_logger = logging.getLogger('twisted')
100
twisted_logger.propagate = False
102
root_logger = logging.root
104
root_logger.setLevel(logging.DEBUG)
105
twisted_logger.setLevel(logging.DEBUG)
107
root_logger.setLevel(logging.INFO)
108
twisted_logger.setLevel(logging.INFO)
110
root_logger.setLevel(logging.WARNING)
111
twisted_logger.setLevel(logging.WARNING)
113
formatter = logging.Formatter(conf.log_format, conf.log_date_format)
117
facility = getattr(logging.handlers.SysLogHandler,
118
conf.syslog_log_facility)
119
except AttributeError:
120
raise ValueError(_("Invalid syslog facility"))
122
handler = logging.handlers.SysLogHandler(address='/dev/log',
125
logfile = conf.log_file
127
logfile = os.path.join(conf.log_dir, logfile)
129
from logging.handlers import WatchedFileHandler
130
handler = WatchedFileHandler(logfile)
132
handler = logging.StreamHandler(sys.stdout)
134
handler.setFormatter(formatter)
135
root_logger.addHandler(handler)
136
twisted_logger.addHandler(handler)
140
def default_cfgfile(filename='jrapi.conf', args=None):
144
if arg.find('config-file') != -1:
145
return arg[arg.index('config-file') + len('config-file') + 1:]
147
if not os.path.isabs(filename):
148
# turn relative filename into an absolute path
149
script_dir = os.path.dirname(inspect.stack()[-1][1])
150
filename = os.path.abspath(os.path.join(script_dir, filename))
151
if not os.path.exists(filename):
152
filename = "./jrapi.conf"
153
if not os.path.exists(filename):
154
filename = '/etc/jrapi/jrapi.conf'
155
if os.path.exists(filename):
156
cfgfile = '--config-file=%s' % filename
157
args.insert(1, cfgfile)