~ubuntu-branches/ubuntu/vivid/ceilometer/vivid

« back to all changes in this revision

Viewing changes to ceilometer/openstack/common/config/generator.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-03-06 14:44:28 UTC
  • mto: (28.1.1 utopic-proposed) (1.2.1)
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: package-import@ubuntu.com-20140306144428-rvphsh4igwyulzf0
Tags: upstream-2014.1~b3
ImportĀ upstreamĀ versionĀ 2014.1~b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2012 SINA Corporation
 
2
# Copyright 2014 Cisco Systems, Inc.
2
3
# All Rights Reserved.
3
4
#
4
5
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
18
19
 
19
20
from __future__ import print_function
20
21
 
 
22
import argparse
21
23
import imp
22
24
import os
23
25
import re
27
29
 
28
30
from oslo.config import cfg
29
31
import six
 
32
import stevedore.named
30
33
 
31
34
from ceilometer.openstack.common import gettextutils
32
35
from ceilometer.openstack.common import importutils
38
41
INTOPT = "IntOpt"
39
42
FLOATOPT = "FloatOpt"
40
43
LISTOPT = "ListOpt"
 
44
DICTOPT = "DictOpt"
41
45
MULTISTROPT = "MultiStrOpt"
42
46
 
43
47
OPT_TYPES = {
46
50
    INTOPT: 'integer value',
47
51
    FLOATOPT: 'floating point value',
48
52
    LISTOPT: 'list value',
 
53
    DICTOPT: 'dict value',
49
54
    MULTISTROPT: 'multi valued',
50
55
}
51
56
 
52
57
OPTION_REGEX = re.compile(r"(%s)" % "|".join([STROPT, BOOLOPT, INTOPT,
53
 
                                              FLOATOPT, LISTOPT,
 
58
                                              FLOATOPT, LISTOPT, DICTOPT,
54
59
                                              MULTISTROPT]))
55
60
 
56
61
PY_EXT = ".py"
59
64
WORDWRAP_WIDTH = 60
60
65
 
61
66
 
62
 
def generate(srcfiles):
 
67
def generate(argv):
 
68
    parser = argparse.ArgumentParser(
 
69
        description='generate sample configuration file',
 
70
    )
 
71
    parser.add_argument('-m', dest='modules', action='append')
 
72
    parser.add_argument('-l', dest='libraries', action='append')
 
73
    parser.add_argument('srcfiles', nargs='*')
 
74
    parsed_args = parser.parse_args(argv)
 
75
 
63
76
    mods_by_pkg = dict()
64
 
    for filepath in srcfiles:
 
77
    for filepath in parsed_args.srcfiles:
65
78
        pkg_name = filepath.split(os.sep)[1]
66
79
        mod_str = '.'.join(['.'.join(filepath.split(os.sep)[:-1]),
67
80
                            os.path.basename(filepath).split('.')[0]])
75
88
    # The options list is a list of (module, options) tuples
76
89
    opts_by_group = {'DEFAULT': []}
77
90
 
78
 
    extra_modules = os.getenv("CEILOMETER_CONFIG_GENERATOR_EXTRA_MODULES", "")
79
 
    if extra_modules:
80
 
        for module_name in extra_modules.split(','):
81
 
            module_name = module_name.strip()
 
91
    if parsed_args.modules:
 
92
        for module_name in parsed_args.modules:
82
93
            module = _import_module(module_name)
83
94
            if module:
84
95
                for group, opts in _list_opts(module):
85
96
                    opts_by_group.setdefault(group, []).append((module_name,
86
97
                                                                opts))
87
98
 
 
99
    # Look for entry points defined in libraries (or applications) for
 
100
    # option discovery, and include their return values in the output.
 
101
    #
 
102
    # Each entry point should be a function returning an iterable
 
103
    # of pairs with the group name (or None for the default group)
 
104
    # and the list of Opt instances for that group.
 
105
    if parsed_args.libraries:
 
106
        loader = stevedore.named.NamedExtensionManager(
 
107
            'oslo.config.opts',
 
108
            names=list(set(parsed_args.libraries)),
 
109
            invoke_on_load=False,
 
110
        )
 
111
        for ext in loader:
 
112
            for group, opts in ext.plugin():
 
113
                opt_list = opts_by_group.setdefault(group or 'DEFAULT', [])
 
114
                opt_list.append((ext.name, opts))
 
115
 
88
116
    for pkg_name in pkg_names:
89
117
        mods = mods_by_pkg.get(pkg_name)
90
118
        mods.sort()
201
229
        return value.replace(BASEDIR, '')
202
230
    elif value == _get_my_ip():
203
231
        return '10.0.0.1'
204
 
    elif value == socket.gethostname() and 'host' in name:
 
232
    elif value in (socket.gethostname(), socket.getfqdn()) and 'host' in name:
205
233
        return 'ceilometer'
206
234
    elif value.strip() != value:
207
235
        return '"%s"' % value
219
247
    except (ValueError, AttributeError) as err:
220
248
        sys.stderr.write("%s\n" % str(err))
221
249
        sys.exit(1)
222
 
    opt_help += ' (' + OPT_TYPES[opt_type] + ')'
 
250
    opt_help = u'%s (%s)' % (opt_help,
 
251
                             OPT_TYPES[opt_type])
223
252
    print('#', "\n# ".join(textwrap.wrap(opt_help, WORDWRAP_WIDTH)))
224
253
    if opt.deprecated_opts:
225
254
        for deprecated_opt in opt.deprecated_opts:
249
278
        elif opt_type == LISTOPT:
250
279
            assert(isinstance(opt_default, list))
251
280
            print('#%s=%s' % (opt_name, ','.join(opt_default)))
 
281
        elif opt_type == DICTOPT:
 
282
            assert(isinstance(opt_default, dict))
 
283
            opt_default_strlist = [str(key) + ':' + str(value)
 
284
                                   for (key, value) in opt_default.items()]
 
285
            print('#%s=%s' % (opt_name, ','.join(opt_default_strlist)))
252
286
        elif opt_type == MULTISTROPT:
253
287
            assert(isinstance(opt_default, list))
254
288
            if not opt_default: