Skip to content

Commit 32a046e

Browse files
committed
Update | Script V2.0-beta3
Point a améliorer : - Bug dans la console.
1 parent 0d7d0de commit 32a046e

File tree

1 file changed

+130
-72
lines changed

1 file changed

+130
-72
lines changed

.assets/new.sh

Lines changed: 130 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
#!/usr/bin/env bash
2-
# new.sh — bootstrap multi-distros (console propre + logs détaillés)
2+
# ==============================================================================
3+
# new.sh — bootstrap multi-distros (console propre + logs détaillés + récap par catégories)
4+
#
5+
# USAGE:
6+
# sudo ./new.sh [OPTIONS]
7+
#
8+
# OPTIONS:
9+
# --debug Active le mode debug (bash -x) et les messages [DEBUG]
10+
# --dry-run N'exécute pas les commandes (simule) — tout est loggué, console propre
11+
# --quiet|-q Réduit la verbosité console (n'affiche que WARN/ERROR, le log reste complet)
12+
#
13+
# COMPORTEMENT:
14+
# - Détecte la distribution / gestionnaire de paquets
15+
# - MAJ index + upgrade système
16+
# - Installe des paquets communs (curl, wget, htop, …) + groupes spécifiques (gnupg, lm-sensors, dnsutils…)
17+
# - Installe fastfetch depuis les dépôts si dispo, sinon via script externe
18+
# - Remplace le ~/.bashrc (sans rechargement)
19+
# - Règle la timezone sur Europe/Paris
20+
# - Active avahi-daemon si présent
21+
# - Console : statut d’étapes uniquement ; Logs : détails complets et lisibles (/var/log)
22+
# ==============================================================================
23+
324
set -euo pipefail
425

526
# ============================== CLI Flags ======================================
@@ -22,7 +43,7 @@ LOG_FILE="${LOG_DIR}/new-basics-${LOG_TS}.log"
2243
mkdir -p "$LOG_DIR"
2344
touch "$LOG_FILE"
2445

25-
# Couleurs console (log fichier = sans couleurs)
46+
# Couleurs console
2647
BOLD="\e[1m"; DIM="\e[2m"; RED="\e[31m"; YEL="\e[33m"; GRN="\e[32m"; BLU="\e[34m"; C0="\e[0m"
2748
_now() { date "+%Y-%m-%d %H:%M:%S%z"; }
2849
_since() { local s=$1; printf "%ds" $(( $(date +%s) - s )); }
@@ -57,17 +78,15 @@ run() {
5778
local desc="$1"; shift
5879
local ts=$(date +%s)
5980
local cmd_str="$*"
60-
local tmp_out
61-
tmp_out="$(mktemp)"
81+
local tmp_out; tmp_out="$(mktemp)"
6282
if [[ $DRYRUN -eq 1 ]]; then
6383
printf "%b[%s] [EXEC ]%b %s\n" "$BOLD" "$(_now)" "$C0" "$desc"
6484
log "OK: (dry-run) $desc"
6585
REPORT_SKIP+=("$desc")
6686
_step_log_block "$desc" "SKIPPED (dry-run)" "0" "$cmd_str" /dev/null
67-
return 0
87+
return 100 # code spécial "skip"
6888
fi
6989
printf "%b[%s] [EXEC ]%b %s\n" "$BOLD" "$(_now)" "$C0" "$desc"
70-
# Exécute la commande, sortie capturée UNIQUEMENT dans le log
7190
if "$@" >"$tmp_out" 2>&1; then
7291
local d=$(_since "$ts"); log "OK: $desc (durée $d)"
7392
REPORT_OK+=("$desc")
@@ -77,13 +96,30 @@ run() {
7796
else
7897
local rc=$?; local d=$(_since "$ts")
7998
err "ECHEC: $desc (durée $d, rc=$rc)"
80-
REPORT_FAIL+=("$desc (rc=$rc)")
99+
REPORT_FAIL+=("$desc (rc=$rc)")
81100
_step_log_block "$desc" "FAIL (rc=$rc)" "${d%s}" "$cmd_str" "$tmp_out"
82101
rm -f "$tmp_out"
83102
return $rc
84103
fi
85104
}
86105

106+
# ============================== Catégories =====================================
107+
# Status: 0=UNKNOWN, 1=OK, 2=SKIP, 3=FAIL (plus grand = pire)
108+
declare -A CAT
109+
_set_cat() {
110+
local key="$1" ; local val="$2"
111+
local cur="${CAT[$key]:-0}"
112+
(( val > cur )) && CAT["$key"]="$val" || true
113+
}
114+
_status_str() {
115+
case "$1" in
116+
1) printf "%b✓ OK%b" "$GRN" "$C0" ;;
117+
2) printf "%b⏭ SKIP%b" "$YEL" "$C0" ;;
118+
3) printf "%b✗ FAIL%b" "$RED" "$C0" ;;
119+
*) printf "-" ;;
120+
esac
121+
}
122+
87123
# ============================== Root & contexte ================================
88124
if [[ $EUID -ne 0 ]]; then
89125
warn "Ce script doit être exécuté en root. Tentative avec sudo…"
@@ -165,6 +201,7 @@ case "$DIST_ID" in
165201
;;
166202
esac
167203
log "Gestionnaire de paquets: $PKG_MGR"
204+
_set_cat "detect_pm" 1 # OK
168205

