Sie sind auf Seite 1von 8

Deploying a Rails App with

Capistrano (Part 1)
What is Capistrano?
Capistrano is a remote server automation and
deployment tool written in ruby. Put simply, it helps you
get your code on a server and easily run commands on
a server so your application is ready for the world to
use.

Using Capistrano
The first step in using Capistrano is installing the
Capistrano gem on your local machine. Capistrano only
requires that you have access to your server via Secure
Shell (SSH).
geminstallcapistrano

Once the gem is installed the next step is to cd into


your repositorys directory and run the following
command:
Capify.

This creates a Capfile, which is where Capistrano reads


instructions from. This is what it looks like out of the
package:
load'deploy'
#UncommentifyouareusingRails'asset
pipeline
#load'deploy/assets'
load'config/deploy'
#removethislinetoskiploadinganyofthe
defaulttasks

The capify command also creates a file in the config


directory called deploy.rb, which the Capfile loads from
as you can see above. The deploy file contains
information about the servers you want to connect to
and the tasks you want to run on those servers.
Below is the default deploy.rb file:
set:application,"setyourapplicationnamehere"
set:repository,"setyourrepositorylocationhere"
#set:scm,:git#Youcanset:scmexplicitlyorCapistrano
willmakeanintelligentguessbasedonknownversioncontrol
directorynames
#Or:`accurev`,`bzr`,`cvs`,`darcs`,`git`,`mercurial`,
`perforce`,`subversion`or`none`
role:web,"yourwebserverhere"#
YourHTTPserver,Apache/etc
role:app,"yourappserverhere"#
Thismaybethesameasyour`Web`server
role:db,"yourprimarydbserverhere",:primary=>true#
ThisiswhereRailsmigrationswillrun
role:db,"yourslavedbserverhere"
#ifyouwanttocleanupoldreleasesoneachdeploy
uncommentthis:
#after"deploy:restart","deploy:cleanup"
#ifyou'restillusingthescript/reaperhelperyouwillneed
#thesehttp://github.com/rails/irs_process_scripts
#IfyouareusingPassengermod_railsuncommentthis:
#namespace:deploydo
#task:startdo;end
#task:stopdo;end
#task:restart,:roles=>:app,:except=>{:no_release=>
true}do
#run"#{try_sudo}touch
#{File.join(current_path,'tmp','restart.txt')}"
#end
#end
We worked off of an awesome deployment guide by Spike Grobstein that
recommended starting with the following deploy.rb file:

require'bundler/capistrano'#forbundlersupport
set:application,"setyourapplicationnamehere"
set:repository,"setyourrepositorylocationhere"
set:user,'USERNAME'
set:deploy_to,"/home/#{user}/#{application}"
set:use_sudo,false
set:scm,:git
default_run_options[:pty]=true
role:web,"96.8.123.64"
#YourHTTPserver,Apache/etc
role:app,"96.8.123.64"
#Thismaybethesameasyour`Web`server
#ifyouwanttocleanupoldreleasesoneachdeploy
uncommentthis:
#after"deploy:restart","deploy:cleanup"
#ifyou'restillusingthescript/reaperhelperyouwillneed
#thesehttp://github.com/rails/irs_process_scripts
#IfyouareusingPassengermod_railsuncommentthis:
namespace:deploydo
task:startdo;end
task:stopdo;end
task:restart,:roles=>:app,:except=>{:no_release=>
true}do
run"#{try_sudo}touch
#{File.join(current_path,'tmp','restart.txt')}"
end
end

This version differs slightly from the default deploy.rb


file, namely adding lines to set the user information,
where to deploy to, and a couple other small settings.
Below are some exaplanations of each of the lines in
this file.
set:application,"studentbody"

This code sets the application variable. It will become

the name of the overarching folder on the server and is


also used in the deploy_to location a couple lines below.
set:repository,"setyourrepositorylocation
here"
#example
set:repository,
"git@github.com:YourGithubUsername/ExRepo.git"

The repository variable tells Capistrano where to find


your code. We used a github hosted repository.
set:user,'USERNAME'

The above line sets the name of the user we are


deploying as. For example if you are SSHing into your
server with the following command ssh
example@192.241.555.55 then youd set :user to
example.
set:deploy_to,"/home/#{user}/#{application
}"

