Compare commits
9 Commits
dbb756c2c6
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 061e2212d9 | |||
| 49ee17aa40 | |||
| b231b0715f | |||
| cd2127b592 | |||
| 80735fb393 | |||
| f2ddbfbb1b | |||
| 932f2b2d89 | |||
| ee188a30d7 | |||
| 2707542a45 |
23
README.md
23
README.md
@@ -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)`
|
||||
|
||||
@@ -2,6 +2,14 @@
|
||||
|
||||
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 () {
|
||||
# check for ubuntu only
|
||||
if ! grep -qi '^ID=ubuntu' /etc/os-release; then
|
||||
@@ -16,33 +24,43 @@ function main () {
|
||||
fi
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
apt update && apt install -y curl jq
|
||||
IPINFO="$(curl -s ipinfo.io)"
|
||||
|
||||
if [ -z "$IPINFO" ]; then
|
||||
echo "Failed to get IP info" > /dev/stderr
|
||||
echo "Failed to geolocate (fetch). Exiting." > /dev/stderr
|
||||
exit 1
|
||||
fi
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
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
|
||||
fi
|
||||
|
||||
@@ -52,6 +70,12 @@ function main () {
|
||||
|
||||
find /etc/apt/sources.list.d/ -type f -exec \
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user