Friday, July 6, 2012

Installing SABnzbd

Here are some basic instructions for installing SABnzbd on your NAS. As with the other guides on this blog, they are specific to Open Media Vault on my HP N40L, but will probably work for any Debian-based linux distribution.

The simplest approach is probably to follow the instructions on the SABnzbd website, taking advantage of the package put together by JCFP. But I wanted to do a manual installation... partly so I can customise the install to my personal preferences, and partly so I can upgrade if/when I want, without waiting a new package. Also, it's been a learning experience.

As with other packages I've installed, I'll be installing to the directory /opt and will configure the software to run using a dedicated user (for security purposes). I'm also assuming you'll be logged into OMV with a non-root user that has sudo access.

Step 1. Take Care of Dependencies


The SABnzbd github site has a list of dependencies, both optional and mandatory. If you've already installed other python-based software, like Sick Beard, chances are you already have some of these packages installed. Not that it matters, since apt will work it all out for you.

sudo apt-get install python python-cheetah python-openssl python-yenc par2 unzip

Step 2. Download and unpack SABnzbd


Make sure you have changed directory to somewhere with write access.

cd /tmp

We'll use wget to download the package;

wget http://sourceforge.net/projects/sabnzbdplus/files/sabnzbdplus/0.7.1/SABnzbd-0.7.1-src.tar.gz

Now unpack it with tar;

tar xvf SABnzbd-0.7.1-src.tar.gz

We're going to install SABnzbd to the /opt directory. If it doesn't already exist, create it and then mv the extracted package there;sudo mv SABnzbd-0.7.1/ /opt/sabnzbd/Congratulations, SABnzbd is now essentially "installed". There are still a few things we'll want to do in order to get it running nicely.

Step 3. Create a dedicated user for running SABnzbd


Instead of running SABnzbd under our own username, or even worse under root, we will create a dedicated user for SABnzbd to run. There are a few reasons why this is a good idea, not the least of which is for security purposes.

sudo useradd sabnzbd
sudo usermod -d /opt/sabnzbd sabnzbd
sudo chown -R sabnzbd:sabnzbd /opt/sabnzbd


These commands add a new user, and then set the "home" directory for the user to be /opt/sabnzbd (ie, the installation directory). SABnzbd automatically uses the home directory to store any configuration files, in a hidden subdirectory called .sabnzbd (note the fullstop). There are pros and cons to having configurations kept in the same directory as the install. It will be a little bit easier when it comes to backups, since everything should be maintained in the one place. But you need to be careful not to accidentally overwrite the configuration directory when upgrading.

Lastly, the chown command changes ownership of the directory from root to our new user.

Step 4. Test SABnzbd


You'll probably want to edit the config file for SABnzbd. Most configurations can be handled through the GUI, but you may need to directly edit the config file in order to set the IP of your server before you can get this running.

nano /opt/sabnzbd/.sabnzbd
Find the setting labelled "host" and enter the IP address of your machine running SABnzbd. The default port is usually 8080, which will typically suffice. But if you like, you can change it here.

At this point, you can test your installation by trying to run it with the newly created user.

sudo -u sabnzbd /usr/bin/python SABnzbd.py

Point your favourite web browser to http://<<YourServerIP>>:8080. Check out your installation and make sure it is working. While you are looking around, go over to Config->General and find your API key. Take a note of this, because we'll need it for the next section.

Step 5. Configure SABnzbd to start automatically


Running things manually is all very well and good, but if your server restarts (or even worse, crashes) you'll want SABnzbd to startup automatically on boot. We can do this by creating a startup script. There are a number of startup scripts floating around the internet that we could use, but we'll use the one available on the SABnzbd site here. It's not very sophisticated, but should work.

sudo nano /etc/init.d/sabnzbd

Cut and paste the script into your nano session, and then edit it accordingly. Find the line;

/usr/bin/sudo -u sabuser -H /usr/local/src/SABnzbd/SABnzbd.py -d -f /home/sabuser/.sabnzbd/sabnzbd.ini

And replace it with;

/usr/bin/sudo -u sabnzbd -H /opt/sabnzbd/sabnzbd.py -d -f /opt/sabnzbdb/.sabnzbd/sabnzbd.ini

Also, find the line;

/usr/bin/wget -q --delete-after "http://HOSTADDRESS:PORT/sabnzbd/api?mode=shutdown&apikey=ENTERAPIKEYHERE"

And replace HOSTADDRESS, PORT and ENTERAPIKEYHERE with the values from your own system. Your script should be ready to work, now. We can test it;

sudo /etc/init.d/sabnzbd start

This should successfully start the instance of SABnzbd while;

sudo /etc/init.d/sabnzbd stop

should stop it cleanly. Assuming this all works, we just need to instruct the server to run the script on startup and shutdown.

sudo update-rc.d sabnzbd defaults

Everything should now be working nicely.

Addendum


The first time I ran update-rc.d, everything worked nicely. Subsequently, I ran into some issues due to a conflict between the sabnzbd startup script and a script called openmediavault-beep. From what I can gather, these two scripts are both vying to be the last to run on startup and update-rc.d cannot determine their order of precedence. A quick inspection of the script openmediavault-beep, and it looks like this is just designed to send a beep so we could probably just nuke it. But instead, we'll try and fix this by giving update-rc.d a little more guidance.

Let's edit the sabnzbd startup script;

sudo nano /etc/init.d/sabnzbd

Now, at the top of the script, but just below the line marked #!/bin/sh place the following lines;

### BEGIN INIT INFO
# Provides: sabnzbd
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Should-Start: $NetworkManager
# Should-Stop: $NetworkManager
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts instance of SABnzbd
# Description: Simple script to start/stop SABnzbd
### END INIT INFO


I pinched this from the Sick Beard startup. Basically it specifies that SABnzbd should startup after the local and remote filesystems become available, and after the network starts up. This will be before openmediavault-beep. We can now try and re-run update-rc.d;

sudo update-rc.d sabnzbd defaults

And everything should now work. You can reboot your system to test it if you like.