Compare commits
7 Commits
ee188a30d7
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 061e2212d9 | |||
| 49ee17aa40 | |||
| b231b0715f | |||
| cd2127b592 | |||
| 80735fb393 | |||
| f2ddbfbb1b | |||
| 932f2b2d89 |
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)`
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user