169206
# ============================== Helpers paquets ================================
170207
pkg_available() {
@@ -193,23 +230,24 @@ install_if_exists() {
193230
if ((${#ok[@]})); then
194231
if [[ $DRYRUN -eq 0 ]]; then
195232
# shellcheck disable=SC2086
196-
run "Installer ($group)" $PKG_INSTALL ${ok[*]} || warn "Erreur installation ($group)"
233+
if run "Installer ($group)" $PKG_INSTALL ${ok[*]}; then
234+
return 0
235+
else
236+
return 1
237+
fi
197238
else
198239
run "Installer ($group) — dry-run" echo "$PKG_INSTALL ${ok[*]}" >/dev/null
240+
return 100
199241
fi
200242
else
201243
warn "Aucun paquet installable pour le lot: $group"
202-
fi
203-
if ((${#skip[@]})); then
204-
warn "Indisponibles ($group): ${skip[*]}"
244+
return 100
205245
fi
206246
}
207247

208248
# ============================== MAJ système ===================================
209-
ts_update=$(date +%s)
210-
run "MAJ index paquets" bash -lc "$PKG_UPDATE" || warn "Update partielle/échouée"
211-
run "Upgrade système" bash -lc "$PKG_UPGRADE" || warn "Upgrade partielle/échouée"
212-
log "Section MAJ terminée (durée $(_since "$ts_update"))"
249+
rc=$(run "MAJ index paquets" bash -lc "$PKG_UPDATE"); (( rc==0 )) && _set_cat "update_index" 1 || _set_cat "update_index" 3
250+
rc=$(run "Upgrade système" bash -lc "$PKG_UPGRADE"); (( rc==0 )) && _set_cat "upgrade_system" 1 || _set_cat "upgrade_system" 3
213251

214252
# ============================== Mapping paquets ===============================
215253
PKG_GNUPG=(gnupg gnupg2)
@@ -229,72 +267,78 @@ PKG_BPY=(bpytop)
229267
PKG_BTOP=(btop bashtop)
230268

231269
log "Sélection des paquets selon famille: $DIST_ID ($PKG_MGR)"
270+
# Paquets communs (même nom multi-distros)
232271
case "$PKG_MGR" in
233-
apt)
234-
install_if_exists "gnupg" "${PKG_GNUPG[@]}"
235-
install_if_exists "lm-sensors" "${PKG_LMSENS_DEB_RPM[@]}"
236-
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_DEB[@]}" "${PKG_DNS_DEB[@]}" "${PKG_AVAHI_DEB[@]}"
237-
if pkg_available "${PKG_BPY[0]}"; then install_if_exists "monitoring" "${PKG_BPY[@]}"; else install_if_exists "monitoring" "${PKG_BTOP[@]}"; fi
238-
;;
239-
dnf|yum)
240-
install_if_exists "gnupg" gnupg
241-
install_if_exists "lm-sensors" "${PKG_LMSENS_DEB_RPM[@]}"
242-
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_RPM[@]}" "${PKG_DNS_RPM[@]}" "${PKG_AVAHI_OTH[@]}"
243-
if pkg_available "${PKG_BPY[0]}"; then install_if_exists "monitoring" "${PKG_BPY[@]}"; else install_if_exists "monitoring" "${PKG_BTOP[@]}"; fi
244-
;;
245-
pacman)
246-
install_if_exists "gnupg" gnupg
247-
install_if_exists "lm-sensors" "${PKG_LMSENS_ARCH[@]}"
248-
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_ARCH[@]}" "${PKG_DNS_ARCH[@]}" "${PKG_AVAHI_OTH[@]}"
249-
if pkg_available "${PKG_BPY[0]}"; then install_if_exists "monitoring" "${PKG_BPY[@]}"; else install_if_exists "monitoring" "${PKG_BTOP[@]}"; fi
250-
;;
251-
zypper)
252-
install_if_exists "gnupg" gpg2 gnupg
253-
install_if_exists "lm-sensors" "${PKG_LMSENS_DEB_RPM[@]}"
254-
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_RPM[@]}" "${PKG_DNS_RPM[@]}" "${PKG_AVAHI_OTH[@]}"
255-
if pkg_available "${PKG_BPY[0]}"; then install_if_exists "monitoring" "${PKG_BPY[@]}"; else install_if_exists "monitoring" "${PKG_BTOP[@]}"; fi
256-
;;
257-
apk)
258-
install_if_exists "gnupg" gnupg
259-
install_if_exists "lm-sensors" lm-sensors
260-
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_RPM[@]}" "${PKG_DNS_ALP[@]}" "${PKG_AVAHI_OTH[@]}"
261-
if pkg_available "${PKG_BPY[0]}"; then install_if_exists "monitoring" "${PKG_BPY[@]}"; else install_if_exists "monitoring" "${PKG_BTOP[@]}"; fi
262-
;;
272+
apt) rc=$(install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_DEB[@]}" "${PKG_DNS_DEB[@]}" "${PKG_AVAHI_DEB[@]}") ;;
273+
dnf|yum)rc=$(install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_RPM[@]}" "${PKG_DNS_RPM[@]}" "${PKG_AVAHI_OTH[@]}") ;;
274+
pacman) rc=$(install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_ARCH[@]}" "${PKG_DNS_ARCH[@]}" "${PKG_AVAHI_OTH[@]}") ;;
275+
zypper) rc=$(install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_RPM[@]}" "${PKG_DNS_RPM[@]}" "${PKG_AVAHI_OTH[@]}") ;;
276+
apk) rc=$(install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_RPM[@]}" "${PKG_DNS_ALP[@]}" "${PKG_AVAHI_OTH[@]}") ;;
277+
esac
278+
case "$rc" in
279+
0) _set_cat "install_common" 1 ;;
280+
100) _set_cat "install_common" 2 ;;
281+
*) _set_cat "install_common" 3 ;;
263282
esac
264283

