Use the left/right arrow keys to navigate, 's' to enable/disable scrolling.

System Automation

with

Capistrano

Capistrano

  • What is it?
  • When it can be useful?
  • Install
  • Create your first script
  • Config files
  • Run
  • Debug
  • Advanced
  • Link

What is it?

Ruby internal DSL (Domain Specific Language)

Automate System Administration Tasks

Run shell commands over SSH

Upload and Download small files (over SSH)

When it can be useful?

Application Deploy

Management Tasks

System Bootstraping

When it can be DANGEROUS?

Remember, with great power comes great responsibility

(Uncle Ben - Spiderman)

Common Use Cases

Check servers uptime

Check mail queues

Run a script as workaround to an incident

Install/Uninstall packages

Install capistrano

prereqs

ruby

ruby-dev

rubygems


# gem install capistrano
...

# capify 
Please specify the directory to capify, e.g. `capify .'

Create your first Capistrano script


$ mkdir hello_capistrano
$ capify hello_capistrano
[add] writing 'hello_capistrano/Capfile'
[add] making directory 'hello_capistrano/config'
[add] writing 'hello_capistrano/config/deploy.rb'
[done] capified!

hello_capistrano ----> Capfile
                    |
                    --> config ---> deploy.rb

Capfile

1load 'deploy' if respond_to?(:namespace) # cap2 differentiator
2Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
3 
4load 'config/deploy' # remove this line to skip loading any of the default tasks

config/deploy.rb

01set :application, "set your application name here"
02set :repository"set your repository location here"
03 
04set :scm, :subversion
05# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
06 
07role :web, "your web-server here"                          # Your HTTP server, Apache/etc
08role :app, "your app-server here"                          # This may be the same as your `Web` server
09role :db"your primary db-server here", :primary => true # This is where Rails migrations will run
10role :db"your slave db-server here"
11 
12# If you are using Passenger mod_rails uncomment this:
13# if you're still using the script/reapear helper you will need
15 
16# namespace :deploy do
17#   task :start {}
18#   task :stop {}
19#   task :restart, :roles => :app, :except => { :no_release => true } do
20#     run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
21#   end
22# end

Run


hello_capistrano$ cap -T
cap deploy               # Deploys your project.
cap deploy:check         # Test deployment dependencies.
cap deploy:cleanup       # Clean up old releases.
cap deploy:cold          # Deploys and starts a `cold' application.
cap deploy:migrate       # Run the migrate rake task.
cap deploy:migrations    # Deploy and run pending migrations.
cap deploy:pending       # Displays the commits since your last deploy.
cap deploy:pending:diff  # Displays the `diff' since your last deploy.
cap deploy:restart       # Restarts your application.
cap deploy:rollback      # Rolls back to a previous version and restarts.
cap deploy:rollback:code # Rolls back to the previously deployed version.
cap deploy:setup         # Prepares one or more servers for deployment.
cap deploy:start         # Start the application servers.
cap deploy:stop          # Stop the application servers.
cap deploy:symlink       # Updates the symlink to the most recently deployed ...
cap deploy:update        # Copies your project and updates the symlink.
cap deploy:update_code   # Copies your project to the remote servers.
cap deploy:upload        # Copy files to the currently deployed version.
cap deploy:web:disable   # Present a maintenance page to visitors.
cap deploy:web:enable    # Makes the application web-accessible again.
cap invoke               # Invoke a single command on the remote servers.
cap shell                # Begin an interactive Capistrano session.

Some tasks were not listed, either because they have no description,
or because they are only used internally by other tasks. To see all
tasks, type `cap -vT'.

Extended help may be available for these tasks.
Type `cap -e taskname' to view it.

cap invoke

send simultaneously one command to all servers


$ cap invoke COMMAND="echo 'Hello World'"
$ cap invoke COMMAND="echo 'Hello World'" SUDO=1

cap shell

all connections that are established during the duration of the shell session are cached and reused

Disable Rails Stuff in the Capfile

1# load 'deploy' if respond_to?(:namespace) # cap2 differentiator
2Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
3 
4load 'config/deploy' # remove this line to skip loading any of the default tasks

Minimal capistrano scripting

01set :application, "My Big Service"
02set :user, "root"
03 
04role :web_server, "web.mydomain"
05role :db_server, "db.mydomain"
06 
07desc "Hello World task"
08namespace :hello do
09  task :world, :roles => [:db_server] do
10    puts "Hello world, from capistrano"
11    run "ls -l"
12  end
13end

Advanced Capistrano

refactoring, debugging and integrating

capistrano IS ruby

(define functions and/or classes)

debug capistrano scripts as ruby applications

and

you can use it as a library

CAPISTRANO is NOT a Configuration Management system

it’s a system automation tools

for full Configuration Management evaluate a complete solution as

CFENGINE, BCFG2, Puppet or Chef

Copyright 2010 - Alca Soc. Coop.


http://learn.alcacoop.it - learn@alcacoop.it



released under CreativeCommons 2.5 by-nc-sa