Add status/run/rotate options & improve updater
tools/pve/cron-update-lxcs.sh: add show_status, run_now and rotate_log functions; expose new menu options (Status, Run, Rotate) and enlarge menu. Consolidate license line formatting. tools/pve/update-lxcs-cron.sh: tighten variable scoping, improve ostype detection and logging, add fallback for hostname, handle unknown OS types, harden apt workflow (retry with By-Hash workaround and refresh lists), skip template containers correctly, add error reporting on container updates, and use a timed shutdown. Update author/license header. Overall: adds manual control and status/log rotation to the cron manager and makes the container updater more robust and safer for Debian/Ubuntu-based containers.
This commit is contained in:
@@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
# Copyright (c) 2021-2026 community-scripts ORG
|
# Copyright (c) 2021-2026 community-scripts ORG
|
||||||
# Author: MickLesk (CanbiZ)
|
# Author: MickLesk (CanbiZ)
|
||||||
# License: MIT
|
# License: MIT | https://github.com/community-scripts/ProxmoxVED/raw/main/LICENSE
|
||||||
# https://github.com/community-scripts/ProxmoxVED/raw/main/LICENSE
|
|
||||||
#
|
#
|
||||||
# This script manages a local cron job for automatic LXC container OS updates.
|
# This script manages a local cron job for automatic LXC container OS updates.
|
||||||
# The update script is downloaded once, displayed for review, and installed
|
# The update script is downloaded once, displayed for review, and installed
|
||||||
@@ -191,19 +190,88 @@ view_script() {
|
|||||||
echo ""
|
echo ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
show_status() {
|
||||||
|
echo ""
|
||||||
|
if [[ -f "$LOCAL_SCRIPT" ]]; then
|
||||||
|
local hash
|
||||||
|
hash=$(sha256sum "$LOCAL_SCRIPT" | awk '{print $1}')
|
||||||
|
ok "Script installed: ${LOCAL_SCRIPT}"
|
||||||
|
echo -e " \e[36mSHA256:\e[0m ${hash}"
|
||||||
|
echo -e " \e[36mInstalled:\e[0m $(stat -c '%y' "$LOCAL_SCRIPT" 2>/dev/null | cut -d. -f1)"
|
||||||
|
else
|
||||||
|
err "Script not installed"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if crontab -l -u root 2>/dev/null | grep -q "${LOCAL_SCRIPT}"; then
|
||||||
|
local schedule
|
||||||
|
schedule=$(crontab -l -u root 2>/dev/null | grep "${LOCAL_SCRIPT}" | awk '{print $1,$2,$3,$4,$5}')
|
||||||
|
ok "Cron active: ${schedule}"
|
||||||
|
else
|
||||||
|
err "Cron not configured"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -f "$CONF_FILE" ]]; then
|
||||||
|
local excludes
|
||||||
|
excludes=$(grep -oP '^\s*EXCLUDE\s*=\s*\K.*' "$CONF_FILE" 2>/dev/null || echo "(none)")
|
||||||
|
echo -e " \e[36mExcluded:\e[0m ${excludes:-"(none)"}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -f "$LOG_FILE" ]]; then
|
||||||
|
local log_size last_run
|
||||||
|
log_size=$(du -h "$LOG_FILE" | awk '{print $1}')
|
||||||
|
last_run=$(grep -oP '^\s+\K\w.*' "$LOG_FILE" | tail -1)
|
||||||
|
echo -e " \e[36mLog file:\e[0m ${LOG_FILE} (${log_size})"
|
||||||
|
[[ -n "${last_run:-}" ]] && echo -e " \e[36mLast run:\e[0m ${last_run}"
|
||||||
|
else
|
||||||
|
echo -e " \e[36mLog file:\e[0m (no runs yet)"
|
||||||
|
fi
|
||||||
|
echo ""
|
||||||
|
}
|
||||||
|
|
||||||
|
run_now() {
|
||||||
|
if [[ ! -f "$LOCAL_SCRIPT" ]]; then
|
||||||
|
err "No local script found at ${LOCAL_SCRIPT}. Use 'Add' first."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
info "Running update script now..."
|
||||||
|
bash "$LOCAL_SCRIPT" | tee -a "$LOG_FILE"
|
||||||
|
ok "Run completed. Log appended to ${LOG_FILE}"
|
||||||
|
}
|
||||||
|
|
||||||
|
rotate_log() {
|
||||||
|
if [[ ! -f "$LOG_FILE" ]]; then
|
||||||
|
info "No log file to rotate."
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
local log_size
|
||||||
|
log_size=$(stat -c '%s' "$LOG_FILE" 2>/dev/null || echo 0)
|
||||||
|
local log_size_h
|
||||||
|
log_size_h=$(du -h "$LOG_FILE" | awk '{print $1}')
|
||||||
|
if confirm "Rotate log file? (current size: ${log_size_h})"; then
|
||||||
|
mv "$LOG_FILE" "${LOG_FILE}.old"
|
||||||
|
ok "Rotated: ${LOG_FILE} → ${LOG_FILE}.old"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
OPTIONS=(
|
OPTIONS=(
|
||||||
Add "Download, review & install cron schedule"
|
Add "Download, review & install cron schedule"
|
||||||
Remove "Remove cron schedule & local script"
|
Remove "Remove cron schedule & local script"
|
||||||
Update "Update local script from repository"
|
Update "Update local script from repository"
|
||||||
|
Status "Show installation status & last run"
|
||||||
|
Run "Run update script now (manual trigger)"
|
||||||
View "View currently installed script"
|
View "View currently installed script"
|
||||||
|
Rotate "Rotate log file"
|
||||||
)
|
)
|
||||||
|
|
||||||
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Cron Update LXCs" --menu "Select an option:" 12 68 4 \
|
CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Cron Update LXCs" --menu "Select an option:" 16 68 7 \
|
||||||
"${OPTIONS[@]}" 3>&1 1>&2 2>&3) || exit 0
|
"${OPTIONS[@]}" 3>&1 1>&2 2>&3) || exit 0
|
||||||
|
|
||||||
case $CHOICE in
|
case $CHOICE in
|
||||||
"Add") add ;;
|
"Add") add ;;
|
||||||
"Remove") remove ;;
|
"Remove") remove ;;
|
||||||
"Update") update_script ;;
|
"Update") update_script ;;
|
||||||
|
"Status") show_status ;;
|
||||||
|
"Run") run_now ;;
|
||||||
"View") view_script ;;
|
"View") view_script ;;
|
||||||
|
"Rotate") rotate_log ;;
|
||||||
esac
|
esac
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# Copyright (c) 2021-2026 community-scripts ORG
|
# Copyright (c) 2021-2026 community-scripts ORG
|
||||||
# Author: tteck (tteckster) | Rewritten by community-scripts
|
# Author: MickLesk (CanbiZ)
|
||||||
# License: MIT
|
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
|
||||||
# https://github.com/community-scripts/ProxmoxVED/raw/main/LICENSE
|
|
||||||
#
|
#
|
||||||
# This script is installed locally by cron-update-lxcs.sh and executed
|
# This script is installed locally by cron-update-lxcs.sh and executed
|
||||||
# by cron. It updates all LXC containers using their native package manager.
|
# by cron. It updates all LXC containers using their native package manager.
|
||||||
@@ -26,16 +25,26 @@ if [[ -f "$CONF_FILE" ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
function update_container() {
|
function update_container() {
|
||||||
container=$1
|
local container=$1
|
||||||
name=$(pct exec "$container" hostname)
|
local name
|
||||||
echo -e "\n [Info] Updating $container : $name \n"
|
name=$(pct exec "$container" hostname 2>/dev/null || echo "unknown")
|
||||||
|
local os
|
||||||
os=$(pct config "$container" | awk '/^ostype/ {print $2}')
|
os=$(pct config "$container" | awk '/^ostype/ {print $2}')
|
||||||
|
echo -e "\n [Info] Updating $container : $name (os: $os)"
|
||||||
case "$os" in
|
case "$os" in
|
||||||
alpine) pct exec "$container" -- ash -c "apk -U upgrade" ;;
|
alpine) pct exec "$container" -- ash -c "apk -U upgrade" ;;
|
||||||
archlinux) pct exec "$container" -- bash -c "pacman -Syyu --noconfirm" ;;
|
archlinux) pct exec "$container" -- bash -c "pacman -Syyu --noconfirm" ;;
|
||||||
fedora | rocky | centos | alma) pct exec "$container" -- bash -c "dnf -y update && dnf -y upgrade" ;;
|
fedora | rocky | centos | alma) pct exec "$container" -- bash -c "dnf -y update && dnf -y upgrade" ;;
|
||||||
ubuntu | debian | devuan) pct exec "$container" -- bash -c "apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -y; rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED" ;;
|
ubuntu | debian | devuan) pct exec "$container" -- bash -c '
|
||||||
|
apt-get update || {
|
||||||
|
echo "Acquire::By-Hash \"no\";" >/etc/apt/apt.conf.d/99no-by-hash
|
||||||
|
rm -rf /var/lib/apt/lists/*
|
||||||
|
apt-get update
|
||||||
|
}
|
||||||
|
DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -y
|
||||||
|
rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED' ;;
|
||||||
opensuse) pct exec "$container" -- bash -c "zypper ref && zypper --non-interactive dup" ;;
|
opensuse) pct exec "$container" -- bash -c "zypper ref && zypper --non-interactive dup" ;;
|
||||||
|
*) echo " [Warn] Unknown OS type '$os' for container $container, skipping" ;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,16 +61,19 @@ for container in $(pct list | awk '{if(NR>1) print $1}'); do
|
|||||||
sleep 1
|
sleep 1
|
||||||
else
|
else
|
||||||
status=$(pct status "$container")
|
status=$(pct status "$container")
|
||||||
template=$(pct config "$container" | grep -q "template:" && echo "true" || echo "false")
|
if pct config "$container" 2>/dev/null | grep -q "^template:"; then
|
||||||
if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then
|
echo -e "[Info] Skipping template $container"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
if [ "$status" == "status: stopped" ]; then
|
||||||
echo -e "[Info] Starting $container"
|
echo -e "[Info] Starting $container"
|
||||||
pct start "$container"
|
pct start "$container"
|
||||||
sleep 5
|
sleep 5
|
||||||
update_container "$container"
|
update_container "$container" || echo " [Error] Update failed for $container"
|
||||||
echo -e "[Info] Shutting down $container"
|
echo -e "[Info] Shutting down $container"
|
||||||
pct shutdown "$container" &
|
pct shutdown "$container" --timeout 60 &
|
||||||
elif [ "$status" == "status: running" ]; then
|
elif [ "$status" == "status: running" ]; then
|
||||||
update_container "$container"
|
update_container "$container" || echo " [Error] Update failed for $container"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user