Files
Network-MGMT/deploy.sh
T

168 lines
8.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ═══════════════════════════════════════════════════════════════════
# Network-MGMT — Deploy-Skript
#
# Ausführen auf deinem lokalen Rechner: bash deploy.sh
# Der Zielserver klont die App direkt von Gitea.
#
# Voraussetzung: SSH-Zugang zum Zielserver
# ═══════════════════════════════════════════════════════════════════
set -euo pipefail
G='\033[0;32m'; Y='\033[1;33m'; B='\033[0;34m'; BOLD='\033[1m'; NC='\033[0m'
echo ""
echo -e "${B}${BOLD}╔═══════════════════════════════════════════╗${NC}"
echo -e "${B}${BOLD}║ Network-MGMT — Deployment ║${NC}"
echo -e "${B}${BOLD}╚═══════════════════════════════════════════╝${NC}"
echo ""
# ── Eingaben ───────────────────────────────────────────────────────
echo -e "${BOLD}Kunden-Server (Ziel)${NC}"
read -rp " IP-Adresse / Hostname : " SERVER_HOST
read -rp " SSH-Benutzer [root] : " SERVER_USER; SERVER_USER="${SERVER_USER:-root}"
read -rp " SSH-Port [22] : " SERVER_PORT; SERVER_PORT="${SERVER_PORT:-22}"
echo ""
echo -e "${BOLD}Gitea${NC}"
read -rp " Gitea-URL (dein Server) : " GITEA_URL; GITEA_URL="${GITEA_URL:-https://git.mms-systemservice.de}"
read -rp " Gitea-Benutzer : " GITEA_USER; GITEA_USER="${GITEA_USER:-gitea-mms}"
read -rsp " Gitea-Passwort : " GITEA_PASS; echo
echo ""
echo -e "${BOLD}App-Einstellungen${NC}"
read -rp " App-Port [8080] : " APP_PORT; APP_PORT="${APP_PORT:-8080}"
APP_URL="http://${SERVER_HOST}:${APP_PORT}"
echo -e " → Erreichbar unter: ${G}${APP_URL}${NC}"
echo ""
echo -e "${BOLD}Datenbank${NC}"
read -rsp " DB-Passwort : " DB_PASSWORD; echo
DB_ROOT_PASSWORD="${DB_PASSWORD}ROOT"
# ── Bestätigung ────────────────────────────────────────────────────
echo ""
echo -e "${Y}────────────────────────────────────────────────${NC}"
echo " Ziel: ${SERVER_USER}@${SERVER_HOST}:${SERVER_PORT}"
echo " Quelle: ${GITEA_URL}/${GITEA_USER}/Network-MGMT"
echo " App-URL: ${APP_URL}"
echo -e "${Y}────────────────────────────────────────────────${NC}"
echo ""
read -rp "Deployment starten? [j/N] " CONFIRM
[[ "${CONFIRM,,}" == "j" ]] || { echo "Abgebrochen."; exit 0; }
# ── Setup-Skript mit eingebetteten Werten erzeugen ─────────────────
REMOTE_SCRIPT="/tmp/nm_deploy_$$.sh"
{
printf '#!/bin/bash\nset -euo pipefail\n\n'
printf 'G='"'"'\033[0;32m'"'"'; Y='"'"'\033[1;33m'"'"'; BOLD='"'"'\033[1m'"'"'; NC='"'"'\033[0m'"'"'\n\n'
printf 'GITEA_URL=%q\n' "$GITEA_URL"
printf 'GITEA_USER=%q\n' "$GITEA_USER"
printf 'GITEA_PASS=%q\n' "$GITEA_PASS"
printf 'APP_URL=%q\n' "$APP_URL"
printf 'APP_PORT=%q\n' "$APP_PORT"
printf 'DB_PASSWORD=%q\n' "$DB_PASSWORD"
printf 'DB_ROOT_PASSWORD=%q\n' "$DB_ROOT_PASSWORD"
printf 'GITEA_FULL_URL=%q\n' "${GITEA_URL}"
cat << 'SETUP'
DEPLOY_PATH="/opt/network-mgmt"
REPO_URL="${GITEA_URL/https:\/\//https://${GITEA_USER}:${GITEA_PASS}@}"
REPO_URL="${REPO_URL}/gitea-mms/Network-MGMT.git"
# Credentials in URL einbauen
CLONE_URL=$(echo "$GITEA_URL" | sed "s|https://|https://${GITEA_USER}:${GITEA_PASS}@|")
CLONE_URL="${CLONE_URL}/${GITEA_USER}/Network-MGMT.git"
echo ""
echo -e "${Y}[1/4] Docker prüfen / installieren ...${NC}"
if ! command -v docker &>/dev/null; then
curl -fsSL https://get.docker.com | sh
systemctl enable --now docker
fi
echo -e "${G} ✓ Docker: $(docker --version | awk '{print $3}' | tr -d ',')${NC}"
if ! docker compose version &>/dev/null 2>&1; then
ARCH="$(uname -m)"
mkdir -p "${HOME}/.docker/cli-plugins"
curl -fsSL \
"https://github.com/docker/compose/releases/latest/download/docker-compose-linux-${ARCH}" \
-o "${HOME}/.docker/cli-plugins/docker-compose"
chmod +x "${HOME}/.docker/cli-plugins/docker-compose"
fi
echo -e "${G} ✓ Docker Compose: $(docker compose version --short 2>/dev/null)${NC}"
echo ""
echo -e "${Y}[2/4] Repository von Gitea klonen / aktualisieren ...${NC}"
if [ -d "${DEPLOY_PATH}/.git" ]; then
cd "$DEPLOY_PATH"
git pull
echo -e "${G} ✓ Repository aktualisiert${NC}"
else
git clone "$CLONE_URL" "$DEPLOY_PATH"
echo -e "${G} ✓ Repository geklont${NC}"
fi
echo ""
echo -e "${Y}[3/4] Konfiguration ...${NC}"
cd "$DEPLOY_PATH"
mkdir -p storage/logs storage/framework/{cache,sessions,views} bootstrap/cache
chmod -R 775 storage bootstrap/cache
if [ ! -f ".env" ]; then
cp .env.example .env
sed -i "s|APP_URL=.*|APP_URL=${APP_URL}|" .env
sed -i "s|APP_PORT=.*|APP_PORT=${APP_PORT}|" .env
sed -i "s|DB_PASSWORD=secret|DB_PASSWORD=${DB_PASSWORD}|" .env
sed -i "s|DB_ROOT_PASSWORD=rootsecret|DB_ROOT_PASSWORD=${DB_ROOT_PASSWORD}|" .env
sed -i "s|GITEA_URL=.*|GITEA_URL=${GITEA_FULL_URL}|" .env
sed -i "s|GITEA_REPO=.*|GITEA_REPO=${GITEA_USER}/Network-MGMT|" .env
echo -e "${G} ✓ .env erstellt${NC}"
else
echo -e "${G} ✓ .env bereits vorhanden (nicht überschrieben)${NC}"
fi
echo ""
echo -e "${Y}[4/4] Container bauen und starten ...${NC}"
echo " (Erster Start: 35 Minuten)"
docker compose up -d --build
echo " Warte auf Start ..."
sleep 8
for i in $(seq 1 20); do
docker compose ps | grep -qE "healthy|Up" && break || sleep 5
done
echo ""
docker compose ps
echo ""
echo -e "${G}${BOLD}╔══════════════════════════════════════════════════╗${NC}"
echo -e "${G}${BOLD}║ ✓ Network-MGMT erfolgreich installiert! ║${NC}"
echo -e "${G}${BOLD}║ ║${NC}"
printf "${G}${BOLD}║ Browser: %-40s║${NC}\n" "${APP_URL}"
printf "${G}${BOLD}║ Pfad: %-40s║${NC}\n" "${DEPLOY_PATH}"
echo -e "${G}${BOLD}║ ║${NC}"
echo -e "${G}${BOLD}║ Logs: docker compose logs -f ║${NC}"
echo -e "${G}${BOLD}║ Stop: docker compose down ║${NC}"
echo -e "${G}${BOLD}╚══════════════════════════════════════════════════╝${NC}"
echo ""
SETUP
} > "$REMOTE_SCRIPT"
chmod +x "$REMOTE_SCRIPT"
# ── Script auf Server übertragen und ausführen ─────────────────────
echo ""
echo -e "${Y}▶ Verbinde mit ${SERVER_USER}@${SERVER_HOST} ...${NC}"
scp -q -P "$SERVER_PORT" "$REMOTE_SCRIPT" "${SERVER_USER}@${SERVER_HOST}:/tmp/nm_deploy.sh"
rm -f "$REMOTE_SCRIPT"
echo -e "${Y}▶ Setup läuft (Ausgabe vom Server):${NC}"
echo -e "${Y}────────────────────────────────────────────────${NC}"
ssh -p "$SERVER_PORT" "${SERVER_USER}@${SERVER_HOST}" \
"bash /tmp/nm_deploy.sh; rm -f /tmp/nm_deploy.sh"
echo -e "${Y}────────────────────────────────────────────────${NC}"
echo ""
echo -e "${G}${BOLD}✓ Fertig! → ${APP_URL}${NC}"
echo ""