[tools.update-lxcs] feat: optional CLEAN_CACHE to remove downloaded packages after each update (#19192)

Co-authored-by: Matt Stein <matt@ninestone.ca>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-12-02 11:11:03 +01:00
1 parent 073eb20c52
commit deb17387c8
2 files changed
+33 -2

No files matched your search

+5
View File
@@ -119,6 +119,11 @@ EXCLUDE=
# Healthchecks.io Ping URL (optional)
# PING_URL=
# Remove downloaded packages after each successful update (optional).
# Without it, apt and the other package managers keep every package they
# download, and the cache grows on each run until the container's disk fills.
# CLEAN_CACHE=yes
CONF
ok "Created config ${CONF_FILE}"
fi
+28 -2
View File
@@ -13,6 +13,7 @@ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
CONF_FILE="/etc/update-lxcs.conf"
LOG_FILE="/var/log/update-lxcs-cron.log"
PING_URL=""
CLEAN_CACHE=""
# Collect excluded containers from arguments
excluded_containers=("$@")
@@ -20,6 +21,7 @@ excluded_containers=("$@")
# Merge exclusions and healthchecks URL from config file if it exists
if [[ -f "$CONF_FILE" ]]; then
PING_URL=$(grep -oP '^\s*PING_URL\s*=\s*\K.+' "$CONF_FILE" 2>/dev/null | tr -d '"' | tr -d "'" || true)
CLEAN_CACHE=$(grep -oP '^\s*CLEAN_CACHE\s*=\s*\K.+' "$CONF_FILE" 2>/dev/null | tr -d '"' | tr -d "'" || true)
conf_exclude=$(grep -oP '^\s*EXCLUDE\s*=\s*\K[0-9,]+' "$CONF_FILE" 2>/dev/null || true)
IFS=',' read -ra conf_ids <<<"$conf_exclude"
for id in "${conf_ids[@]}"; do
@@ -39,6 +41,22 @@ fi
echo -e "\n $(date)"
# Remove downloaded package files after a successful update (CLEAN_CACHE=yes).
# The upgrade commands keep every package they download, so without this the
# cache grows on every run until the container's disk is full.
function clean_container() {
local container=$1
local os=$2
echo -e " [Info] Cleaning package cache in $container"
case "$os" in
alpine) pct exec "$container" -- ash -c "apk cache clean 2>/dev/null || true" ;;
archlinux) pct exec "$container" -- bash -c "pacman -Sc --noconfirm" ;;
fedora | rocky | centos | alma) pct exec "$container" -- bash -c "dnf -y clean packages" ;;
ubuntu | debian | devuan) pct exec "$container" -- bash -c "apt-get clean" ;;
opensuse) pct exec "$container" -- bash -c "zypper --non-interactive clean" ;;
esac
}
function update_container() {
local container=$1
local name
@@ -46,14 +64,22 @@ function update_container() {
local os
os=$(pct config "$container" | awk '/^ostype/ {print $2}')
echo -e "\n [Info] Updating $container : $name (os: $os)"
local rc=0
case "$os" in
alpine) pct exec "$container" -- ash -c "apk -U upgrade" ;;
archlinux) pct exec "$container" -- bash -c "pacman -Syyu --noconfirm" ;;
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; status=\$?; rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED || true; exit \$status" ;;
opensuse) pct exec "$container" -- bash -c "zypper ref && zypper --non-interactive dup" ;;
*) echo " [Warn] Unknown OS type '$os' for container $container, skipping" ;;
esac
*)
echo " [Warn] Unknown OS type '$os' for container $container, skipping"
return 0
;;
esac || rc=$?
if [[ $rc -eq 0 && "${CLEAN_CACHE,,}" =~ ^(1|yes|true)$ ]]; then
clean_container "$container" "$os" || echo " [Warn] Cache cleanup failed for $container"
fi
return $rc
}
update_status=0