From a09917a0ce809ac2741bf64237f25f301585d40f Mon Sep 17 00:00:00 2001 From: James Westby Date: Wed, 30 Nov 2016 15:11:33 +0000 Subject: [PATCH] Retry apt if it fails in `install_packages`. (#8) Sometimes apt will fail due a transient network issue. Often that will be fixed by retrying. This is particularly useful as part of an automated build pipeline. --- README.md | 6 +++++- mkimage | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index eb991fa..e1b5dda 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,14 @@ small images, and having many quality packages available for easy integration. These images also include an `install_packages` command -that you can use instead of apt. This does two things: +that you can use instead of apt. This takes care of some things +for you: 1. Install the named packages, skipping prompts etc. 2. Clean up the apt metadata afterwards to keep the image small. + 3. Retrying if apt fails. Sometimes a package will fail to download + due to a network issue, and this may fix that, which is + particularly useful in an automated build pipeline. e.g. diff --git a/mkimage b/mkimage index 9508d5e..22f1956 100755 --- a/mkimage +++ b/mkimage @@ -164,8 +164,25 @@ cat > "$rootfsDir/usr/sbin/install_packages" <<-'EOF' set -e set -u export DEBIAN_FRONTEND=noninteractive -apt-get update -qq -apt-get install -y --no-install-recommends "$@" +n=0 +max=2 +until [ $n -gt max ]; do + set +e + ( + apt-get update -qq && + apt-get install -y --no-install-recommends "$@" + ) + CODE=$? + set -e + if [ $CODE -eq 0 ]; then + break + fi + if [ $n -eq $max ]; then + exit $CODE + fi + echo "apt failed, retrying" + n=$[$n+1] +done rm -r /var/lib/apt/lists /var/cache/apt/archives EOF chmod 0755 "$rootfsDir/usr/sbin/install_packages"