0% found this document useful (0 votes)
5K views

Capistrano Cheatsheet

This cheat sheet provides a summary of key Capistrano concepts and commands for deploying and managing applications remotely: 1) It outlines the directory structure and files used by Capistrano, including the releases, shared, and current directories that store application versions and shared files. 2) It describes common Capistrano tasks like deploy, rollback, and helper methods for running commands and transferring files remotely. 3) It explains how to define custom tasks, capture output, render templates, and use roles to group servers and define primary/secondary servers.

Uploaded by

kostabongo
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5K views

Capistrano Cheatsheet

This cheat sheet provides a summary of key Capistrano concepts and commands for deploying and managing applications remotely: 1) It outlines the directory structure and files used by Capistrano, including the releases, shared, and current directories that store application versions and shared files. 2) It describes common Capistrano tasks like deploy, rollback, and helper methods for running commands and transferring files remotely. 3) It explains how to define custom tasks, capture output, render templates, and use roles to group servers and define primary/secondary servers.

Uploaded by

kostabongo
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Cheat Sheet CAPISTRANO 1.2.

0
Helper Methods config/deploy.rb Directory structure Shell commands
run Setting and using variables releases
Takes a single string identifying any valid shell 1 set :application, "flipper" Installation
command to execute 2 set :user, "homersimpson" 20050725121411 gem install capistrano
run <<-CMD 3 puts "The application name is 20050801090107
if [[ -d #{release_path}/status.txt #{application}" 20050802231414 Add your application to Capistrano (capistranize)
]]; then 4 puts "The user is #{user}" cap --apply-to /path/to/your/app
cat #{release_path}/status.txt ...
Redefining the restart task 20050824141402 YourApplicationName
fi
CMD 1 desc "This task restarts the web
server" Rakefile Execute the setup task
sudo 2 task :restart, :roles => app rake remote:exec ACTION=setup
Exactly like the run command, except that it :app do config
executes the command via sudo 3 sudo "apachectl graceful" Execute the cold_deploy task
sudo "apachectl graceful" 4 end db
lib rake remote:exec ACTION=cold_deploy
put Defining tasks
Lets you transfer data to a file on the remote 1 task :hello_world do log-->/shared/log Deploy your application
host. Takes two parameters: a string containing 2 run "echo Hello, $HOSTNAME" public rake deploy
the data to transfer, and the name of the file to 3 end system-->/shared/system
receive the data on each remote host Rollback a release from production
put(File.read('templates/database.yml' 1 task :hello_world, :roles => script
[:db, :app] do test rake rollback
),"#{release_path}/config/database.yml
",:mode => 0444) 2 puts "calling hello_world..." vendor Execute the disable_web task
delete 3 hello_world
4 end shared rake remote_exec ACTION=disable_web \
A convenience for executing rm via run. It just log UNTIL="tomorrow morning" \
attempts to do an rm -f on the remote Transactions
server(s), for the named file. To do a recursive 1 task :cold_deploy do system REASON="vital upgrade"
delete, pass :recursive => true 2 transaction do current-->/releases/20050824141402
delete "#{release_path}/certs", 3 task_one_here Using the invoke task
:recursive => true 4 task_two_here rake remote_exec ACTION=invoke \
on_rollback 5 end COMMAND="svn up
6 task_three_not_in_transaction
Allows a task to specify a callback to use if that
task raises an exception when invoked inside of 7 end Capistrano Shell /u/apps/flipper/current/app/views" \
ROLES=app
a transaction Capturing output with run
task :update_code do 1 run "sudo ls -la" do |channel, Start the interactive Capistrano shell
on_rollback { delete release_path, stream, data| cap -v shell
:recursive => true } 2 if data =~ /^Password:/
... 3 logger.info "#{channel[:host]} Execute Capistrano tasks Roles
end asked for password" !deploy
render 4 channel.send_data "mypass\n" 1 role :web , "www.capistrano.com"
5 end !update_code symlink 2 role :app , "app1.capistrano.com",
An interface for rendering ERb* templates and 6 end
returning the result. If you pass a string to !setup deploy "app2.capistrano.com"
render, it is interpreted as the name of a Parsing & saving an ERb template with render on app2.foo.com !setup 3 role :db , "master.capistrano.com",
template file with .rhtml appended, relative 1 buffer = render(:template => with app,db !setup deploy :primary => true
either to the current directory, or to <<EXAMPLE_TEMPLATE)
capistrano/recipes/templates to be 2 This template will be rendered 4 role :db , "slave.capistrano.com"
rendered. If you don’t want to render a file, but replacing variables 5 role :spare , "genghis.capistrano.com
instead have a string containing an ERb <%= like_this_variable =>
template that you want to render, use the with their values.  standard, predefined roles
second example below *Embedded Ruby 3 EXAMPLE_TEMPLATE  user-defined roles
render "maintenance" 4 put buffer,
render :template => "Hello <%= target "path/to/save/file.txt",
5 :mode => 0755
%>", :target => "world"
Standard tasks
Pre-defined variables cleanup cleans up the releases directory, leaving the five most recent releases
:application (required) The name of your application. Used to build other values, like the cold_deploy used when deploying an application for the first time. Starts the
deployment directory. application’s spinner (via the spinner task) and then does a normal
deploy
:repository (required) The location of your code’s scm repository
deploy updates all the code on your server (via update_code and symlink
:gateway nil The address of the server to use as a gateway. If given, all other tasks), then restarts the FastCGI listeners on the application servers (via
connections will be tunneled through this server. the restart task).
:user (current user) The name of the user to use when logging into the remote host(s) diff_from_last_ prints the difference between what was last deployed, and what is
:password (prompted) The password to use for logging into the remote host(s). deploy currently in your repository
:deploy_to “/u/apps/#{app The root of the directory tree on the remote host(s) that the disable_web puts up a static maintenance page that is displayed to visitors
lication}” application should be deployed to enable_web removes the maintenance page
:version_dir “releases” The directory under deploy_to that should contain each invoke allows you to send commands directly
deployed revision
migrate changes to the directory of your current release (as indicated by the
:current_dir “current” The name to use (relative to deploy_to) for the symlink that current symlink), and runs rake RAILS_ENV=production migrate
points at the current release
restart restarts all FastCGI listeners for your application by calling the reaper
:shared_dir “shared” The name of the directory under deploy_to that will contain command without arguments. Only executed on :app servers
directories and files to be shared between all releases
rollback rolls your application back to the previously deployed version
:revision (latest revision) This specifies the revision you want to check out on the remote
machines rollback_code determines the previous release , updates the current symlink to point
to that, and then deletes the latest release
:scm :subversion The source control module to use. Current supported are
:subversion, :cvs, :darcs setup Creates and chmods the directory tree:
releases_path directory 0775
:svn, :cvs, (path) The location on the remote host of the source control executable shared_path directory
:darcs shared_path/system 0775
:checkout “co” The subversion operation to use when checking out code on the shared_path/log 0777
remote host. Can be set to “export” show_tasks inspect the existing tasks and display them to standard out in
:ssh_options Hash.new Hash of additional options passed to the SSH connection routine. alphabetical order, along with their descriptions
This lets you set (among other things) a non-standard port to spinner starts the spinner process for your application
connect on (ssh_options[:port] = 2345)
symlink updates the current symlink to the latest deployed version of the code
:use_sudo true Whether or not tasks that can use sudo, ought to use sudo. In a
update_code Checks out your source code, deletes the log and public/system
shared environment, this is typically not desirable (or possible),
and in that case you should set this variable to false directories in your new release, symlinks log to
#{shared_path}/log, symlinks public/system to
:sudo sets the path to sudo #{shared_path}/system
task already has a defined on_rollback handler when using transactions

www.dizzy.co.uk/cheatsheets This work is licensed under the Creative Commons


Attribution-NonCommercial-NoDerivs 2.0 License. To
view a copy of this license, visit
http://creativecommons.org/licenses/by-nc-nd/2.0/uk

You might also like