summaryrefslogtreecommitdiff
path: root/bin/swift-config
blob: 1a2db221cef8cb72ab713047c051e0730d8aa8a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# 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.

from __future__ import print_function
import optparse
import os
import sys

from swift.common.manager import Server
from swift.common.utils import readconf
from swift.common.wsgi import appconfig

parser = optparse.OptionParser('%prog [options] SERVER')
parser.add_option('-c', '--config-num', metavar="N", type="int",
                  dest="number", default=0,
                  help="parse config for the Nth server only")
parser.add_option('-s', '--section', help="only display matching sections")
parser.add_option('-w', '--wsgi', action='store_true',
                  help="use wsgi/paste parser instead of readconf")


def _context_name(context):
    return ':'.join((context.object_type.name, context.name))


def inspect_app_config(app_config):
    conf = {}
    context = app_config.context
    section_name = _context_name(context)
    conf[section_name] = context.config()
    if context.object_type.name == 'pipeline':
        filters = context.filter_contexts
        pipeline = []
        for filter_context in filters:
            conf[_context_name(filter_context)] = filter_context.config()
            pipeline.append(filter_context.entry_point_name)
        app_context = context.app_context
        conf[_context_name(app_context)] = app_context.config()
        pipeline.append(app_context.entry_point_name)
        conf[section_name]['pipeline'] = ' '.join(pipeline)
    return conf


def main():
    options, args = parser.parse_args()
    options = dict(vars(options))

    if not args:
        return 'ERROR: specify type of server or conf_path'
    conf_files = []
    for arg in args:
        if os.path.exists(arg):
            conf_files.append(arg)
        else:
            conf_files += Server(arg).conf_files(**options)
    for conf_file in conf_files:
        print('# %s' % conf_file)
        if options['wsgi']:
            app_config = appconfig(conf_file)
            conf = inspect_app_config(app_config)
        else:
            conf = readconf(conf_file)
        flat_vars = {}
        for k, v in conf.items():
            if options['section'] and k != options['section']:
                continue
            if not isinstance(v, dict):
                flat_vars[k] = v
                continue
            print('[%s]' % k)
            for opt, value in v.items():
                print('%s = %s' % (opt, value))
            print()
        for k, v in flat_vars.items():
            print('# %s = %s' % (k, v))
        print()


if __name__ == "__main__":
    sys.exit(main())