Skip to content

Commit 22352dc

Browse files
authored
Upgrade to new better apt select script
1 parent e19bd45 commit 22352dc

File tree

1 file changed

+59
-32
lines changed

1 file changed

+59
-32
lines changed

addons/locate_mirror.sh

+59-32
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,84 @@
22

33
# T&M Hansson IT AB © - 2024, https://www.hanssonit.se/
44

5-
# shellcheck disable=2034,2059
65
true
7-
SCRIPT_NAME="Locate Mirror"
6+
SCRIPT_NAME="Locate mirror"
87
# shellcheck source=lib.sh
9-
source /var/scripts/fetch_lib.sh || source <(curl -sL https://raw.githubusercontent.com/techandme/wordpress-vm/master/lib.sh)
8+
source /var/scripts/fetch_lib.sh
9+
SCRIPT_EXPLAINER="To make downloads as fast as possible when updating Ubuntu \
10+
you should download mirrors that are as geographically close to you as possible.
11+
12+
Please note that there are no guarantees that the download mirrors \
13+
this script finds will remain for the lifetime of this server.
14+
Because of this, we don't recommend that you change the mirror unless you live far away from the default.
15+
16+
This is the method used: https://github.com/vegardit/fast-apt-mirror.sh"
17+
18+
# Check for errors + debug code and abort if something isn't right
19+
# 1 = ON
20+
# 0 = OFF
21+
DEBUG=0
22+
debug_mode
1023

1124
# Must be root
1225
root_check
1326

14-
# Use another method if the new one doesn't work
15-
if [ -z "$REPO" ]
27+
# Check if Locate Mirror is already installed
28+
if ! [ -f /usr/local/bin/fast-apt-mirror ]
1629
then
17-
REPO=$(apt update -q4 && apt-cache policy | grep http | tail -1 | awk '{print $2}')
30+
# Ask for installing
31+
install_popup "$SCRIPT_NAME"
32+
else
33+
# Ask for removal or reinstallation
34+
reinstall_remove_menu "$SCRIPT_NAME"
35+
# Removal
36+
rm -f /usr/local/bin/fast-apt-mirror
37+
rm -f /etc/apt/sources.list.backup
38+
# Show successful uninstall if applicable
39+
removal_popup "$SCRIPT_NAME"
1840
fi
1941

20-
# Check where the best mirrors are and update
21-
msg_box "To make downloads as fast as possible when updating Ubuntu \
22-
you should have download mirrors that are as close to you as possible.
23-
24-
Please note that there are no gurantees that the download mirrors \
25-
this script will find are staying up for the lifetime of this server.
26-
Because of this, we don't recommend to change the mirror, except you live far away from the default mirror.
42+
# Install
43+
install_if_not bash
44+
install_if_not curl
45+
install_if_not apt-transport-https
46+
install_if_not ca-certificates
47+
curl_to_dir https://raw.githubusercontent.com/vegardit/fast-apt-mirror.sh/v1/ fast-apt-mirror.sh /usr/local/bin
48+
mv /usr/local/bin/fast-apt-mirror.sh /usr/local/bin/fast-apt-mirror
49+
chmod 755 /usr/local/bin/fast-apt-mirror
2750

28-
This is the method used: https://github.com/jblakeman/apt-select"
29-
msg_box "Your current server repository is: $REPO"
51+
# Check current mirror
52+
CURRENT_MIRROR="$(fast-apt-mirror current)"
53+
msg_box "Current mirror is $CURRENT_MIRROR"
3054

55+
# Ask
3156
if ! yesno_box_no "Do you want to try to find a better mirror?"
3257
then
33-
print_text_in_color "$ICyan" "Keeping $REPO as mirror..."
58+
print_text_in_color "$ICyan" "Keeping $CURRENT_MIRROR as mirror..."
3459
sleep 1
3560
else
36-
if [[ "$KEYBOARD_LAYOUT" =~ ,|/|_ ]]
61+
if [[ "$KEYBOARD_LAYOUT" =~ ,|/|_ ]]
3762
then
38-
msg_box "Your keymap contains more than one language, or a special character. ($KEYBOARD_LAYOUT)
39-
This script can only handle one keymap at the time.\nThe default mirror ($REPO) will be kept."
63+
msg_box "Your keymap (country code) contains more than one language, or a special character. ($KEYBOARD_LAYOUT)
64+
This script can only handle one keymap at the time.\nThe default mirror ($CURRENT_MIRROR) will be kept."
4065
exit 1
4166
fi
67+
# Find
68+
FIND_MIRROR="$(fast-apt-mirror find -v --healthchecks 100 --speedtests 10 --country "$KEYBOARD_LAYOUT")"
4269
print_text_in_color "$ICyan" "Locating the best mirrors..."
43-
curl_to_dir https://bootstrap.pypa.io get-pip.py /tmp
44-
install_if_not python3
45-
install_if_not python3-testresources
46-
install_if_not python3-distutils
47-
cd /tmp && python3 get-pip.py
48-
pip install \
49-
--upgrade pip \
50-
apt-select
51-
check_command apt-select -m up-to-date -t 4 -c -C "$KEYBOARD_LAYOUT"
52-
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup && \
53-
if [ -f sources.list ]
70+
if [ "$CURRENT_MIRROR" != "$FIND_MIRROR" ]
5471
then
55-
sudo mv sources.list /etc/apt/
72+
if yesno_box_yes "Do you want to replace the $CURRENT_MIRROR with $FIND_MIRROR?"
73+
then
74+
# Backup
75+
cp -f /etc/apt/sources.list /etc/apt/sources.list.backup
76+
# Replace
77+
if fast-apt-mirror set "$FIND_MIRROR"
78+
then
79+
msg_box "Your Ubuntu repo was successfully changed to $FIND_MIRROR"
80+
fi
81+
fi
82+
else
83+
msg_box "You already have the fastest mirror available, congrats!"
5684
fi
57-
msg_box "The apt-mirror was successfully changed."
5885
fi

0 commit comments

Comments
 (0)