284+
# Groupes spécifiques
285+
install_if_exists "gnupg" "${PKG_GNUPG[@]}" >/dev/null 2>&1 || true
286+
if [[ "$PKG_MGR" == "pacman" ]]; then
287+
install_if_exists "lm-sensors" "${PKG_LMSENS_ARCH[@]}" >/dev/null 2>&1 || true
288+
else
289+
install_if_exists "lm-sensors" "${PKG_LMSENS_DEB_RPM[@]}" >/dev/null 2>&1 || true
290+
fi
291+
if pkg_available "${PKG_BPY[0]}"; then
292+
install_if_exists "monitoring" "${PKG_BPY[@]}" >/dev/null 2>&1 || true
293+
else
294+
install_if_exists "monitoring" "${PKG_BTOP[@]}" >/dev/null 2>&1 || true
295+
fi
296+
265297
# ============================== Fastfetch (repo > fallback) ====================
266298
FASTFETCH_URL="https://raw.githubusercontent.com/OverStyleFR/AutoScriptBash/main/.assets/fastfetch-install.sh"
267299
install_fastfetch() {
268-
local ts=$(date +%s)
269300
if pkg_available fastfetch; then
270301
run "Installer (fastfetch via dépôts)" $PKG_INSTALL fastfetch
271-
return 0
302+
return $?
272303
fi
273304
warn "fastfetch non dispo dans les dépôts — fallback script externe"
274305
if [[ $DRYRUN -eq 1 ]]; then
275306
run "Installer (fastfetch via script) — dry-run" echo "bash <(curl -fsSL $FASTFETCH_URL)" >/dev/null
276-
return 0
307+
return 100
277308
fi
278309
if command -v curl >/dev/null 2>&1; then
279310
run "Installer (fastfetch via script)" bash -lc "bash <(curl -fsSL \"$FASTFETCH_URL\")"
311+
return $?
280312
elif command -v wget >/dev/null 2>&1; then
281313
run "Installer (fastfetch via script)" bash -lc "bash <(wget -qO- \"$FASTFETCH_URL\")"
314+
return $?
282315
else
283316
err "Ni curl ni wget disponibles — fastfetch non installé."
284317
return 1
285318
fi
286319
}
287-
install_fastfetch || warn "Installation fastfetch échouée (script)."
320+
rc=$(install_fastfetch)
321+
case "$rc" in
322+
0) _set_cat "fastfetch" 1 ;;
323+
100) _set_cat "fastfetch" 2 ;;
324+
*) _set_cat "fastfetch" 3 ;;
325+
esac
288326

