If (like me) you find yourself re-installing your machines a lot (and let's face it, that's what Foreman is for), then a package cache can save you a huge amount of data traffic. There are many ways to achieve this, but I'll describe how I'm using apt-cacher-ng to help me save data.

Apt-cacher-ng is particularly helpful since it can support almost any OS - the documentation has instructions for Debian, Ubuntu, Fedora, CentOS, OpenSUSE, and even Archlinux. Today I'll focus on Debian & Ubuntu, but I have plans for an upcoming article on Foreman & Archlinux, so stay tuned if that's of interest to you.

Installation

Since I run my Foreman installation on Debian, I can just get it from the repository:


apt-get install apt-cacher-ng


on your Foreman box, and then go to http://foreman:3142 to check it's running. You can tail /var/log/apt-cacher-ng/apt-cacher.log to watch it work.

Usage

There are a number of ways to configure Apt to use a package cache. I'll describe two. Firstly, you can edit your source list (/etc/apt/sources.list or files in sources.list.d) and replace all the servers with your proxy. So a line like:

deb http://ftp.uk.debian.org/debian/ squeeze main non-free contrib

becomes

deb http://foreman:3142/debian/ squeeze main non-free contrib

That's messy though - the easier way is to add the proxy to the Apt preferences:

echo 'Acquire::http::Proxy "http://foreman:3142/";' >> /etc/apt/apt.conf

Foreman

All that is easy enough, and well documented elsewhere on the web. Let's get to our favourite topic: Foreman!

Tweaking Foreman is easy enough. Firstly go to More -> Provisioning Templates and edit the provisioning template for your machines. That would be Preseed Default if your using the out-of-the box Foreman installation. Find the mirror/http/proxy line and add your proxy, so my complete line looks like


d-i mirror/http/proxy string http://foreman:3142/


It's quite possible to use different proxies for different hosts, for example, using a parameter:


d-i mirror/http/proxy string <%= @host.params['proxy_string'] %>


Obviously this will differ with Kickstart, but I've no experience there.

You'll also need to modify your finish script (Preseed Default finish by default) - there is a wget call to Foreman at the end which fails due to the presence of proxy data. Add --no-proxy to fix this. For example:


/usr/bin/wget --no-proxy --quiet --output-document=/dev/null --no-check-certificate <%= foreman_url %>


That's all you need - if you tail your cache log as above and kick off a new Debian build, you should see it hitting the proxy.

Ubuntu tweak on Debian apt-cacher-ng

I discovered that using apt-cacher-ng on Debian didn't quite set up Ubuntu quite right - I had to do this:


echo "http://gb.archive.ubuntu.com/ubuntu/" > /etc/apt-cacher-ng/backends_ubuntu


I then restarted apt-cacher-ng and all was well.