fixed external networks to all find caddy correctly and implemented systemd service files for auto start

This commit is contained in:
spinach 2025-03-01 07:55:38 -05:00
parent 905bd0b3a9
commit 4524e77c2f
6 changed files with 58 additions and 7 deletions

4
.gitignore vendored
View File

@ -11,3 +11,7 @@ volumes/
# prevents env from being leaked anywhere
**/.env
# prevents username or directory structure leak
init/
!init/readme.md

View File

@ -60,6 +60,11 @@ def parse_env(env_file):
:service: is a string of the service name to parse, must match folder name
"""
if os.path.isfile(env_file):
print(f"{env_file} not found... skipping")
return
f = open(env_file)
c = dict()
for line in f:
@ -93,6 +98,42 @@ def gen_env(kv):
return env
def gen_init(service, force=False):
"""gen_init takes in a service name and creates a service file"""
service_file = f"init/{service}.service"
# prevent overwrite
if os.path.isfile(service_file) and not force:
print(f"{service_file} already exists... skipping")
return
PWD = os.getenv("PWD")
contents = f"""[Unit]
Description=Starts {service}
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=true
WorkingDirectory={PWD}/{service}
ExecStart=/usr/bin/docker compose up -d
ExecStop=/usr/bin/docker compose down
[Install]
WantedBy=multi-user.target"""
f = open(service_file, 'w')
f.write(contents)
f.close()
def enable_service(service, force=False):
gen_init(service,force)
subprocess.run(["sudo", "install", "-m", "644", f"init/{service}.service", "/etc/systemd/system/"])
subprocess.run(["sudo", "systemctl", "enable", f"{service}"])
subprocess.run(["sudo", "systemctl", "start", f"{service}"])
def config_service(service, force=False):
"""config_service processes service env vars to generate private .env file
@ -101,8 +142,9 @@ def config_service(service, force=False):
:force: is an optional parameter to overwrite existing file
default is False
"""
# setup directory
subprocess.run(["mkdir", "-p", f"{service}/priv"])
enable_service(service, force)
priv_file = f"{service}/priv/env"
# prevent overwrite
@ -110,8 +152,10 @@ def config_service(service, force=False):
print(f"{priv_file} already exists... skipping")
return parse_env(priv_file)
c = parse_env(f"{service}/env")
subprocess.run(["mkdir", "-p", f"{service}/priv"])
f = open(priv_file, 'w')
f.write(gen_env(c))
f.close()

View File

@ -1,8 +1,8 @@
include:
- gitea/docker-compose.yml
- seafile/docker-compose.yml
# - seafile/docker-compose.yml
- caddy/docker-compose.yml
- radicale/docker-compose.yml
# - radicale/docker-compose.yml
# networks:
# caddy:

View File

@ -35,6 +35,7 @@ networks:
gitea:
external: false
caddy:
external: false
external:
name: caddy_caddy
driver: bridge

View File

@ -22,5 +22,6 @@ services:
networks:
caddy:
external: false
external:
name: caddy_caddy
driver: bridge

View File

@ -50,5 +50,6 @@ networks:
seafile:
external: false
caddy:
external: false
external:
name: caddy_caddy
driver: bridge