Files
ai-agent/deploy.sh
2026-03-24 08:05:24 +01:00

84 lines
3.6 KiB
Bash
Executable File
Raw Permalink 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.
#!/usr/bin/env bash
# deploy.sh Baut Binary lokal (cross-compile) und deployt es per scp auf den Server
set -euo pipefail
# Credentials aus deploy.env laden
ENV_FILE="$(dirname "$0")/deploy.env"
if [[ ! -f "$ENV_FILE" ]]; then
echo "❌ deploy.env nicht gefunden. Kopiere deploy.env.example nach deploy.env und passe sie an."
exit 1
fi
source "$ENV_FILE"
: "${DEPLOY_HOST:?DEPLOY_HOST fehlt in deploy.env}"
: "${DEPLOY_USER:?DEPLOY_USER fehlt in deploy.env}"
: "${DEPLOY_PASS:?DEPLOY_PASS fehlt in deploy.env}"
: "${DEPLOY_DIR:=/home/${DEPLOY_USER}/brain-bot}"
# Sudo-Passwort: standardmäßig gleich wie SSH-Passwort
SUDO_PASS="${SUDO_PASS:-$DEPLOY_PASS}"
SSH_OPTS="-o StrictHostKeyChecking=no -o BatchMode=no"
if ! command -v sshpass &>/dev/null; then
echo "❌ sshpass nicht installiert: sudo apt install sshpass"
exit 1
fi
ssh_cmd() {
sshpass -p "$DEPLOY_PASS" ssh $SSH_OPTS "${DEPLOY_USER}@${DEPLOY_HOST}" "$@"
}
scp_cmd() {
sshpass -p "$DEPLOY_PASS" scp $SSH_OPTS "$@"
}
sudo_cmd() {
ssh_cmd "echo '${SUDO_PASS}' | sudo -S -p '' $*"
}
# ── Lokaler Cross-Compile-Build ─────────────────────────────────────────────
echo "🔨 Baue Linux/amd64-Binary lokal..."
mkdir -p bin
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/discord-bot ./cmd/discord/
echo "✅ Binary gebaut: bin/discord-bot"
# ── Systemd-Service deaktivieren (einmalige Migration) ──────────────────────
if ssh_cmd "systemctl is-enabled --quiet brain-bot 2>/dev/null"; then
echo "🔄 Migriere von systemd zu Docker..."
sudo_cmd "systemctl stop brain-bot" 2>/dev/null || true
sudo_cmd "systemctl disable brain-bot" 2>/dev/null || true
echo "✅ Systemd-Service deaktiviert"
fi
# ── Alten Container stoppen ─────────────────────────────────────────────────
echo "⏹️ Stoppe laufenden Container..."
ssh_cmd "cd '${DEPLOY_DIR}' && docker compose down 2>/dev/null" || true
echo "✅ Container gestoppt"
# ── Zielverzeichnis anlegen ─────────────────────────────────────────────────
echo "📁 Stelle Zielverzeichnis sicher: ${DEPLOY_DIR}..."
ssh_cmd "mkdir -p '${DEPLOY_DIR}'"
# ── Dateien übertragen ───────────────────────────────────────────────────────
echo "📦 Übertrage Binary..."
scp_cmd bin/discord-bot "${DEPLOY_USER}@${DEPLOY_HOST}:${DEPLOY_DIR}/discord-bot"
echo "📋 Übertrage Dockerfile + docker-compose.yml..."
scp_cmd Dockerfile docker-compose.yml "${DEPLOY_USER}@${DEPLOY_HOST}:${DEPLOY_DIR}/"
echo "📋 Übertrage config.yml..."
scp_cmd config.yml "${DEPLOY_USER}@${DEPLOY_HOST}:${DEPLOY_DIR}/config.yml"
# tasks.json anlegen falls nicht vorhanden (Volume-Mount braucht existierende Datei)
ssh_cmd "test -f '${DEPLOY_DIR}/tasks.json' || echo '[]' > '${DEPLOY_DIR}/tasks.json'"
# ── Docker-Image auf Server bauen und starten ────────────────────────────────
echo "🚀 Baue Image und starte Container..."
ssh_cmd "cd '${DEPLOY_DIR}' && docker compose up -d --build"
echo ""
echo "✅ Deployment abgeschlossen!"
echo " Status: ssh ${DEPLOY_USER}@${DEPLOY_HOST} 'cd ${DEPLOY_DIR} && docker compose ps'"
echo " Logs: ssh ${DEPLOY_USER}@${DEPLOY_HOST} 'cd ${DEPLOY_DIR} && docker compose logs -f'"