aboutsummaryrefslogtreecommitdiff
path: root/kerbana/config.py
blob: 358d2fe5988462734ecae1257fdb71f9420ce723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import logging
import os
import sys

import strictyaml

log = logging.getLogger(__name__)


def read_from_yaml(fp):
    data = strictyaml.load(fp.read())
    for k, v in data.data.items():
        setattr(sys.modules[__name__], k, v)


conf_file = "/etc/kerbana/kerbana.yaml"
try:
    with open(conf_file) as fp:
        read_from_yaml(fp)
        log.info("Configuration read from %s", conf_file)
except FileNotFoundError:
    log.info("File %s not found.", conf_file)
except strictyaml.YAMLError as error:
    log.error("Syntax error in %s", conf_file)
    log.error(error)
    sys.exit(1)


conf_file = os.path.join(
    os.path.dirname(os.path.abspath(__file__)),
    "..",
    "kerbana.yaml"
)
try:
    with open(conf_file) as fp:
        read_from_yaml(fp)
        log.info("Configuration read from %s", conf_file)
except FileNotFoundError:
    log.info("File %s not found.", conf_file)
except strictyaml.YAMLError as error:
    log.error("Syntax error in %s", conf_file)
    log.error(error)
    sys.exit(1)

conf_file = os.environ.get("KERBANA_CONFIG")
if conf_file:
    try:
        with open(conf_file) as fp:
            read_from_yaml(fp)
            log.info("Configuration read from %s", conf_file)
    except FileNotFoundError:
        log.info("File %s (as in $KERBANA_CONFIG) not found.", conf_file)
    except strictyaml.YAMLError as error:
        log.error("Syntax error in %s (as in $KERBANA_CONFIG)", conf_file)
        log.error(error)
        sys.exit(1)
else:
    log.info("Variable KERBANA_CONFIG not available")