289327
# ============================== Timezone ======================================
290328
if command -v timedatectl >/dev/null 2>&1; then
291-
run "Réglage timezone Europe/Paris" timedatectl set-timezone Europe/Paris || warn "Echec réglage timezone"
329+
rc=$(run "Réglage timezone Europe/Paris" timedatectl set-timezone Europe/Paris);
292330
else
293331
warn "timedatectl indisponible — tentative via /etc/localtime"
294332
ZF="/usr/share/zoneinfo/Europe/Paris"
295-
[[ -f "$ZF" ]] && run "Lien /etc/localtime → Europe/Paris" ln -sf "$ZF" /etc/localtime || warn "Zoneinfo non trouvée"
296-
[[ -w /etc/timezone ]] && echo "Europe/Paris" >/etc/timezone || true
333+
if [[ -f "$ZF" ]]; then
334+
rc=$(run "Lien /etc/localtime → Europe/Paris" ln -sf "$ZF" /etc/localtime || true; echo)
335+
[[ -w /etc/timezone ]] && echo "Europe/Paris" >/etc/timezone || true
336+
else
337+
rc=1
338+
err "Zoneinfo non trouvée"
339+
fi
297340
fi
341+
(( rc==0 || rc==100 )) && _set_cat "timezone" $(( rc==100 ? 2 : 1 )) || _set_cat "timezone" 3
298342

299343
# ============================== .bashrc (sans reload) ==========================
300344
BRC_URL="https://raw.githubusercontent.com/OverStyleFR/AutoScriptBash/main/.assets/.bashrc"
@@ -303,50 +347,64 @@ TARGET_HOME="$(eval echo "~$TARGET_USER")"
303347
TARGET_RC="$TARGET_HOME/.bashrc"
304348
BK="$TARGET_HOME/.bashrc.bak.$(date +%Y%m%d-%H%M%S)"
305349

350+
BRC_RC=0
306351
log "Remplacement du .bashrc pour $TARGET_USER ($TARGET_HOME)"
307-
[[ -f "$TARGET_RC" ]] && run "Backup $TARGET_RC" cp -a "$TARGET_RC" "$BK" || true
352+
[[ -f "$TARGET_RC" ]] && { run "Backup $TARGET_RC" cp -a "$TARGET_RC" "$BK" || BRC_RC=1; } || true
308353
if command -v curl >/dev/null 2>&1; then
309-
[[ $DRYRUN -eq 1 ]] && run "Télécharger .bashrc — dry-run" echo "curl -fsSL $BRC_URL -o $TARGET_RC" >/dev/null \
310-
|| run "Télécharger .bashrc" curl -fsSL "$BRC_URL" -o "$TARGET_RC"
354+
if ! run "Télécharger .bashrc" curl -fsSL "$BRC_URL" -o "$TARGET_RC"; then BRC_RC=1; fi
311355
elif command -v wget >/dev/null 2>&1; then
312-
[[ $DRYRUN -eq 1 ]] && run "Télécharger .bashrc — dry-run" echo "wget -q $BRC_URL -O $TARGET_RC" >/dev/null \
313-
|| run "Télécharger .bashrc" wget -q "$BRC_URL" -O "$TARGET_RC"
356+
if ! run "Télécharger .bashrc" wget -q "$BRC_URL" -O "$TARGET_RC"; then BRC_RC=1; fi
314357
else
315-
warn "Ni curl ni wget pour récupérer le .bashrc"
358+
warn "Ni curl ni wget pour récupérer le .bashrc"; BRC_RC=1
316359
fi
317-
[[ $DRYRUN -eq 0 && -f "$TARGET_RC" ]] && run "Chown .bashrc" chown "$TARGET_USER":"$TARGET_USER" "$TARGET_RC" || true
360+
[[ -f "$TARGET_RC" ]] && { run "Chown .bashrc" chown "$TARGET_USER":"$TARGET_USER" "$TARGET_RC" || BRC_RC=1; } || true
318361
if [[ -d /etc/skel && -f "$TARGET_RC" ]]; then
319-
run "Copie .bashrc vers /etc/skel" cp -f "$TARGET_RC" /etc/skel/.bashrc || true
362+
run "Copie .bashrc vers /etc/skel" cp -f "$TARGET_RC" /etc/skel/.bashrc || BRC_RC=1
320363
fi
364+
(( BRC_RC==0 )) && _set_cat "bashrc" 1 || _set_cat "bashrc" 3
321365
# NOTE: rechargement du .bashrc volontairement désactivé
322366

