I have a test version of uberstudentupdate that automatically tests for and places the fastest mirror every 30 days, or whichever day after 30 that you are connected to the internet.
The startup script adds a subprocess:
Code:
#!/usr/bin/python
import os, commands, subprocess
cmdline = commands.getoutput("cat /proc/cmdline")
if ((not "boot=casper" in cmdline) and (not "boot=live" in cmdline)):
subprocess.Popen(["sudo", "/usr/lib/uberstudent/uberstudentUpdate/apt-mirror-check"]).wait()
os.system("/usr/lib/uberstudent/uberstudentUpdate/uberstudentUpdate.py")
The subprocess script the python startup script calls, apt-mirror-check, is:
Code:
#!/bin/bash
##############################################################
# MIRROR SPEED TEST AND FASTEST MIRROR PLACEMENT
# Ref: http://uberstudent.com/phpBB/viewtopic.php?f=15&t=1195
##############################################################
# For testing file age, make it old: touch -a -m -t 201512180130.09 /tmp/aptmirrortest/age
#if test `find "/tmp/aptmirrortest/age" -mtime +30` ; then
# If the find command *does not equal* (!=) empty, i.e., if it's not over 30 days old.
# This should use less resources then the *if test* line, above.
if [ "$(find /tmp/aptmirrortest/age -mtime +30)" != "" ]; then
# Needed sleep to ensure network is up and all other start programs have started
sleep 15
# Test if decently internet-connected, and if so, proceed, or else pass to line 135 "else"
wget -q --tries=1 --timeout=15 http://uberstudent.net -O /tmp/test.idx &> /dev/null
if [ -s /tmp/test.idx ]; then
echo "Mirror is over 30 days old. Taking actions...."
# Notify the user.
notify-send -u critical -t 0 --icon /usr/share/pixmaps/uberstudentupdate.png "IMPORTANT!" \
"<b>UberStudent Update's software mirrors are more than 30 days old.</b>
The mirrors are currently being updated in the background. It should take about 5 minutes.
<i>Do not try to install or remove any software until this process has finished</i>. You'll be notified when it's done.
<b>You must click this message to dismiss it.</b>"
# Prevent a catastrophe by locking /var/lib/dpkg/lock so apt can't run while this does
exec {lock_fd}>/var/lib/dpkg/lock || exit 1
flock -n "$lock_fd" || { echo "ERROR: flock() failed." >&2; exit 1; }
if [ ! -d /tmp/aptmirrortest ]; then
mkdir /tmp/aptmirrortest
fi
# Create this directory, it's required for the below actions.
if [ -d /tmp/aptmirror.test ]; then
rm -rf /tmp/aptmirror.test
mkdir /tmp/aptmirror.test
elif [ ! -d /tmp/aptmirror.test ]; then
mkdir /tmp/aptmirror.test
fi
sleep 1
# Run the mirror speed test string and write the result to a temp file we read from below.
# This is the magic line that tests the mirror speeds and selects the fastest one,
# which is from http://askubuntu.com/a/719551/274446
curl -s http://mirrors.ubuntu.com/mirrors.txt | xargs -n1 -I {} sh -c 'echo `curl -r 0-102400 -s -w %{speed_download} -o /dev/null {}/ls-lR.gz` {}' |sort -g -r |head -1| awk '{ print $2 }' > /tmp/aptmirror.test/test
sleep 1
# Below, take actions contingent on the user's installation and apt state:
#
# Pre-4.3
#
# If the old xorg version and user DOES NOT have sources list enabled
if [ ! -f /usr/share/doc/xserver-xorg-lts-vivid/copyright ] && [ ! -f /etc/apt/sources.list.d/official-source-repositories.list ]; then
# File editing magic here that is perfect for when precision matters.
# Ref: http://unix.stackexchange.com/a/164284
FILE1=$(</tmp/aptmirror.test/test)
FILE2=$(</usr/share/uberstudent/uberstudentsources/pre-heraclitus/official-package-repositories.list)
echo "${FILE2//marker/$FILE1}" > /tmp/aptmirror.test/package
mv /tmp/aptmirror.test/package /etc/apt/sources.list.d/official-package-repositories.list
# If the old xorg version and user DOES have sources list enabled
elif [ ! -f /usr/share/doc/xserver-xorg-lts-vivid/copyright ] && [ -f /etc/apt/sources.list.d/official-source-repositories.list ]; then
# File editing magic here that is perfect for when precision matters.
# Ref: http://unix.stackexchange.com/a/164284
FILE1=$(</tmp/aptmirror.test/test)
FILE2=$(</usr/share/uberstudent/uberstudentsources/pre-heraclitus/official-package-repositories.list)
FILE3=$(</usr/share/uberstudent/uberstudentsources/pre-heraclitus/official-source-repositories.list)
echo "${FILE2//marker/$FILE1}" > /tmp/aptmirror.test/package
echo "${FILE3//marker/$FILE1}" > /tmp/aptmirror.test/source
mv /tmp/aptmirror.test/package /etc/apt/sources.list.d/official-package-repositories.list
mv /tmp/aptmirror.test/source /etc/apt/sources.list.d/official-source-repositories.list
#
# 4.3
#
# If the new xorg version and user DOES NOT have sources.list enabled, we add heraclitus-backports to the default apt configs
elif [ -f /usr/share/doc/xserver-xorg-lts-vivid/copyright ] && [ ! -f /etc/apt/sources.list.d/official-source-repositories.list ]; then
# File editing magic here that is perfect for when precision matters.
# Ref: http://unix.stackexchange.com/a/164284
FILE1=$(</tmp/aptmirror.test/test)
FILE2=$(</usr/share/uberstudent/uberstudentsources/heraclitus/official-package-repositories.list)
echo "${FILE2//marker/$FILE1}" > /tmp/aptmirror.test/package
mv /tmp/aptmirror.test/package /etc/apt/sources.list.d/official-package-repositories.list
# If the new xorg version and user DOES have sources.list enabled
elif [ -f /usr/share/doc/xserver-xorg-lts-vivid/copyright ] && [ -f /etc/apt/sources.list.d/official-source-repositories.list ]; then
# File editing magic here that is perfect for when precision matters.
# Ref: http://unix.stackexchange.com/a/164284
FILE1=$(</tmp/aptmirror.test/test)
FILE2=$(</usr/share/uberstudent/uberstudentsources/heraclitus/official-package-repositories.list)
FILE3=$(</usr/share/uberstudent/uberstudentsources/heraclitus/official-source-repositories.list)
echo "${FILE2//marker/$FILE1}" > /tmp/aptmirror.test/package
echo "${FILE3//marker/$FILE1}" > /tmp/aptmirror.test/source
mv /tmp/aptmirror.test/package /etc/apt/sources.list.d/official-package-repositories.list
mv /tmp/aptmirror.test/source /etc/apt/sources.list.d/official-source-repositories.list
fi
sleep 1
# Silently update apt with the new mirror.
apt-get update > /dev/null
sleep 1
# Notify the user.
FILE=$(</tmp/aptmirror.test/test)
notify-send -u critical -t 0 --icon /usr/share/pixmaps/uberstudentupdate.png "SOFTWARE MIRRORS HAVE BEEN UPDATED!" \
"<b>The fastest possible mirror was determined to be:</b>
• $FILE
<b>The new mirror has been set into your apt software configurations.</b>
<b>You must click this message to dismiss it.</b>"
# Clean up the temp file(s) we made.
if [ -d /tmp/aptmirror.test ]; then
rm -rf /tmp/aptmirror.test
fi
if [ -s /tmp/test.idx ]; then
rm /tmp/test.idx
fi
if [ -f /tmp/aptmirrortest/age ]; then
rm /tmp/aptmirrortest/age
echo "This file is needed by apt-mirror-test. Please leave it here." > /tmp/aptmirrortest/age
fi
fi
else
# If either /tmp/aptmirrortest/age is younger than 30 days old or there is no decent internet connection,
# we pass the whole script to do nothing, the ":". Comment in echo and comment out : for CLI testing.
:
# echo "Mirror is younger than 30 days old, so doing nothing."
fi
The script apt-mirror-check is set in /etc/sudoers.d/ to run without root requirements, something neccesary in this case.
What do you think about these additions? It works without flaw on my system, but I worry that this might be too intrusive.