Skip to content
Myr

apt-install.sh

This file

Path

.github/scripts/apt-install.sh

52 lines · 1.4 KiB · Shell

Last changed by

[actions] never let an apt install wait on a promptJordan Harbandbefore adoption077156e

History of this file

Take it

[actions] retry apt package installs through Ubuntu mirror outagesJordan Harbandbefore adoption50e73c4

lines 1–22why, one step back
1#!/bin/sh
23# Install apt packages, riding out Ubuntu/Debian mirror outages: apt itself4# retries each request, and the whole update+install is retried with backoff5# for about 10 minutes, since outages have lasted longer than apt's own retries.6#7# usage: sh .github/scripts/apt-install.sh <package>...8# Runs apt-get through sudo when not root.910set -u1112if [ "$#" -eq 0 ]; then13 echo 'usage: apt-install.sh <package>...' >&214 exit 215fi1617SUDO=''18if [ "$(id -u)" != '0' ]; then19 SUDO='sudo'20fi2122apt_get() {

[actions] never let an apt install wait on a promptJordan Harbandbefore adoption077156e

line 23why, one step back
23 # never prompt: an upgrade (e.g. libssl asking to restart services) would wait forever

[actions] retry apt package installs through Ubuntu mirror outagesJordan Harbandbefore adoption50e73c4

lines 24–27why, one step back
24 $SUDO env DEBIAN_FRONTEND=noninteractive apt-get \
25 -o Acquire::Retries=5 \26 -o Acquire::http::Timeout=30 \27 -o Acquire::https::Timeout=30 \

[actions] never let an apt install wait on a promptJordan Harbandbefore adoption077156e

lines 28–30why, one step back
28 -o Dpkg::Options::=--force-confdef \
29 -o Dpkg::Options::=--force-confold \30 "$@" < /dev/null

[actions] retry apt package installs through Ubuntu mirror outagesJordan Harbandbefore adoption50e73c4

lines 31–52why, one step back
31}
3233ATTEMPT=134MAX_ATTEMPTS=835DELAY=1536while :; do37 apt_get update || echo "apt-get update failed (attempt ${ATTEMPT}/${MAX_ATTEMPTS})" >&238 if apt_get install -y "$@"; then39 exit 040 fi41 if [ "${ATTEMPT}" -ge "${MAX_ATTEMPTS}" ]; then42 echo "apt-get install failed after ${MAX_ATTEMPTS} attempts: $*" >&243 exit 144 fi45 echo "apt-get install failed (attempt ${ATTEMPT}/${MAX_ATTEMPTS}); retrying in ${DELAY}s" >&246 sleep "${DELAY}"47 ATTEMPT=$((ATTEMPT + 1))48 DELAY=$((DELAY * 2))49 if [ "${DELAY}" -gt 120 ]; then50 DELAY=12051 fi52done