Email notifications or displaying the build pipeline on monitors are some of the ways to know the build status and whether or not something failed. What about adding a hamster wheel? It is both visual, audible and fun. This post will help you get started. Let’s name this hamster Whisker and get it setup to run on Jenkins.

Run Whisker run…

Where to start?

Get the team a USB hamster wheel. A friend of mine got one of these as a gift but never used it. It will not work without the right drivers. They are available on Amazon: USB Hamster Wheel. This one in particular is meant to make the hamster run by typing – the faster one types, the faster it runs.

Make Whisker run

If you are running on Linux, then the easiest way is to take a look at this project: Hamster Wheel. Follow the README file which details the steps – ensure that the hamster can run before moving on to the next steps. One of the prerequisites is to have libusb, to install the package on Ubuntu the following can be run:

 
sudo apt-get update
sudo apt-get install libusb-dev

Alternatively, to use a cross platform solution, check out libusb as a way to access USB devices. It also uses C.

Don’t forget to make sure the device has batteries.

Expose a REST service

Ubuntu versions after 14.04 LTS should have Python pre-installed, check the Ubuntu wiki on Python for more details. Let’s create a script in Python using Flask to expose a service which can be called from your Jenkins server.

 
#!/usr/bin/env python

from subprocess import call
from flask import Flask

app = Flask(__name__)

@app.route('/whisker/run')
def whisker_run():
    try:
        call("sudo ~/hamsterwheel/hamster long", shell=True)
        print "Whisker is exercising..."
        return '1'
    except:
        print "Whisker got tired. Please check that it has food and H20."
        return '0'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8082)

The subprocess module will be used to make a system call to the hamster command.

Side note: whenever using shell set to true, remember there are security considerations to avoid shell injection. The python 2 docs have an excellent example on this: ‘Frequently Used Arguments

Run the process in the background

Ensure there is no hangup when running the script and redirect the standard err and output to a nohup.out file instead of on the terminal. The process will continue in the background even after your SSH session has closed – if the server is restarted then it will need to be run again.

nohup ./whisker.py &

Call the service

http://{HOST}:8082/whisker/run

Setup post build job on failure

In Jenkins, depending on your setup you can either call this in groovy as part of the build flow when something goes wrong. You could add another job that calls this service via a script as part of a post build job on failure.

if `curl -m 3 http://{HOST}:8082/whisker/run` ; then
    echo 'Run Whisker run...'
else
    exit 0
fi

Further links:

nohup and &

Leave a comment

Your email address will not be published. Required fields are marked *