This command sets the location of deployment. In the


case where the the user is jsmith11 and the app name
is example-app, the above code would deploy the app
to /home/jsmith11/example-app directory on the
remote server.
set:use_sudo,false

The user_sudo variable tells Capistrano whether or not


to prefix sudo infront of all commands. Sudo is a prefix
that allows you to run programs with the security
priveledges of another user (commonly used with the
superuser/root). In our case we were deploying to a
location that our user owned, so we set this to false.
set:scm,:git

The scm variable sets the source-code-management


system, which in our case was git.
In a follow up post I will talk more about the rest of the
deploy.rb file, custom tasks we ran in our HireMe app,
and getting the app up and running using the cap
deploy command.

Deploying a Rails App With


Capistrano (Part 2)
My last post outlined using Capistrano to deploy my 6week long Flatiron School project. The post included a
brief introduction of Capistrano, including what it is and
the steps to get a Capfile (where Capistrano reads
instructions from) into your app.
In this post, I am going to continue to walk through a
basic deploy.rb file. If you dont know what what a
deploy.rb file is you should check out the earlier post
first.
require'bundler/capistrano'#forbundlersupport
set:application,"setyourapplicationnamehere"
set:repository,"setyourrepositorylocationhere"
set:user,'USERNAME'
set:deploy_to,"/home/#{user}/#{application}"
set:use_sudo,false
set:scm,:git
default_run_options[:pty]=true
role:web,"96.8.123.64"#YourHTTPserver,Apache/etc
role:app,"96.8.123.64"#Thismaybethesameasyour`Web`
server

#ifyouwanttocleanupoldreleasesoneachdeploy
uncommentthis:
#after"deploy:restart","deploy:cleanup"
#ifyou'restillusingthescript/reaperhelperyouwillneed
#thesehttp://github.com/rails/irs_process_scripts
#IfyouareusingPassengermod_railsuncommentthis:
namespace:deploydo
task:startdo;end
task:stopdo;end
task:restart,:roles=>:app,:except=>{:no_release=>
true}do
run"#{try_sudo}touch
#{File.join(current_path,'tmp','restart.txt')}"
end
end

In the previous post I left off with the set :scm, :git
line, so I will start with the following line:
default_run_options[:pty]=true

This is included to make sure Capistrano interacts


properly with the shell on our server. For example,
when using github your server needs to be able to
access your repo. This capistrano config settting is
needed for the password prompt from git to work so
you can access it.
role:web,"96.8.123.64"#YourHTTPserver,
Apache/etc
role:app,"96.8.123.64"#Thismaybethesame
asyourWebserver

The above two lines assign the web and app roles to
specific servers. Capistrano roles allow you to have
different servers handle different parts of the app
(server for app, server for handling requests, and
server for the database). We used one server for all

aspects so assigned all roles to the same server.


Since we used Passenger, we also uncommented the
:deploy namespace as is instructed in the standard
deploy.rb file. This part of the file is an example of
Capistranos task-oriented nature. Similar to rake tasks,
we can create cap tasks with the task keyword. For
example, take the following 3 lines of code:
task:hellodo
puts"HelloWorld"
end

We can run this code with the following command in


the terminal:
1 cap hello

We also used a common capistrano task to symlink


specific files that were purposely excluded from our git
repo. For example, we had an application.yml file with
our Crunchbase API key and gmail username/password.
Since we open sourced the repo, we had to make sure
that file was included in our .gitignore. Because
capistrano is getting the code from github, we have to
manually link these ignored files to the server in order
for them to be included. The following task helps us do
just that. ruby
task:symlink_config,:roles=>:appdo
run"lnnfs#{shared_path}/config/application.yml
#{release_path}/config/application.yml"
end

In order for the symlink to work though, the file must


already be in the shared folder on the server. You can
securely copy a file from your local machine using a
variation of the following command:
scpconfig/application.ymlexample@123.456.789.12:/
home/example/example_app/shared

And that concludes my Capistrano configuration


process. To deploy and update the app I simply run the
following command:
capdeploy

If youre interested in learning more about deploying a


rails app with Capistrano, there is a good railscast and
also a nice wiki on Github.

Das könnte Ihnen auch gefallen