diff options
author | Asteroide <thomas.duval@orange.com> | 2017-10-19 09:39:09 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-10-19 09:39:09 +0000 |
commit | 2a6a0241e1c40e95c6782513dd99d437ebf73c62 (patch) | |
tree | 712680c07ee40bb40ebcf01384eff3dcaa613f7c | |
parent | e493f6692072574d339972f0db3adb8270bdeccd (diff) | |
parent | 7cc08044446719483e8cc235742e6f9a0d500287 (diff) |
Merge "Add the moonforming container."
-rw-r--r-- | moonv4/templates/moonforming/Dockerfile | 8 | ||||
-rw-r--r-- | moonv4/templates/moonforming/README.md | 12 | ||||
-rw-r--r-- | moonv4/templates/moonforming/conf2consul.py | 103 | ||||
-rw-r--r-- | moonv4/templates/moonforming/run.sh | 7 |
4 files changed, 130 insertions, 0 deletions
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 |