20
Capistrano by Jason Noble http://jasonnoble.org

Capistrano

Embed Size (px)

DESCRIPTION

Walk through on how to deploy applications via capistrano

Citation preview

Page 1: Capistrano

Capistrano

by Jason Noble

http://jasonnoble.org

Page 2: Capistrano

Getting Started

• http://github.com/jasonnoble/capistrano-demo/• Click fork• Click “Your Clone URL”• Paste into a terminal window• Two branches in this repo

– master (base shopping cart, no capistrano changes)– working_capistrano_deploys

Page 3: Capistrano

Quick Demo of App

• Create/Migrate databases– rake db:create:all– rake db:migrate

• script/server• http://localhost:3000/store• http://localhost:3000/admin– admin/admin

• http://localhost:3000/users

Page 4: Capistrano

Server preperation

• Install Git (1.5.5.6)• Install Rake gem (0.8.7)• Install Rails gem (2.3.5)• Install mongrel_cluster gem (1.0.5)• Mysql server / Mysql gem

Page 5: Capistrano

Local Preparation

• sudo gem install capistrano– Latest is 2.5.10 (as of 11/2009)

• capify .– Creates ./Capfile– Creates ./config/deploy.rb

Page 6: Capistrano

config/deploy.rb

• set :application, “shopping_cart”• set :repository,

[email protected]:jasonnoble/capistrano-demo.git• set :scm, :git• set :user, “jasonn”• default_run_options[:pty] = true– Enables password entry for git

• set :branch, ”master”• set :deploy_to, "/var/www/#{user}/#{application}”• set :deploy_via, :remote_cache

Page 7: Capistrano

config/deploy.rb cont.

• role :web, “192.168.255.54”• role :app, “192.168.255.54”• role :db, “192.168.255.54”, :primary => true

• set :mongrel_conf, “#{current_path}/config/mongrel_cluster.yml”

• set :use_sudo, false

Page 8: Capistrano

config/deploy.rb cont.namespace :deploy do namespace :mongrel do [:stop, :start, :restart ].each do |t| desc "#{t.to_s.capitalize} the mongrel appserver" task t, :roles => :app do invoke_command "mongrel_rails cluster::#{t.to_s} -C #{mongrel_conf}", :via => run_method end end end desc "Customer restart task for mongrel cluster" task :restart, :roles => :app, :except => { :no_release => true } do deploy.mongrel.restart end desc "Customer start task for mongrel cluster" task :start, :roles => :app, :except => { :no_release => true } do deploy.mongrel.start end desc "Customer stop task for mongrel cluster" task :stop, :roles => :app, :except => { :no_release => true } do deploy.mongrel.stop endend

Page 9: Capistrano

config/mongrel_cluster.yml

--- cwd: /var/www/rails/jasonn/shopping_cart/currentlog_file: log/mongrel.logport: "7010”environment: productionaddress: 0.0.0.0pid_file: tmp/pids/mongrel.pidservers: 2

Page 10: Capistrano

config/database.yml

Production setting, change socket: /tmp/mysql.sock

tosocket: /var/lib/mysql/mysql.sock

Modify the database name to be unique

Page 11: Capistrano

Setup SSH keys on the server• scp ~/.ssh/id_dsa.pub 192.168.255.54:~/• ssh [email protected]– Login with your password

• mkdir –p ~/.ssh• chmod 755 ~/.ssh• mv ~/id_dsa.pub .ssh/authorized_keys• Logout. When you log back in it should not ask for a

password.• While logged in, run the following:– ssh github.com

• This adds the GitHub host to your “known hosts” file• Will prevent later errors

Page 12: Capistrano

Add Deploy SSH key to Github

• On your server:– ssh-keygen –d– cat ~/.ssh/id_dsa.pub • Copy

• http://github.com/jasonnoble/capistrano-demo/edit– Click Add another deploy key– Paste your key into the box

Page 13: Capistrano

cap -T

• Capistrano uses the “cap” command• Similar to Rake, has tasks available• cap –T

– cap deploy # Deploys your project.– cap deploy:check # Test deployment dependencies.– cap deploy:cleanup # Clean up old releases.

Page 14: Capistrano

cap deploy:setup

• This makes a bunch of directories that capistrano needs to work

• /var/www/rails/username/app_name– shared– releases

• Sets up directory ownership properly

Page 15: Capistrano

cap deploy:check

• Verifies required directories are there• Tests SSH connectivity• Verifies some minimal gems are installed

Page 16: Capistrano

Git installed in custom dir

• If you get the following error:The following dependencies failed. Please check them and try again:

--> `git' could not be found in the path (127.0.0.1)

• Add the following to config/deploy.rb– set :scm_command, "/path/to/git/bin/git" – set :local_scm_command, "git"

Page 17: Capistrano

cap deploy:cold

• First time deploy method• This should error out when trying to use the

database (we haven’t created it!)• Login to the server– RAILS_ENV=production rake db:create

• Logout and run cap deploy:cold again

Page 18: Capistrano

Your site is live!

• http://192.168.255.54:7010/ – Use the mongrel port you specified in

config/mongrel_cluster.yml

Page 19: Capistrano

Make a change to the rails app

• Change the text in one of the views• commit it via git• cap deploy– We use deploy instead of deploy:cold because it’s

already been deployed once

Page 20: Capistrano

Questions?