Ideas and Code

giovedì 6 agosto 2009

Tomcat on EC2 hot deployment

I'm running a simple application on Amazon EC2 on a single Tomcat and I have been looking for a while for the best way to deploy updates.

I'm builing the application with Maven 2 and so I first I considered using the Cargo plugin to connect with a running Tomcat Manager. It didn't work for me: the Tomcat Manager stops and undeploys the application before starting receiving the new war file. In my case I was waiting almost 2 minutes to see my new war up and running (2 minutes in which the application is not reachable). I considered that too long.

I ended up uploading the application with scp and then moving it in place in the running Tomcat.

1. Create this script on the server. hotdeploy.sh
#!/bin/sh
TOMCATWEBAPPS=/var/local/tomcat-buzzbox/webapps
chown tomcat:tomcat ROOT.war

if [ -f $TOMCATWEBAPPS/ROOT.war ]; then
cp -p $TOMCATWEBAPPS/ROOT.war ROOT.war.previous
fi

cp -p ROOT.war $TOMCATWEBAPPS/ROOT.war


It just move the war file in tomcat/webapps, while tomcat is running

2. Make sure your Tomcat is configured for auto deploy (in server.xml)
<Host name="localhost"  appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">


3. Create this script on your local machine (or development environment). deploy.bat - I'm using windows and cygwin.
echo off
echo Overwrite App with new version. Are you sure?
echo -- press any key to continue; CNTR+C to cancel

pause

scp -i keypair1.pem ROOT.war root@domain.com:/root/ROOT.war
ssh -i keypair1.pem root@domain.com 'cd /root/bb-deploy; ./hotdeploy.sh'


That is: scp to move your war file and ssh to run the hostdeploy.sh script on the server. Tomcat auto deploys does the rest.

That's all. It works great for me. Only few seconds of downtime.
What do you think? How would you deploy a war in such an environment?

Nessun commento:

Posta un commento