Files
runner-images/images/ubuntu/scripts/build/configure-apt-mock.sh
T
Fahrzin HemmatiandGitHub 925185f01e [ubuntu] Update configure-apt-mock.sh to enable parallelism (#11467)
This adds a few errors that happen when another update/install process is running in parallel. `apt-get update` doesn't handle race conditions well, and `apt-get install` has one as well.

This is pre-cursor to another change that enables parallelism in the packer templates to run the `install-*.sh` scripts parallel, as I mentioned in https://github.com/actions/runner-images/discussions/2320
2026-01-28 15:07:36 +01:00

64 lines
2.0 KiB
Bash

#!/bin/bash -e
################################################################################
## File: configure-apt-mock.sh
## Desc: A temporary workaround for https://github.com/Azure/azure-linux-extensions/issues/1238.
## Cleaned up during cleanup.sh.
################################################################################
prefix=/usr/local/bin
for real_tool in /usr/bin/apt /usr/bin/apt-get /usr/bin/apt-key; do
tool=$(basename $real_tool)
cat >$prefix/$tool <<EOT
#!/bin/sh
i=1
while [ \$i -le 30 ];do
err=\$(mktemp)
$real_tool "\$@" 2>\$err
# no errors, break the loop and continue normal flow
test -f \$err || break
cat \$err >&2
retry=false
if grep -q 'Could not get lock' \$err;then
# apt db locked needs retry
retry=true
elif grep -q 'Could not get lock /var/lib/apt/lists/lock' \$err;then
# apt update did not complete (race condition), needs retry
retry=true
elif grep -q 'Problem renaming the file /var/cache/apt/pkgcache.bin.* to /var/cache/apt/pkgcache.bin' \$err;then
# apt-get did not complete (race condition), needs retry
retry=true
elif grep -q 'Problem renaming the file /var/cache/apt/srcpkgcache.bin.* to /var/cache/apt/srcpkgcache.bin' \$err;then
# apt update did not complete (race condition), needs retry
retry=true
elif grep -q 'Could not open file /var/lib/apt/lists' \$err;then
# apt update is not completed, needs retry
retry=true
elif grep -q 'IPC connect call failed' \$err;then
# the delay should help with gpg-agent not ready
retry=true
elif grep -q 'Temporary failure in name resolution' \$err;then
# It looks like DNS is not updated with random generated hostname yet
retry=true
elif grep -q 'dpkg frontend is locked by another process' \$err;then
# dpkg process is busy by another process
retry=true
fi
rm \$err
if [ \$retry = false ]; then
break
fi
sleep 5
echo "...retry \$i"
i=\$((i + 1))
done
EOT
chmod +x $prefix/$tool
done