From 46d0d8c502b8ad90ccb4da72f11debd879141cc8 Mon Sep 17 00:00:00 2001 From: Clemens Klug Date: Fri, 29 Mar 2019 12:44:20 +0100 Subject: [PATCH] support plain text lists as input & proper logging --- sample.lst | 6 ++++++ start.py | 47 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 sample.lst diff --git a/sample.lst b/sample.lst new file mode 100644 index 0000000..45ca8de --- /dev/null +++ b/sample.lst @@ -0,0 +1,6 @@ +/srv/services/gitea +/srv/services/http +/srv/services/icinga +/srv/services/rz_measurements +/srv/services/traefik +/home/clemens/hg/biodiv2go/games/web diff --git a/start.py b/start.py index cd2c415..d3c2d4a 100644 --- a/start.py +++ b/start.py @@ -2,24 +2,45 @@ import argparse import json import os import subprocess +import logging -def start(config): - with open(config, "r") as src: +log = logging.getLogger(__name__) + + +def change_service(path, cmd): + r = subprocess.run(cmd, cwd=path) + log.info(f"processed {path}", extra={"path": path, "cmd": cmd, "returncode": r.returncode}) + + +def load_json(config_file): + with open(config_file, "r") as src: data = json.load(src) - if data: - for base in data: - for service in data[base]: - path = os.path.join(base, service) - print(path) - r = subprocess.run(["docker-compose", "up", "-d"], cwd=path) - print(r) + return [os.path.join(base, service) for base in data for service in data[base]] + + +def load_raw(config_file): + with open(config_file) as src: + return [line.strip() for line in src] + + +config_types = { + "json": load_json, + "raw": load_raw +} + + +def apply(config_file, action, type): + cmd = ["docker-compose"] + action.split() + for path in config_types[type](config_file): + change_service(path, cmd) if __name__ == "__main__": + logging.basicConfig(format="%(message)s (status %(returncode)s)", level=logging.INFO) parser = argparse.ArgumentParser(description="Docker-compose Autostart") parser.add_argument("config_file") - + parser.add_argument("--type", "-t", default="raw", choices=config_types) + parser.add_argument("--action", "-a", default="up -d") args = parser.parse_args() - - start(args.config_file) - \ No newline at end of file + + apply(args.config_file, args.action, args.type)