| 1 |
# Copyright(c) 2016 Nippon Telegraph and Telephone Corporation |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
""" |
| 15 |
This is the single point of entry to generate the sample configuration |
| 16 |
file for Masakari Monitors. It collects all the necessary info from the |
| 17 |
other modules in this package. It is assumed that: |
| 18 |
|
| 19 |
* every other module in this package has a 'list_opts' function which |
| 20 |
return a dict where |
| 21 |
* the keys are strings which are the group names |
| 22 |
* the value of each key is a list of config options for that group |
| 23 |
* the masakari.conf package doesn't have further packages with config options |
| 24 |
* this module is only used in the context of sample file generation |
| 25 |
""" |
| 26 |
|
| 27 |
import collections |
| 28 |
import importlib |
| 29 |
import os |
| 30 |
import pkgutil |
| 31 |
|
| 32 |
LIST_OPTS_FUNC_NAME = "list_opts" |
| 33 |
|
| 34 |
|
| 35 |
def _tupleize(dct): |
| 36 |
"""Take the dict of options and convert to the 2-tuple format.""" |
| 37 |
return [(key, val) for key, val in dct.items()] |
| 38 |
|
| 39 |
|
| 40 |
def list_opts(): |
| 41 |
opts = collections.defaultdict(list) |
| 42 |
module_names = _list_module_names() |
| 43 |
imported_modules = _import_modules(module_names) |
| 44 |
_append_config_options(imported_modules, opts) |
| 45 |
return _tupleize(opts) |
| 46 |
|
| 47 |
|
| 48 |
def _list_module_names(): |
| 49 |
module_names = [] |
| 50 |
package_path = os.path.dirname(os.path.abspath(__file__)) |
| 51 |
for _, modname, ispkg in pkgutil.iter_modules(path=[package_path]): |
| 52 |
if modname == "opts" or ispkg: |
| 53 |
continue |
| 54 |
else: |
| 55 |
module_names.append(modname) |
| 56 |
return module_names |
| 57 |
|
| 58 |
|
| 59 |
def _import_modules(module_names): |
| 60 |
imported_modules = [] |
| 61 |
for modname in module_names: |
| 62 |
mod = importlib.import_module("masakarimonitors.conf." + modname) |
| 63 |
if not hasattr(mod, LIST_OPTS_FUNC_NAME): |
| 64 |
msg = "The module 'masakarimonitors.conf.%s' should have a" \ |
| 65 |
" '%s' function which returns the config options." % \ |
| 66 |
(modname, LIST_OPTS_FUNC_NAME) |
| 67 |
raise Exception(msg) |
| 68 |
else: |
| 69 |
imported_modules.append(mod) |
| 70 |
return imported_modules |
| 71 |
|
| 72 |
|
| 73 |
def _process_old_opts(configs): |
| 74 |
"""Convert old-style 2-tuple configs to dicts.""" |
| 75 |
if isinstance(configs, tuple): |
| 76 |
configs = [configs] |
| 77 |
return {label: options for label, options in configs} |
| 78 |
|
| 79 |
|
| 80 |
def _append_config_options(imported_modules, config_options): |
| 81 |
for mod in imported_modules: |
| 82 |
configs = mod.list_opts() |
| 83 |
if not isinstance(configs, dict): |
| 84 |
configs = _process_old_opts(configs) |
| 85 |
for key, val in configs.items(): |
| 86 |
config_options[key].extend(val) |