~jrapi/jrapi/jrapi

« back to all changes in this revision

Viewing changes to jrapi/config.py

  • Committer: Juan L. Negron
  • Date: 2012-05-25 23:13:53 UTC
  • mfrom: (69.1.11 jrapi)
  • Revision ID: juan.negron@canonical.com-20120525231353-zszehoyartmiblxw
Merging Patrick's changes from MP:107481

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2012 Patrick Hetu <patrick.hetu@gmail.com>
 
2
#
 
3
# Authors:
 
4
#     Patrick Hetu <patrick.hetu@gmail.com>
 
5
#
 
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
 
9
#
 
10
#    http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
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
 
15
# implied.
 
16
# See the License for the specific language governing permissions and
 
17
# limitations under the License.
 
18
#
 
19
 
 
20
import os
 
21
import sys
 
22
import inspect
 
23
import logging
 
24
 
 
25
from jrapi.openstack.common import cfg
 
26
 
 
27
 
 
28
class JRAPIConfigOpts(cfg.ConfigOpts):
 
29
    def __init__(self, default_config_files=None, **kwargs):
 
30
        super(JRAPIConfigOpts, self).__init__(
 
31
            project='jrapi',
 
32
            version='0.1',
 
33
            default_config_files=default_config_files,
 
34
            **kwargs)
 
35
 
 
36
        # register debug and logging
 
37
        self.register_cli_opts(cfg.CommonConfigOpts.common_cli_opts)
 
38
        self.register_opts(cfg.CommonConfigOpts.logging_cli_opts)
 
39
 
 
40
 
 
41
default_config_files = cfg.find_config_files(project='jrapi',
 
42
                                             prog='jrapi')
 
43
 
 
44
FLAGS = JRAPIConfigOpts(default_config_files=default_config_files)
 
45
 
 
46
common_opts = [
 
47
    cfg.StrOpt("repository",
 
48
               short="r",
 
49
               help = "Path to the charm repository",
 
50
               default = "~/charms"),
 
51
    cfg.IntOpt("bind_port",
 
52
               short="p",
 
53
               help = "",
 
54
               default = 8888),
 
55
    cfg.StrOpt("bind_host",
 
56
               short="H",
 
57
               help = "",
 
58
               default = "0.0.0.0"),
 
59
]
 
60
FLAGS.register_cli_opts(common_opts)
 
61
 
 
62
ssl_group = cfg.OptGroup(name='ssl',
 
63
                               title="SSL options")
 
64
 
 
65
ssl_opts = [
 
66
    cfg.StrOpt("cert_file",
 
67
               help = "",
 
68
               default = ""),
 
69
    cfg.StrOpt("key_file",
 
70
               help = "",
 
71
               default = ""),
 
72
]
 
73
FLAGS.register_group(ssl_group)
 
74
FLAGS.register_opts(ssl_opts, group=ssl_group)
 
75
 
 
76
 
 
77
 
 
78
def setup_logging(conf):
 
79
    """
 
80
    Sets up the logging options for a log with supplied name
 
81
 
 
82
    :param conf: a cfg.ConfOpts object
 
83
    """
 
84
 
 
85
    if conf.log_config:
 
86
        # Use a logging configuration file for all settings...
 
87
        if os.path.exists(conf.log_config):
 
88
            logging.config.fileConfig(conf.log_config)
 
89
            return
 
90
        else:
 
91
            raise RuntimeError("Unable to locate specified logging "
 
92
                               "config file: %s" % conf.log_config)
 
93
 
 
94
    # hook twisted.python.log with standard logging
 
95
    from twisted.python import log as twisted_log
 
96
    observer = twisted_log.PythonLoggingObserver('twisted')
 
97
    observer.start()
 
98
 
 
99
    twisted_logger = logging.getLogger('twisted')
 
100
    twisted_logger.propagate = False
 
101
 
 
102
    root_logger = logging.root
 
103
    if conf.debug:
 
104
        root_logger.setLevel(logging.DEBUG)
 
105
        twisted_logger.setLevel(logging.DEBUG)
 
106
    elif conf.verbose:
 
107
        root_logger.setLevel(logging.INFO)
 
108
        twisted_logger.setLevel(logging.INFO)
 
109
    else:
 
110
        root_logger.setLevel(logging.WARNING)
 
111
        twisted_logger.setLevel(logging.WARNING)
 
112
 
 
113
    formatter = logging.Formatter(conf.log_format, conf.log_date_format)
 
114
 
 
115
    if conf.use_syslog:
 
116
        try:
 
117
            facility = getattr(logging.handlers.SysLogHandler,
 
118
                               conf.syslog_log_facility)
 
119
        except AttributeError:
 
120
            raise ValueError(_("Invalid syslog facility"))
 
121
 
 
122
        handler = logging.handlers.SysLogHandler(address='/dev/log',
 
123
                                                 facility=facility)
 
124
    elif conf.log_file:
 
125
        logfile = conf.log_file
 
126
        if conf.log_dir:
 
127
            logfile = os.path.join(conf.log_dir, logfile)
 
128
 
 
129
        from logging.handlers import WatchedFileHandler
 
130
        handler = WatchedFileHandler(logfile)
 
131
    else:
 
132
        handler = logging.StreamHandler(sys.stdout)
 
133
 
 
134
    handler.setFormatter(formatter)
 
135
    root_logger.addHandler(handler)
 
136
    twisted_logger.addHandler(handler)
 
137
 
 
138
 
 
139
 
 
140
def default_cfgfile(filename='jrapi.conf', args=None):
 
141
    if args is None:
 
142
        args = sys.argv
 
143
    for arg in args:
 
144
        if arg.find('config-file') != -1:
 
145
            return arg[arg.index('config-file') + len('config-file') + 1:]
 
146
    else:
 
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)
 
158
            return filename
 
159