Running Capistrano with Passenger (mod_rails)

December 30th, 2008 at 2:45 pm • permalink2 comments

Capistrano logoThe following recipe enables you to deploy on a Passenger (mod_Rails) server with Capistrano. It’s a ready to use boilerplate, you just need to store it somewhere in your rails project (for example in the config/deploy folder) and require it from your deploy.rb file.

#
# = Capistrano Passenger deploy tasks
#
# Provides tasks for deploying a Rails application with Passenger (aka mod_rails).
#
# Category::    Capistrano
# Package::     Passenger
# Author::      Simone Carletti
# Copyright::   2007-2008 The Authors
# License::     MIT License
# Link::        http://www.simonecarletti.com/
# Source::      http://gist.github.com/2769
#
#

unless Capistrano::Configuration.respond_to?(:instance)
  abort "This extension requires Capistrano 2"
end

Capistrano::Configuration.instance.load do

  namespace :passenger do

    desc <<-DESC
      Restarts your application. \
      This works by creating an empty `restart.txt` file in the `tmp` folder
      as requested by Passenger server.
    DESC
    task :restart, :roles => :app, :except => { :no_release => true } do
      run "touch #{current_path}/tmp/restart.txt"
    end

    desc <<-DESC
      Starts the application servers. \
      Please note that this task is not supported by Passenger server.
    DESC
    task :start, :roles => :app do
      logger.info ":start task not supported by Passenger server"
    end

    desc <<-DESC
      Stops the application servers. \
      Please note that this task is not supported by Passenger server.
    DESC
    task :stop, :roles => :app do
      logger.info ":stop task not supported by Passenger server"
    end

  end

  namespace :deploy do

    desc <<-DESC
      Restarts your application. \
      Overwrites default :restart task for Passenger server.
    DESC
    task :restart, :roles => :app, :except => { :no_release => true } do
      passenger.restart
    end

    desc <<-DESC
      Starts the application servers. \
      Overwrites default :start task for Passenger server.
    DESC
    task :start, :roles => :app do
      passenger.start
    end

    desc <<-DESC
      Stops the application servers. \
      Overwrites default :start task for Passenger server.
    DESC
    task :stop, :roles => :app do
      passenger.stop
    end

  end

end

This extension creates the following Capistrano tasks under the passenger namespace:

  1. start
  2. stop
  3. restart

Additionally, it overwrites the default Capistrano deploy:start/stop/restart tasks to fully integrate Passenger into your current deployment strategy.

Please note that, due to Passenger architecture, only restart task is supported. start and stop task are available only for compatibility purpose. Invoking deploy:start or deploy:stop will simply return a warning.

The most recent version of this recipe is available as a Gist (#3102).

  1. Capistrano: Executing a command as root without using sudo
  2. How to restart God when you deploy a new release via Capistrano
  3. Capistrano: Managing an uploads folder
  4. Capistrano and database.yml
  5. Capistrano: File Transfer actions

Filed in Programming • Tags: , , , ,

Comments

Mike says:

Does the cap deploy work with mod_rails? Touching the restart.txt file in a NEW directory (as this script does) would seem to me to confuse the mod_rails module – is it smart enough to drop the old rails environment and use the new one under the new release directory?

Yes, it does.

This script touches the restart.txt file in the /current directory. By Capistrano convenction, current is a symlink to the most recently deployed application instance and this is the folder configured in your Apache virtual host.

mod_rails requires the restart.txt file to be stored at tmp/restart.txt path from the document directory and this is exactly what this script does.

Add a Comment




Follow Me
    Random Quote