From the project
Finding the closest APT package archive using GeoIP and indexing
In preparation for TurnKey's upcoming release based on Ubuntu Lucid 10.04 LTS, we are knocking off todo list items. One of them is code-named auto-apt-archive. As you can guess from its name, the objective is to configure the closest APT package archive mirror, automatically, without user intervention. It does this by leveraging a new GeoIP service provided by the TurnKey Hub.
By using the closest archive, it is usually much faster, will lessen the load on Ubuntu's main package archive which has been the default up until now, and in certain circumstances, cheaper (for example, bandwidth within Amazon EC2 regions is free).
BTW, TurnKey EC2 builds already include a similar optimization, which leverages ec2metadata to get its associated region and construct the URL for the region specific Ubuntu APT archives.
The new auto-apt-archive solution will replace the old Amazon EC2 adhoc solution, but will also be included in all TurnKey builds, whether it be bare-metal, virtual machines, VPS's or cloud deployment.
So how does it work
Firstly, you might recall a post I made last month, with the somewhat similar title Finding the closest data center using GeoIP and indexing. The GeoIP implementation details are similar, so I won't repeat them here.
For those interested in how auto-apt-archive works, it goes something like this:
On firstboot, auto-apt-archive is called by an inithook, which contacts the Hub requesting the closest Ubuntu APT package archive, and updates APT sources lists accordingly.
The Hub looks up the requesting IP address using GeoIP to find the associated country code which is used in the archive URL.
Ubuntu have implemented a wildcard domain configuration for the archive mirrors, making the URL construction really simple. In the case that there is no local APT archive in your country, you will be routed to Ubuntu's main package archive. When one does become available, you'll automatically be routed there.
http://$CC.archive.ubuntu.com/ubuntu
http://$REGION.archive.ubuntu.com/ubuntu
http://ftp.$CC.debian.org/debian
Polipo is a very lightweight, well designed and generic proxy/cacher. I've been using it for both regular file downloading caching as well as APT caching and has been performing very nicely.