SOHO : Small Office Home Office
Freeware - Opensource software tips, tricks, tweaks & fixes for managing, securing, improving the performance of SOHO Desktop, Laptop, Networks

Saturday, December 26, 2009

How To Set up a repository cache with apt-cacher

When running multiple machine with the same distribution, it is interesting to set up a repository cache on your network so that once a package is downloaded from an official repository, all other machines will download it from your local area network.

Having different machines running the same linux distribution, it becomes interesting to set up a repository cache somewhere on your network. This way, you won't download common packages more than 1 time from official repositories.

Here is the situation, we have one machine called repository-cache, this machine is going to act as the repository cache, basically, any other machines in your network is going to use it as a repository.

1. Getting started

As usual, you need to install the required packages in the first place. So type in a terminal:

$sudo apt-get install apt-cacher apache2

Once this is done, it is time to get into the configuration files in /etc/apt-cacher/apt-cacher.conf

2. Configuring Apt-Cacher

2.1. apt-cacher.conf

So now, open apt-cacher's main configuration file: /etc/apt-cacher/apt-cacher.conf and start editing it according to your settings.

The default port apt-cacher is running on is port 3142. You might want to change this value accordingly to your needs.

allowed_hosts: by default, all host are allowed to use the repository cache. You can change this value if you want to only allow certain host. In my case, I want to allow my LAN 192.168.1.0/24 and localhost (both 127.0.0.1 and 127.0.1.1 on ubuntu boxes), so I changed the value to:

allowed_hosts=192.168.1.0/24, 127.0.1.1

as 127.0.0.1 is always allowed, it was not necessary to add 127.0.0.1

generate_reports: This directive makes apt-cacher create a report on how efficient your cache was on a daily basis. Default is 1, if you want to disable this, set it to 0

path_map: This is an interesting directive. Here you can define different aliases for different repository host. For my ubuntu edgy box, my path_map looks like this:

path_map = debuntu repository.debuntu.org ; ubuntu archive.ubuntu.com/ubuntu; ubuntu-updates archive.ubuntu.com/ubuntu ; ubuntu-security security.ubuntu.com/ubuntu

Let me explain that bit. Here I created mappings from names:

* debuntu to host repository.debuntu.org
* ubuntuand ubuntu-updates to host archive.ubuntu.com/ubuntu
* and ubuntu-security to security.ubuntu.com

Now, in order to access a specific repository, we simply need to append the mapping name to our cache repository server, like: repository_cache_machine:port/mapping_name

So, for instance, we can access debuntu repository through http://repository-cache:3142/debuntu and ubuntu secutiry repository through http://repository-cache:3142/ubuntu-security.

2.2. Activating apt-cacher to start

In order to start, apt-cacher needs to be activated from /etc/default/apt-cacher. So open /etc/default/apt-cacher and set AUTOSTART to 1:

AUTOSTART=1

Now restart apt-cacher:

$sudo /etc/init.d/apt-cacher restart

Now that apt-cacher runs, it is time to update all our clients /etc/apt/sources.list files so every host on the network will use our repository-cache machine.

3. Setting up the Clients and Server sources.list

Now it is time to set up the client hosts apt source list files: /etc/apt/sources.list. It make sense to use the repository cache on the server too, as that way, any updates made by the server will fill up the cache.

Here is the original /etc/apt/sources.list:

#debuntu repository
deb http://repository.debuntu.org edgy multiverse
deb-src http://repository.debuntu.org edgy multiverse

#ubuntu main repository
deb http://archive.ubuntu.com/ubuntu/ edgy main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ edgy main restricted universe multiverse

#ubuntu updates repository
deb http://archive.ubuntu.com/ubuntu/ edgy-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ edgy-updates main restricted universe multiverse

#ubuntu security updates repository
deb http://security.ubuntu.com/ubuntu edgy-security main restricted universe multiverse
deb-src http://security.ubuntu.com/ubuntu edgy-security main restricted universe multiverse

In order to use our cache repository, those entries need to be changed to:

#debuntu repository
deb http://repository-cache:3142/debuntu edgy multiverse
deb-src http://repository-cache:3142/debuntu edgy multiverse

#ubuntu main repository
deb http://repository-cache:3142/ubuntu edgy main restricted universe multiverse
deb-src http://repository-cache:3142/ubuntu edgy main restricted universe multiverse

#ubuntu updates repository
deb http://repository-cache:3142/ubuntu-updates edgy-updates main restricted universe multiverse
deb-src http://repository-cache:3142/ubuntu-updates edgy-updates main restricted universe multiverse

#ubuntu security updates repository
deb http://repository-cache:3142/ubuntu-security edgy-security main restricted universe multiverse
deb-src http://repository-cache:3142/ubuntu-security edgy-security main restricted universe multiverse

Cool, now, every host should be able to retrieve the .deb packages from our repository cache once:

$sudo apt-get update

has been ran on every host.

4.Use as a proxy to APT
Static configuration

In a terminal, type:

sudo nano /etc/apt/apt.conf.d/01proxy

Inside your new file, add a line that says:

Acquire::http::Proxy "http://:3142";

"Roaming" mode

This method is useful if you are alternating between office and home with a laptop for example. It involves using the ping command to determine if the apt-cacher server is available at boot-time and then configure the APT proxy or not.

* Open /etc/rc.local (alt-F2, "gksu gedit /etc/rc.local")
* Change the top from "#!/bin/sh -e" to "#!/bin/bash"
* put this near the end (before "exit 0" if present) replacing "SERVER_NAME_HERE" with your server's resolvable name or it's IP:

. /lib/lsb/init-functions
log_daemon_msg "Configuring APT cache proxy" "(based on SERVER_NAME_HERE's presence...)"
ping -c 1 SERVER_NAME_HERE &> /dev/null
if [ $? = "0" ]; then
echo "Acquire::http::Proxy \"http://SERVER_NAME_HERE:3142\";" > /etc/apt/apt.conf.d/01SERVER_NAME_HEREproxy
else
rm /etc/apt/apt.conf.d/01SERVER_NAME_HEREproxy &> /dev/null
fi
log_end_msg 0

5. Importing existing package from /var/cache/apt/archives/ to apt-cacher repository

It might happen that your server already got a whole lot of packages cached in its local repository: /var/cache/apt/archives/. apt-cacher offers a tool to import those files to apt-cacher repository.

There is a whole lot of usefull script that can be found in /usr/share/apt-cacher/. The one we are interested in here is apt-cacher-import.pl. To import deb files from /var/cache/apt/archives to apt-cacher repository run:

$sudo /usr/share/apt-cacher/apt-cacher-import.pl /var/cache/apt/archives

This must be run as user root or .deb files might not be copied to the cache repository

Now, the directory /var/cache/apt-cacher/packages/ should be filled up with a whole bunch of packages.

6. Getting report usage of your repository cache

If you left the directive generate_reports set to 1, apt-cacher will generate report on the usage of the cache every day.

You will be able to access it at the address: http://repository-cache:3142/report

.

If you need to regenerate the report, run: $sudo /usr/share/apt-cacher/apt-cacher-report.pl

7. Conclusion

apt-cacher is an easy and efficient package which will save you both time and bandwidth when using multiple machines with the same distribution like it could happen in a home network or in at a company.


Sources :
1. http://www.debuntu.org/how-to-set-up-a-repository-cache-with-apt-cacher
2. https://help.ubuntu.com/community/Apt-Cacher-Server

No comments:

Post a Comment