Files
hx-ki.com2/COM2_MAUTIC_RECREATE_BY_SERVICE_ONE_SHOT.sh
2026-03-06 15:22:40 +00:00

90 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
DIR="/opt/hx-ki/com2-stack"
COMPOSE="$DIR/docker-compose.yml"
ENVF="$DIR/.env"
MA_CONTAINER="hxki-mautic"
echo "=== COM2 · MAUTIC RECREATE (service-resolve, no guessing) ==="
[ -f "$COMPOSE" ] || { echo "FAIL: missing $COMPOSE"; exit 1; }
[ -f "$ENVF" ] || { echo "FAIL: missing $ENVF"; exit 1; }
# 1) Find service name that owns container_name: hxki-mautic (authority: compose)
SVC="$(
python3 - <<'PY'
import re, pathlib
p = pathlib.Path("/opt/hx-ki/com2-stack/docker-compose.yml")
s = p.read_text()
m = re.search(r'(?ms)^services:\s*\n(.*?)(?=^\S|\Z)', s)
if not m:
print("")
raise SystemExit(0)
blk = m.group(1)
# parse services by 2-space indentation
services = []
cur = None
cur_lines = []
for line in blk.splitlines(True):
m2 = re.match(r'^ ([A-Za-z0-9_.-]+):\s*$', line)
if m2:
if cur:
services.append((cur, "".join(cur_lines)))
cur = m2.group(1)
cur_lines = []
else:
if cur is not None:
cur_lines.append(line)
if cur:
services.append((cur, "".join(cur_lines)))
# priority 1: container_name match
for name, body in services:
if re.search(r'(?m)^\s{4}container_name:\s*hxki-mautic\s*$', body):
print(name); raise SystemExit(0)
# priority 2: image contains mautic
for name, body in services:
if re.search(r'(?m)^\s{4}image:\s*.*mautic.*$', body, re.I):
print(name); raise SystemExit(0)
print("")
PY
)"
if [ -z "$SVC" ]; then
echo "FAIL: Konnte Mautic-Service-Namen in $COMPOSE nicht finden."
echo "Check: grep -n 'container_name: hxki-mautic' $COMPOSE"
exit 1
fi
echo "OK: Mautic service name in compose = $SVC"
# 2) Ensure admin vars exist in .env (no guessing; generate if missing) - safe charset (no tr range bug)
set -a
# shellcheck disable=SC1090
. "$ENVF" || true
set +a
gen_pw() { tr -dc 'A-Za-z0-9@#%+=:,.!' </dev/urandom | head -c 24; echo; }
: "${MAUTIC_ADMIN_USERNAME:=admin}"
: "${MAUTIC_ADMIN_PASSWORD:=$(gen_pw)}"
: "${MAUTIC_ADMIN_EMAIL:=admin@hxki.local}"
grep -q '^MAUTIC_ADMIN_USERNAME=' "$ENVF" && sed -i "s/^MAUTIC_ADMIN_USERNAME=.*/MAUTIC_ADMIN_USERNAME=$MAUTIC_ADMIN_USERNAME/" "$ENVF" || echo "MAUTIC_ADMIN_USERNAME=$MAUTIC_ADMIN_USERNAME" >> "$ENVF"
grep -q '^MAUTIC_ADMIN_PASSWORD=' "$ENVF" && sed -i "s/^MAUTIC_ADMIN_PASSWORD=.*/MAUTIC_ADMIN_PASSWORD=$MAUTIC_ADMIN_PASSWORD/" "$ENVF" || echo "MAUTIC_ADMIN_PASSWORD=$MAUTIC_ADMIN_PASSWORD" >> "$ENVF"
grep -q '^MAUTIC_ADMIN_EMAIL=' "$ENVF" && sed -i "s/^MAUTIC_ADMIN_EMAIL=.*/MAUTIC_ADMIN_EMAIL=$MAUTIC_ADMIN_EMAIL/" "$ENVF" || echo "MAUTIC_ADMIN_EMAIL=$MAUTIC_ADMIN_EMAIL" >> "$ENVF"
echo "OK: Admin ENV ensured in .env (password stored in file)"
# 3) Recreate using SERVICE name (not container name)
cd "$DIR"
docker compose up -d --force-recreate "$SVC"
echo "=== DONE ==="
echo "If Mautic still not ready, run:"
echo " docker logs --tail=200 $MA_CONTAINER"