Merge branch 'master' of https://git.wiai.de/cklug/docker-autostart
This commit is contained in:
commit
f6b3090837
@ -15,6 +15,10 @@ Examples:
|
|||||||
* check status `python3 discover.py -a ps /opt/docker/testing/`
|
* check status `python3 discover.py -a ps /opt/docker/testing/`
|
||||||
* stop services `python3 discover.py -a "down -v /srv/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
|
## manual config
|
||||||
|
|
||||||
### json config
|
### json config
|
||||||
@ -28,3 +32,7 @@ Examples:
|
|||||||
### other actions
|
### other actions
|
||||||
* default action: up -d
|
* default action: up -d
|
||||||
* add argument -a "<compose action>"
|
* 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
discover.py
17
discover.py
@ -1,11 +1,10 @@
|
|||||||
import argparse
|
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
|
from start import base_args, change_service
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
COMPOSE_FILE = "docker-compose.yml"
|
COMPOSE_FILE = "docker-compose.yml"
|
||||||
@ -55,11 +54,10 @@ def find_autostart_services(services):
|
|||||||
|
|
||||||
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 discovery")
|
parser = base_args("Docker-compose Autostart discovery")
|
||||||
parser.add_argument("service_dir", nargs="+")
|
parser.add_argument("service_dir", nargs="+", help="One or more directories containing docker-compose services, only direct subdirectories are scanned")
|
||||||
parser.add_argument("--action", "-a", default="up -d")
|
|
||||||
parser.add_argument("--list", "-l", action="store_true", help="list autostart services only, no action")
|
parser.add_argument("--list", "-l", action="store_true", help="list autostart services only, no action")
|
||||||
parser.add_argument("--key", "-k", help="alternative label prefix")
|
parser.add_argument("--key", "-k", help=f"alternative label prefix, default: '{PREFIX}'")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.key:
|
if args.key:
|
||||||
@ -67,8 +65,9 @@ if __name__ == "__main__":
|
|||||||
services = find_services(args.service_dir)
|
services = find_services(args.service_dir)
|
||||||
autostarts = find_autostart_services(services)
|
autostarts = find_autostart_services(services)
|
||||||
if args.list:
|
if args.list:
|
||||||
print("\n".join(autostarts))
|
if autostarts:
|
||||||
|
print("\n".join(autostarts))
|
||||||
else:
|
else:
|
||||||
import start
|
import start
|
||||||
for service in autostarts:
|
for service in autostarts:
|
||||||
start.change_service(service, args.action)
|
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'
|
||||||
34
start.py
34
start.py
@ -8,12 +8,26 @@ import sys
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def change_service(path, action):
|
def change_service(path, action, pre_cmd=None, post_cmd=None):
|
||||||
cmd = ["docker-compose"] + action.split()
|
cmd = ["docker-compose"] + action.split()
|
||||||
if path.endswith("/docker-compose.yml"):
|
if path.endswith("/docker-compose.yml"):
|
||||||
path = path[:-len("/docker-compose.yml")]
|
path = path[:-len("/docker-compose.yml")]
|
||||||
r = subprocess.run(cmd, cwd=path)
|
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})
|
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):
|
def load_json(config_file):
|
||||||
@ -36,17 +50,23 @@ def get_loader(config_file):
|
|||||||
return load_stdin
|
return load_stdin
|
||||||
return load_raw
|
return load_raw
|
||||||
|
|
||||||
def apply(config_file, action):
|
def apply(config_file, action, pre=None, post=None):
|
||||||
load = get_loader(config_file)
|
load = get_loader(config_file)
|
||||||
for path in load(config_file):
|
for path in load(config_file):
|
||||||
change_service(path, action)
|
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__":
|
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 = base_args("Docker-compose Autostart")
|
||||||
parser.add_argument("config_file", default="-", help="json file, plain text list or - for stdin")
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
apply(args.config_file, args.action)
|
apply(args.config_file, args.action, pre=args.pre, post=args.post)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user