323367
# ============================== Avahi (enable si présent) =====================
324368
if command -v systemctl >/dev/null 2>&1; then
325369
if systemctl list-unit-files | grep -q '^avahi-daemon\.service'; then
326-
run "Activer avahi-daemon" systemctl enable --now avahi-daemon || warn "Impossible d'activer avahi-daemon"
370+
rc=$(run "Activer avahi-daemon" systemctl enable --now avahi-daemon)
371+
(( rc==0 || rc==100 )) && _set_cat "avahi" $(( rc==100 ? 2 : 1 )) || _set_cat "avahi" 3
327372
else
328373
debug "Service avahi-daemon non présent — ignoré."
374+
_set_cat "avahi" 2
329375
fi
330376
else
331377
debug "systemctl indisponible — probablement sans systemd."
378+
_set_cat "avahi" 2
332379
fi
333380

334-
# ============================== Récapitulatif =================================
381+
# ============================== Récapitulatif (catégories) =====================
335382
echo
336383
echo -e "${BOLD}============================= RÉCAPITULATIF =============================${C0}"
337-
echo " - Journal : $LOG_FILE"
338-
echo " - Distro : ID=$DIST_ID | LIKE=$DIST_LIKE | PM=$PKG_MGR"
339-
echo " - Durée : $(_since "$_start_ts")"
384+
echo " Journal : $LOG_FILE"
385+
echo " Distro : ID=$DIST_ID | LIKE=$DIST_LIKE | PM=$PKG_MGR"
386+
echo " Durée : $(_since "$_start_ts")"
387+
echo
388+
echo -e "${BOLD}Catégories:${C0}"
389+
printf " %-28s : %s\n" "Détection gestionnaire" "$(_status_str "${CAT[detect_pm]:-0}")"
390+
printf " %-28s : %s\n" "MAJ index paquets" "$(_status_str "${CAT[update_index]:-0}")"
391+
printf " %-28s : %s\n" "Upgrade système" "$(_status_str "${CAT[upgrade_system]:-0}")"
392+
printf " %-28s : %s\n" "Paquets communs" "$(_status_str "${CAT[install_common]:-0}")"
393+
printf " %-28s : %s\n" "Fastfetch" "$(_status_str "${CAT[fastfetch]:-0}")"
394+
printf " %-28s : %s\n" "Timezone (Europe/Paris)" "$(_status_str "${CAT[timezone]:-0}")"
395+
printf " %-28s : %s\n" ".bashrc (déploiement)" "$(_status_str "${CAT[bashrc]:-0}")"
396+
printf " %-28s : %s\n" "Avahi (enable si présent)" "$(_status_str "${CAT[avahi]:-0}")"
340397
echo
341-
echo "Étapes OK : ${#REPORT_OK[@]}"
342-
for s in "${REPORT_OK[@]}"; do echo "$s"; done
398+
echo -e "${BOLD}Détail étapes:${C0}"
399+
echo " Étapes OK : ${#REPORT_OK[@]}"
400+
for s in "${REPORT_OK[@]}"; do echo " ${GRN}${C0} $s"; done
343401
echo
344-
echo "Étapes FAIL : ${#REPORT_FAIL[@]}"
345-
for s in "${REPORT_FAIL[@]}"; do echo " $s"; done
346-
if [[ $DRYRUN -eq 1 ]]; then
402+
echo " Étapes FAIL : ${#REPORT_FAIL[@]}"
403+
for s in "${REPORT_FAIL[@]}"; do echo " ${RED}${C0} $s"; done
404+
if [[ $DRYRUN -eq 1 || ${#REPORT_SKIP[@]} -gt 0 ]]; then
347405
echo
348-
echo "Étapes SKIP : ${#REPORT_SKIP[@]} (dry-run)"
349-
for s in "${REPORT_SKIP[@]}"; do echo " $s"; done
406+
echo " Étapes SKIP : ${#REPORT_SKIP[@]}"
407+
for s in "${REPORT_SKIP[@]}"; do echo " ${YEL}${C0} $s"; done
350408
fi
351409
echo -e "${BOLD}=========================================================================${C0}"
352410

0 commit comments

Comments
 (0)