Skip to content

Instantly share code, notes, and snippets.

@nikvdp
Last active August 29, 2015 14:01
Show Gist options
  • Save nikvdp/99f494edb08e2317f62d to your computer and use it in GitHub Desktop.
Save nikvdp/99f494edb08e2317f62d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
##################################################################################
# ubuntu_auto_netselector.sh #
# #
# This script will automatically get list of ubuntu mirrors, choose the fastest #
# one and optionally modify your /etc/apt/sources.list to use it. #
# #
##################################################################################
ubuntu_dist="$(cat /etc/lsb-release | grep DISTRIB_CODENAME | sed s/.*=//)"
timestamp=$(date "+%s")
tmpfile="/tmp/$timestamp"
#TODO: Need to download correct version of netselect based on arch, assuming x64 for now
# install netselect if not present
if ! which netselect > /dev/null 2>1; then
echo "No netselect found, installing"
curl -L http://ftp.us.debian.org/debian/pool/main/n/netselect/netselect_0.3.ds1-25_amd64.deb > netselect.dpkg
sudo dpkg -i netselect.dpkg
else
echo Netselect already here, moving on
fi
# # from http://askubuntu.com/questions/39922/how-do-you-select-the-fastest-mirror-from-the-command-line/141536#141536
echo Gathering server list, please wait up to 2 minutes...
sudo netselect 2>/dev/null -v -s10 -t20 `wget -q -O- https://launchpad.net/ubuntu/+archivemirrors | grep -P -B8 "statusUP|statusSIX" | grep -o -P "(f|ht)tp.*\"" | tr '"\n' ' '` | grep -E '(\.\.)|( [0-9]{2,} (ht|ft))' | tee $tmpfile
new_mirror=$(cat $tmpfile | head -1 | awk '{print $2}')
cat <<EOF
Fastest mirror is [$new_mirror]!
Going to Update /etc/apt/sources.list? (old sources.list will be backed up to /etc/apts/sources.list.bak)
Proceed (y/n)?
EOF
read answer
if [ "$answer" != "y" ]; then
echo "Exiting with no changes, please manually update mirror to [$new_mirror]."
exit 1
fi
echo Backing up /etc/apt/sources.list to /etc/apt/sources.list.bak.$timestamp
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak.$timestamp
tmpfile2=$tmpfile.2
# TODO: Need to read in existing /etc/apt/sources.list, detect the mirror in use
# and use sed to change those to $new_mirror instead of blindly overwriting file
cat <<EOF > $tmpfile2
## Note, this file is written by cloud-init on first boot of an instance
## modifications made here will not survive a re-bundle.
## if you wish to make changes you can:
## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
## or do the same in user-data
## b.) add sources in /etc/apt/sources.list.d
## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl
#
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb $new_mirror $ubuntu_dist main
deb-src $new_mirror $ubuntu_dist main
## Major bug fix updates produced after the final release of the
## distribution.
deb $new_mirror $ubuntu_dist-updates main
deb-src $new_mirror $ubuntu_dist-updates main
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb $new_mirror $ubuntu_dist universe
deb-src $new_mirror $ubuntu_dist universe
deb $new_mirror $ubuntu_dist-updates universe
deb-src $new_mirror $ubuntu_dist-updates universe
## Uncomment the following two lines to add software from the 'backports'
## repository.
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
# deb $new_mirror $ubuntu_dist-backports main restricted universe multiverse
# deb-src $new_mirror $ubuntu_dist-backports main restricted universe multiverse
## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu $ubuntu_dist partner
# deb-src http://archive.canonical.com/ubuntu $ubuntu_dist partner
deb http://security.ubuntu.com/ubuntu $ubuntu_dist-security main
deb-src http://security.ubuntu.com/ubuntu $ubuntu_dist-security main
deb http://security.ubuntu.com/ubuntu $ubuntu_dist-security universe
deb-src http://security.ubuntu.com/ubuntu $ubuntu_dist-security universe
# deb http://security.ubuntu.com/ubuntu $ubuntu_dist-security multiverse
# deb-src http://security.ubuntu.com/ubuntu $ubuntu_dist-security multiverse
EOF
sudo cp $tmpfile2 /etc/apt/sources.list
#clean up tmpfiles
rm $tmpfile
rm $tmpfile2
echo "/etc/apt/sources.list updated to use mirror [$new_mirror]. Update apt-get cache?"
read answer
if [ "$answer" != "y" ]; then
cat <<EOF
Exiting, please run
apt-get update
manually to activate mirror [$new_mirror].
EOF
exit
fi
sudo apt-get update
if [ $? -eq 0 ]; then
echo "Apt-cache updated succesfully"
else
echo "There was a problem updating the cache, reverting to original /etc/apt/sources.list, please edit /etc/apt/sources.list to [$new_mirror] manually"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment