capistrano snippets

Capistrano 2 task for backing up your MySQL production database before each deployment

Tagged ruby, rails, mysql, backup, capistrano  Languages ruby

This Capistrano task connects to your production database and dumps the contents to a file. The file is compressed and put in a directory specified with set :backup_dir, "#{deploy_to}/backups". This is a slight modification of All credit to court3nay.

task :backup, :roles => :db, :only => { :primary => true } do
  filename = "#{backup_dir}/#{application}.dump.#{}.sql.bz2"
  text = capture "cat #{deploy_to}/current/config/database.yml"
  yaml = YAML::load(text)

  on_rollback { run "rm #{filename}" }
  run "mysqldump -u #{yaml['production']['username']} -p #{yaml['production']['database']} | bzip2 -c > #{filename}" do |ch, stream, out|
    ch.send_data "#{yaml['production']['password']}\n" if out =~ /^Enter password:/

To automatically backup your data before you deploy a new version add this to config/deploy.rb:

task :before_deploy do

To restore the backup run the following command:

mysql database_name -uroot < filename.sql

Jump start a Rails project with Rails Edge, Capistrano, Mongrel and Mercurial

Tagged rails, capistrano, mongrel, mercurial  Languages bash
# Create a Rails project
rails project -d sqlite3
cd project
# Delete index file
rm public/index.html
# Use Rails edge. Use  rake rails:freeze:edge TAG=rel_1-2-3 to get a specific version.
rake rails:freeze:edge
# Add Capistrano configuration file
capify .
# Add Mongrel cluster configuration file
sudo mongrel_rails cluster::configure -e production \
    --user mongrel --group mongrel \
    -c /var/www/project-xxx/current \
    -a \
    -p 8000  \
    -N 3
# Create a Mercurial repository
hg init
# Add project to repository
hg commit -A --message "Project started"
# Push changes to a remote repository
hg push ssh://[email protected]:port//var/mercurial/xxx

Cloning is done with hg clone:

hg clone ssh://[email protected]:port//var/mercurial/xxx

How to use Vlad the Deployer with git, nginx, mongrel, mongrel_cluster and Rails

Tagged vlad, deployer, deploy, capistrano, nginx, mongrel, mongrel_cluster  Languages ruby

This is a draft...

Installing Vlad the Deployer

gem install vlad

Configuring Vlad the Deployer

Add this to the end of RakeFile:

  require 'rubygems'
  require 'vlad'
  Vlad.load :scm => :git
rescue LoadError => e
  puts "Unable to load Vlad #{e}."

Note that we're telling Vlad to use git. This snippet gives you a quick introduction on how to use git with Rails.

Creating the deployment recipe

If you're uncertain what these variables mean, have a look at the docs. This folder is also worth a look, and don't forget to take a peek at the vlad source code.

# General configuration
set :ssh_flags,             '-p 666'
set :application,           ''
set :domain,                '127.0.01'
set :deploy_to,             '/var/www/'
set :repository,            '/var/lib/git/repositories/'

# Mongrel configuration
set :mongrel_clean,         true
set :mongrel_command,       'sudo mongrel_rails'
set :mongrel_group,         'www-data'
set :mongrel_port,          9000
set :mongrel_servers,       3

#set :mongrel_address,       ''
#set(:mongrel_conf)          { '#{shared_path}/mongrel_cluster.conf' }
#set :mongrel_config_script, nil
#set :mongrel_environment,   'production'
#set :mongrel_log_file,      nil
#set :mongrel_pid_file,      nil
#set :mongrel_prefix,        nil
#set :mongrel_user,          'mongrel'

# Customize Vlad to our needs
namespace :vlad do
  # Add an after_update hook
  remote_task :update do

  # The after_update hook, which is run after vlad:update
  remote_task :after_update do
  # Link to shared resources, if you have them in .gitignore
  #  run "ln -s #{deploy_to}/shared/system/database.yml #{deploy_to}/current/config/database.yml"

  # Deploys a new version of your application
  remote_task :deploy => [:update, :migrate, :start_app]

Setup the server

$ rake vlad:setup

This will create the necessary folders and mongrel_cluster configuration file.

Deploy the application

Now deploy the application with vlad:deploy, which is a custom rake task that we added to the deployment recipe:

$ rake vlad:deploy

Copying your SSH public key to the remote server

Vlad uses ssh for executing commands on the remotely, and rsync for copying the build to your server, which means you'll quickly grow tired of typing your password each time a command is run.

This problem is solved by copying your public SSH keys to the remote server, this snippet explains how to do exactly that.

My Sinatra+Capistrano+Capinatra deployment recipe

Tagged capistrano, sinatra, capinatra  Languages ruby
require 'capistrano/version'
require 'rubygems'
require 'capinatra'
load 'deploy' if respond_to?(:namespace) # cap2 differentiator

# set an app_class if you're using the more recent style of creating
# Sinatra apps, where app_class would be the name of your subclass
# of Sinatra::Base. if you're just requiring 'sinatra' and using the
# more traditional DSL style of Sinatra, then comment this line out.
set :app_class, 'xxx'

# standard settings
set :app_file, "xxx.rb"
set :application, "xxx"
set :domain, ""
role :app, domain
role :web, domain
role :db,  domain, :primary => true

set :ssh_options, { :forward_agent => true }

#set :use_sudo, false

# environment settings
set :user, "xxx"
set :group, "www-data"
set :deploy_to, "/var/www/#{application}"
set :deploy_via, :copy #:remote_cache
default_run_options[:pty] = true

# scm settings
set :repository, "[email protected]:xxx.git"
#set :repository, "file:///home/git/repositories/xxx.git"
set :scm, "git"
set :branch, "master"
set :git_enable_submodules, 1

# where the apache vhost will be generated
set :apache_vhost_dir, "/etc/apache2/sites-enabled/"

namespace :deploy do
  task :restart do
    run "touch #{current_path}/tmp/restart.txt"

How to prevent Capistrano from asking for password when deploying

Tagged capistrano, ssh, password  Languages ruby

Capistrano might ask for a password when:

# Capistrano is using sudo, so set it to false
set :use_sudo, false

# Your remote server is trying to checkout an SSH protected Git repository
set :repository, '/var/git/repositories/xxx.git' # Remote server also holds the git repository
set :local_repository, 'ssh://xxx/var/git/repositories/xxx.git' # Your development machine points to the remote machine 

# SSH settings, also see ~/.ssh/config
set :user, "jebus"
set :domain, ''
set :port, 666

# Other settings worth checking
ssh_options[:forward_agent] = true
ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "id_rsa")]
default_run_options[:pty] = true # see[email protected]/msg07323.html for details


How to setup a password-less “cap deploy” with Capistrano