2. Capistrano
Gergely Nemeth
● utility and framework for executing commands in
parallel on multiple remote machines
● via SSH
● definable tasks, which can be applied to machines in
certain roles
● automate deploys
● open source: https://github.com/capistrano/capistrano
3. Get Capistrano
Gergely Nemeth
1. install Ruby and RubyGems
2. install Capistrano
gem install capistrano
3. install Capistrano extensions
gem install capistrano-ext
5. Prepare our project
Gergely Nemeth
Navigate to your project’s root, then
capify .
This will create a Capfile and a config folder with a
deploy.rb in it.
● deploy.rb: contains our deploy script
● Capfile: contains our recipes (so it can hold
multiple deploy scripts)
6. Prepare our server(s)
Gergely Nemeth
1. Make sure, you can login via SSH to your destination
servers
2. We will use forever, make sure, it is installed on your
system
npm install forever -g
7. Create our recipe
Gergely Nemeth
The default deploy.rb should look like something like
this:
set :application, "set your application name here"
set :repository, "set your repository location here"
# set :scm, :git # You can set :scm explicitly or Capistrano will make
an intelligent guess based on known version control directory names
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`,
`perforce`, `subversion` or `none`
role :web, "your web-server here" # Your HTTP
server, Apache/etc
role :app, "your app-server here" # This may
be the same as your `Web` server
role :db, "your primary db-server here", :primary => true # This is
where Rails migrations will run
role :db, "your slave db-server here"
8. Create our recipe #2
Gergely Nemeth
Set up server access:
set :user, "ec2-user"
set :use_sudo, false
role :app, "IP_ADDRESS" #multiple address can be added here
#make sure the same SSH key is used on
#every server
In this example, we will use our ssh keys (and forward it
to the server) to access our git repository, to do this, add
the following line to your deploy.rb file:
set :ssh_options, { :forward_agent => true }
Set up your git access:
set :repository, "LINK_TO_YOUR_REPO"
set :branch, "master"
9. Create our recipe #3
Gergely Nemeth
Add deploy hooks:
namespace :deploy do
desc "Stop Forever"
task :stop do
run "/home/ec2-user/.nvm/v0.10.15/bin/forever stopall; true"
end
desc "Start Forever"
task :start, :on_error => :continue do
run "cd #{current_path} && /home/ec2-user/.nvm/v0.10.15/bin/forever
start app.js"
end
desc "Restart Forever"
task :restart do
stop
sleep 5
start
end
desc "Check required packages and install if packages are not installed"
task :install_packages do
run "cd #{release_path} && bower install"
run "cd #{release_path} && /home/ec2-user/.nvm/v0.10.15/bin/npm
install --production --loglevel warn"
end
end
10. Create our recipe #4
Gergely Nemeth
Set deploy destination:
set :deploy_to, "/home/ec2-user/myawesomeproject"
Set server environment
set :default_environment, {
'NODE_ENV' => 'production'
}
Validate our recipe from the command line:
cap deploy:setup
This command will SSH into our server and create the
deploy structure in the folder we specified in: :deploy_to.
Do not proceed until it is not successful.
11. Go live!
Gergely Nemeth
1. Make sure, everything is set
cap deploy:check
2. Deploy
cap deploy
3. If something fails, Capistrano will automatically do a
rollback
4. You can do a rollback to specific version with:
cap deploy:VERSION_NUMBER
Or to the latest with
cap deploy:rollback
12. The full recipe
Gergely Nemeth
The full recipe can be downloaded from:
https://gist.github.com/anonymous/6249142
Have fun trying it out! :)