Compare commits

..

7 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
2 changed files with 49 additions and 16 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

@@ -3,7 +3,11 @@
set -euo pipefail set -euo pipefail
function apt_update_if_stale () { function apt_update_if_stale () {
[ -z "$(find /var/lib/apt/lists -type f -mmin -30 2>/dev/null)" ] && apt-get update 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 () {
@@ -20,43 +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
echo "Changing mirror country to US as a first approximation..." > /dev/stderr
find /etc/apt/sources.list.d/ -type f -exec \
sed -i "s|archive\.ubuntu\.com|us.ubuntu.com|g" {} +
apt_update_if_stale apt_update_if_stale
apt install -y jq curl 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
find /etc/apt/sources.list.d/ -type f -exec \
sed -i "s|archive\.ubuntu\.com|us.archive.ubuntu.com|g" {} +
echo "Detecting actual country..." > /dev/stderr echo "Detecting actual country..." > /dev/stderr
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 if [[ -z "$COUNTRYCODE" ]]; then
echo "Failed to get country code" > /dev/stderr echo "Failed to geolocate (parse). Exiting." > /dev/stderr
exit 1 exit 1
fi 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
@@ -66,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
} }