Files
yourpart3/deploy-server.sh

204 lines
6.1 KiB
Bash
Executable File

#!/bin/bash
# YourPart Daemon Server-Side Deployment Script
# Führen Sie dieses Script auf dem Server aus, nachdem Sie den Code hochgeladen haben
set -euo pipefail
# Farben für Output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Konfiguration
DAEMON_USER="yourpart"
PROJECT_NAME="yourpart-daemon"
REMOTE_DIR="/opt/yourpart"
SERVICE_NAME="yourpart-daemon"
BUILD_DIR="build"
# Funktionen
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Prüfe ob wir im richtigen Verzeichnis sind
if [ ! -f "CMakeLists.txt" ] || [ ! -f "daemon.conf" ]; then
log_error "Bitte führen Sie dieses Script aus dem Projektverzeichnis aus!"
log_info "Stellen Sie sicher, dass CMakeLists.txt und daemon.conf vorhanden sind."
exit 1
fi
log_info "Starte Server-Side Deployment für YourPart Daemon..."
# 1. Prüfe Dependencies
# Prüfe ob wir root-Rechte haben für bestimmte Operationen
check_sudo() {
if ! sudo -n true 2>/dev/null; then
log_info "Einige Operationen benötigen sudo-Rechte..."
fi
}
log_info "Prüfe Dependencies..."
if ! command -v cmake &> /dev/null; then
log_error "CMake nicht gefunden. Führen Sie zuerst install-dependencies-ubuntu22.sh aus!"
exit 1
fi
if ! command -v gcc-15 &> /dev/null && ! command -v gcc &> /dev/null; then
log_error "GCC nicht gefunden. Führen Sie zuerst install-dependencies-ubuntu22.sh aus!"
exit 1
fi
# 2. Baue Projekt
log_info "Baue Projekt auf dem Server..."
if [ ! -d "$BUILD_DIR" ]; then
mkdir "$BUILD_DIR"
fi
cd "$BUILD_DIR"
# Konfiguriere CMake
log_info "Konfiguriere CMake..."
if command -v gcc-15 &> /dev/null; then
log_info "Verwende GCC 15 für C++23"
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=23 -DCMAKE_C_COMPILER=gcc-15 -DCMAKE_CXX_COMPILER=g++-15
elif command -v gcc-13 &> /dev/null; then
log_info "Verwende GCC 13 für C++23"
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=23 -DCMAKE_C_COMPILER=gcc-13 -DCMAKE_CXX_COMPILER=g++-13
else
log_info "Verwende Standard-GCC 11 mit C++20"
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=20
fi
# Kompiliere
log_info "Kompiliere Projekt..."
make -j$(nproc)
cd ..
log_success "Build abgeschlossen"
# 3. Erstelle Benutzer falls nicht vorhanden
log_info "Prüfe Benutzer $DAEMON_USER..."
if ! id "$DAEMON_USER" &>/dev/null; then
log_info "Erstelle Benutzer $DAEMON_USER..."
sudo useradd --system --shell /bin/false --home-dir "$REMOTE_DIR" --create-home "$DAEMON_USER"
log_success "Benutzer $DAEMON_USER erstellt"
else
log_info "Benutzer $DAEMON_USER existiert bereits"
fi
# 4. Erstelle Verzeichnisse
log_info "Erstelle Verzeichnisse..."
mkdir -p "$REMOTE_DIR"/{logs,config}
sudo mkdir -p /etc/yourpart
sudo mkdir -p /var/log/yourpart
# 5. Stoppe Service falls läuft
log_info "Stoppe Service falls läuft..."
if sudo systemctl is-active --quiet "$SERVICE_NAME"; then
log_info "Stoppe laufenden Service..."
sudo systemctl stop "$SERVICE_NAME"
sleep 2
fi
# 6. Kopiere Dateien
log_info "Kopiere Dateien..."
sudo cp "$BUILD_DIR/yourpart-daemon" /usr/local/bin/
# Intelligente Konfigurationsdatei-Verwaltung
log_info "Verwalte Konfigurationsdatei..."
if [ ! -f "/etc/yourpart/daemon.conf" ]; then
log_info "Konfigurationsdatei existiert nicht, kopiere neue..."
sudo cp daemon.conf /etc/yourpart/
sudo chown yourpart:yourpart /etc/yourpart/daemon.conf
else
log_info "Konfigurationsdatei existiert bereits, prüfe auf fehlende Keys..."
# Erstelle temporäre Datei mit neuen Keys
temp_conf="/tmp/daemon.conf.new"
cp daemon.conf "$temp_conf"
# Füge fehlende Keys hinzu
while IFS='=' read -r key value; do
# Überspringe Kommentare und leere Zeilen
if [[ "$key" =~ ^[[:space:]]*# ]] || [[ -z "$key" ]]; then
continue
fi
# Entferne Leerzeichen am Anfang
key=$(echo "$key" | sed 's/^[[:space:]]*//')
# Prüfe ob Key bereits existiert
if ! grep -q "^[[:space:]]*$key[[:space:]]*=" /etc/yourpart/daemon.conf; then
log_info "Füge fehlenden Key hinzu: $key"
echo "$key=$value" | sudo tee -a /etc/yourpart/daemon.conf > /dev/null
fi
done < "$temp_conf"
rm -f "$temp_conf"
fi
sudo cp yourpart-daemon.service /etc/systemd/system/
# 7. Setze Berechtigungen
log_info "Setze Berechtigungen..."
sudo chmod +x /usr/local/bin/yourpart-daemon
sudo chown -R "$DAEMON_USER:$DAEMON_USER" "$REMOTE_DIR"
sudo chown -R "$DAEMON_USER:$DAEMON_USER" /var/log/yourpart
sudo chown yourpart:yourpart /etc/yourpart/daemon.conf
sudo chmod 600 /etc/yourpart/daemon.conf
# 8. Lade systemd neu
log_info "Lade systemd Konfiguration neu..."
sudo systemctl daemon-reload
# 9. Aktiviere Service
log_info "Aktiviere Service..."
sudo systemctl enable "$SERVICE_NAME"
# 10. Starte Service
log_info "Starte Service..."
sudo systemctl start "$SERVICE_NAME" &
sleep 3
# 11. Prüfe Status
log_info "Prüfe Service-Status..."
sleep 2
if sudo systemctl is-active --quiet "$SERVICE_NAME"; then
log_success "Service läuft erfolgreich!"
sudo systemctl status "$SERVICE_NAME" --no-pager
else
log_error "Service konnte nicht gestartet werden!"
log_info "Logs anzeigen mit: sudo journalctl -u $SERVICE_NAME -f"
exit 1
fi
# 11. Zeige nützliche Befehle
log_success "Deployment erfolgreich abgeschlossen!"
log_info ""
log_info "Nützliche Befehle:"
log_info "- Service-Status: sudo systemctl status $SERVICE_NAME"
log_info "- Service stoppen: sudo systemctl stop $SERVICE_NAME"
log_info "- Service starten: sudo systemctl start $SERVICE_NAME"
log_info "- Service neustarten: sudo systemctl restart $SERVICE_NAME"
log_info "- Logs anzeigen: sudo journalctl -u $SERVICE_NAME -f"
log_info "- Logs der letzten 100 Zeilen: sudo journalctl -u $SERVICE_NAME -n 100"
log_info ""
log_info "Konfigurationsdatei: /etc/yourpart/daemon.conf"
log_info "Log-Verzeichnis: /var/log/yourpart/"
log_info "Service-Datei: /etc/systemd/system/$SERVICE_NAME.service"