Tuesday, May 8, 2012

Deploying app with upstart utility




 

 

 Deploying web application with Upstart


Upstart seems like a really cool project to replace the old init.d scripts with an event-based system. However, I've found working with it to be pretty frustrating. Events that I expected to fire don't, and its difficult to debug. After a bit of digging around and plenty of trial and error I've managed to come up with a script that works on Lucid (and Karmic with a few tweaks). So, for my own reference, and in the hope that I spent investigating through Google searches, here is my process for deploying a web application with upstart (in my case i taken Alfred Ticketing web application):

If you don't have one already, I recommend creating a user to run your applications as. The following command will create a user called 'alfred':
# sudo adduser --system --shell /bin/bash --gecos 'user for running web application' --group --disabled-password --home /home/alfred alfred


We can then create an upstart script to start the Alfred web application ticketing server. Upstart scripts go in /etc/init on Ubuntu. Create the file /etc/init/alfred.conf (replacing alfred with a more sensible name for your application), and add the following:
sudo nano /etc/init/alfred.conf 

copy following con-fig in alfred.conf

####################################################################################################
description "Alfred web application"
author "Ajit"

start on (local-filesystem and net-device-up IFACE=eth0)
start on startup
stop on shutdown

respawn

exec sudo -u alfred sh -c "/usr/bin/python /home/alfred/webapp/main.py runserver 0.0.0.0:8888 >> /home/alfred/webapp/alfred.log2>&1”

####################################################################################################

The 'start on' line tells upstart to run the script when local file-systems are available and the network is up (you may want to change eth0 to a different device if you're crazy enough to run a server over wireless).
If you're using Karmic, the above 'start on' line did not work in my tests. I was able to get the script running by using: 'start on run-level [2345]' instead.
Also, note that Ubuntu boots without waiting for /home to be mounted, so I don't advise storing your application there, put it somewhere in /opt or /var instead. It should be possible to force Ubuntu to wait for /home to become available by adding the boot-wait option to your /home partition in /etc/fstab.However, I don't recommend this as it has been associated with bugs in Karmic which may stop the computer from booting. Adding this has never worked for me in Lucid either, perhaps because of an encrypted /home partition. Thankfully /var/local/sites is a much more sensible to put your applications anyway ;)
I've seen a number of examples showing this line as:

start on startup

If you are not on Ubuntu, or are on a newer version than Lucid you might wish to try this. However, I have never managed to get this event to fire.
The re-spawn line tells upstart to run this script again should the process exit unexpectedly and the final line runs the application (in this example its main.py). 

The application is run as the user 'alfred' and logs std-out and std-error to /home/alfred/webapp/alfred.log

Next, I checkout/clone/copy my application to /home/alfred/webapp/


sudo mkdir -p /home/alfred/webapp/ 
 
sudo chown alfred /home/alfred/webapp/


cd /home/alfred/webapp/

if you have your web on git-hub then fetch as follow
sudo -u alfred git clone /path/to/alfred_repository.git .
OR 
Copy your Web-application with win-scp  or scp or pen-drive to your web-server

Then, make sure the log files have the correct permissions:
 
sudo touch /home/alfred/webapp/alfred.log
 
sudo chown alfred:alfred /home/alfred/webapp/alfred.log

We're now ready1 to test the upstart script:
 
$ sudo start alfred
alfred start/running, process 1234

$ sudo restart alfred
alfred start/running, process 1234
 
$ sudo status alfred
alfred start/running, process 1234

The second command ensures that the server didn't immediately die after starting. If you're seeing output similar to the above, you can now try accessing your server to 

see if its running OK:
 
$ curl localhost:8888
 OR
Open Browser and open localhost:8888
here the port no is listening port no of your web application 


If that all looks sensible, lets try stopping the server:
 
$ sudo stop alfred
alfred stop/waiting

If you've got this far then well done! Unfortunately, the most-likely-to-break part is yet to come... getting the script to run on startup! Try restarting and see if your application is running once the machine is back up. 

If you have any problems, take a look at my advice above regarding the 'start on' line and where to store your application files. Failing that, you should be able to take a look at the logs for your application at /home/alfred/webapp/alfred.log.

Once you've got a template that works, deploying with upstart is a breeze. Good luck



No comments:

Post a Comment