Skip to content
calavera edited this page Oct 13, 2010 · 6 revisions

Capistrano scripts

This assumes you have set up init-d-scripts.

Basically, you can use standard capistrano recipes and only override start, stop and restart.

This script assumes you use a staging and production environment, and uses the simplified multi-stage-deployment described in capify.org
It also uses git for scm, and no sudo.

set :application, "app"
set :repository,  "git@your.domain.com:app"
set :user, "www-data"
set :use_sudo, false

set :scm, :git
# If you have different branches, you can specify deploys with BRANCH=branch, defaults to "release"
set :branch, ENV['BRANCH'] || "release"

# Speeds up subsequent deploys
set :deploy_via, :remote_cache
set :deploy_to, '/path/to/app'
set :gateway, 'gateway.your.domain.com'
ssh_options[:forward_agent] = true

# Explicit paths to rake (for migrations)
set :rake, '/usr/local/jruby/bin/jruby -S rake'

default_run_options[:pty] = false

task :production do
  set :rails_env, 'production'
  role :web, "web1", "web2"
  role :app, "web1", "web2", "web3"
  role :db,  "db",
end

task :staging do
  role :web, "sweb1", "sweb2"
  role :app, "sweb1", "sweb2"
  role :db,  "sdb", :primary => true
  set :rails_env, "staging"
end

namespace :deploy do
  desc "Restarting rails"
  task :restart, :roles => :app, :except => { :no_release => true } do
    stop
    start
  end

  desc "Stopping rails"
  task :stop, :roles => :app, :except => { :no_release => true } do
    run "/etc/init.d/trinidad stop"
  end

  desc "Starting rails app"
  task :start, :roles => :app do
    run "/etc/init.d/trinidad start"
  end

  desc "After symlinking current version, install database.yml, assets, and update rights"
  after 'deploy:update_code' do
    %w(database.yml local_environment.rb).each do |c|
      run "ln -s #{deploy_to}/#{shared_dir}/config/#{c} #{current_release}/config/#{c}"
    end

    # Only applicable if you have multiple servers which needs symlinks in /public from their "export" dir
    %w(shared_media_dirs).each do |c|
      run "ln -s /export/shared_files/#{c} #{current_release}/public/#{c}"
    end
  end

  after 'deploy:update_code', 'deploy:web:disable'
  after 'deploy:stop', 'deploy:web:disable'
  after 'deploy:start', 'deploy:web:enable'

  # This is probably needed if you want to show a maintenance page during deploy 
  namespace :web do
    desc "Disable web somehow"
    task :disable, :roles => [:web] do
      # A possible implementation is to let the web server (nginx/apache) show something if a certain file exists
      # run "touch #{deploy_to}/#{shared_dir}/down"
    end

    desc "Disable web somehow"
    task :enable, :roles => [:web] do
      # A possible implementation is to let the web server (nginx/apache) show something if a certain file exists
      # set :downfile, "#{deploy_to}/#{shared_dir}/down"
      # run "if [ -f #{downfile} ]; then rm #{downfile}; fi"
    end
  end
end

Simplified recipe assuming you have trinidad_daemon and trinidad_hotdeploy_extension, based on Passenger’s recipe:

set :application, "myapp"
set :domain,      "example.com"
set :repository,  "ssh://#{domain}/path-to-your-git-repo/#{application}.git"
set :use_sudo,    false
set :deploy_to,   "/path-to-your-web-app-directory/#{application}"
set :scm,         "git"

role :app, domain
role :web, domain
role :db,  domain, :primary => true

namespace :deploy do
  task :start, :roles => :app do
    run "/etc/init.d/trinidad-daemon.sh start"
    # if trinidad is installed as a service the previous line can be replaced by: run "touch #{current_release}/tmp/restart.txt"
  end

  task :stop, :roles => :app do
    run "/etc/init.d/trinidad-daemon.sh stop" # if trinidad is installed as a service this line can be removed
  end

  desc "Restart Application"
  task :restart, :roles => :app do
    run "touch #{current_release}/tmp/restart.txt"
  end
end
Clone this wiki locally