Compare commits

...

9 Commits

Author SHA1 Message Date
061e2212d9 latest 2025-03-29 01:17:58 -07:00
49ee17aa40 add readme 2025-03-29 01:04:56 -07:00
b231b0715f fix typo 2025-03-29 00:42:26 -07:00
cd2127b592 latest 2025-03-29 00:41:10 -07:00
80735fb393 fix bug where mirror is changed but metadata isn't refreshed 2025-03-29 00:37:29 -07:00
f2ddbfbb1b bugfix for set -euo 2025-03-29 00:34:21 -07:00
932f2b2d89 set -x for debugging 2025-03-29 00:32:05 -07:00
ee188a30d7 efficiency, don't apt update again if already done 2025-03-29 00:30:17 -07:00
2707542a45 make update logic smarter 2025-03-29 00:27:49 -07:00
2 changed files with 58 additions and 11 deletions

View File

@@ -0,0 +1,23 @@
# ubuntu geoip mirror selector
Add to `Dockerfile`:
```
RUN apt-get update && apt-get install -y curl jq bash
RUN curl -fsSL https://git.eeqj.de/sneak/ubuntulocal/raw/branch/main/updatemirror.sh | bash
```
Alternately, paste in:
```
apt-get update && apt-get install -y curl jq bash
curl -fsSL https://git.eeqj.de/sneak/ubuntulocal/raw/branch/main/updatemirror.sh | bash
```
# License
WTFPL
# Author
sneak `[sneak@sneak.berlin](mailto:sneak@sneak.berlin)`

View File

@@ -2,6 +2,14 @@
set -euo pipefail set -euo pipefail
function apt_update_if_stale () {
if ! find /var/lib/apt/lists -type f -mmin -30 | grep -q .; then
apt-get update
else
echo "Skipping apt update, metadata is fresh." > /dev/stderr
fi
}
function main () { function main () {
# check for ubuntu only # check for ubuntu only
if ! grep -qi '^ID=ubuntu' /etc/os-release; then if ! grep -qi '^ID=ubuntu' /etc/os-release; then
@@ -16,33 +24,43 @@ function main () {
fi fi
if ! grep -qE 'http(s)?://archive\.ubuntu\.com/' /etc/apt/sources.list.d/*; then if ! grep -qE 'http(s)?://archive\.ubuntu\.com/' /etc/apt/sources.list.d/*; then
echo "Not using default ubuntu repo, script will not function" > /dev/stderr echo "Not using default ubuntu repo, nothing for us to change. Exiting." > /dev/stderr
exit 1 exit 1
fi fi
apt_update_if_stale
if ! which jq > /dev/null; then
apt-get install -y jq
fi
if ! which curl > /dev/null; then
apt-get install -y curl
fi
echo "Changing mirror country to US as a first approximation..." > /dev/stderr echo "Changing mirror country to US as a first approximation..." > /dev/stderr
find /etc/apt/sources.list.d/ -type f -exec \ find /etc/apt/sources.list.d/ -type f -exec \
sed -i "s|archive\.ubuntu\.com|us.ubuntu.com|g" {} + sed -i "s|archive\.ubuntu\.com|us.archive.ubuntu.com|g" {} +
echo "Detecting actual country..." > /dev/stderr echo "Detecting actual country..." > /dev/stderr
apt update && apt install -y curl jq
IPINFO="$(curl -s ipinfo.io)" IPINFO="$(curl -s ipinfo.io)"
if [ -z "$IPINFO" ]; then if [ -z "$IPINFO" ]; then
echo "Failed to get IP info" > /dev/stderr echo "Failed to geolocate (fetch). Exiting." > /dev/stderr
exit 1 exit 1
fi fi
COUNTRYCODE="$(jq .country -r <<< "$IPINFO")" COUNTRYCODE="$(jq .country -r <<< "$IPINFO")"
if [[ -z "$COUNTRYCODE" ]]; then
echo "Failed to geolocate (parse). Exiting." > /dev/stderr
exit 1
fi
echo "IP geolocated as country: $COUNTRYCODE" > /dev/stderr echo "IP geolocated as country: $COUNTRYCODE" > /dev/stderr
if ! grep -qE 'http(s)?://us\.archive\.ubuntu\.com/' /etc/apt/sources.list.d/*; then
echo "Not using default ubuntu repo, script will not function" > /dev/stderr
exit 1
fi
if [[ "$COUNTRYCODE" == "US" ]]; then if [[ "$COUNTRYCODE" == "US" ]]; then
echo "Mirror already set to US, nothing to do" > /dev/stderr echo "Mirror was already set to US, nothing to do. Exiting." > /dev/stderr
exit 0 exit 0
fi fi
@@ -52,6 +70,12 @@ function main () {
find /etc/apt/sources.list.d/ -type f -exec \ find /etc/apt/sources.list.d/ -type f -exec \
sed -i "s|us\.archive\.ubuntu\.com|$MIRRORHOST|g" {} + sed -i "s|us\.archive\.ubuntu\.com|$MIRRORHOST|g" {} +
# update is actually mandatory after changing the mirror:
apt-get update
echo "Finished." > /dev/stderr
exit 0 exit 0
} }