-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathom-mirror-selector.sh
executable file
·138 lines (124 loc) · 4.31 KB
/
om-mirror-selector.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
# [email protected] 2019
# This is only a proof of concept which uses average ping
# time for each mirrors from the list and choose one with lowest time
# If anyone want to work on this you may want to make it better:
# - use geoip to get lattitude and logitude of client and mirrors ip
# - calculate distance betwwen these two IPs
# - worth to use https://en.wikipedia.org/wiki/Haversine_formula
# - grab the data from http://downloads.openmandriva.org/mm/
# - omit those mirrors which are not up to date
# List of mirrors comes from http://downloads.openmandriva.org/mm/
# (tpg) status on 2019-05-10 date
# changes by - crazy -
# and - [email protected] -
QUIET=false
MANUAL=false
AUTOSELECT=false
DRYRUN=false
MIRRORLIST=false
for i in "$@"; do
case "$i" in
-h|--help)
cat <<EOF
Usage: $0 [-h|--help] [-q|--quiet] [-m|--manual] [-d|--dry-run]
By default, $0 returns repository configs to using
the mirror list from mirrors.openmandriva.org.
To do anything else, use the following parameters:
-a/--autoselect: Auto-select mirror with best ping time
-h/--help: Display this help text
-q/--quiet: Quiet mode - don't show progress
-m/--manual: Pick a mirror manually, don't probe ping latency
-d/--dry-run: Show what would be done instead of doing it
EOF
exit 0
;;
-a|--autoselect)
AUTOSELECT=true
;;
-q|--quiet)
QUIET=true
;;
-m|--manual)
MANUAL=true
;;
-d|--dry-run)
DRYRUN=true
;;
*)
echo "Unknown/invalid option $i" >&2
exit 1
;;
esac
shift
done
mirrors=( $(curl https://mirror.openmandriva.org/README.txt?mirrorlist |grep '<a href=' |sed -e 's,.*<a href=,,' |cut -d'"' -f2 |grep /mirrors/README.txt |sed -e 's,/mirrors/README.txt,,') )
if [ ${#mirrors[@]} = 0 ]; then
echo "Couldn't download mirror list - probably the server is down, try again later."
exit 1
fi
# array with pinged mirrors
declare -A pinged_mirrors
if $MANUAL; then
echo "Please select the mirror you want to use by typing the number next to it:"
num=1
for key in "${mirrors[@]}"; do
echo "$num $key"
num=$((num+1))
done
echo
while [ -z "$best_mirror" ]; do
read m
[[ "$m" =~ ^[0-9]+$ ]] && best_mirror="${mirrors[$((m-1))]}"
[ -z "$best_mirror" ] && echo "Please enter just the number next to the mirror you'd like to use."
done
elif $AUTOSELECT; then
# gathering average ping time for each mirror from array
best_time=999999999
for key in "${mirrors[@]}"; do
mirror="${key}"
$QUIET || echo -n "Pinging $mirror... "
mirror_aping="$(ping -q -c 3 $(printf '%s\n' "$key" | cut -d "/" -f3) | cut -d "/" -s -f5 | cut -d "." -s -f1)"
if [ "$?" = "0" -a -n "$mirror_aping" ]; then
pinged_mirrors+=( [${mirror_aping}]="${mirror}" )
if [ -z "$best_mirror" ] || [ "$mirror_aping" -lt "$best_time" ]; then
best_time="${mirror_aping}"
best_mirror="${mirror}"
$QUIET || echo "$mirror_aping ms *"
else
$QUIET || echo "$mirror_aping ms"
fi
else
$QUIET || echo "timed out"
fi
done
if [[ "${#pinged_mirrors[@]}" = "0" ]]; then
echo "Could not reach any mirrors. Network down?"
exit 1
fi
else
MIRRORLIST=true
fi
$QUIET || echo
if ! $MIRRORLIST; then
$QUIET || echo "Selecting mirror $best_mirror"
else
$QUIET || echo "Using mirror.openmandriva.org"
fi
if $DRYRUN || [ "$(id -u)" != '0' ]; then
$DRYRUN || echo 'You need root privileges for the next step.'
echo 'Run (as root):'
if $MIRRORLIST; then
echo " sed -i -e \"s|^baseurl=|# baseurl=|;s|^# mirrorlist=|mirrorlist=|\" /etc/yum.repos.d/openmandriva-*.repo"
else
echo " sed -i -e \"s|^mirrorlist=|# mirrorlist=|;s|^# baseurl=|baseurl=|\" -e \"s|^baseurl=.*\/cooker|baseurl=$best_mirror/cooker|g\" -e \"s|^baseurl=.*\/rock|baseurl=$best_mirror/rock|g\" -e \"s|^baseurl=.*\/rolling|baseurl=$best_mirror/rolling|g\" -e \"s|^baseurl=.*/\$releasever|baseurl=$best_mirror/\$releasever|g\" /etc/yum.repos.d/openmandriva-*.repo"
fi
exit 1
else
# update dnf repos with best mirror
if $MIRRORLIST; then
sed -i -e "s|^baseurl=|# baseurl=|;s|^# mirrorlist=|mirrorlist=|" /etc/yum.repos.d/openmandriva-*.repo
else
sed -i -e "s|^mirrorlist=|# mirrorlist=|;s|^# baseurl=|baseurl=|" -e "s|^baseurl=.*\/cooker|baseurl=$best_mirror/cooker|g" -e "s|^baseurl=.*\/rock|baseurl=$best_mirror/rock|g" -e "s|^baseurl=.*\/rolling|baseurl=$best_mirror/rolling|g" -e "s|^baseurl=.*/\$releasever|baseurl=$best_mirror/\$releasever|g" /etc/yum.repos.d/openmandriva-*.repo
fi
fi