This commit is contained in:
sharky committed 2026-10-04 16:18:59 +02:00
1 parent 2f5714cbae
commit 697270a803
1 file changed
+159
+159
View File
@@ -19,6 +19,12 @@ EXCLUDE_GUESTS="${EXCLUDE_GUESTS:-}"
APT_LOCK_TIMEOUT="${APT_LOCK_TIMEOUT:-600}" APT_LOCK_TIMEOUT="${APT_LOCK_TIMEOUT:-600}"
LOG_FILE="${LOG_FILE:-/var/log/pve-guest-update.log}" LOG_FILE="${LOG_FILE:-/var/log/pve-guest-update.log}"
# Proxmox Notification System
NOTIFY_TARGET="${NOTIFY_TARGET:-iCloud-SMTP}"
NOTIFY_MATCHER="${NOTIFY_MATCHER:-proxmox-updater}"
NOTIFY_TEMPLATE="${NOTIFY_TEMPLATE:-proxmox-updater}"
NOTIFY_ENABLED="${NOTIFY_ENABLED:-yes}"
MODE="update" MODE="update"
ONLY_CT="" ONLY_CT=""
ONLY_VM="" ONLY_VM=""
@@ -27,6 +33,8 @@ NO_REBOOT="no"
ERRORS=0 ERRORS=0
HOST_OK="not-run" HOST_OK="not-run"
HOST_REBOOT_REQUIRED="no" HOST_REBOOT_REQUIRED="no"
HOST_REBOOT_SCHEDULED="no"
MAIL_STATUS_DETAIL="Keine automatischen Neustarts erforderlich."
# Laufende Gäste, die nach erfolgreichem Update einen Neustart benötigen. # Laufende Gäste, die nach erfolgreichem Update einen Neustart benötigen.
# Die Neustarts werden erst am Ende des gesamten Laufs entschieden. # Die Neustarts werden erst am Ende des gesamten Laufs entschieden.
@@ -61,6 +69,133 @@ log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
} }
ensure_notification_setup() {
[[ "$NOTIFY_ENABLED" == "yes" ]] || return 0
# Das konfigurierte Proxmox-Target muss existieren.
if ! pvesh get /cluster/notifications/targets --output-format json 2>/dev/null |
grep -Fq "\"name\":\"$NOTIFY_TARGET\""
then
log "WARNUNG: Notification-Target '$NOTIFY_TARGET' nicht gefunden; keine Mail."
return 1
fi
# Eigene Templates für Betreff und Nachricht.
local template_dir="/etc/pve/notification-templates/default"
mkdir -p "$template_dir"
cat > "$template_dir/${NOTIFY_TEMPLATE}-subject.txt.hbs" <<'EOF'
{{ title }}
EOF
cat > "$template_dir/${NOTIFY_TEMPLATE}-body.txt.hbs" <<'EOF'
{{ message }}
EOF
cat > "$template_dir/${NOTIFY_TEMPLATE}-body.html.hbs" <<'EOF'
<pre>{{ message }}</pre>
EOF
# Eigener Matcher: nur Meldungen dieses Updaters an das vorhandene
# Proxmox-Notification-Target senden.
if ! pvesh get "/cluster/notifications/matchers/$NOTIFY_MATCHER" \
>/dev/null 2>&1
then
if ! pvesh create /cluster/notifications/matchers \
--name "$NOTIFY_MATCHER" \
--mode all \
--match-field 'exact:type=proxmox-updater' \
--target "$NOTIFY_TARGET" \
--comment 'Proxmox Guest Updater' \
>/dev/null
then
log "WARNUNG: Notification-Matcher '$NOTIFY_MATCHER' konnte nicht angelegt werden."
return 1
fi
fi
return 0
}
send_update_notification() {
[[ "$MODE" == "update" ]] || return 0
[[ "$NOTIFY_ENABLED" == "yes" ]] || return 0
if ! ensure_notification_setup; then
return 0
fi
local severity="info"
local status="Erfolgreich"
local title
local message
if (( ERRORS > 0 )); then
severity="error"
status="FEHLER"
fi
title="Proxmox Updater - $status"
message="Proxmox Guest Updater
Host: $(hostname)
Zeit: $(date '+%d.%m.%Y %H:%M:%S')
Host:
Update: $HOST_OK
Reboot erforderlich: $HOST_REBOOT_REQUIRED
Reboot geplant: $HOST_REBOOT_SCHEDULED
LXC:
Erfolgreich: $LXC_OK
Fehlgeschlagen: $LXC_FAILED
Übersprungen: $LXC_SKIPPED
VM:
Erfolgreich: $VM_OK
Fehlgeschlagen: $VM_FAILED
Übersprungen: $VM_SKIPPED
Fehler gesamt: $ERRORS
Neustarts:
$MAIL_STATUS_DETAIL
Log:
$LOG_FILE"
export PVE_UPDATER_SEVERITY="$severity"
export PVE_UPDATER_TITLE="$title"
export PVE_UPDATER_MESSAGE="$message"
export PVE_UPDATER_TEMPLATE="$NOTIFY_TEMPLATE"
if perl -MPVE::Notify -e '
my $severity = $ENV{PVE_UPDATER_SEVERITY};
my $template = $ENV{PVE_UPDATER_TEMPLATE};
my $data = {
%{PVE::Notify::common_template_data()},
title => ($ENV{PVE_UPDATER_TITLE} // ""),
message => ($ENV{PVE_UPDATER_MESSAGE} // ""),
};
my $fields = {
type => "proxmox-updater",
};
PVE::Notify::notify($severity, $template, $data, $fields);
'
then
log "E-Mail-Benachrichtigung über '$NOTIFY_TARGET' versendet: $title"
else
# Ein Mailproblem macht einen ansonsten erfolgreichen Update-Lauf
# nicht nachträglich zu einem Updatefehler.
log "WARNUNG: E-Mail-Benachrichtigung konnte nicht versendet werden."
fi
unset PVE_UPDATER_SEVERITY PVE_UPDATER_TITLE PVE_UPDATER_MESSAGE PVE_UPDATER_TEMPLATE
}
usage() { usage() {
cat <<'EOF' cat <<'EOF'
Proxmox Guest Updater Proxmox Guest Updater
@@ -861,6 +996,8 @@ log "============================================================"
if [[ "$MODE" == "update" ]]; then if [[ "$MODE" == "update" ]]; then
if (( ERRORS > 0 )); then if (( ERRORS > 0 )); then
MAIL_STATUS_DETAIL="Keine automatischen Neustarts ausgeführt, weil $ERRORS Fehler im Update-Lauf aufgetreten sind."
if [[ -z "$ONLY_CT" && -z "$ONLY_VM" && "$HOST_REBOOT_REQUIRED" == "yes" ]]; then if [[ -z "$ONLY_CT" && -z "$ONLY_VM" && "$HOST_REBOOT_REQUIRED" == "yes" ]]; then
log "" log ""
log "Host-Neustart erforderlich, aber automatischer Neustart wird NICHT ausgeführt." log "Host-Neustart erforderlich, aber automatischer Neustart wird NICHT ausgeführt."
@@ -876,7 +1013,13 @@ if [[ "$MODE" == "update" ]]; then
elif [[ -n "$ONLY_CT" || -n "$ONLY_VM" ]]; then elif [[ -n "$ONLY_CT" || -n "$ONLY_VM" ]]; then
# Gezielter Gast-Lauf: Der Proxmox-Host wurde nicht aktualisiert. # Gezielter Gast-Lauf: Der Proxmox-Host wurde nicht aktualisiert.
# Deshalb notwendige Gast-Neustarts direkt am Ende ausführen. # Deshalb notwendige Gast-Neustarts direkt am Ende ausführen.
local_pending=$(( ${#LXC_REBOOT_PENDING[@]} + ${#VM_REBOOT_PENDING[@]} ))
reboot_pending_guests reboot_pending_guests
if (( local_pending > 0 )); then
MAIL_STATUS_DETAIL="$local_pending notwendige(r) Gast-Neustart(s) wurde(n) verarbeitet."
else
MAIL_STATUS_DETAIL="Kein Neustart erforderlich."
fi
elif [[ "$HOST_OK" == "yes" \ elif [[ "$HOST_OK" == "yes" \
&& "$HOST_REBOOT_REQUIRED" == "yes" \ && "$HOST_REBOOT_REQUIRED" == "yes" \
@@ -890,6 +1033,9 @@ if [[ "$MODE" == "update" ]]; then
log "Automatischer Neustart in 1 Minute." log "Automatischer Neustart in 1 Minute."
log "Proxmox fährt die laufenden Gäste geordnet herunter." log "Proxmox fährt die laufenden Gäste geordnet herunter."
HOST_REBOOT_SCHEDULED="yes"
MAIL_STATUS_DETAIL="Host-Neustart in 1 Minute geplant; notwendige Gast-Neustarts werden dadurch mit erledigt."
sync sync
shutdown -r +1 \ shutdown -r +1 \
@@ -899,8 +1045,21 @@ if [[ "$MODE" == "update" ]]; then
# Kein automatischer Host-Neustart: notwendige Gast-Neustarts jetzt # Kein automatischer Host-Neustart: notwendige Gast-Neustarts jetzt
# einzeln ausführen. Das gilt auch bei --no-reboot oder wenn der # einzeln ausführen. Das gilt auch bei --no-reboot oder wenn der
# automatische Host-Neustart in der Konfiguration deaktiviert ist. # automatische Host-Neustart in der Konfiguration deaktiviert ist.
local_pending=$(( ${#LXC_REBOOT_PENDING[@]} + ${#VM_REBOOT_PENDING[@]} ))
reboot_pending_guests reboot_pending_guests
if (( local_pending > 0 )); then
MAIL_STATUS_DETAIL="$local_pending notwendige(r) Gast-Neustart(s) wurde(n) verarbeitet."
elif [[ "$HOST_REBOOT_REQUIRED" == "yes" ]]; then
MAIL_STATUS_DETAIL="Host-Neustart erforderlich, aber automatischer Host-Reboot ist deaktiviert/unterdrückt."
else
MAIL_STATUS_DETAIL="Kein Neustart erforderlich."
fi
fi fi
# Erst nach allen Update- und Reboot-Entscheidungen versenden, damit
# der Bericht den tatsächlichen Endzustand des Laufs enthält.
send_update_notification
fi fi
if (( ERRORS > 0 )); then if (( ERRORS > 0 )); then