Monday, July 12, 2010

Apt-Cacher-NG Client side perl script

First off a short explanation of Apt-Cacher-NG (acng) is in order. Based on a similar project simply called Apt-Cacher, acng is a download proxy for software packages, primarily for Debian/Ubuntu clients. (http://freshmeat.net/projects/acng/)
The way it works is the server at Community Computer Center, goes out to the official package and release servers and downloads the latest updates, that way when I, or anyone else wants to download updates, our computers simply have to download from the CCC server over the lan, which makes the download about ten times faster.

(Editorial Note: If you haven't set up apt-cacher-ng yet, follow this post first.)

The only downside is whenever I want to download the updates and I'm not at CCC, I have to go and edit the hosts file in /etc, so I wrote a perl script to cut down on the steps.

Copy the code into a text file and save it as a .pl, then to use, type sudo perl yourfile.pl.
While this is not the most elegant solution, it was my first attempt at using perl, enjoy.

#!usr/bin/perl -w
#script by @robots_unix

{
print "Are you at CCC?";
$name = ;
chomp ($name);
if ($name eq "yes"){ #When I'm at CCC
open(INFILE, '>/etc/hosts');
print INFILE "127.0.0.1 localhost
127.0.1.1 uname
apt-cacher_ip apt-cacher
#127.0.0.1 apt-cacher
apt-cacher_ip server name
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts\n";
close(INFILE);
}else{ #When I'm not at CCC
open(INFILE, '>/etc/hosts');
print INFILE "127.0.0.1 localhost
127.0.1.1 uname
#apt-cacher_ip apt-cacher
127.0.0.1 apt-cacher
apt-cacher_ip servername
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts\n";
close(INFILE);
}
}

1 comment:

Jeff Day said...

Neil,

Since I'm in a larger netwoked environment, working with classroom computers, office computers, and so forth, I sometimes have further local customizations to my hosts file which would need to be preserved. So, here's what I would propose as an improvement to your script (although I don't know how to program it in perl...)

Your script should actually parse through the hosts file line-by-line and find the line that contains apt-cacher, replacing that line with the desired line.

If no apt-cacher line is found, I would recommend inserting a new apt-cacher line immediately before the first blank line encountered, which in most situations, will put it in a very good place (that way it isn't mixed down in with the ipv6 entries), or if no blank line is encountered, insert a new line altogether at the end of the file.

Also, you might write a similar tool (or maybe combined into the same tool) to patch the acng.conf, create the ubuntu_security file, and update the sources.list automatically, by handling the necessary search and replaces (but avoiding commented-out lines), which would really make this into a turn-key solution.