77 lines
3.3 KiB
Python
Executable File
77 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Выгружает все oauth2-proxy в compose-файлы репозитория.
|
|
|
|
Зачем: 43 контейнера созданы `docker run` — конфигурация нигде не сохранена.
|
|
При пересоздании (обновление образа, перезагрузка) настройки теряются.
|
|
|
|
Правила:
|
|
* где `deploy/ctNNN/docker-compose.yml` ещё нет — пишем туда;
|
|
* где уже есть — пишем отдельный `oauth2-proxy.compose.yml`
|
|
(так уже сделано для ct118 и ct119);
|
|
* секреты в Git не попадают: в compose ${VAR}, значения — в /root/.env-out.
|
|
|
|
Запуск: python3 ops/scripts/oauth2-compose.py [--dry]
|
|
"""
|
|
import importlib.util
|
|
import os
|
|
import sys
|
|
|
|
REPO = "/root/buryan-infra"
|
|
D2C = os.path.join(REPO, "ops", "scripts", "docker-to-compose.py")
|
|
|
|
spec = importlib.util.spec_from_file_location("d2c", D2C)
|
|
d2c = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(d2c)
|
|
|
|
|
|
def main():
|
|
dry = "--dry" in sys.argv
|
|
import subprocess
|
|
cts = subprocess.run("pct list | awk 'NR>1{print $1}'", shell=True,
|
|
capture_output=True, text=True).stdout.split()
|
|
total = 0
|
|
written = []
|
|
for ct in cts:
|
|
try:
|
|
services, volumes, envfile, networks = d2c.build(ct)
|
|
except Exception as e:
|
|
print("CT%s: ошибка %s" % (ct, str(e)[:60]))
|
|
continue
|
|
# берём только oauth2-proxy
|
|
services = {k: v for k, v in services.items() if k.startswith("oauth2-proxy")}
|
|
if not services:
|
|
continue
|
|
d = os.path.join(REPO, "deploy", "ct" + ct)
|
|
main_file = os.path.join(d, "docker-compose.yml")
|
|
target = main_file if not os.path.exists(main_file) else os.path.join(
|
|
d, "oauth2-proxy.compose.yml")
|
|
body = ("# Сгенерировано oauth2-compose.py по живому состоянию (10.11.2026).\n"
|
|
"# Секреты — в .env на CT (права 600), в Git не попадают.\n"
|
|
"# Проверка: docker compose -f %s config\n\n"
|
|
% os.path.basename(target)) + d2c.yaml_dump(services, volumes, networks)
|
|
print("%-8s %-2d служб -> %s" % ("CT" + ct, len(services),
|
|
os.path.relpath(target, REPO)))
|
|
written.append((ct, target, len(services), len(envfile)))
|
|
total += len(services)
|
|
if dry:
|
|
continue
|
|
os.makedirs(d, exist_ok=True)
|
|
with open(target, "w", encoding="utf-8") as f:
|
|
f.write(body)
|
|
with open(os.path.join(d, "oauth2-env.example"), "w", encoding="utf-8") as f:
|
|
f.write("# Значения положить в .env на CT%s (chmod 600)\n" % ct)
|
|
for k in sorted(envfile):
|
|
f.write("%s=\n" % k)
|
|
os.makedirs("/root/.env-out", exist_ok=True)
|
|
envpath = "/root/.env-out/ct%s.env" % ct
|
|
with open(envpath, "w", encoding="utf-8") as f:
|
|
for k in sorted(envfile):
|
|
f.write('%s="%s"\n' % (k, envfile[k]))
|
|
os.chmod(envpath, 0o600)
|
|
print("\nвсего служб:", total, "| файлов:", len(written))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|