From b0f3a290dcadf583ba9ea2058209e6b94ab5cebe Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Thu, 7 Sep 2023 19:03:26 +0200 Subject: [PATCH] fix: [docker-host] make ``apt-get update`` failure fatal, and deal with some known and fixable issues --- precise/base-0k/hooks/install.d/00-base.sh | 53 +++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/precise/base-0k/hooks/install.d/00-base.sh b/precise/base-0k/hooks/install.d/00-base.sh index 45afee6..22589c4 100755 --- a/precise/base-0k/hooks/install.d/00-base.sh +++ b/precise/base-0k/hooks/install.d/00-base.sh @@ -33,7 +33,58 @@ fi ## We can now do the ``apt-get update`` safely... -apt-get update +apt:update() { + tried=() + backup= + + while ! out=$(apt-get update 2>&1); do + echo "Failed to 'apt-get update', looking for fixes..." + old_tried_length="${#tried[@]}" + failed_fetch=$(printf "%s" "$out" | egrep "^E: Failed to fetch .*404\s+Not Found") + failed_release=$(printf "%s" "$out" | egrep "^[EW]: The repository '.*' does (no longer|not) have a Release file.$") + if [[ " ${tried[*]} " != *" stretch-updates-fix "* ]] && + [[ "$failed_fetch" == *" http://archive.debian.org/dists/stretch/updates/"* ]]; then + tried+=("stretch-updates-fix") + [ -z "$backup" ] && { + backup=1 + echo "Backup old /etc/apt/sources.list" + cp -v /etc/apt/sources.list{,.myc-update} + } + echo "Applying stretch-updates-fix" + sed -ri 's%^(\s*deb(-src)? http://archive.debian.org/? stretch/updates .*)$%# \1%g' /etc/apt/sources.list + fi + + + if [[ " ${tried[*]} " != *" stretch-archive-fix "* ]] && + [[ "$failed_release" == *"'http://deb.debian.org/debian stretch"* ]]; then + tried+=("stretch-archive-fix") + [ -z "$backup" ] && { + backup=1 + echo "Backup old /etc/apt/sources.list" + cp -v /etc/apt/sources.list{,.myc-update} + } + echo "Applying stretch-archive-fix" + sed -i 's,http://deb.debian.org,http://archive.debian.org,g; + s,http://security.debian.org,http://archive.debian.org,g; + s,\(.*stretch-updates\),#\1,' \ + /etc/apt/sources.list + fi + + if [[ "$old_tried_length" == "${#tried[@]}" ]]; then + echo "Failing 'apt-get update'. Couldn't fix it automatically. Stopping." + if [ -n "$backup" ]; then + mv -v /etc/apt/sources.list{,.myc-update-fix} + mv -v /etc/apt/sources.list{.myc-update,} + fi + printf "%s\n" "$out" | sed -r 's/^/ | /g' + return 1 + fi + done + echo "Successful 'apt-get update'." +} + +apt:update || exit 1 + apt-get -y install bash-completion wget bzip2 git-core \ less tmux mosh \ sudo git vim file gawk