~drscream
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.
Use virtualenv:
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
Install via pip
:
Simple install bandersnatch
via pip
. It also handle the requirements for
you.
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
mirror.
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
utils.sh
.
Send your comment by mail.
Tue 11/08/15, 5:32 pm
I came across your Nagios script for monitoring Bandersnatch to ensure your mirror is staying updated. That’s perfect for what I need. Can you tell me what url you point your script to? I know it’s “URL to the last-modified file”, but I’m not sure what URL that would be. Just pick a specific package URL to monitor?
/
Tue 11/08/15, 6:18 pm
Ah nm, figured it out. I didn’t realize there was an actual file called last-modified in the web root J. Great script – The github link to it was dead, but you should add it back there.
/
Tue 11/08/15, 10:13 pm
I will also do my best to update the GitHub link as soon as possible. I hope the script is working for you.
/