Files
Docker/run.sh
2026-03-11 10:20:12 +01:00

178 lines
4.5 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
START_TIME=$(date +%s)
BASE_URL="https://git.thomasmlg.fr/thomas/Docker/raw/branch/release"
WORKDIR="/opt/lxc-updater"
LOG_ROOT="/var/log/lxc-updater-TM"
DATE="$(date +%F)"
TS="$(date +%H:%M:%S)"
TS_FILE="$(date +%H%M%S)"
LOG_DIR="${LOG_ROOT}/${DATE}"
RUN_LOG="${LOG_DIR}/lxc-updater-run.log"
RUN_LOG_ARCHIVE="${LOG_DIR}/lxc-updater-run-${TS_FILE}.log"
mkdir -p "$WORKDIR" "$LOG_DIR"
: > "$RUN_LOG"
: > "$RUN_LOG_ARCHIVE"
log() {
echo "$1"
echo "$1" >>"$RUN_LOG"
echo "$1" >>"$RUN_LOG_ARCHIVE"
}
ensure_jq() {
if command -v jq >/dev/null 2>&1; then
return 0
fi
echo "📦 Installation de jq..."
if command -v apt-get >/dev/null 2>&1; then
apt-get update >>"$RUN_LOG" 2>&1
apt-get install -y jq >>"$RUN_LOG" 2>&1
return 0
fi
if command -v apk >/dev/null 2>&1; then
apk add --no-cache jq >>"$RUN_LOG" 2>&1
return 0
fi
echo "❌ Impossible d'installer jq automatiquement"
return 1
}
ensure_jq
send_matrix() {
local message="$1"
MATRIX_URL="https://element.thomasmlg.fr"
ROOM_ID="!qMftKWAtfFIQzithEu:malgouyres"
ACCESS_TOKEN="syt_YWRtaW4_HyZwdmZXmngmUNGdxgTV_0tb2UI"
local payload
payload="$(jq -Rn --arg msg "$message" '{msgtype:"m.text", body:$msg}')"
curl -sS -X POST \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d "$payload" \
"${MATRIX_URL}/_matrix/client/r0/rooms/${ROOM_ID}/send/m.room.message" \
>>"$RUN_LOG" 2>&1
}
on_error() {
local end_time duration
end_time=$(date +%s)
duration=$((end_time - START_TIME))
send_matrix "❌ LXC Updater (TM)
🖥️ Conteneur : $(hostname)
📅 Date : ${DATE}
🕒 Heure : $(date +%H:%M:%S)
⚠️ Statut : échec de la mise à jour
⏱ Durée : ${duration}s"
}
trap on_error ERR
header() {
local step="$1"
clear
echo "╔══════════════════════════════════════╗"
echo "║ LXC Updater (TM) ║"
echo "╚══════════════════════════════════════╝"
echo "🖥️ Conteneur : $(hostname)"
echo "📅 Date : ${DATE} 🕒 Heure : $(date +%H:%M:%S)"
if [[ -n "$step" ]]; then
echo "📌 Étape : ${step}"
fi
echo ""
}
# ------------------------------------------------
# Téléchargement des scripts
# ------------------------------------------------
header ""
echo "⬇️ Téléchargement des scripts..."
curl -fsSL "${BASE_URL}/detect.sh" -o "${WORKDIR}/detect.sh" >>"$RUN_LOG" 2>&1
curl -fsSL "${BASE_URL}/update.sh" -o "${WORKDIR}/update.sh" >>"$RUN_LOG" 2>&1
chmod +x "${WORKDIR}/detect.sh" "${WORKDIR}/update.sh"
# ------------------------------------------------
# Détection silencieuse
# ------------------------------------------------
MODE="$("${WORKDIR}/detect.sh")"
# ------------------------------------------------
# Notification début
# ------------------------------------------------
send_matrix "🔄 LXC Updater (TM)
🖥️ Conteneur : $(hostname)
📅 Date : ${DATE}
🕒 Heure : $(date +%H:%M:%S)
🚀 Statut : mise à jour en cours"
# ------------------------------------------------
# ÉTAPE 1 — Mise à jour du système
# ------------------------------------------------
header "Mise à jour du système"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📦 Mise à jour du système"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
"${WORKDIR}/update.sh" "${MODE}" system
sleep 2
# ------------------------------------------------
# ÉTAPE 2 — Mise à jour Docker
# ------------------------------------------------
header "Mise à jour des conteneurs Docker"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🐳 Mise à jour des conteneurs Docker"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
"${WORKDIR}/update.sh" "${MODE}" docker
# ------------------------------------------------
# Fin
# ------------------------------------------------
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
echo ""
echo "🧾 Log run : ${RUN_LOG}"
echo "🗃️ Archive run : ${RUN_LOG_ARCHIVE}"
echo "🎉 Terminé"
SUMMARY="🧰 LXC Updater (TM)
🖥️ Conteneur : $(hostname)
📅 Date : ${DATE}
🕒 Heure : $(date +%H:%M:%S)
📦 Mise à jour système : terminée
🐳 Mise à jour Docker : terminée
⏱ Durée : ${DURATION}s"
send_matrix "$SUMMARY"