aboutsummaryrefslogtreecommitdiffstats
path: root/moon_forming/conf2consul.py
blob: 148bf92369beab778a65ea69871108691f772bf6 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import sys
import requests
import yaml
import logging
import json
import base64

logging.basicConfig(level=logging.INFO)
log = logging.getLogger("moon.conf2consul")
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.WARNING)
requests_log.propagate = True

if len(sys.argv) == 2:
    if os.path.isfile(sys.argv[1]):
        CONF_FILENAME = sys.argv[1]
        CONSUL_HOST = "consul"
    else:
        CONF_FILENAME = "moon.conf"
        CONSUL_HOST = sys.argv[1]
    CONSUL_PORT = 8500
else:
    CONSUL_HOST = sys.argv[1] if len(sys.argv) > 1 else "consul"
    CONSUL_PORT = sys.argv[2] if len(sys.argv) > 2 else 8500
    CONF_FILENAME = sys.argv[3] if len(sys.argv) > 3 else "moon.conf"
HEADERS = {"content-type": "application/json"}


def search_config_file():
    data_config = None
    for _file in (
            CONF_FILENAME,
            "conf/moon.conf",
            "../moon.conf",
            "../conf/moon.conf",
            "/etc/moon/moon.conf",
    ):
        try:
            data_config = yaml.safe_load(open(_file))
        except FileNotFoundError:
            data_config = None
            continue
        else:
            break
    if not data_config:
        raise Exception("Configuration file not found...")
    return data_config


def put(key, value):
    url = "http://{host}:{port}/v1/kv/{key}".format(host=CONSUL_HOST, port=CONSUL_PORT, key=key)
    log.info(url)
    req = requests.put(
        url,
        headers=HEADERS,
        json=value
    )
    if req.status_code != 200:
        raise Exception("Error connecting to Consul ({}, {})".format(req.status_code, req.text))


def get(key):
    url = "http://{host}:{port}/v1/kv/{key}".format(host=CONSUL_HOST, port=CONSUL_PORT, key=key)
    req = requests.get(url)
    data = req.json()
    for item in data:
        log.info("{} {} -> {}".format(
            req.status_code,
            item["Key"],
            json.loads(base64.b64decode(item["Value"]).decode("utf-8"))
        ))
        yield json.loads(base64.b64decode(item["Value"]).decode("utf-8"))


def main():
    data_config = search_config_file()
    req = requests.head("http://{}:{}/ui/".format(CONSUL_HOST, CONSUL_PORT))
    if req.status_code != 200:
        log.critical("Consul is down...")
        log.critical("request info: {}/{}".format(req, req.text))
        sys.exit(1)

    put("database", data_config["database"])
    # put("messenger", data_config["messenger"])
    # put("slave", data_config["slave"])
    # put("docker", data_config["docker"])
    put("logging", data_config["logging"])
    # put("components_port_start", data_config["components"]["port_start"])

    for _key, _value in data_config["components"].items():
        put("components/{}".format(_key), data_config["components"][_key])

    # for _key, _value in data_config["plugins"].items():
    #     put("plugins/{}".format(_key), data_config["plugins"][_key])

    for _key, _value in data_config["openstack"].items():
        put("openstack/{}".format(_key), data_config["openstack"][_key])


main()