Compare commits
2 Commits
a152333525
...
d5da00e608
| Author | SHA1 | Date |
|---|---|---|
|
|
d5da00e608 | |
|
|
ed00808e8b |
21
Readme.md
21
Readme.md
|
|
@ -1,13 +1,28 @@
|
|||
# Usage
|
||||
|
||||
## json config
|
||||
## autodiscover
|
||||
|
||||
usage: `python3 discover.py [-a <action>] [-l] service_dir [service_dir …]`
|
||||
|
||||
add label "de.wie-ei.autostart=true" to any service in a docker-compose-file
|
||||
|
||||
Examples:
|
||||
|
||||
* start services (up -d) `python3 discover.py /srv/services/ /opt/docker/testing/`
|
||||
* which services are configured for autostart? `python3 discover.py -l /srv/services/ /opt/docker/testing/`
|
||||
* check status `python3 discover.py -a ps /opt/docker/testing/`
|
||||
* stop services `python3 discover.py -a "down -v /srv/testing/`
|
||||
|
||||
## manual config
|
||||
|
||||
### json config
|
||||
* create config.json from sample.json
|
||||
* add `python3 /opt/docker-autostart/start.py /path/to/your/config.json -t json` to `/etc/rc.local`
|
||||
|
||||
## raw config
|
||||
### raw config
|
||||
* create config.lst from sample.lst
|
||||
* add `python3 /opt/docker-autostart/start.py /path/to/your/config.lst` to `/etc/rc.local`
|
||||
|
||||
## other actions
|
||||
### other actions
|
||||
* default action: up -d
|
||||
* add argument -a "<compose action>"
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
import argparse
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
COMPOSE_FILE = "docker-compose.yml"
|
||||
CONFIG_CMD = "docker-compose -f {path} config "
|
||||
AUTOSTART_KEY = 'de.wie-ei.autostart: "true"'
|
||||
AUTOSTART_KEY = 'de.wie-ei.autostart=true'
|
||||
|
||||
def complete_compose(entry):
|
||||
path = entry.path
|
||||
if not path.endswith(COMPOSE_FILE):
|
||||
return os.path.join(path, COMPOSE_FILE)
|
||||
return path
|
||||
|
||||
def has_compose(path):
|
||||
return os.path.exists(path)
|
||||
|
||||
def find_services(dirs):
|
||||
entries = [entry for base in dirs for entry in os.scandir(base) if entry.is_dir()]
|
||||
return filter(has_compose, map(complete_compose, entries))
|
||||
|
||||
def should_autostart(service):
|
||||
#r = subprocess.run(CONFIG_CMD.format(path=service).split(), capture_output=True, encoding="utf8")
|
||||
#return AUTOSTART_KEY in r.stdout
|
||||
with open(service) as src:
|
||||
return AUTOSTART_KEY in src.read()
|
||||
|
||||
def find_autostart_services(services):
|
||||
return list(filter(should_autostart, services))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(format="%(message)s (status %(returncode)s)", level=logging.INFO)
|
||||
parser = argparse.ArgumentParser(description="Docker-compose Autostart discovery")
|
||||
parser.add_argument("service_dir", nargs="+")
|
||||
parser.add_argument("--action", "-a", default="up -d")
|
||||
parser.add_argument("--list", "-l", action="store_true", help="list autostart services only, no action")
|
||||
args = parser.parse_args()
|
||||
services = find_services(args.service_dir)
|
||||
autostarts = find_autostart_services(services)
|
||||
if args.list:
|
||||
print("\n".join(autostarts))
|
||||
else:
|
||||
import start
|
||||
for service in autostarts:
|
||||
start.change_service(service, args.action)
|
||||
36
start.py
36
start.py
|
|
@ -3,11 +3,15 @@ import json
|
|||
import os
|
||||
import subprocess
|
||||
import logging
|
||||
import sys
|
||||
|
||||
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)
|
||||
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)
|
||||
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]
|
||||
|
||||
def load_stdin(_):
|
||||
for line in sys.stdin:
|
||||
yield line.strip()
|
||||
|
||||
config_types = {
|
||||
"json": load_json,
|
||||
"raw": load_raw
|
||||
}
|
||||
def get_loader(config_file):
|
||||
if config_file.endswith(".json"):
|
||||
return load_json
|
||||
if "-" == config_file:
|
||||
return load_stdin
|
||||
return 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)
|
||||
def apply(config_file, action):
|
||||
load = get_loader(config_file)
|
||||
for path in load(config_file):
|
||||
change_service(path, action)
|
||||
|
||||
|
||||
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")
|
||||
parser.add_argument("config_file", default="-", help="json file, plain text list or - for stdin")
|
||||
parser.add_argument("--action", "-a", default="up -d", help="docker-compose action to apply, default: up -d")
|
||||
args = parser.parse_args()
|
||||
|
||||
apply(args.config_file, args.action, args.type)
|
||||
apply(args.config_file, args.action)
|
||||
|
|
|
|||
Loading…
Reference in New Issue