From 7cc08044446719483e8cc235742e6f9a0d500287 Mon Sep 17 00:00:00 2001 From: asteroide Date: Thu, 12 Oct 2017 10:03:18 +0200 Subject: Add the moonforming container. Change-Id: I8da07a3cdca18b837ea51e64045006562ca03c7a --- moonv4/templates/moonforming/Dockerfile | 8 +++ moonv4/templates/moonforming/README.md | 12 ++++ moonv4/templates/moonforming/conf2consul.py | 103 ++++++++++++++++++++++++++++ moonv4/templates/moonforming/run.sh | 7 ++ 4 files changed, 130 insertions(+) create mode 100644 moonv4/templates/moonforming/Dockerfile create mode 100644 moonv4/templates/moonforming/README.md create mode 100644 moonv4/templates/moonforming/conf2consul.py create mode 100644 moonv4/templates/moonforming/run.sh diff --git a/moonv4/templates/moonforming/Dockerfile b/moonv4/templates/moonforming/Dockerfile new file mode 100644 index 00000000..14b12b60 --- /dev/null +++ b/moonv4/templates/moonforming/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3 +WORKDIR /usr/src/app +RUN pip install --no-cache-dir --upgrade requests pyyaml moon_utilities moon_db + +ADD conf2consul.py /root +ADD run.sh /root + +CMD ["bash", "/root/run.sh" ] \ No newline at end of file diff --git a/moonv4/templates/moonforming/README.md b/moonv4/templates/moonforming/README.md new file mode 100644 index 00000000..66b509d1 --- /dev/null +++ b/moonv4/templates/moonforming/README.md @@ -0,0 +1,12 @@ +Introduction +============ + +moonforming is a container used to automatize the configuration of the Moon patform + +Usage +===== + +```bash +docker run asteroide/moonforming:1.0 +``` + diff --git a/moonv4/templates/moonforming/conf2consul.py b/moonv4/templates/moonforming/conf2consul.py new file mode 100644 index 00000000..3c9a1e2e --- /dev/null +++ b/moonv4/templates/moonforming/conf2consul.py @@ -0,0 +1,103 @@ +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(): + if type(_value) is dict: + 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() + diff --git a/moonv4/templates/moonforming/run.sh b/moonv4/templates/moonforming/run.sh new file mode 100644 index 00000000..97e29d98 --- /dev/null +++ b/moonv4/templates/moonforming/run.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +# TODO: wait for consul +python3 /root/conf2consul.py /etc/moon/moon.conf + +# TODO: wait for database +moon_db_manager upgrade \ No newline at end of file -- cgit 1.2.3-korg