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

104 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
WEB_DIR="/opt/hx-ki/web/hx-ki-website"
COMPOSE_FILE="/opt/hx-ki/docker/docker-compose.yml"
CADDY_FILE="/opt/hx-ki/caddy/Caddyfile"
echo "==> Prüfe, ob Web-Verzeichnis existiert: $WEB_DIR"
if [ ! -d "$WEB_DIR" ]; then
echo "FEHLER: Verzeichnis $WEB_DIR existiert nicht."
echo "Bitte sicherstellen: ZIP entpackt nach /opt/hx-ki/web/hx-ki-website"
exit 1
fi
echo "==> Erstelle/überschreibe Dockerfile im Web-Verzeichnis ..."
cat > "$WEB_DIR/Dockerfile" << 'EOF_DF'
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
ENV NODE_ENV=production
ENV PORT=3003
EXPOSE 3003
CMD ["npm", "start"]
EOF_DF
echo "==> Sicherung von docker-compose.yml anlegen ..."
cp "$COMPOSE_FILE" "${COMPOSE_FILE}.bak_website_$(date +%Y%m%d_%H%M%S)"
echo "==> Prüfe, ob Service 'hxki-web' bereits existiert ..."
if grep -q "hxki-web:" "$COMPOSE_FILE"; then
echo "Service 'hxki-web' ist bereits in $COMPOSE_FILE vorhanden. Überspringe Einfügen."
else
echo "==> Füge Service 'hxki-web' in docker-compose.yml ein ..."
awk '
BEGIN {added=0}
/^services:/ {print; in_services=1; next}
in_services && !added && ($0 !~ /^ /) {
print " hxki-web:";
print " container_name: hxki-web";
print " build:";
print " context: /opt/hx-ki/web/hx-ki-website";
print " restart: unless-stopped";
print " networks:";
print " - hxki-internal";
print "";
added=1;
}
{print}
END {
if (in_services && !added) {
print " hxki-web:";
print " container_name: hxki-web";
print " build:";
print " context: /opt/hx-ki/web/hx-ki-website";
print " restart: unless-stopped";
print " networks:";
print " - hxki-internal";
print "";
}
}
' "$COMPOSE_FILE" > "${COMPOSE_FILE}.tmp"
mv "${COMPOSE_FILE}.tmp" "$COMPOSE_FILE"
fi
echo "==> Sicherung der Caddyfile anlegen ..."
cp "$CADDY_FILE" "${CADDY_FILE}.bak_website_$(date +%Y%m%d_%H%M%S)"
echo "==> Prüfe, ob hx-ki.com bereits in Caddyfile existiert ..."
if grep -q "hx-ki.com" "$CADDY_FILE"; then
echo "ACHTUNG: Ein Eintrag für hx-ki.com existiert bereits."
echo "Ich füge KEINEN neuen Block hinzu, um nichts zu zerschießen."
echo "Bitte später manuell prüfen/anpassen: $CADDY_FILE"
else
echo "==> Ergänze hx-ki.com Block in Caddyfile ..."
cat >> "$CADDY_FILE" << 'EOF_CF'
hx-ki.com {
encode gzip
reverse_proxy hxki-web:3003
}
EOF_CF
fi
echo "==> Baue Service hxki-web über das Orchester-Compose ..."
cd /opt/hx-ki/docker
docker compose build hxki-web
echo "==> Starte komplettes Orchester (inkl. hxki-web) ..."
docker compose up -d
echo "==> Caddy neu laden ..."
docker exec -it hx-caddy-caddy-1 caddy reload || echo "Hinweis: Caddy-Reload fehlgeschlagen, bitte prüfen."
echo "==> Fertig. hxki-web ist jetzt Teil des Orchesters und im internen Netz erreichbar."