#!/usr/bin/env bash set -euo pipefail usage() { cat <<'EOF' Usage: ./scripts/update-proxmox-lxc.sh \ --proxmox-host 192.168.1.10 \ --proxmox-user root@pam \ --proxmox-password 'motdepasse' \ --ctid 120 Options principales: --proxmox-host IP ou nom DNS du serveur Proxmox --proxmox-user Utilisateur SSH Proxmox (defaut: root@pam) --proxmox-password Mot de passe SSH Proxmox --ssh-port Port SSH Proxmox (defaut: 22) --ctid CTID du LXC a mettre a jour --hostname Nom du LXC si le CTID n'est pas fourni (defaut: chesscubing-web) --branch Branche Git a deployer (defaut: main) -h, --help Affiche cette aide EOF } die() { printf 'Erreur: %s\n' "$*" >&2 exit 1 } need_cmd() { command -v "$1" >/dev/null 2>&1 || die "La commande '$1' est requise." } PROXMOX_HOST="" PROXMOX_USER="root@pam" PROXMOX_PASSWORD="${PROXMOX_PASSWORD:-}" PROXMOX_PORT="22" CTID="" LXC_HOSTNAME="chesscubing-web" REPO_BRANCH="main" while [[ $# -gt 0 ]]; do case "$1" in --proxmox-host) PROXMOX_HOST="${2:-}" shift 2 ;; --proxmox-user) PROXMOX_USER="${2:-}" shift 2 ;; --proxmox-password) PROXMOX_PASSWORD="${2:-}" shift 2 ;; --ssh-port) PROXMOX_PORT="${2:-}" shift 2 ;; --ctid) CTID="${2:-}" shift 2 ;; --hostname) LXC_HOSTNAME="${2:-}" shift 2 ;; --branch) REPO_BRANCH="${2:-}" shift 2 ;; -h | --help) usage exit 0 ;; *) die "Option inconnue: $1" ;; esac done [[ -n "$PROXMOX_HOST" ]] || die "Merci de fournir --proxmox-host." [[ -n "$PROXMOX_USER" ]] || die "Merci de fournir --proxmox-user." if [[ -z "$PROXMOX_PASSWORD" ]]; then read -rsp "Mot de passe SSH pour ${PROXMOX_USER}@${PROXMOX_HOST}: " PROXMOX_PASSWORD echo fi need_cmd ssh need_cmd sshpass printf 'Mise a jour du LXC ChessCubing sur %s...\n' "$PROXMOX_HOST" sshpass -p "$PROXMOX_PASSWORD" \ ssh \ -p "$PROXMOX_PORT" \ -o StrictHostKeyChecking=accept-new \ -o PreferredAuthentications=password \ -o PubkeyAuthentication=no \ "$PROXMOX_USER@$PROXMOX_HOST" \ bash -s -- \ "$CTID" \ "$LXC_HOSTNAME" \ "$REPO_BRANCH" <<'REMOTE' set -euo pipefail ctid="$1" lxc_hostname="$2" repo_branch="$3" die() { printf 'Erreur: %s\n' "$*" >&2 exit 1 } find_ctid_by_hostname() { local wanted="$1" local candidate="" local candidate_hostname="" while read -r candidate; do [[ -n "$candidate" ]] || continue candidate_hostname="$(pct config "$candidate" 2>/dev/null | awk -F ': ' '/^hostname:/ { print $2; exit }')" if [[ "$candidate_hostname" == "$wanted" ]]; then printf '%s\n' "$candidate" return 0 fi done < <(pct list | awk 'NR > 1 { print $1 }') return 1 } command -v pct >/dev/null 2>&1 || die "La commande 'pct' est absente sur Proxmox." if [[ -z "$ctid" ]]; then ctid="$(find_ctid_by_hostname "$lxc_hostname" || true)" fi [[ -n "$ctid" ]] || die "Impossible de retrouver le LXC. Passe --ctid ou --hostname." if ! pct status "$ctid" >/dev/null 2>&1; then die "Le conteneur $ctid est introuvable." fi detected_hostname="$(pct config "$ctid" 2>/dev/null | awk -F ': ' '/^hostname:/ { print $2; exit }')" if [[ -n "$detected_hostname" ]]; then lxc_hostname="$detected_hostname" fi if ! pct status "$ctid" | grep -q "running"; then pct start "$ctid" sleep 5 fi pct exec "$ctid" -- test -x /usr/local/bin/update-chesscubing || die "Le script /usr/local/bin/update-chesscubing est absent dans le conteneur." pct exec "$ctid" -- /usr/local/bin/update-chesscubing "$repo_branch" container_ip="$(pct exec "$ctid" -- bash -lc "hostname -I | awk '{print \$1}'" 2>/dev/null | tr -d '\r' || true)" cat <} EOF REMOTE