| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Copyright (c) 2023 Arnaud Rebillout <[email protected]> |
| 4 |
# Distributed under the same license as mirrorbits. |
| 5 |
|
| 6 |
set -euo pipefail |
| 7 |
|
| 8 |
USAGE="$(basename $0) [-f [-f]] [--] [MIRRORBITS_ARGS...] |
| 9 |
|
| 10 |
Update geolocation for all mirrors. |
| 11 |
|
| 12 |
The argument '-f' controls the exact behavior of the script: |
| 13 |
* no -f: ask for confirmation for each mirror. |
| 14 |
* one -f: if there are only latitude and longitude changes, don't ask for |
| 15 |
confirmation. Otherwise (eg. ASN, country or continent changed), either |
| 16 |
1) ask for confirmation if running interactively, or |
| 17 |
2) print the changes and skip this mirror - in the end the script returns |
| 18 |
the special exit code 33 to signal that some mirrors were not updated. |
| 19 |
* two -f: update all mirrors without asking for confirmation. |
| 20 |
|
| 21 |
Trailing arguments are handed over to the mirrorbits command. |
| 22 |
|
| 23 |
For example, the command: |
| 24 |
|
| 25 |
mirrorbits-geoupdate-all -f -- -p 3391 |
| 26 |
|
| 27 |
updates all the mirrors for the mirrorbits instance listening on port 3391. |
| 28 |
|
| 29 |
BUGS: This script assumes that calling 'mirrorbits geoupdate \$mirror' twice |
| 30 |
in a row returns the same result, but this is not always true. For some |
| 31 |
mirrors, the hostname returns more than one address, and each address might |
| 32 |
have a different geolocation, maybe even different a ASN. |
| 33 |
" |
| 34 |
|
| 35 |
FORCE=0 |
| 36 |
|
| 37 |
while [ $# -gt 0 ]; do |
| 38 |
case $1 in |
| 39 |
-f|--force) FORCE=$((FORCE + 1)) ;; |
| 40 |
-h|--help) echo "$USAGE"; exit 0 ;; |
| 41 |
--) shift; break ;; |
| 42 |
*) break ;; |
| 43 |
esac |
| 44 |
shift |
| 45 |
done |
| 46 |
|
| 47 |
MIRRORBITS="mirrorbits $@" |
| 48 |
|
| 49 |
# Get all the mirrors |
| 50 |
MIRRORS=$($MIRRORBITS list -state=false | grep -iv "^identifier\b" || :) |
| 51 |
MIRRORS=$(echo "$MIRRORS" | LC_ALL=C sort -u) |
| 52 |
|
| 53 |
# Helper to print like ansible, bash-fu from: |
| 54 |
# https://stackoverflow.com/a/54505990/776208 |
| 55 |
echo_updating() { |
| 56 |
COLUMNS=$(tput cols 2>/dev/null || echo 80) |
| 57 |
printf "%-${COLUMNS}s\n" "[^Updating^mirror:^$1^]^" | tr "^ " " =" |
| 58 |
} |
| 59 |
|
| 60 |
# Iterate over the mirrors |
| 61 |
EXIT_STATUS=0 |
| 62 |
MIRRORS_WITH_CHANGES=0 |
| 63 |
for mirror in $MIRRORS; do |
| 64 |
# Ask to update mirror, but bail out. We only want to see the changes. |
| 65 |
output=$(echo n | $MIRRORBITS geoupdate $mirror) |
| 66 |
|
| 67 |
# Keep only the lines that list the changes. |
| 68 |
changes=$(echo "$output" | grep "^[+-] " || :) |
| 69 |
|
| 70 |
# No change? Keep going then. |
| 71 |
if [ -z "$changes" ]; then |
| 72 |
continue |
| 73 |
fi |
| 74 |
|
| 75 |
MIRRORS_WITH_CHANGES=$((MIRRORS_WITH_CHANGES + 1)) |
| 76 |
|
| 77 |
echo |
| 78 |
echo_updating $mirror |
| 79 |
|
| 80 |
# FORCE == 0 aka. interactive. Always ask for confirmation. |
| 81 |
if [ $FORCE -eq 0 ]; then |
| 82 |
$MIRRORBITS geoupdate $mirror |
| 83 |
continue |
| 84 |
fi |
| 85 |
|
| 86 |
# FORCE == 2 aka. just do it. Never ask for confirmation. |
| 87 |
if [ $FORCE -ge 2 ]; then |
| 88 |
$MIRRORBITS geoupdate -f $mirror |
| 89 |
continue |
| 90 |
fi |
| 91 |
|
| 92 |
# FORCE == 1 aka. automatic but conservative. |
| 93 |
# Filter out latitude and longitude changes. If there are no other |
| 94 |
# changes, act without waiting for confirmation. If there are other |
| 95 |
# changes, and we're running interactively, ask for confirmation. |
| 96 |
# Otherwise, print a warning, and keep going. We'll return a special |
| 97 |
# exit code in the end. |
| 98 |
changes=$(echo "$changes" | grep -Ev "L(at|ong)itude:" || :) |
| 99 |
if [ -z "$changes" ]; then |
| 100 |
$MIRRORBITS geoupdate -f $mirror |
| 101 |
elif [ -t 0 ]; then |
| 102 |
$MIRRORBITS geoupdate $mirror |
| 103 |
else |
| 104 |
echo "NOT UPDATING! Changes need review, see below:" |
| 105 |
echo "$output" | grep -iv "y/n" || : |
| 106 |
EXIT_STATUS=33 |
| 107 |
fi |
| 108 |
done |
| 109 |
|
| 110 |
if [ $MIRRORS_WITH_CHANGES = 0 ]; then |
| 111 |
echo "No mirror to update, nothing was done." |
| 112 |
fi |
| 113 |
|
| 114 |
exit $EXIT_STATUS |