forked from StackStorm/st2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_gen.py
executable file
·174 lines (142 loc) · 5.09 KB
/
config_gen.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
import collections
import importlib
import six
import sys
import traceback
from oslo_config import cfg
CONFIGS = ['st2actions.config',
'st2actions.notifier.config',
'st2actions.resultstracker.config',
'st2api.config',
'st2stream.config',
'st2auth.config',
'st2common.config',
'st2exporter.config',
'st2reactor.rules.config',
'st2reactor.sensor.config',
'st2reactor.garbage_collector.config']
SKIP_GROUPS = ['api_pecan', 'rbac', 'results_tracker']
# We group auth options together to nake it a bit more clear what applies where
AUTH_OPTIONS = {
'common': [
'enable',
'mode',
'logging',
'api_url',
'token_ttl',
'service_token_ttl',
'debug'
],
'standalone': [
'host',
'port',
'use_ssl',
'cert',
'key',
'backend',
'backend_kwargs'
]
}
# Some of the config values change depenending on the environment where this script is ran so we
# set them to static values to ensure consistent and stable output
STATIC_OPTION_VALUES = {
'actionrunner': {
'virtualenv_binary': '/data/stanley/virtualenv/bin/virtualenv',
'python_binary': '/data/stanley/virtualenv/bin/python'
},
'webui': {
'webui_base_url': 'https://localhost'
}
}
COMMON_AUTH_OPTIONS_COMMENT = """
# Common option - options below apply in both scenarios - when auth service is running as a WSGI
# service (e.g. under Apache or Nginx) and when it's running in the standalone mode.
""".strip()
STANDALONE_AUTH_OPTIONS_COMMENT = """
# Standalone mode options - options below only apply when auth service is running in the standalone
# mode.
""".strip()
def _import_config(config):
try:
return importlib.import_module(config)
except:
traceback.print_exc()
return None
def _read_current_config(opt_groups):
for k, v in six.iteritems(cfg.CONF._groups):
if k in SKIP_GROUPS:
continue
if k not in opt_groups:
opt_groups[k] = v
return opt_groups
def _clear_config():
cfg.CONF.reset()
def _read_group(opt_group):
all_options = opt_group._opts.values()
if opt_group.name == 'auth':
print(COMMON_AUTH_OPTIONS_COMMENT)
print('')
common_options = [option for option in all_options if option['opt'].name in
AUTH_OPTIONS['common']]
_print_options(opt_group=opt_group, options=common_options)
print('')
print(STANDALONE_AUTH_OPTIONS_COMMENT)
print('')
standalone_options = [option for option in all_options if option['opt'].name in
AUTH_OPTIONS['standalone']]
_print_options(opt_group=opt_group, options=standalone_options)
if len(common_options) + len(standalone_options) != len(all_options):
msg = ('Not all options are declared in AUTH_OPTIONS dict, please update it')
raise Exception(msg)
else:
options = all_options
_print_options(opt_group=opt_group, options=options)
def _read_groups(opt_groups):
opt_groups = collections.OrderedDict(sorted(opt_groups.items()))
for name, opt_group in six.iteritems(opt_groups):
print('[%s]' % name)
_read_group(opt_group)
print('')
def _print_options(opt_group, options):
for opt in options:
opt = opt['opt']
# Special case for options which could change during this script run
static_option_value = STATIC_OPTION_VALUES.get(opt_group.name, {}).get(opt.name, None)
if static_option_value:
opt.default = static_option_value
# Special handling for list options
if isinstance(opt, cfg.ListOpt):
if opt.default:
value = ','.join(opt.default)
else:
value = ''
value += ' # comma separated list allowed here.'
else:
value = opt.default
print('# %s' % opt.help)
print('%s = %s' % (opt.name, value))
def main(args):
opt_groups = {}
for config in CONFIGS:
mod = _import_config(config)
mod.register_opts()
_read_current_config(opt_groups)
_clear_config()
_read_groups(opt_groups)
if __name__ == '__main__':
main(sys.argv)