config.py 1.06 KB
Newer Older
kaiyou's avatar
kaiyou committed
1
2
import yaml
import os
kaiyou's avatar
kaiyou committed
3
4
5
import glob

from amonit import util
kaiyou's avatar
kaiyou committed
6
7
8
9
10
11
12
13
14
15
16


def expand(conf):
    """ Expand a configuration object according to the 'instances' field
    """
    for key, value in conf.copy().items():
        if type(value) is dict:
            if "instances" in value:
                instances = value["instances"]
                del conf[key]["instances"]
                conf.update(expand({
17
                    key + "_" + instance_key: util.merge(value, instance)
kaiyou's avatar
kaiyou committed
18
19
20
21
22
23
24
25
26
27
28
                    for instance_key, instance in instances.items()
                }))
                del conf[key]
            else:
                expand(conf[key])
    return conf


def load():
    """ Load the configuration from a yaml file
    """
kaiyou's avatar
kaiyou committed
29
30
31
32
33
34
    config_path = glob.glob(os.environ.get("AMONIT_CONFIG", "amonit.yaml"))
    if not config_path:
        raise ValueError("No configuration was loaded")
    loaded = [{}]
    for filename in config_path:
        with open(filename, "r") as handle:
kaiyou's avatar
kaiyou committed
35
            loaded.append(expand(yaml.load(handle, Loader=yaml.FullLoader)))
kaiyou's avatar
kaiyou committed
36
    return util.merge(*loaded)