Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-26 08:18:01

0001 #!/bin/bash
0002 # Point apt at a mirror near the runner, when the runner fleet names one.
0003 #
0004 # archive.ubuntu.com is reachable but slow from some networks: measured from CERN
0005 # it served ~48 kB/s against ~1.9 MB/s for the Swiss mirror, which made an
0006 # apt-get the longest step in jobs that were not otherwise network-bound.
0007 #
0008 # The mirror URL is NOT hardcoded here, because the right answer depends on where
0009 # the job is running and this repository cannot know that. Self-hosted fleets
0010 # export HUSK_UBUNTU_APT_MIRROR into the job container; GitHub-hosted runners do
0011 # not set it, and then this script does nothing at all. That keeps the knowledge
0012 # in the one place that has it, and keeps the hosted path provably unchanged.
0013 #
0014 # The UBUNTU in the name is load-bearing: a distribution mirror is only valid for
0015 # the distribution whose suites it carries. Pointing a Debian image at an Ubuntu
0016 # mirror does not degrade to slow, it 404s, because the suite names do not overlap
0017 # (bookworm vs noble). The gate below enforces that rather than leaving it to the
0018 # rewrite patterns to happen to miss.
0019 #
0020 # Safe to run more than once, on any image: it edits only lines that still point
0021 # at the upstream archive.
0022 
0023 set -u
0024 
0025 if [ -z "${HUSK_UBUNTU_APT_MIRROR:-}" ]; then
0026   # Exiting mute is right for a hosted runner, which is never expected to name a
0027   # mirror. On a self-hosted slot it is a misconfiguration whose only other
0028   # symptom is an unexplained slow apt-get, so leave a trace to grep for.
0029   if [ "${RUNNER_ENVIRONMENT:-}" = "self-hosted" ]; then
0030     echo "apt mirror: HUSK_UBUNTU_APT_MIRROR unset on a self-hosted runner, using upstream archive"
0031   fi
0032   exit 0
0033 fi
0034 
0035 command -v apt-get > /dev/null 2>&1 || exit 0
0036 
0037 # Debian and its derivatives all ship apt, so apt-get is not evidence of Ubuntu.
0038 # Absent os-release, skip: an unidentifiable image is not one to rewrite blind.
0039 distro=""
0040 if [ -r /etc/os-release ]; then
0041   # shellcheck disable=SC1091
0042   distro=$(. /etc/os-release && echo "${ID:-}")
0043 fi
0044 if [ "$distro" != "ubuntu" ]; then
0045   echo "apt mirror: skipping, distro is '${distro:-unknown}', not ubuntu"
0046   exit 0
0047 fi
0048 
0049 if command -v sudo > /dev/null 2>&1; then SUDO="sudo"; else SUDO=""; fi
0050 
0051 # 24.04 uses deb822 (sources.list.d/*.sources); older images use sources.list.
0052 # Rewrite whichever exist, and both hosts: the country mirrors carry -security
0053 # too, so leaving security.ubuntu.com alone would leave half the fetch slow.
0054 #
0055 # ports.ubuntu.com (arm64 and friends) is deliberately NOT rewritten: the country
0056 # mirrors that carry the x86 archive frequently do not carry ports, so redirecting
0057 # it would break rather than speed up a non-x86 job.
0058 changed=0
0059 for f in /etc/apt/sources.list /etc/apt/sources.list.d/*.sources \
0060   /etc/apt/sources.list.d/*.list; do
0061   [ -f "$f" ] || continue
0062   # One expression per scheme rather than `https\?://`: that is a GNU extension,
0063   # and silently matching nothing is the worst failure mode here — the job would
0064   # simply stay slow with no error to notice.
0065   before=$($SUDO cat "$f")
0066   $SUDO sed -i \
0067     -e "s|http://archive\.ubuntu\.com/ubuntu|${HUSK_UBUNTU_APT_MIRROR}|g" \
0068     -e "s|https://archive\.ubuntu\.com/ubuntu|${HUSK_UBUNTU_APT_MIRROR}|g" \
0069     -e "s|http://security\.ubuntu\.com/ubuntu|${HUSK_UBUNTU_APT_MIRROR}|g" \
0070     -e "s|https://security\.ubuntu\.com/ubuntu|${HUSK_UBUNTU_APT_MIRROR}|g" \
0071     "$f"
0072   [ "$before" = "$($SUDO cat "$f")" ] || changed=$((changed + 1))
0073 done
0074 
0075 # Report what actually happened. An unconditional success line would hide the
0076 # case this script most needs to surface: the mirror was named, the image is
0077 # ubuntu, and yet nothing matched — meaning the sources are in a form we do not
0078 # recognise and the job is silently still on the slow path.
0079 if [ "$changed" -gt 0 ]; then
0080   echo "apt mirror set to ${HUSK_UBUNTU_APT_MIRROR} (${changed} source file(s) rewritten)"
0081 else
0082   echo "apt mirror: ${HUSK_UBUNTU_APT_MIRROR} named, but no upstream archive URLs found to rewrite"
0083 fi