#!/bin/bash # Proxmox LXC Container Setup: 3D-Druck Kostenkalkulation # Auf dem Proxmox-Host ausfuehren (als root). # # Usage: # ./proxmox-setup.sh [CT_ID] [CT_NAME] # Default: CT_ID=201, CT_NAME=3ddruck-kalkulation set -euo pipefail CT_ID="${1:-201}" CT_NAME="${2:-3ddruck-kalkulation}" TEMPLATE="ubuntu-24.04-standard_24.04-2_amd64.tar.zst" STORAGE="local-lvm" BRIDGE="vmbr0" GIT_REPO="https://gitea.teamthiele.de/ethiele/3ddruckkalkulation.git" ROOT_PASS="$(openssl rand -base64 24)" echo "=== Root-Passwort fuer Container: $ROOT_PASS ===" # Pruefen ob Template vorhanden, sonst downloaden if ! pveam list local | grep -q "$TEMPLATE"; then echo "=== Lade Ubuntu-24.04-Template ... ===" pveam download local "$TEMPLATE" fi # Pruefen ob Container bereits existiert if pct list | grep -q "^ *$CT_ID "; then echo "=== Container $CT_ID existiert bereits. Loesche ... ===" pct stop "$CT_ID" 2>/dev/null || true pct destroy "$CT_ID" 2>/dev/null || true fi # Container erstellen echo "=== Erstelle Container $CT_ID ($CT_NAME) ... ===" pct create "$CT_ID" "local:vztemplate/$TEMPLATE" \ --storage "$STORAGE" \ --net0 "name=eth0,bridge=$BRIDGE,ip=dhcp" \ --cores 1 \ --memory 512 \ --swap 512 \ --unprivileged 1 \ --ostype ubuntu \ --hostname "$CT_NAME" \ --password "$ROOT_PASS" # Container starten echo "=== Starte Container ... ===" pct start "$CT_ID" sleep 5 # Warten bis Netzwerk bereit echo "=== Warte auf Netzwerk ... ===" for i in $(seq 1 30); do if pct exec "$CT_ID" -- ping -c1 -W1 8.8.8.8 &>/dev/null; then echo "Netzwerk OK" break fi sleep 2 done # System-Update und Basispakete echo "=== Installiere Systempakete ... ===" pct exec "$CT_ID" -- apt-get update pct exec "$CT_ID" -- apt-get install -y git python3 python3-pip python3-venv # App-Verzeichnis und Repo echo "=== Klone Repository ... ===" pct exec "$CT_ID" -- git clone "$GIT_REPO" /opt/3ddruck-kalkulation # Python-Venv + Dependencies echo "=== Installiere Python-Dependencies ... ===" pct exec "$CT_ID" -- python3 -m venv /opt/3ddruck-kalkulation/.venv pct exec "$CT_ID" -- /opt/3ddruck-kalkulation/.venv/bin/pip install flask fpdf2 pyyaml # systemd-Service anlegen echo "=== Erstelle systemd-Service ... ===" pct exec "$CT_ID" -- tee /etc/systemd/system/3ddruck-kalkulation.service > /dev/null <<'SERVICE' [Unit] Description=3D-Druck Kostenkalkulation After=network-online.target Wants=network-online.target [Service] Type=simple User=root WorkingDirectory=/opt/3ddruck-kalkulation ExecStart=/opt/3ddruck-kalkulation/.venv/bin/python app.py --no-reload Restart=on-failure RestartSec=5 Environment=PYTHONUNBUFFERED=1 [Install] WantedBy=multi-user.target SERVICE # Service aktivieren und starten echo "=== Aktiviere und starte Service ... ===" pct exec "$CT_ID" -- systemctl daemon-reload pct exec "$CT_ID" -- systemctl enable --now 3ddruck-kalkulation.service # Warten und Status pruefen sleep 3 pct exec "$CT_ID" -- systemctl status 3ddruck-kalkulation.service --no-pager || true # IP anzeigen CT_IP=$(pct exec "$CT_ID" -- hostname -I | awk '{print $1}') echo "" echo "=== Fertig! ===" echo "Container $CT_ID ($CT_NAME) laeuft." echo "Zugriff: http://$CT_IP:5000" echo "Root-Passwort: $ROOT_PASS" echo "" echo "Zum Einloggen: pct enter $CT_ID"