title: Rolling Deployment of Java/Tomcat application using Capistrano
body: <p>I am using Capistrano 2.15.5 to deploy Java web applications to Tomcat application servers. For the time being, we are tied and committed to this version of Capistrano and we cannot upgrade to v3. The deploy task will call on the other tasks to get executed in parallel on all servers in the role. This works as expected and has been working well for a while. </p>
  
  <p>Now we need to implement a <em>Rolling Deployment</em> when the tasks are executed on one server at a time. So, based on the code below, we would execute <code>shutdown_tomcat, download_bits, deploy_bits, start_tomcat</code> on <code>prdapp01</code> first and then <code>prdapp02</code> and so on... So that only one server in the cluster is offline at any given time, thus maximizing our uptime. </p>
  
  <p>What is the best way to implement this <em>Rolling Deployment</em> strategy with Capistrano? </p>
  
  <p>This is what a paired-down version of my Capistrano script looks like currently:</p>
  
  <pre><code>task :production do
    role :app,   "deployuser@prdapp01", "deployuser@prdapp02", "deployuser@prdapp03"
    # ...
  end
  
  task :deploy do
    shutdown_tomcat
    download_bits
    deploy_bits
    start_tomcat
  end
  
  task :shutdown_tomcat, :roles =&gt; :app do
    run "sudo /sbin/service tomcat stop || exit 1;"
  end
  
  task :download_bits, :roles =&gt; :app do
    run &lt;&lt;-EOS
      [ -f /tmp/app_download ] &amp;&amp; rm -rf /tmp/app_download;
      mkdir -p /tmp/app_download;
      cd /tmp/app_download;
      wget -q https://internal.server/path/to/application.war || exit 1;
    EOS
  end
  
  task :deploy_bits, :roles =&gt; :app do
    run &lt;&lt;-EOS
      cd /tmp/app_download;
      unzip -q -o /tmp/app_download/application.war -d /usr/local/tomcat/webapps/ || exit 1;
      exit 0;
    EOS
  end
  
  task :start_tomcat, :roles =&gt; :app do
    run "sudo /sbin/service tomcat start || exit 1;"
  end
  </code></pre>