|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import yaml |
| 4 | + |
| 5 | +cfg = {} |
| 6 | + |
| 7 | + |
| 8 | +def eprint(*args, **kwargs): |
| 9 | + print(*args, file=sys.stderr, **kwargs) |
| 10 | + |
| 11 | +def debug(*args, **kwargs): |
| 12 | + # can't use log.debug since that calls back to cfg |
| 13 | + if "LOG_LEVEL" in os.environ and os.environ["LOG_LEVEL"] == "DEBUG": |
| 14 | + print(*args, **kwargs) |
| 15 | + |
| 16 | +def _load_cfg(): |
| 17 | + # load config yaml |
| 18 | + yml_file = None |
| 19 | + config_dirs = [] |
| 20 | + # check if there is a command line optionfor config directory |
| 21 | + for i in range(1, len(sys.argv)): |
| 22 | + #print(i, sys.argv[i]) |
| 23 | + if sys.argv[i].startswith("--config-dir"): |
| 24 | + # use the given directory |
| 25 | + arg = sys.argv[i] |
| 26 | + config_dirs.append(arg[len("--config-dir")+1:]) # return text after option string |
| 27 | + debug(f"got cmd line override for config-dir: {config_dirs[0]}") |
| 28 | + break |
| 29 | + if not config_dirs and "CONFIG_DIR" in os.environ: |
| 30 | + config_dirs.append(os.environ["CONFIG_DIR"]) |
| 31 | + debug(f"got environment override for config-dir: {config_dirs[0]}") |
| 32 | + if not config_dirs: |
| 33 | + config_dirs = [".", "/config", "/etc/hsds/"] # default locations |
| 34 | + for config_dir in config_dirs: |
| 35 | + file_name = os.path.join(config_dir, "config.yml") |
| 36 | + debug("checking config path:", file_name) |
| 37 | + if os.path.isfile(file_name): |
| 38 | + yml_file = file_name |
| 39 | + break |
| 40 | + if not yml_file: |
| 41 | + msg = "unable to find config file" |
| 42 | + eprint(msg) |
| 43 | + raise FileNotFoundError(msg) |
| 44 | + debug(f"_load_cfg with '{yml_file}'") |
| 45 | + try: |
| 46 | + with open(yml_file, "r") as f: |
| 47 | + yml_config = yaml.safe_load(f) |
| 48 | + except yaml.scanner.ScannerError as se: |
| 49 | + msg = f"Error parsing config.yml: {se}" |
| 50 | + eprint(msg) |
| 51 | + raise KeyError(msg) |
| 52 | + |
| 53 | + # load override yaml |
| 54 | + yml_override = None |
| 55 | + if "CONFIG_OVERRIDE_PATH" in os.environ: |
| 56 | + override_yml_filepath = os.environ["CONFIG_OVERRIDE_PATH"] |
| 57 | + else: |
| 58 | + override_yml_filepath = "/config/override.yml" |
| 59 | + if os.path.isfile(override_yml_filepath): |
| 60 | + debug(f"loading override configuation: {override_yml_filepath}") |
| 61 | + try: |
| 62 | + with open(override_yml_filepath, "r") as f: |
| 63 | + yml_override = yaml.safe_load(f) |
| 64 | + except yaml.scanner.ScannerError as se: |
| 65 | + msg = f"Error parsing '{override_yml_filepath}': {se}" |
| 66 | + eprint(msg) |
| 67 | + raise KeyError(msg) |
| 68 | + |
| 69 | + |
| 70 | + # apply overrides for each key and store in cfg global |
| 71 | + for x in yml_config: |
| 72 | + cfgval = yml_config[x] |
| 73 | + # see if there is a command-line override |
| 74 | + option = '--'+x+'=' |
| 75 | + override = None |
| 76 | + for i in range(1, len(sys.argv)): |
| 77 | + if sys.argv[i].startswith(option): |
| 78 | + # found an override |
| 79 | + arg = sys.argv[i] |
| 80 | + override = arg[len(option):] # return text after option string |
| 81 | + debug(f"got cmd line override for {x}") |
| 82 | + |
| 83 | + |
| 84 | + # see if there are an environment variable override |
| 85 | + if override is None and x.upper() in os.environ: |
| 86 | + override = os.environ[x.upper()] |
| 87 | + debug(f"got env value override for {x} ") |
| 88 | + |
| 89 | + # see if there is a yml override |
| 90 | + if override is None and yml_override and x in yml_override: |
| 91 | + override = yml_override[x] |
| 92 | + debug(f"got config override for {x}") |
| 93 | + |
| 94 | + if override: |
| 95 | + if cfgval is not None: |
| 96 | + try: |
| 97 | + override = type(cfgval)(override) # convert to same type as yaml |
| 98 | + except ValueError as ve: |
| 99 | + msg = f"Error applying command line override value for key: {x}: {ve}" |
| 100 | + eprint(msg) |
| 101 | + # raise KeyError(msg) |
| 102 | + cfgval = override # replace the yml value |
| 103 | + |
| 104 | + if isinstance(cfgval, str) and len(cfgval) > 1 and cfgval[-1] in ('g', 'm', 'k') and cfgval[:-1].isdigit(): |
| 105 | + # convert values like 512m to corresponding integer |
| 106 | + u = cfgval[-1] |
| 107 | + n = int(cfgval[:-1]) |
| 108 | + if u == 'k': |
| 109 | + cfgval = n * 1024 |
| 110 | + elif u == 'm': |
| 111 | + cfgval = n * 1024*1024 |
| 112 | + else: # u == 'g' |
| 113 | + cfgval = n * 1024*1024*1024 |
| 114 | + cfg[x] = cfgval |
| 115 | + |
| 116 | +def get(x): |
| 117 | + if not cfg: |
| 118 | + _load_cfg() |
| 119 | + if x not in cfg: |
| 120 | + raise KeyError(f"config value {x} not found") |
| 121 | + return cfg[x] |
0 commit comments