Skip to content
No description or website provided.
Ruby Shell JavaScript CoffeeScript
Find file
Pull request Compare This branch is 1 commit ahead of railscasts:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
blog-after
blog-before
README.md

README.md

RailsCasts Episode #337: Capistrano Recipes

http://railscasts.com/episodes/337-capistrano-recipes

Requires Ruby 1.9.2 or higher.

ssh root@72.14.183.209
adduser deployer --ingroup admin
exit
cap deploy:install
cap deploy:setup
cap deploy:cold

config/deploy.rb

require "bundler/capistrano"

load "config/recipes/base"
load "config/recipes/nginx"
load "config/recipes/unicorn"
load "config/recipes/postgresql"
load "config/recipes/nodejs"
load "config/recipes/rbenv"
load "config/recipes/check"

server "72.14.183.209", :web, :app, :db, primary: true

set :user, "deployer"
set :application, "blog"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false

set :scm, "git"
set :repository, "git@github.com:ryanb/#{application}.git"
set :branch, "master"

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

after "deploy", "deploy:cleanup" # keep only the last 5 releases

config/recipes/base.rb

def template(from, to)
  erb = File.read(File.expand_path("../templates/#{from}", __FILE__))
  put ERB.new(erb).result(binding), to
end

def set_default(name, *args, &block)
  set(name, *args, &block) unless exists?(name)
end

namespace :deploy do
  desc "Install everything onto the server"
  task :install do
    run "#{sudo} apt-get -y update"
    run "#{sudo} apt-get -y install python-software-properties"
  end
end

config/recipes/nginx.rb

namespace :nginx do
  desc "Install latest stable release of nginx"
  task :install, roles: :web do
    run "#{sudo} add-apt-repository ppa:nginx/stable"
    run "#{sudo} apt-get -y update"
    run "#{sudo} apt-get -y install nginx"
  end
  after "deploy:install", "nginx:install"

  desc "Setup nginx configuration for this application"
  task :setup, roles: :web do
    template "nginx_unicorn.erb", "/tmp/nginx_conf"
    run "#{sudo} mv /tmp/nginx_conf /etc/nginx/sites-enabled/#{application}"
    run "#{sudo} rm -f /etc/nginx/sites-enabled/default"
    restart
  end
  after "deploy:setup", "nginx:setup"

  %w[start stop restart].each do |command|
    desc "#{command} nginx"
    task command, roles: :web do
      run "#{sudo} service nginx #{command}"
    end
  end
end

config/recipes/postgresql.rb

set_default(:postgresql_password) { Capistrano::CLI.password_prompt "PostgreSQL Password: " }
Something went wrong with that request. Please try again.