forked from cklug/docker-autostart
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f6b3090837 | ||
|
|
b6adbca987 | ||
| d100f5e255 | |||
| f163eb1e2c | |||
| f6cb85c211 | |||
| b897438c2c | |||
| 52409ee52e | |||
| 984d97b60f | |||
| 826f1e461f | |||
| d5da00e608 | |||
| ed00808e8b | |||
| a152333525 | |||
| da306c6b27 | |||
| 4afbd6c070 | |||
| 46d0d8c502 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
__pycache__/
|
||||
36
Readme.md
36
Readme.md
@ -1,4 +1,38 @@
|
||||
# Usage
|
||||
|
||||
## 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
|
||||
|
||||
!! This label is discovered as string, not by parsing Yaml. Commenting will not work!
|
||||
|
||||
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/`
|
||||
|
||||
### pre/post exec
|
||||
|
||||
e.g. `python3 discover.py ./sample/ -a ps --pre 'echo {path} {step}' --post './sample/notify_telegram.sh <telegram_bot_key> <telegram_room> {path} {cmd} {returncode} \n {stdout}'`
|
||||
|
||||
## manual config
|
||||
|
||||
### json config
|
||||
* create config.json from sample.json
|
||||
* add `python3 /opt/docker-autostart/start.py /path/to/your/config.json` to `/etc/rc.local`
|
||||
* add `python3 /opt/docker-autostart/start.py /path/to/your/config.json -t json` to `/etc/rc.local`
|
||||
|
||||
### 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
|
||||
* default action: up -d
|
||||
* add argument -a "<compose action>"
|
||||
|
||||
### pre/post exec
|
||||
|
||||
e.g. `python3 start.py test.lst -a ps --pre 'echo {path} {step}' --post './sample/notify_telegram.sh <telegram_bot_key> <telegram_room> {path} {cmd} {returncode} \n {stdout}'`
|
||||
17
config.json
17
config.json
@ -1,17 +0,0 @@
|
||||
{
|
||||
"/opt/docker/": [
|
||||
"frontend"
|
||||
],
|
||||
"/opt/docker/services/": [
|
||||
"ldap",
|
||||
"keycloak",
|
||||
"cloud",
|
||||
"cms",
|
||||
"ics_merger",
|
||||
"keycloak_account_selector",
|
||||
"antragsgruen",
|
||||
"zitate"
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
73
discover.py
Normal file
73
discover.py
Normal file
@ -0,0 +1,73 @@
|
||||
import os
|
||||
import logging
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
from start import base_args, change_service
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
COMPOSE_FILE = "docker-compose.yml"
|
||||
CONFIG_CMD = "docker-compose -f {path} config "
|
||||
AUTOSTART_KEY = "{prefix}.autostart=true"
|
||||
PRIORITY_KEY = "{prefix}.autostart.priority="
|
||||
PREFIX = "de.wie-ei"
|
||||
|
||||
Service = namedtuple("Service", ("path", "prio"))
|
||||
|
||||
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):
|
||||
with open(service) as src:
|
||||
service_definitions = src.read()
|
||||
key = AUTOSTART_KEY.format(prefix=PREFIX)
|
||||
if not key in service_definitions:
|
||||
return False
|
||||
prio_key = PRIORITY_KEY.format(prefix=PREFIX)
|
||||
pos = service_definitions.find(prio_key)
|
||||
if pos >= 0:
|
||||
start = pos + len(prio_key)
|
||||
end = pos + service_definitions[pos:].find('"\n')
|
||||
prio = service_definitions[start:end]
|
||||
return int(prio)
|
||||
return True
|
||||
|
||||
def find_autostart_services(services):
|
||||
start_services = []
|
||||
for service in services:
|
||||
prio = should_autostart(service)
|
||||
if prio:
|
||||
start_services.append(Service(path=service, prio=prio))
|
||||
return [x.path for x in sorted(start_services, key=lambda x:x.prio, reverse=True)]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(format="%(message)s (status %(returncode)s)", level=logging.INFO)
|
||||
parser = base_args("Docker-compose Autostart discovery")
|
||||
parser.add_argument("service_dir", nargs="+", help="One or more directories containing docker-compose services, only direct subdirectories are scanned")
|
||||
parser.add_argument("--list", "-l", action="store_true", help="list autostart services only, no action")
|
||||
parser.add_argument("--key", "-k", help=f"alternative label prefix, default: '{PREFIX}'")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.key:
|
||||
PREFIX = args.key
|
||||
services = find_services(args.service_dir)
|
||||
autostarts = find_autostart_services(services)
|
||||
if args.list:
|
||||
if autostarts:
|
||||
print("\n".join(autostarts))
|
||||
else:
|
||||
import start
|
||||
for service in autostarts:
|
||||
start.change_service(service, args.action, pre_cmd=args.pre, post_cmd=args.post)
|
||||
19
docker-autostart.openrc
Normal file
19
docker-autostart.openrc
Normal file
@ -0,0 +1,19 @@
|
||||
#!/sbin/openrc-run
|
||||
|
||||
command="python3"
|
||||
command_args="/opt/docker/docker-autostart/discover.py /opt/docker/ /opt/docker/services/"
|
||||
|
||||
depend(){
|
||||
need docker
|
||||
after sshd
|
||||
}
|
||||
|
||||
start(){
|
||||
#python3 /opt/docker/docker-autostart/discover.py /opt/docker/ /opt/docker/services/
|
||||
$command $command_args
|
||||
}
|
||||
|
||||
stop(){
|
||||
#python3 -a down /opt/docker/docker-autostart/discover.py /opt/docker/ /opt/docker/services/
|
||||
$command $command_args -a down
|
||||
}
|
||||
10
sample/http-prio/docker-compose.yml
Normal file
10
sample/http-prio/docker-compose.yml
Normal file
@ -0,0 +1,10 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
http:
|
||||
image: httpd:alpine
|
||||
ports:
|
||||
- "8080:80"
|
||||
labels:
|
||||
- "de.wie-ei.autostart=true"
|
||||
- "de.wie-ei.autostart.priority=100"
|
||||
9
sample/http/docker-compose.yml
Normal file
9
sample/http/docker-compose.yml
Normal file
@ -0,0 +1,9 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
http:
|
||||
image: httpd:alpine
|
||||
ports:
|
||||
- "8080:80"
|
||||
labels:
|
||||
- "de.wie-ei.autostart=true"
|
||||
7
sample/notify_telegram.sh
Executable file
7
sample/notify_telegram.sh
Executable file
@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
key="$1"
|
||||
room="$2"
|
||||
shift 2
|
||||
url="https://api.telegram.org/bot${key}/sendMessage"
|
||||
json='{"chat_id":'${room}', "text":"'"$@"'"}'
|
||||
curl "$url" --data "$json" -X POST -H 'Content-Type: application/json'
|
||||
6
sample/sample.lst
Normal file
6
sample/sample.lst
Normal file
@ -0,0 +1,6 @@
|
||||
/srv/services/gitea
|
||||
/srv/services/http
|
||||
/srv/services/icinga/
|
||||
/srv/services/rz_measurements/docker-compose.yml
|
||||
/srv/services/traefik
|
||||
/home/clemens/hg/biodiv2go/games/web
|
||||
76
start.py
76
start.py
@ -2,23 +2,71 @@ import argparse
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import logging
|
||||
import sys
|
||||
|
||||
def start(config):
|
||||
with open(config, "r") as src:
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def change_service(path, action, pre_cmd=None, post_cmd=None):
|
||||
cmd = ["docker-compose"] + action.split()
|
||||
if path.endswith("/docker-compose.yml"):
|
||||
path = path[:-len("/docker-compose.yml")]
|
||||
args = {
|
||||
"args": cmd,
|
||||
"cwd": path,
|
||||
}
|
||||
if post_cmd:
|
||||
args["capture_output"] = True
|
||||
args["text"] = True
|
||||
if pre_cmd:
|
||||
subprocess.run(pre_cmd.format(path=path, cmd=cmd, step="pre").split())
|
||||
r = subprocess.run(**args)
|
||||
log.info(f"processed {path}", extra={"path": path, "cmd": cmd, "returncode": r.returncode})
|
||||
if post_cmd:
|
||||
if r.stdout: print(r.stdout)
|
||||
if r.stderr: print(r.stderr, file=sys.stderr)
|
||||
subprocess.run(post_cmd.format(path=path, cmd=cmd, step="post", returncode=r.returncode, stdout=r.stdout, stderr=r.stderr).split())
|
||||
print()
|
||||
|
||||
|
||||
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]
|
||||
|
||||
def load_stdin(_):
|
||||
for line in sys.stdin:
|
||||
yield line.strip()
|
||||
|
||||
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, pre=None, post=None):
|
||||
load = get_loader(config_file)
|
||||
for path in load(config_file):
|
||||
change_service(path, action, pre_cmd=pre, post_cmd=post)
|
||||
|
||||
def base_args(desc):
|
||||
parser = argparse.ArgumentParser(description=desc)
|
||||
parser.add_argument("--action", "-a", default="up -d", help="docker-compose action to apply, default: up -d")
|
||||
parser.add_argument("--pre", "-s", help="pre-exec: command to run before each action")
|
||||
parser.add_argument("--post", "-e", help="post-exec: command to run after each action")
|
||||
return parser
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Docker-compose Autostart")
|
||||
parser.add_argument("config_file")
|
||||
|
||||
logging.basicConfig(format="%(message)s (status %(returncode)s)", level=logging.INFO)
|
||||
parser = base_args("Docker-compose Autostart")
|
||||
parser.add_argument("config_file", default="-", help="json file, plain text list or - for stdin")
|
||||
args = parser.parse_args()
|
||||
|
||||
start(args.config_file)
|
||||
|
||||
apply(args.config_file, args.action, pre=args.pre, post=args.post)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user