|
| 1 | +""" |
| 2 | +Module implements merging of default configuration obtained via Dynaconf |
| 3 | +with command line arguments. |
| 4 | +""" |
| 5 | +import argparse |
| 6 | +import logging |
| 7 | +import warnings |
| 8 | +from typing import Dict, Optional |
| 9 | + |
| 10 | +from dynaconf import Dynaconf |
| 11 | + |
| 12 | +settings = Dynaconf( |
| 13 | + environments=True, |
| 14 | + lowercase_read=False, |
| 15 | + load_dotenv=True, |
| 16 | + settings_files=["settings.yaml", ".secrets.yaml", "settings.yml", ".secrets.yml"] |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def _resolve_cloud_name(args: argparse.Namespace) -> Optional[Dict]: |
| 21 | + defaults = settings.as_dict() |
| 22 | + |
| 23 | + if defaults['CLOUD'][args.cloud].get('environments', None) is None: |
| 24 | + warnings.warn('[DEPRECATION WARNING] The structure of settings.yaml is changed, ' |
| 25 | + 'please use environments list and default_env option. This behavior will be ' |
| 26 | + 'removed in future releases.') |
| 27 | + return defaults['CLOUD'][args.cloud] |
| 28 | + default_env = defaults['CLOUD'][args.cloud].get('cloud_env', None) \ |
| 29 | + if args.cloud_environment is None else \ |
| 30 | + args.cloud_environment |
| 31 | + if default_env is None: |
| 32 | + logging.error("Couldn't resolve default environment") |
| 33 | + raise Exception("Invalid environment setup, base_env is missing") |
| 34 | + for env in defaults['CLOUD'][args.cloud]['environments']: |
| 35 | + if env['name'] == default_env: |
| 36 | + return env |
| 37 | + logging.debug("No environment found, maybe all variables are passed from command line") |
| 38 | + return None |
| 39 | + |
| 40 | + |
| 41 | +def read_config(args: argparse.Namespace, default_args: Dict) -> Dict: |
| 42 | + """ |
| 43 | + Reads config from Dynaconf and merges it with arguments provided via commandline. |
| 44 | + """ |
| 45 | + result = {'cloud': None, |
| 46 | + 'dns': None, |
| 47 | + 'cloud_name': None, |
| 48 | + 'cluster_name': args.cluster_name} |
| 49 | + if not args.__contains__('cloud'): |
| 50 | + return result |
| 51 | + defaults = settings.as_dict() |
| 52 | + |
| 53 | + if args.dns_provider is not None: |
| 54 | + result['dns'] = {'provider': args.dns_provider, |
| 55 | + 'conf': defaults['DNS'][args.dns_provider]} |
| 56 | + result['dns']['conf'].update( |
| 57 | + {j[4:]: i['proc'](vars(args)[j]) |
| 58 | + for j, i in default_args['dns'].items() |
| 59 | + if vars(args)[j] is not None} |
| 60 | + ) |
| 61 | + if args.cloud is not None: |
| 62 | + cloud_defaults = _resolve_cloud_name(args) |
| 63 | + result['cloud_name'] = args.cloud |
| 64 | + result['cloud'] = cloud_defaults |
| 65 | + result['cloud'].update( |
| 66 | + {j: i['proc'](vars(args)[j]) for j, i in default_args['install'].items() |
| 67 | + if vars(args)[j] is not None} |
| 68 | + ) |
| 69 | + |
| 70 | + if result['dns'] is not None: |
| 71 | + result['dns']['conf'].update({ |
| 72 | + 'cluster_name': args.cluster_name, |
| 73 | + 'base_domain': result['cloud']['base_domain'] |
| 74 | + }) |
| 75 | + return result |
0 commit comments