mise à jour de la logique d'install et update

This commit is contained in:
2026-03-18 08:20:13 +01:00
parent 312ba59343
commit fba9bc89e2
2 changed files with 104 additions and 313 deletions

View File

@@ -17,45 +17,42 @@ MANIFEST_URL="${UPDATE_BASE_URL}/manifest.txt"
INSTALL_DEPS="${INSTALL_DEPS:-true}"
# --- Fonctions ---
# --- Fonctions d'affichage ---
info() { echo -e "\e[34m[INFO]\e[0m $1"; }
ok() { echo -e "\e[32m[OK]\e[0m $1"; }
warn() { echo -e "\e[33m[WARN]\e[0m $1"; }
err() { echo -e "\e[31m[ERR]\e[0m $1"; }
# --- Fonctions Techniques ---
require_root() {
if [ "${EUID}" -ne 0 ]; then
echo "ERREUR: Ce script doit être exécuté en root." >&2
err "Ce script doit être exécuté en root."
exit 1
fi
}
install_deps() {
if [ "${INSTALL_DEPS}" != "true" ]; then
return 0
fi
echo "--- Installation des dépendances ---"
if [ "${INSTALL_DEPS}" != "true" ]; then return 0; fi
info "Vérification des dépendances système..."
if command -v apt-get >/dev/null 2>&1; then
apt-get update
apt-get install -y curl coreutils findutils grep sed gawk util-linux ca-certificates
# Ajout des modules PHP nécessaires pour vos scripts (curl pour ntfy)
apt-get install -y php-cli php-curl php-common
else
echo "AVERTISSEMENT: Gestionnaire de paquets apt non détecté. Assurez-vous que php-cli et php-curl sont installés."
apt-get update -qq
apt-get install -y -qq curl coreutils findutils grep sed gawk util-linux ca-certificates php-cli php-curl php-common > /dev/null
ok "Dépendances installées."
fi
}
prepare_dirs() {
echo "--- Préparation des répertoires ---"
info "Préparation de l'arborescence dans ${BASE_DIR}..."
mkdir -p "${BASE_DIR}/bin" "${BASE_DIR}/lib" "${CONF_DIR}" "${LOG_DIR}" "${STATE_DIR}" "${LOCK_DIR}" "${TMP_DIR}"
chmod 755 "${BASE_DIR}" "${CONF_DIR}" "${LOG_DIR}" "${STATE_DIR}" "${LOCK_DIR}"
}
fetch_manifest() {
echo "--- Récupération du manifeste ---"
info "Téléchargement du manifeste distant..."
curl -fsS "${MANIFEST_URL}" -o "${TMP_DIR}/manifest.txt"
}
validate_manifest() {
# Validation du format : Hash Mode Chemin
# Exemple : a1b2... 755 bin/script.php
awk '
NF == 3 &&
$1 ~ /^[0-9a-fA-F]{64}$/ &&
@@ -65,35 +62,36 @@ validate_manifest() {
' "${TMP_DIR}/manifest.txt"
}
download_one() {
download_and_install() {
local expected_hash="$1"
local mode="$2"
local rel_path="$3"
local url="${UPDATE_BASE_URL}/${rel_path}"
local dst="${BASE_DIR}/${rel_path}"
# On ignore le téléchargement si c'est un fichier de conf qui existe déjà
if [[ "$rel_path" == conf/* ]] && [ -f "$dst" ]; then
echo "Skip: $rel_path (existe déjà)"
return 0
if [ -f "$dst" ]; then
local current_hash
current_hash=$(sha256sum "$dst" | awk '{print $1}')
if [ "$current_hash" == "$expected_hash" ]; then
return 0 # Déjà à jour
fi
info "Mise à jour : $rel_path"
else
info "Installation : $rel_path"
fi
echo "Téléchargement: $rel_path"
local tmp_file
tmp_file="$(mktemp "${TMP_DIR}/file.XXXXXX")"
if ! curl -fsS "$url" -o "$tmp_file"; then
echo "ERREUR: Échec du téléchargement de ${url}" >&2
if ! curl -fsS "${UPDATE_BASE_URL}/${rel_path}" -o "$tmp_file"; then
err "Échec du téléchargement pour $rel_path"
rm -f "$tmp_file"
return 1
fi
local got_hash
got_hash="$(sha256sum "$tmp_file" | awk '{print $1}')"
got_hash=$(sha256sum "$tmp_file" | awk '{print $1}')
if [ "$got_hash" != "$expected_hash" ]; then
echo "ERREUR: Hash invalide pour ${rel_path}" >&2
err "Hash invalide pour $rel_path"
rm -f "$tmp_file"
return 1
fi
@@ -103,34 +101,24 @@ download_one() {
chmod "$mode" "$dst"
}
install_from_manifest() {
echo "--- Installation des fichiers ---"
while read -r hash mode rel_path; do
[ -n "${hash:-}" ] || continue
download_one "$hash" "$mode" "$rel_path"
done < "${TMP_DIR}/manifest-valid.txt"
}
purge_obsolete_files() {
info "Analyse des fichiers obsolètes (Synchronisation avec Git)..."
# On scanne bin, lib et conf
find "${BASE_DIR}/bin" "${BASE_DIR}/lib" "${BASE_DIR}/conf" -type f | while read -r local_file; do
local rel_path="${local_file#$BASE_DIR/}"
show_next_steps() {
cat <<EOF
# PROTECTION : On ne touche jamais aux fichiers locaux personnels
if [[ "$rel_path" == *".local."* ]]; then
continue
fi
Installation terminée avec succès dans ${BASE_DIR}.
Étapes suivantes :
1. Configurez vos alertes :
cp ${CONF_DIR}/alert-engine.conf.php ${CONF_DIR}/alert-engine.local.conf.php
nano ${CONF_DIR}/alert-engine.local.conf.php
2. Initialisez la configuration globale :
cp ${CONF_DIR}/monitoring.conf.php ${CONF_DIR}/monitoring.local.conf.php
3. Lancez un audit des configurations :
php ${BASE_DIR}/bin/monitoring-update-config.php
4. Planifiez les tâches (cron) :
*/5 * * * * php ${BASE_DIR}/bin/alert-engine.php
10 3 * * * php ${BASE_DIR}/bin/monitoring-update.php
EOF
# Si le fichier n'est pas dans le manifeste validé, on le supprime
if ! grep -qW "$rel_path" "${TMP_DIR}/manifest-valid.txt"; then
warn "Suppression : $rel_path (absent du dépôt)"
rm -f "$local_file"
fi
done
}
# --- Main ---
@@ -142,16 +130,29 @@ main() {
fetch_manifest
if ! validate_manifest > "${TMP_DIR}/manifest-valid.txt"; then
echo "ERREUR: Le manifeste est invalide ou corrompu." >&2
err "Le manifeste est invalide ou corrompu."
exit 1
fi
install_from_manifest
# Nettoyage
echo "--------------------------------------------------"
info "Phase 1 : Installation et mises à jour"
while read -r hash mode rel_path; do
[ -n "${hash:-}" ] || continue
download_and_install "$hash" "$mode" "$rel_path"
done < "${TMP_DIR}/manifest-valid.txt"
echo "--------------------------------------------------"
info "Phase 2 : Nettoyage"
purge_obsolete_files
rm -rf "${TMP_DIR}"
echo "--------------------------------------------------"
ok "Opération terminée avec succès."
show_next_steps
# Rappel final
if [ ! -f "${CONF_DIR}/monitoring.local.conf.php" ]; then
warn "Pensez à créer votre fichier ${CONF_DIR}/monitoring.local.conf.php"
fi
}
main "$@"