252 lines
8.1 KiB
Python
Executable File
252 lines
8.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import argparse
|
|
import os
|
|
|
|
|
|
def parse_env(path: Path) -> dict[str, str]:
|
|
out: dict[str, str] = {}
|
|
if not path.exists():
|
|
return out
|
|
for raw in path.read_text().splitlines():
|
|
line = raw.strip()
|
|
if not line or line.startswith("#"):
|
|
continue
|
|
if "=" not in line:
|
|
continue
|
|
key, value = line.split("=", 1)
|
|
out[key.strip()] = value.strip().strip('"').strip("'")
|
|
return out
|
|
|
|
|
|
def abs_from(base: Path, raw: str, fallback: str) -> Path:
|
|
candidate = (raw or fallback).strip()
|
|
if not candidate:
|
|
candidate = fallback
|
|
p = Path(candidate).expanduser()
|
|
if not p.is_absolute():
|
|
p = (base / p).resolve()
|
|
return p
|
|
|
|
|
|
def write_unit(path: Path, content: str) -> None:
|
|
path.write_text(content.strip() + "\n")
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--stack-env", default="stack.env")
|
|
parser.add_argument("--output-dir", default=str(Path.home() / ".config/containers/systemd"))
|
|
args = parser.parse_args()
|
|
|
|
repo_root = Path(__file__).resolve().parents[2]
|
|
stack_env_path = abs_from(repo_root, args.stack_env, "stack.env")
|
|
env = parse_env(stack_env_path)
|
|
|
|
repo_dir = abs_from(repo_root, env.get("REPO_DIR", "."), ".")
|
|
app_db_file = abs_from(repo_root, env.get("APP_DATABASE_FILE", "./db.sqlite3"), "./db.sqlite3")
|
|
|
|
redis_data_dir = abs_from(repo_root, env.get("QUADLET_REDIS_DATA_DIR", "./.podman/gia_redis_data"), "./.podman/gia_redis_data")
|
|
whatsapp_data_dir = abs_from(repo_root, env.get("QUADLET_WHATSAPP_DATA_DIR", "./.podman/gia_whatsapp_data"), "./.podman/gia_whatsapp_data")
|
|
|
|
vrun_dir = Path("/code/vrun")
|
|
signal_cli_dir = (repo_dir / "signal-cli-config").resolve()
|
|
uwsgi_ini = (repo_dir / "docker" / "uwsgi.ini").resolve()
|
|
redis_conf = (repo_dir / "docker" / "redis.conf").resolve()
|
|
|
|
for p in (redis_data_dir, whatsapp_data_dir, vrun_dir, signal_cli_dir):
|
|
p.mkdir(parents=True, exist_ok=True)
|
|
|
|
out_dir = Path(args.output_dir).expanduser().resolve()
|
|
out_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
env_file = stack_env_path
|
|
|
|
pod_unit = """
|
|
[Unit]
|
|
Description=GIA Pod
|
|
|
|
[Pod]
|
|
PodName=gia
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
"""
|
|
|
|
target_unit = """
|
|
[Unit]
|
|
Description=GIA Stack Target
|
|
Wants=gia-redis.service gia-signal.service gia-migration.service gia-collectstatic.service gia-app.service gia-asgi.service gia-ur.service gia-scheduling.service
|
|
After=gia-redis.service gia-signal.service gia-migration.service gia-collectstatic.service
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
"""
|
|
|
|
redis_unit = f"""
|
|
[Unit]
|
|
Description=GIA Redis
|
|
PartOf=gia.target
|
|
After=network-online.target
|
|
Wants=network-online.target
|
|
|
|
[Container]
|
|
Image=docker.io/library/redis:latest
|
|
ContainerName=redis_gia
|
|
Pod=gia.pod
|
|
Volume={redis_conf}:/etc/redis.conf:ro
|
|
Volume={redis_data_dir}:/data
|
|
Volume={vrun_dir}:/var/run
|
|
Exec=redis-server /etc/redis.conf
|
|
|
|
[Service]
|
|
Restart=always
|
|
RestartSec=2
|
|
|
|
[Install]
|
|
WantedBy=gia.target
|
|
"""
|
|
|
|
signal_unit = f"""
|
|
[Unit]
|
|
Description=GIA Signal API
|
|
PartOf=gia.target
|
|
After=network-online.target
|
|
Wants=network-online.target
|
|
|
|
[Container]
|
|
Image=docker.io/bbernhard/signal-cli-rest-api:latest
|
|
ContainerName=signal
|
|
Pod=gia.pod
|
|
Volume={signal_cli_dir}:/home/.local/share/signal-cli
|
|
Environment=MODE=json-rpc
|
|
|
|
[Service]
|
|
Restart=always
|
|
RestartSec=2
|
|
|
|
[Install]
|
|
WantedBy=gia.target
|
|
"""
|
|
|
|
def gia_container(name: str, container_name: str, command: str, include_uwsgi: bool, include_whatsapp: bool, after: str, requires: str, one_shot: bool = False) -> str:
|
|
lines = [
|
|
"[Unit]",
|
|
f"Description={name}",
|
|
"PartOf=gia.target",
|
|
f"After={after}",
|
|
f"Requires={requires}",
|
|
"",
|
|
"[Container]",
|
|
"Image=localhost/xf/gia:prod",
|
|
f"ContainerName={container_name}",
|
|
"Pod=gia.pod",
|
|
f"EnvironmentFile={env_file}",
|
|
"Environment=SIGNAL_HTTP_URL=http://127.0.0.1:8080",
|
|
f"Volume={repo_dir}:/code",
|
|
f"Volume={app_db_file}:/conf/db.sqlite3",
|
|
f"Volume={vrun_dir}:/var/run",
|
|
]
|
|
if include_uwsgi:
|
|
lines.append(f"Volume={uwsgi_ini}:/conf/uwsgi.ini")
|
|
if include_whatsapp:
|
|
lines.append(f"Volume={whatsapp_data_dir}:{env.get('WHATSAPP_DB_DIR', '/var/tmp/whatsapp')}")
|
|
lines.append(f"Exec={command}")
|
|
lines.extend(["", "[Service]"])
|
|
if one_shot:
|
|
lines.extend([
|
|
"Type=oneshot",
|
|
"RemainAfterExit=yes",
|
|
"TimeoutStartSec=0",
|
|
"ExecStartPre=/bin/sh -c 'for i in $(seq 1 60); do [ -S /code/vrun/gia-redis.sock ] && exit 0; sleep 1; done; exit 1'",
|
|
])
|
|
else:
|
|
lines.extend([
|
|
"Restart=always",
|
|
"RestartSec=2",
|
|
])
|
|
lines.extend(["", "[Install]", "WantedBy=gia.target"])
|
|
return "\n".join(lines)
|
|
|
|
migration_unit = gia_container(
|
|
"GIA Migration",
|
|
"migration_gia",
|
|
"sh -c '. /venv/bin/activate && python manage.py migrate --noinput'",
|
|
include_uwsgi=False,
|
|
include_whatsapp=False,
|
|
after="gia-redis.service gia-signal.service",
|
|
requires="gia-redis.service gia-signal.service",
|
|
one_shot=True,
|
|
)
|
|
|
|
collectstatic_unit = gia_container(
|
|
"GIA Collectstatic",
|
|
"collectstatic_gia",
|
|
"sh -c '. /venv/bin/activate && python manage.py collectstatic --noinput'",
|
|
include_uwsgi=False,
|
|
include_whatsapp=False,
|
|
after="gia-migration.service",
|
|
requires="gia-migration.service",
|
|
one_shot=True,
|
|
)
|
|
|
|
app_unit = gia_container(
|
|
"GIA App",
|
|
"gia",
|
|
"sh -c 'if [ \\\"$OPERATION\\\" = \\\"uwsgi\\\" ] ; then . /venv/bin/activate && uwsgi --ini /conf/uwsgi.ini ; else . /venv/bin/activate && exec python manage.py runserver 0.0.0.0:8000; fi'",
|
|
include_uwsgi=True,
|
|
include_whatsapp=True,
|
|
after="gia-collectstatic.service gia-redis.service gia-signal.service",
|
|
requires="gia-collectstatic.service gia-redis.service gia-signal.service",
|
|
)
|
|
|
|
asgi_unit = gia_container(
|
|
"GIA ASGI",
|
|
"asgi_gia",
|
|
"sh -c 'rm -f /var/run/asgi-gia.sock && . /venv/bin/activate && python -m pip install --disable-pip-version-check -q uvicorn && python -m uvicorn app.asgi:application --uds /var/run/asgi-gia.sock --workers 1'",
|
|
include_uwsgi=False,
|
|
include_whatsapp=True,
|
|
after="gia-collectstatic.service gia-redis.service gia-signal.service",
|
|
requires="gia-collectstatic.service gia-redis.service gia-signal.service",
|
|
)
|
|
|
|
ur_unit = gia_container(
|
|
"GIA Unified Router",
|
|
"ur_gia",
|
|
"sh -c '. /venv/bin/activate && python manage.py ur'",
|
|
include_uwsgi=True,
|
|
include_whatsapp=True,
|
|
after="gia-collectstatic.service gia-redis.service gia-signal.service",
|
|
requires="gia-collectstatic.service gia-redis.service gia-signal.service",
|
|
)
|
|
|
|
scheduling_unit = gia_container(
|
|
"GIA Scheduling",
|
|
"scheduling_gia",
|
|
"sh -c '. /venv/bin/activate && python manage.py scheduling'",
|
|
include_uwsgi=True,
|
|
include_whatsapp=False,
|
|
after="gia-collectstatic.service gia-redis.service gia-signal.service",
|
|
requires="gia-collectstatic.service gia-redis.service gia-signal.service",
|
|
)
|
|
|
|
write_unit(out_dir / "gia.pod", pod_unit)
|
|
write_unit(out_dir / "gia.target", target_unit)
|
|
write_unit(out_dir / "gia-redis.container", redis_unit)
|
|
write_unit(out_dir / "gia-signal.container", signal_unit)
|
|
write_unit(out_dir / "gia-migration.container", migration_unit)
|
|
write_unit(out_dir / "gia-collectstatic.container", collectstatic_unit)
|
|
write_unit(out_dir / "gia-app.container", app_unit)
|
|
write_unit(out_dir / "gia-asgi.container", asgi_unit)
|
|
write_unit(out_dir / "gia-ur.container", ur_unit)
|
|
write_unit(out_dir / "gia-scheduling.container", scheduling_unit)
|
|
|
|
print(f"Wrote Quadlet units to: {out_dir}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|