|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import glob |
| 5 | +import envtpl |
| 6 | +import sys |
| 7 | +from mflog import get_logger |
| 8 | +from mfutil.plugins import layerapi2_label_to_plugin_name |
| 9 | +from configparser_extended import ExtendedConfigParser |
| 10 | + |
| 11 | +MODULE_RUNTIME_HOME = os.environ["MODULE_RUNTIME_HOME"] |
| 12 | +MFBASE_PLUGINS_HOME = os.path.join(MODULE_RUNTIME_HOME, |
| 13 | + "var", "plugins") |
| 14 | +CONFIG = os.environ.get('MFCONFIG', 'GENERIC') |
| 15 | +HOSTNAME = os.environ.get('MFCOM_HOSTNAME') |
| 16 | +HOSTNAME_FULL = os.environ.get('MFCOM_HOSTNAME_FULL') |
| 17 | +MODULE = os.environ['MODULE'] |
| 18 | + |
| 19 | +LOGGER = get_logger("__make_nginx_conf") |
| 20 | + |
| 21 | + |
| 22 | +def get_conf(plugin_configuration_file): |
| 23 | + plugin_conf = {} |
| 24 | + plugin_directory = os.path.dirname(plugin_configuration_file) |
| 25 | + parser = ExtendedConfigParser(config=CONFIG, strict=False, |
| 26 | + inheritance='im', interpolation=None) |
| 27 | + try: |
| 28 | + with open(os.path.join(plugin_directory, |
| 29 | + ".layerapi2_label"), "r") as f: |
| 30 | + label = f.read().strip() |
| 31 | + except Exception: |
| 32 | + LOGGER.warning( |
| 33 | + "can't read %s/.layerapi2_label => ignoring this plugin", |
| 34 | + plugin_directory) |
| 35 | + return None |
| 36 | + try: |
| 37 | + plugin_name = layerapi2_label_to_plugin_name(label) |
| 38 | + except Exception as e: |
| 39 | + LOGGER.warning("can't read %s/.layerapi2_label with error: %s => " |
| 40 | + "ignoring this plugin", plugin_directory, e) |
| 41 | + return None |
| 42 | + plugin_conf['directory'] = plugin_directory |
| 43 | + plugin_conf['name'] = plugin_name |
| 44 | + parser.read(plugin_configuration_file) |
| 45 | + plugin_conf['use_postgresql'] = "1" |
| 46 | + if parser.has_option('general', 'use_postgresql'): |
| 47 | + if parser.getint("general", "use_postgresql") == 0: |
| 48 | + plugin_conf['use_postgresql'] = "0" |
| 49 | + plugin_conf['use_storage'] = "1" |
| 50 | + if parser.has_option('general', 'use_storage'): |
| 51 | + if parser.getint("general", "use_storage") == 0: |
| 52 | + plugin_conf['use_storage'] = "0" |
| 53 | + return plugin_conf |
| 54 | + |
| 55 | + |
| 56 | +plugins = [] |
| 57 | +if len(sys.argv) == 2: |
| 58 | + config_files = glob.glob(sys.argv[1] + "/config.ini") |
| 59 | +else: |
| 60 | + config_files = glob.glob(MFBASE_PLUGINS_HOME + "/*/config.ini") |
| 61 | +for config_file in config_files: |
| 62 | + plugin_conf = get_conf(config_file) |
| 63 | + if plugin_conf: |
| 64 | + plugins.append(plugin_conf) |
| 65 | + |
| 66 | +nginx_conf_file = os.path.join(os.environ['MODULE_HOME'], 'config', |
| 67 | + 'nginx.conf') |
| 68 | + |
| 69 | +with open(nginx_conf_file, "r") as f: |
| 70 | + extra_variables = { |
| 71 | + "PLUGINS": plugins |
| 72 | + } |
| 73 | + content = envtpl.render_string(f.read(), extra_variables=extra_variables, |
| 74 | + keep_multi_blank_lines=False) |
| 75 | + |
| 76 | +print(content) |
0 commit comments