This commit is contained in:
2025-08-17 13:02:48 +02:00
parent a73b5d13d2
commit 3f7727ec57
3 changed files with 261 additions and 102 deletions

View File

@@ -18,10 +18,42 @@ SSH_TIMEOUT="5"
run_ssh() {
local machine="$1"
local cmd="$2"
timeout "$SSH_TIMEOUT" ssh $SSH_OPTS "$machine" "$cmd"
local start_time=$(date +%s)
shift
if [ $# -eq 0 ]; then
ssh "$machine" 'bash -s' < /dev/stdin
else
ssh "$machine" "$@"
fi
local end_time=$(date +%s)
local duration=$(( end_time - start_time ))
echo -e " ⏱️ Durée : ${duration}s"
}
extract_stats_block() {
local label="$1"
grep -A2 "^>>> $label" "$logfile" | grep -E '^[0-9]+ mis à jour' | tail -n1
}
parse_summary_line() {
awk '
{
updated += $1
installed += $4
removed += $7
last_notup = $11
}
END {
print updated, installed, removed, (last_notup ? last_notup : 0)
}'
}
run_scp() {
local src="$1"
local dest="$2"
@@ -30,46 +62,107 @@ run_scp() {
}
# Fonction pour mettre à jour avec apt
update_with_apt() {
local machine="$1"
local tmpfile="/tmp/update_${machine}_$(date +%s).log"
local uniqkey="$2"
local start_time=$(date +%s)
echo -e " - Mise à jour avec apt-get sur $machine \n"
# Exécution distante avec apt-get uniquement, log récupéré localement
run_ssh "$machine" '
set -e
export DEBIAN_FRONTEND=noninteractive
LOGFILE=$(mktemp)
stats_tmp=$(mktemp)
sudo apt-get -y -q clean
sudo apt-get -y -q update
sudo apt-get -y -q --with-new-pkgs full-upgrade >> "$LOGFILE" 2>&1
sudo apt-get -y -q autoremove >> "$LOGFILE" 2>&1
run_ssh "$machine" <<'EOF' | tee "$stats_tmp"
export DEBIAN_FRONTEND=noninteractive
cat "$LOGFILE"
rm -f "$LOGFILE"
' > "$tmpfile"
echo ">>> clean"
sudo apt-get -y clean
# Analyse locale du log pour extraire les lignes intéressantes
updates=$(grep "^Les paquets suivants seront mis à jour" "$tmpfile" | wc -w)
installs=$(grep "^Les NOUVEAUX paquets suivants seront installés" "$tmpfile" | wc -w)
removes=$(grep "^Les paquets suivants seront ENLEVÉS" "$tmpfile" | wc -w)
echo ">>> update"
sudo apt-get update
# Retirer le nombre de mots fixes den-tête pour chaque ligne
updates=$(( updates > 6 ? updates - 6 : 0 ))
installs=$(( installs > 8 ? installs - 8 : 0 ))
removes=$(( removes > 6 ? removes - 6 : 0 ))
echo ">>> check-upgrade"
UPGRADABLE=$(apt-get -s upgrade | grep "^Inst" | wc -l)
if [ "$updates" -gt 0 ] || [ "$installs" -gt 0 ] || [ "$removes" -gt 0 ]; then
echo -e "📦 ${GREEN}$machine${NC} : $updates mis à jour, $installs installés, $removes supprimés"
if [ "${UPGRADABLE:-0}" -gt 0 ] 2>/dev/null; then
echo ">>> upgrade"
sudo apt-get -y upgrade
echo ">>> full-upgrade"
sudo apt-get -y full-upgrade
echo ">>> autoremove"
sudo apt-get -y autoremove
else
echo ">>> Aucun paquet à mettre à jour"
fi
if [ -f /var/run/reboot-required ]; then
echo ">>> REDÉMARRAGE NÉCESSAIRE"
fi
EOF
local end_time=$(date +%s)
local duration=$(( end_time - start_time ))
log_capture=$(cat "$stats_tmp")
local ssh_status=$?
if [ "$ssh_status" -ne 0 ]; then
echo -e "❌ Échec SSH ou erreur distante sur $machine (code $ssh_status)"
else
echo -e "$machine : Terminé avec succès"
fi
rm -f "$tmpfile"
echo
# Récupération stats à partir des logs visibles en live
# On extrait les chiffres comme dans le message de apt
# Initialisation des compteurs
updated=0
installed=0
removed=0
unchanged=0
# Extraire les lignes après chaque bloc
line_upgrade=$(echo "$log_capture" | grep -A2 '^>>> upgrade' | grep -E '^[0-9]+ mis à jour' | tail -n1)
line_fullup=$(echo "$log_capture" | grep -A2 '^>>> full-upgrade' | grep -E '^[0-9]+ mis à jour' | tail -n1)
line_autorm=$(echo "$log_capture" | grep -A2 '^>>> autoremove' | grep -E '^[0-9]+ mis à jour' | tail -n1)
# Ajouter les lignes ensemble
line_all="$line_upgrade"$'\n'"$line_fullup"$'\n'"$line_autorm"
read updated installed removed unchanged <<< $(echo "$line_all" | parse_summary_line)
# Date/heure actuelle
now=$(date '+%Y-%m-%d %H:%M:%S')
# Fichier de stats
stats_file="$HOME/.config/updateall-stats"
mkdir -p "$(dirname "$stats_file")"
# Ajouter len-tête si le fichier est vide
if [ ! -s "$stats_file" ]; then
echo "machine,date,duree,updated,installed,removed,unchanged,uniqkey" > "$stats_file"
fi
# Ajout ligne CSV
echo "$machine,$now,$duration,$updated,$installed,$removed,$unchanged,$uniqkey" >> "$stats_file"
}
# Fonction pour mettre à jour avec dnf
update_with_dnf() {
echo -e " - Mise à jour avec dnf sur $1 \n"
@@ -151,7 +244,7 @@ update_machine () {
if timeout 5 ssh "$machine" which apt > /dev/null 2>&1; then
echo -e "${GREEN}apt${NC}"
update_with_apt "$machine"
update_with_apt "$machine" "$uniqkey"
elif timeout 5 ssh "$machine" which dnf > /dev/null 2>&1; then
echo -e "${GREEN}dnf${NC}"
update_with_dnf "$machine"
@@ -241,17 +334,21 @@ for machine in "${machines[@]}"; do
((current++))
echo -ne ">> $machine ($current/$total)\n"
if grep -q "^$machine " ~/.config/updateall-hosts; then
status=$(grep "^$machine " ~/.config/updateall-hosts | awk '{print $2}')
while read host status; do
# ignorer commentaires et lignes vides
[[ -z "$host" || "$host" =~ ^# ]] && continue
if [ "$machine" = "$host" ]; then
if [ "$status" = "1" ]; then
echo -e "${GREEN}connue${NC}"
check_host "$machine"
machine_online="$?"
if [ "$machine_online" -eq 1 ]; then
check_keyinstall "$machine" "$uniqkey"
keyinstall_present="$?"
if [ "$keyinstall_present" -eq 0 ]; then
update_machine "$machine"
create_installkey "$machine" "$uniqkey"
@@ -264,22 +361,27 @@ for machine in "${machines[@]}"; do
echo -e "${RED}ignorée${NC}"
ignored_machines+=("$machine")
fi
else
check_host "$machine"
machine_online="$?"
if [ "$machine_online" -eq 1 ]; then
echo -e "${RED}vue pour la 1re fois${NC}"
confirm_update "$machine"
update_machine "$machine"
run_custom_script "$machine"
create_installkey "$machine" "$uniqkey"
ok_machines+=("$machine")
else
echo -e "${RED}non accessible${NC}"
error_machines+=("$machine")
fi
matched=1
break
fi
done < ~/.config/updateall-hosts
if [ "$matched" != "1" ]; then
check_host "$machine"
machine_online="$?"
if [ "$machine_online" -eq 1 ]; then
echo -e "${RED}vue pour la 1re fois${NC}"
confirm_update "$machine"
update_machine "$machine"
run_custom_script "$machine"
create_installkey "$machine" "$uniqkey"
ok_machines+=("$machine")
else
echo -e "${RED}non accessible${NC}"
error_machines+=("$machine")
fi
fi
done