Implement automatic Nginx updates and hosts synchronization for development and production environments
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
# Automatische Subdomain-Hosts-Synchronisation
|
||||
|
||||
## Problem
|
||||
Nach jedem Neustart waren die Subdomains nicht mehr im `/etc/hosts` eingetragen, was zu Verbindungsproblemen führte. Das musste manuell mit `sync_dev_hosts.sh` behoben werden.
|
||||
|
||||
## Lösung
|
||||
|
||||
Diese Lösung bietet **3 Ebenen der Automatisierung**:
|
||||
|
||||
### 1. ✅ Automatische Sync bei Container-Start
|
||||
**Datei:** `launch_prod.sh` und `launch_dev.sh`
|
||||
|
||||
- Nach `docker compose up` wird `sync_dev_hosts.sh` automatisch aufgerufen
|
||||
- Ein Background-Daemon sorgt für regelmäßige Synchronisation
|
||||
- **Development:** Alle 30 Sekunden (konfigurierbar via `DEV_HOSTS_REFRESH_INTERVAL`)
|
||||
- **Production:** Alle 3600 Sekunden/1 Stunde (konfigurierbar via `PROD_HOSTS_REFRESH_INTERVAL`)
|
||||
|
||||
### 2. ✅ systemd Service für permanente Automatisierung
|
||||
**Dateien:** `invario-hosts-sync.service`, `setup-hosts-sync.sh`
|
||||
|
||||
Der systemd-Service stellt sicher, dass die Hosts-Synchronisation auch nach System-Reboots aktiviert bleibt.
|
||||
|
||||
#### Installation:
|
||||
```bash
|
||||
cd /home/max/Dokumente/repos/Key-service-Server
|
||||
sudo ./setup-hosts-sync.sh
|
||||
```
|
||||
|
||||
#### Status überprüfen:
|
||||
```bash
|
||||
sudo systemctl status invario-hosts-sync
|
||||
```
|
||||
|
||||
#### Logs anschauen:
|
||||
```bash
|
||||
sudo journalctl -u invario-hosts-sync -f
|
||||
```
|
||||
|
||||
#### Deinstallation:
|
||||
```bash
|
||||
sudo systemctl disable invario-hosts-sync
|
||||
sudo systemctl stop invario-hosts-sync
|
||||
sudo rm /etc/systemd/system/invario-hosts-sync.service
|
||||
sudo systemctl daemon-reload
|
||||
```
|
||||
|
||||
### 3. ✅ Manuelle Synchronisation
|
||||
Jederzeit manuell möglich:
|
||||
```bash
|
||||
cd /home/max/Dokumente/repos/Key-service-Server/Website
|
||||
./sync_dev_hosts.sh
|
||||
```
|
||||
|
||||
## Wie es funktioniert
|
||||
|
||||
1. **sync_dev_hosts.sh** liest alle Subdomains aus der MongoDB-Datenbank
|
||||
2. Aktualisiert `/etc/hosts` mit den gültigen Subdomains
|
||||
3. Läuft auf verschiedenen Ebenen:
|
||||
- Beim Container-Start (launch_prod.sh/launch_dev.sh)
|
||||
- Im Hintergrund als regelmäßiger Job
|
||||
- Als systemd-Service für permanente Überwachung
|
||||
|
||||
## Konfiguration
|
||||
|
||||
### Environment-Variablen:
|
||||
|
||||
**Für Development:**
|
||||
```bash
|
||||
DEV_HOSTS_IP=192.168.1.100 # Optionale IP (Standard: auto-detect)
|
||||
DEV_HOSTS_REFRESH_INTERVAL=30 # Sekunden zwischen Syncs (0 = deaktiviert)
|
||||
```
|
||||
|
||||
**Für Production:**
|
||||
```bash
|
||||
PROD_HOSTS_REFRESH_INTERVAL=3600 # Sekunden zwischen Syncs (0 = deaktiviert)
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Service startet nicht
|
||||
```bash
|
||||
# Logs prüfen
|
||||
sudo journalctl -u invario-hosts-sync --no-pager
|
||||
|
||||
# Service neu laden
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl restart invario-hosts-sync
|
||||
```
|
||||
|
||||
### Hosts werden nicht aktualisiert
|
||||
```bash
|
||||
# Manuell test
|
||||
./Website/sync_dev_hosts.sh
|
||||
|
||||
# MongoDB-Verbindung prüfen
|
||||
docker compose ps mongodb
|
||||
docker compose logs mongodb
|
||||
```
|
||||
|
||||
### Berechtigungen-Fehler
|
||||
Das Script benötigt Zugriff auf `/etc/hosts`. Normalerweise wird es als `root` ausgeführt:
|
||||
```bash
|
||||
# Wenn nötig mit sudo
|
||||
sudo ./Website/sync_dev_hosts.sh
|
||||
```
|
||||
@@ -18,6 +18,12 @@ echo " DEV_HOSTS_REFRESH_INTERVAL=$DEV_HOSTS_REFRESH_INTERVAL"
|
||||
docker compose build website
|
||||
docker compose up -d
|
||||
|
||||
NGINX_SCRIPT="$SCRIPT_DIR/../nginx.sh"
|
||||
if [[ -x "$NGINX_SCRIPT" ]]; then
|
||||
echo "Aktualisiere Nginx Reverse Proxy..."
|
||||
sudo "$NGINX_SCRIPT" apply || echo "Warnung: Nginx konnte nicht automatisch aktualisiert werden."
|
||||
fi
|
||||
|
||||
if [[ -x "$SCRIPT_DIR/sync_dev_hosts.sh" ]]; then
|
||||
"$SCRIPT_DIR/sync_dev_hosts.sh" || true
|
||||
if [[ "${DEV_HOSTS_REFRESH_INTERVAL}" != "0" ]]; then
|
||||
|
||||
@@ -27,5 +27,27 @@ echo " INSTANCE_WILDCARD_KEY_FILE=$INSTANCE_WILDCARD_KEY_FILE"
|
||||
docker compose build website
|
||||
docker compose up -d
|
||||
|
||||
NGINX_SCRIPT="$SCRIPT_DIR/../nginx.sh"
|
||||
if [[ -x "$NGINX_SCRIPT" ]]; then
|
||||
echo "Aktualisiere Nginx Reverse Proxy..."
|
||||
sudo "$NGINX_SCRIPT" apply || echo "Warnung: Nginx konnte nicht automatisch aktualisiert werden."
|
||||
fi
|
||||
|
||||
# Automatically sync hosts after container start
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
if [[ -x "$SCRIPT_DIR/sync_dev_hosts.sh" ]]; then
|
||||
echo "Warte auf MongoDB-Verfügbarkeit..."
|
||||
sleep 5
|
||||
"$SCRIPT_DIR/sync_dev_hosts.sh" || true
|
||||
echo "Production Hosts werden synchronisiert (stündlich)"
|
||||
|
||||
# Starte Background-Daemon für stündliche Sync
|
||||
if [[ "${PROD_HOSTS_REFRESH_INTERVAL}" != "0" ]]; then
|
||||
nohup bash -lc "while true; do '$SCRIPT_DIR/sync_dev_hosts.sh' >/dev/null 2>&1 || true; sleep ${PROD_HOSTS_REFRESH_INTERVAL:-3600}; done" \
|
||||
>"$SCRIPT_DIR/prod-hosts-sync.log" 2>&1 &
|
||||
echo " Prod-Hosts-Sync läuft im Hintergrund (Log: $SCRIPT_DIR/prod-hosts-sync.log)"
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "Production stack is running on http://localhost:4999"
|
||||
echo "Provisioning expects wildcard certificate for all subdomains."
|
||||
|
||||
@@ -40,8 +40,8 @@
|
||||
<h2>4. Kontaktinformationen</h2>
|
||||
<p>
|
||||
<strong>Telefon:</strong> +49 (0) 123 / 456789<br>
|
||||
<strong>E-Mail:</strong> info@invario.cloud<br>
|
||||
<strong>Website:</strong> www.invario.cloud
|
||||
<strong>E-Mail:</strong> info@invario.eu<br>
|
||||
<strong>Website:</strong> www.invario.eu
|
||||
</p>
|
||||
</section>
|
||||
|
||||
|
||||
@@ -126,6 +126,16 @@ start_services() {
|
||||
start_gitea_service
|
||||
start_website_service
|
||||
|
||||
if [ -x "$SCRIPT_DIR/nginx.sh" ]; then
|
||||
echo "Aktualisiere Nginx Reverse Proxy..."
|
||||
sudo "$SCRIPT_DIR/nginx.sh" apply || echo "Warnung: Nginx konnte nicht automatisch aktualisiert werden."
|
||||
fi
|
||||
|
||||
if [ -x "$WEBSITE_DIR/sync_dev_hosts.sh" ]; then
|
||||
echo "Aktualisiere Hosts-Datei fuer Subdomains..."
|
||||
sudo "$WEBSITE_DIR/sync_dev_hosts.sh" || echo "Warnung: Hosts-Sync konnte nicht automatisch aktualisiert werden."
|
||||
fi
|
||||
|
||||
echo "-------------------------------------------------------"
|
||||
echo "Docker Services wurden gestartet!"
|
||||
echo "Gitea Intern: $GITEA_BIND_IP:$GITEA_HTTP_PORT"
|
||||
@@ -135,8 +145,8 @@ start_services() {
|
||||
echo "Web Domain: $WEBSITE_DOMAIN"
|
||||
echo "Aktueller Host: $(current_host_info)"
|
||||
echo ""
|
||||
echo "Nginx wird jetzt ueber ein separates Skript verwaltet."
|
||||
echo "Fuehre danach aus: sudo ./nginx.sh apply"
|
||||
echo "Nginx Reverse Proxy wurde automatisch aktualisiert, falls verfuegbar."
|
||||
echo "Hosts-Datei wurde automatisch synchronisiert, falls verfuegbar."
|
||||
echo "-------------------------------------------------------"
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
[Unit]
|
||||
Description=Invario Subdomains /etc/hosts Synchronization
|
||||
Documentation=man:systemd.service(5)
|
||||
After=docker.service
|
||||
Wants=docker.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
WorkingDirectory=/home/max/Dokumente/repos/Key-service-Server/Website
|
||||
ExecStart=/home/max/Dokumente/repos/Key-service-Server/Website/sync_dev_hosts.sh
|
||||
Restart=always
|
||||
RestartSec=60
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
SyslogIdentifier=invario-hosts-sync
|
||||
|
||||
# Umgebungsvariablen
|
||||
Environment="DEV_HOSTS_IP="
|
||||
Environment="PROD_HOSTS_REFRESH_INTERVAL=3600"
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,15 @@
|
||||
[Unit]
|
||||
Description=Invario Stack Autostart
|
||||
After=docker.service network-online.target
|
||||
Wants=docker.service network-online.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
User=root
|
||||
WorkingDirectory=/home/max/Dokumente/repos/Key-service-Server
|
||||
ExecStart=/home/max/Dokumente/repos/Key-service-Server/start-stack-on-boot.sh
|
||||
RemainAfterExit=yes
|
||||
TimeoutStartSec=0
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
Executable
+62
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
SERVICE_NAME="invario-hosts-sync"
|
||||
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
|
||||
SYNC_SCRIPT="$SCRIPT_DIR/Website/sync_dev_hosts.sh"
|
||||
SERVICE_TEMPLATE="$SCRIPT_DIR/${SERVICE_NAME}.service"
|
||||
|
||||
echo "=== Invario Hosts-Sync Automatische Einrichtung ==="
|
||||
echo ""
|
||||
|
||||
# Prüfe ob root
|
||||
if [[ "${EUID}" -ne 0 ]]; then
|
||||
echo "❌ Fehler: Dieses Skript benötigt root-Rechte"
|
||||
echo " Starten Sie mit: sudo $0"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Prüfe Abhängigkeiten
|
||||
if [[ ! -x "$SYNC_SCRIPT" ]]; then
|
||||
echo "❌ Fehler: sync_dev_hosts.sh nicht gefunden unter $SYNC_SCRIPT"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f "$SERVICE_TEMPLATE" ]]; then
|
||||
echo "❌ Fehler: Service-Template nicht gefunden unter $SERVICE_TEMPLATE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Installiere Service-Datei
|
||||
echo "📋 Installiere systemd-Service..."
|
||||
cp "$SERVICE_TEMPLATE" "$SERVICE_FILE"
|
||||
chmod 644 "$SERVICE_FILE"
|
||||
|
||||
# Lade systemd-Konfiguration neu
|
||||
echo "🔄 Lade systemd-Konfiguration neu..."
|
||||
systemctl daemon-reload
|
||||
|
||||
# Aktiviere und starte Service
|
||||
echo "▶️ Starte und aktiviere ${SERVICE_NAME}.service..."
|
||||
systemctl enable "$SERVICE_NAME.service"
|
||||
systemctl start "$SERVICE_NAME.service"
|
||||
|
||||
# Anzeige Status
|
||||
echo ""
|
||||
echo "✅ Installation abgeschlossen!"
|
||||
echo ""
|
||||
echo "📊 Status überprüfen:"
|
||||
echo " sudo systemctl status $SERVICE_NAME"
|
||||
echo ""
|
||||
echo "📝 Logs anschauen:"
|
||||
echo " sudo journalctl -u $SERVICE_NAME -f"
|
||||
echo ""
|
||||
echo "🛑 Service stoppen:"
|
||||
echo " sudo systemctl stop $SERVICE_NAME"
|
||||
echo ""
|
||||
echo "🗑️ Service deinstallieren:"
|
||||
echo " sudo systemctl disable $SERVICE_NAME"
|
||||
echo " sudo systemctl stop $SERVICE_NAME"
|
||||
echo " sudo rm $SERVICE_FILE"
|
||||
echo " sudo systemctl daemon-reload"
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
SERVICE_NAME="invario-stack-autostart"
|
||||
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
|
||||
SERVICE_TEMPLATE="$SCRIPT_DIR/${SERVICE_NAME}.service"
|
||||
BOOT_SCRIPT="$SCRIPT_DIR/start-stack-on-boot.sh"
|
||||
|
||||
if [[ "${EUID}" -ne 0 ]]; then
|
||||
echo "This script must be run as root. Use: sudo $0"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -x "$BOOT_SCRIPT" ]]; then
|
||||
echo "Missing executable boot script: $BOOT_SCRIPT"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f "$SERVICE_TEMPLATE" ]]; then
|
||||
echo "Missing service template: $SERVICE_TEMPLATE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp "$SERVICE_TEMPLATE" "$SERVICE_FILE"
|
||||
chmod 644 "$SERVICE_FILE"
|
||||
|
||||
systemctl daemon-reload
|
||||
systemctl enable "$SERVICE_NAME.service"
|
||||
|
||||
echo "Autostart installed. Start it now with: systemctl start $SERVICE_NAME"
|
||||
echo "Check status with: systemctl status $SERVICE_NAME"
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
# Start the full stack and refresh routing on every boot.
|
||||
./gitea.sh start
|
||||
Reference in New Issue
Block a user