From 862a6098f032a6e41de644f9cb1aad6ea6ac2803 Mon Sep 17 00:00:00 2001 From: Tanya Degurechaff <34323200+TanyaEleventhGoddess@users.noreply.github.com> Date: Mon, 13 Jul 2020 16:11:51 +0200 Subject: [PATCH] Handle suite mismatch, don't download packages if update failed Signed-off-by: Tanya Degurechaff <34323200+TanyaEleventhGoddess@users.noreply.github.com> --- templates/lxc-debian.in | 32 ++++++++++++++++++++++++++++---- templates/lxc-devuan.in | 34 +++++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/templates/lxc-debian.in b/templates/lxc-debian.in index d446a84..5039843 100644 --- a/templates/lxc-debian.in +++ b/templates/lxc-debian.in @@ -67,6 +67,16 @@ find_interpreter() return 1 } +release_info() { + if wget "${1}/dists/${release}/Release" -O /dev/null --quiet 2> /dev/null; then + local release_file=$(wget "${1}/dists/${release}/Release" -O - --quiet 2> /dev/null) + distro_suite=${release_file#*Suite: } + distro_suite=${distro_suite%%$'\n'*} + return 0 + fi + return 1 +} + configure_debian() { rootfs=$1 @@ -237,14 +247,26 @@ EOF fi } +apt_update_packages(){ + local apt_update_args="update" + local lpath="${rootfs}/var/lib/apt/lists/$(echo "$MIRROR" | sed 's#.*://##;s#/#_#g')_dists_${release}_Release" + if [ -f "$lpath" ] && [ ! -z "$distro_suite" ]; then + local lsuite=$(cat "$lpath" | grep 'Suite:') + [ "${lsuite#Suite: }" != "$distro_suite" ] && apt_update_args="--allow-releaseinfo-change $apt_update_args" + fi + chroot "${rootfs}" apt-get $apt_update_args + return $? +} + install_packages() { local rootfs="$1"; shift local packages="$*" - - chroot "${rootfs}" apt-get update - if [ -n "${packages}" ]; then + + if [ -n "${packages}" ] && apt_update_packages; then chroot "${rootfs}" apt-get install --force-yes -y --no-install-recommends ${packages} + elif [ -n "${packages}" ]; then + echo "Apt update failed, not downloading packages" fi } @@ -811,10 +833,12 @@ fi release=${release:-stable} permanent_releases=('stable' 'testing' 'sid' 'unstable') if [[ ! "${permanent_releases[*]}" =~ (^|[^[:alpha:]])$release([^[:alpha:]]|$) ]]; then - if ! wget "${MIRROR}/dists/${release}/Release" -O /dev/null 2> /dev/null; then + if ! release_info "${MIRROR}"; then echo "Invalid release ${release} (not found in mirror)" exit 1 fi +else + release_info "${MIRROR}" fi # detect rootfs diff --git a/templates/lxc-devuan.in b/templates/lxc-devuan.in index 408a93e..b5b41e2 100644 --- a/templates/lxc-devuan.in +++ b/templates/lxc-devuan.in @@ -67,6 +67,16 @@ find_interpreter() return 1 } +release_info() { + if wget "${1}/dists/${release}/Release" -O /dev/null --quiet 2> /dev/null; then + local release_file=$(wget "${1}/dists/${release}/Release" -O - --quiet 2> /dev/null) + distro_suite=${release_file#*Suite: } + distro_suite=${distro_suite%%$'\n'*} + return 0 + fi + return 1 +} + configure_devuan() { rootfs=$1 @@ -240,14 +250,26 @@ EOF fi } +apt_update_packages(){ + local apt_update_args="update" + local lpath="${rootfs}/var/lib/apt/lists/$(echo "$MIRROR" | sed 's#.*://##;s#/#_#g')_dists_${release}_Release" + if [ -f "$lpath" ] && [ ! -z "$distro_suite" ]; then + local lsuite=$(cat "$lpath" | grep 'Suite:') + [ "${lsuite#Suite: }" != "$distro_suite" ] && apt_update_args="--allow-releaseinfo-change $apt_update_args" + fi + chroot "${rootfs}" apt-get $apt_update_args + return $? +} + install_packages() { local rootfs="$1"; shift local packages="$*" - - chroot "${rootfs}" apt-get update - if [ -n "${packages}" ]; then + + if [ -n "${packages}" ] && apt_update_packages; then chroot "${rootfs}" apt-get install --force-yes -y --no-install-recommends ${packages} + elif [ -n "${packages}" ]; then + echo "Apt update failed, not downloading packages" fi } @@ -755,12 +777,14 @@ if [ -n "$authkey" ]; then fi release=${release:-stable} -permanent_releases=('stable' 'testing' 'ceres' 'unstable') +permanent_releases=('stable' 'testing' 'sid' 'unstable') if [[ ! "${permanent_releases[*]}" =~ (^|[^[:alpha:]])$release([^[:alpha:]]|$) ]]; then - if ! wget "${MIRROR}/dists/${release}/Release" -O /dev/null 2> /dev/null; then + if ! release_info "${MIRROR}"; then echo "Invalid release ${release} (not found in mirror)" exit 1 fi +else + release_info "${MIRROR}" fi # detect rootfs