PyPI Mirror on Ubuntu machine
This post will contains some details howto create a PyPI mirror on an Ubuntu machine. I write this post because I run into some problem with an cronjob setup.
Setup the mirror
You should use the bandersnatch scripts to sync your mirror from the master server. It works much better than the pep381run script. I followed the official guide from the PyPI site.
I recommend to use a python virtualenv to keep your system clean.
# Setup virtualenv to /opt virtualenv /opt/mirror-pypi # Switch to virtualenv source /opt/mirror-pypi/bin/activate
pip. It also handle the requirements for
pip install bandersnatch
Issue with cronjob
The default shell for cronjobs in Ubuntu is
/bin/sh which is a symlink to
/bin/dash. The documentation of bandersnatch contains an howto setup a cronjob.
The problem is, it's using a
bash function with piping to logger:
# Will not work in dash or sh bandersnatch mirror |& logger -t bandersnatch[mirror]
To fix the problem switch to
bash as default cronjob shell. Modify
/etc/crontab and change the SHELL variable:
# Replace SHELL from /bin/sh to /bin/bash SHELL=/bin/bash
Now it's time to setup the cronjob which using the virtualenv:
# Modify crontab crontab -e # Add the following line to run every hour 0 * * * /opt/mirror-pypi/bin/python /opt/mirror-pypi/bin/bandersnatch mirror |& logger -t bandersnatch[mirror]
Monitoring mirror status
Because of the cronjob issue I created a monitoring script for nagios. This
script check the
last-modified file created by
bandersnatch. It's a simple bash script
which covert every date to UTC and unix timestamp to check the age of the
It's build for Nagios and using the
utils.sh script from it. Move the file to
/usr/lib/nagios/plugins or maybe any other plugins folder which contains