add reading from stdin
parent
a152333525
commit
ed00808e8b
36
start.py
36
start.py
|
|
@ -3,11 +3,15 @@ import json
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import logging
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def change_service(path, cmd):
|
def change_service(path, action):
|
||||||
|
cmd = ["docker-compose"] + action.split()
|
||||||
|
if path.endswith("/docker-compose.yml"):
|
||||||
|
path = path[:-len("/docker-compose.yml")]
|
||||||
r = subprocess.run(cmd, cwd=path)
|
r = subprocess.run(cmd, cwd=path)
|
||||||
log.info(f"processed {path}", extra={"path": path, "cmd": cmd, "returncode": r.returncode})
|
log.info(f"processed {path}", extra={"path": path, "cmd": cmd, "returncode": r.returncode})
|
||||||
|
|
||||||
|
|
@ -17,30 +21,32 @@ def load_json(config_file):
|
||||||
data = json.load(src)
|
data = json.load(src)
|
||||||
return [os.path.join(base, service) for base in data for service in data[base]]
|
return [os.path.join(base, service) for base in data for service in data[base]]
|
||||||
|
|
||||||
|
|
||||||
def load_raw(config_file):
|
def load_raw(config_file):
|
||||||
with open(config_file) as src:
|
with open(config_file) as src:
|
||||||
return [line.strip() for line in src]
|
return [line.strip() for line in src]
|
||||||
|
|
||||||
|
def load_stdin(_):
|
||||||
|
for line in sys.stdin:
|
||||||
|
yield line.strip()
|
||||||
|
|
||||||
config_types = {
|
def get_loader(config_file):
|
||||||
"json": load_json,
|
if config_file.endswith(".json"):
|
||||||
"raw": load_raw
|
return load_json
|
||||||
}
|
if "-" == config_file:
|
||||||
|
return load_stdin
|
||||||
|
return load_raw
|
||||||
|
|
||||||
|
def apply(config_file, action):
|
||||||
def apply(config_file, action, type):
|
load = get_loader(config_file)
|
||||||
cmd = ["docker-compose"] + action.split()
|
for path in load(config_file):
|
||||||
for path in config_types[type](config_file):
|
change_service(path, action)
|
||||||
change_service(path, cmd)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
logging.basicConfig(format="%(message)s (status %(returncode)s)", level=logging.INFO)
|
logging.basicConfig(format="%(message)s (status %(returncode)s)", level=logging.INFO)
|
||||||
parser = argparse.ArgumentParser(description="Docker-compose Autostart")
|
parser = argparse.ArgumentParser(description="Docker-compose Autostart")
|
||||||
parser.add_argument("config_file")
|
parser.add_argument("config_file", default="-", help="json file, plain text list or - for stdin")
|
||||||
parser.add_argument("--type", "-t", default="raw", choices=config_types)
|
parser.add_argument("--action", "-a", default="up -d", help="docker-compose action to apply, default: up -d")
|
||||||
parser.add_argument("--action", "-a", default="up -d")
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
apply(args.config_file, args.action, args.type)
|
apply(args.config_file, args.action)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue