Files
Key-service-Server/Website/sync_dev_hosts.sh
T
Aiirondev_dev c4a483a57c Add admin instance management and system tools
- Implemented sync_dev_hosts.sh for managing local hosts entries for development.
- Created admin_instances.html for managing school instances, including creation and listing.
- Developed admin_system.html for core services management, including restart and backup operations.
- Added my_instance.html for users to view and manage their assigned instances.
- Introduced provision_instance.sh for provisioning new instances with Docker, including Nginx configuration and SSL certificate handling.
2026-04-14 22:12:14 +02:00

46 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
HOSTS_FILE="/etc/hosts"
START_MARK="# >>> invario-dev-hosts >>>"
END_MARK="# <<< invario-dev-hosts <<<"
if ! command -v docker >/dev/null 2>&1; then
echo "docker fehlt; Hosts-Sync übersprungen"
exit 0
fi
if ! docker compose ps mongodb >/dev/null 2>&1; then
echo "mongodb-compose-Service nicht erreichbar; Hosts-Sync übersprungen"
exit 0
fi
DOMAINS_RAW="$(docker compose exec -T mongodb mongosh --quiet --eval 'db.getSiblingDB("Invario_Website").school_instances.find({},{_id:0,domain:1}).toArray().forEach(d=>{ if(d.domain){ print(String(d.domain).trim()) } })' 2>/dev/null || true)"
TMP_BLOCK="$(mktemp)"
trap 'rm -f "$TMP_BLOCK"' EXIT
{
echo "$START_MARK"
echo "127.0.0.1 localhost"
while IFS= read -r line; do
domain="$(echo "$line" | tr -d '\r' | xargs)"
if [[ -n "$domain" ]]; then
echo "127.0.0.1 $domain"
fi
done <<< "$DOMAINS_RAW"
echo "$END_MARK"
} > "$TMP_BLOCK"
if [[ "${EUID}" -eq 0 ]]; then
sed -i "/$START_MARK/,/$END_MARK/d" "$HOSTS_FILE"
cat "$TMP_BLOCK" >> "$HOSTS_FILE"
else
sudo sed -i "/$START_MARK/,/$END_MARK/d" "$HOSTS_FILE"
sudo tee -a "$HOSTS_FILE" < "$TMP_BLOCK" >/dev/null
fi
echo "Hosts-Sync abgeschlossen"