Table of Contents

Middleware

Capistrano

Capistrano 2.x (Legacy)



Installing

gem install capistrano -v 2.15.9


Commands

$ cap -V 
Capistrano v2.15.5
$ cap -h
Usage: cap [options] action ...
    -d, --debug                      Prompts before each remote command execution.
    -e, --explain TASK               Displays help (if available) for the task.
    -F, --default-config             Always use default config, even with -f.
    -f, --file FILE                  A recipe file to load. May be given more than once.
    -H, --long-help                  Explain these options and environment variables.
    -h, --help                       Display this help message.
    -l [STDERR|STDOUT|file]          Choose logger method. STDERR used by default.
        --logger
    -n, --dry-run                    Prints out commands without running them.
    -p, --password                   Immediately prompt for the password.
    -q, --quiet                      Make the output as quiet as possible.
    -r, --preserve-roles             Preserve task roles
    -S, --set-before NAME=VALUE      Set a variable before the recipes are loaded.
    -s, --set NAME=VALUE             Set a variable after the recipes are loaded.
    -T, --tasks [PATTERN]            List all tasks (matching optional PATTERN) in the loaded recipe files.
    -t, --tool                       Abbreviates the output of -T for tool integration.
    -V, --version                    Display the Capistrano version, and exit.
    -v, --verbose                    Be more verbose. May be given more than once.
    -X, --skip-system-config         Don't load the system config file (capistrano.conf)
    -x, --skip-user-config           Don't load the user config file (.caprc)
$
$ cap -H
-----------------------------
Capistrano
-----------------------------

Capistrano is a utility for automating the execution of commands across multiple remote machines. It was originally
conceived as an aid to deploy Ruby on Rails web applications, but has since evolved to become a much more general-purpose
tool.

The command-line interface to Capistrano is via the `cap' command.

  cap [ option ] ... action ...

The following options are understood:

  -f, --file FILE
  Causes the named file to be loaded. Capistrano will search both its own recipe directory, as well as the current
  directory, looking for the named file. An ".rb" extension is optional. The -f option may be given any number of times, but
  if it is given, it will take the place of the normal `Capfile' or `capfile' detection. Use -F if you want the default
  capfile to be loaded when you use -f.


-----------------------------
Environment Variables
-----------------------------

  HOSTS
  Execute the tasks against this comma-separated list of hosts.  Effectively, this makes the host(s) part of every roles.

  HOSTFILTER
  Execute tasks against this comma-separated list of host, but only if the host has the proper role for the task.

  HOSTROLEFILTER
  Execute tasks against the hosts in this comma-separated list of roles, but only if the host has the proper role for the
  task.

  ROLES
  Execute tasks against this comma-separated list of roles.  Hosts which do not have the right roles will be skipped.
$ cap -f config/test.rb -vT
cap check_hostname   #
cap invoke           # Invoke a single command on the remote servers.
cap shell            # Begin an interactive Capistrano session.

Extended help may be available for these tasks.
Type `cap -e taskname' to view it.
$
$ cap -f config/test.rb -e invoke
------------------------------------------------------------
cap invoke
------------------------------------------------------------
Invoke a single command on the remote servers. This is useful for performing one-off commands that may not require a full
task to be written for them. Simply specify the command to execute via the COMMAND environment variable. To execute the
command only on certain roles, specify the ROLES environment variable as a comma-delimited list of role names.
Alternatively, you can specify the HOSTS environment variable as a comma-delimited list of hostnames to execute the task on
those hosts, explicitly. Lastly, if you want to execute the command via sudo, specify a non-empty value for the SUDO
environment variable.

Sample usage:

  $ cap COMMAND=uptime HOSTS=foo.capistano.test invoke
  $ cap ROLES=app,web SUDO=1 COMMAND="tail -f /var/log/messages" invoke

$


Example of cap shell

$ cap -f config/test.rb shell HOSTS=192.168.0.101,192.168.0.102
  * 2016-11-18 10:12:16 executing `shell'
====================================================================
Welcome to the interactive Capistrano shell! This is an experimental
feature, and is liable to change in future releases. Type 'help' for
a summary of how to use the shell.
--------------------------------------------------------------------
cap> hostname
[establishing connection(s) to 192.168.0.101, 192.168.0.102]
 ** [out :: 192.168.0.101] test-server1
 ** [out :: 192.168.0.102] test-server2
cap> exit
exiting


Sample Execute Commands

cap -f config/test.rb check_hostname HOSTS=192.168.0.10,192.168.0.11
cap -f config/test.rb invoke COMMAND="hostname;uptime"  HOSTS=192.168.0.10,192.168.0.11
cap -f config/test.rb invoke COMMAND="hostname;uptime"  ROLES=web,ap


Configuration

Example

test.rb

set :repository,  "set your repository location here"
set :user, "deployuerser1"
set :runner, "deployuerser1"
set :use_sudo, false
set :default_run_options, :pty => true
ssh_options[:config]=false
ssh_options[:auth_methods] = "publickey"
#ssh_options[:verbose] = :debug
ssh_options[:forward_agent] = true

task :check_hostname, :max_hosts => 1, :roles => "x" do
  run "hostname"
end
cap -f config/test.rb check_hostname HOSTS=192.168.0.10


Include A File

load "config/hostlist.rb"

hostlist.rb

server '192.168.0.11',  :test_web1,   :test_web_group ,  :all
server '192.168.0.12',  :test_web2,   :test_web_group ,  :all


Task Examples

Localhost

task :check_var, :max_hosts => 1, :roles => "x" do
  system "hostname"

  var1 =  "echo test1"
  puts var1
end

var

cap -f config/test.rb check_var HOSTS=x.x.x.x

VAR1="aaaa"

task :check_var, :max_hosts => 1, :roles => "x" do
  run  "echo $CAPISTRANO:HOST$"
  run  "echo #{VAR1}"
end
task :check_var, :max_hosts => 1, :roles => "x" do
  var1 =  "echo test1"
  puts var1
end

upload, sudo

task :jdk_add16_19, :max_hosts =>1, :roles => "x" do
 upload("src2/jdk1.6.0_19.tar.bz2", "/tmp", :via => :scp)
 sudo "bash -c 'cd /opt && tar jvxf /tmp/jdk1.6.0_19.tar.bz2'"
 sudo "rm /tmp/jdk*bz2"
 run "ls -al /opt"
end

add routing

task :route_add_20111007, :max_hosts => 1, :roles => "x" do
 sudo "cp -p /etc/sysconfig/static-routes /etc/sysconfig/static-routes.`date '+%Y%m%d'`;true"
 sudo "bash -c 'echo \"any host 192.168.100.100 gw 192.168.0.1\" >> /etc/sysconfig/static-routes;true'"
 sudo "bash -c '/sbin/route add -host 192.168.100.100 gw 192.168.0.1'"
end







Middleware

Capistrano