capistrano 2 rocks my world

21
Capistrano 2 Rocks my world Graeme Mathieson, Rubaidh Ltd <[email protected] > http://woss.name / & http://www.rubaidh.com /

Upload: mathie

Post on 18-Jan-2015

3.618 views

Category:

Technology


5 download

DESCRIPTION

The talk I gave at ScotRUG last night about Capistrano 2 and some of the things I've been doing with it.

TRANSCRIPT

Page 1: Capistrano 2 Rocks My World

Capistrano 2Rocks my world

Graeme Mathieson, Rubaidh Ltd<[email protected]>

http://woss.name/ & http://www.rubaidh.com/

Page 2: Capistrano 2 Rocks My World

• So, what is it?

• Installing 2.0

• What’s new in Cap 2?

• Stuff I’ve done with cap 2

What’s the plan?

Page 3: Capistrano 2 Rocks My World

• A Rails deployment tool

• A Java deployment tool

• A troubleshooting tool

• An ad-hoc systems management tool

What is it?

Page 4: Capistrano 2 Rocks My World

Without a Capfile

$ cap2 invoke COMMAND=uname HOSTS=macallan,cardhu * executing `invoke' * executing "uname" servers: ["macallan", "cardhu"] [macallan] executing command [cardhu] executing command ** [out :: cardhu] SunOS ** [out :: macallan] Linux command finished$

Page 5: Capistrano 2 Rocks My World

Using cap shell

$ cap2 shell HOSTS=balvenie,cardhu * executing `shell'cap> uptime[establishing connection(s) to balvenie, cardhu] ** [out :: balvenie] 19:33:52 up 211 days, 7:25, 1 user, load average: 0.06, 0.07, 0.07 ** [out :: cardhu] 7:33pm up 10 day(s), 6:47, 2 users, load average: 0.18, 0.16, 0.15cap> uname -r ** [out :: balvenie] 2.4.21-47.0.1.EL ** [out :: cardhu] 5.10cap>

Page 6: Capistrano 2 Rocks My World

Installing 2.0

$ gem install -y net-ssh net-ftp highline[ ... ]$ gem install -s http://gems.rubyonrails.com/ capistrano[ ... ]

Done!

Page 7: Capistrano 2 Rocks My World

Co-existing with 1.x

# In ~/.bashrcalias cap1="`which cap` _1.4.1_"alias cap2="`which cap`"alias cap="echo 'Please explicitly choose cap1 or cap2.'"

Page 8: Capistrano 2 Rocks My World

New in 2.0?

• More rake-like

- Use `cap2 -T` to list tasks

- Namespaces

• Deployment strategies

• Dependencies

• Generic hooks

Page 9: Capistrano 2 Rocks My World

New task names

• deploy ⇒ well, umm, deploy

• deploy_with_migrations ⇒ deploy:migrations

• diff_from_last_deploy ⇒ deploy:pending:diff

• setup ⇒ deploy:setup

• Same idea, just with namespaces

Page 10: Capistrano 2 Rocks My World

New Deployment Tasks

• Subversion log of undeployed changes

• deploy:pending

• Command, gem, file, directory dependencies

• deploy:check

Page 11: Capistrano 2 Rocks My World

What I’ve been up to

Page 12: Capistrano 2 Rocks My World

Recipes in plugins

# New CapfileDir['vendor/plugins/*/recipes'].each do |plugin| load_paths << pluginendload 'deploy' if respond_to?(:namespace)load 'config/deploy'

Page 13: Capistrano 2 Rocks My World

Daemon Strategies

• Default uses spin/spawner/reaper

• Solaris Service Management Framework

• runit

• daemontools

• Autogenerate script/spin

Page 14: Capistrano 2 Rocks My World

Staging

• Separate clusters for staging & deployment

• Use svnmerge to manage branches

• Trunk is deployed to staging server

• Production branch gets deployed live

Page 15: Capistrano 2 Rocks My World

General Strategy

• Create a rake task to do what you want.

• Create a Capistrano task that runs the rake task.

• Hook it into the deployment.

Page 16: Capistrano 2 Rocks My World

DB Backup - Rake

desc "Dumps the database into db/env-data.sql."task :dump => :environment do abc = ActiveRecord::Base.configurations[RAILS_ENV] cmd = ['mysqldump'] cmd << "--host='#{abc['host']}'" unless abc['host'].blank? cmd << "--user='#{abc['username']}'" cmd << "--password='#{abc['password']}'" cmd << abc['database'] cmd << " | gzip > #{RAILS_ROOT}/db/#{RAILS_ENV}-data.sql.gz" sh cmd.flatten.join ' 'end

Page 17: Capistrano 2 Rocks My World

DB Backup - Capfile

desc “Back up the production database”task :backup, :roles => :db, :only => { :primary => true } do rake = fetch(:rake, 'rake') rails_env = fetch(:rails_env, 'production')

run "cd #{current_path}; #{rake} RAILS_ENV=#{rails_env} db:dump" get "#{current_path}/db/#{rails_env}-data.sql.gz", "db/#{rails_env}-data.sql.gz"end

Page 18: Capistrano 2 Rocks My World

Hook into deployment

before "deploy:migrate", "db:backup"

Page 19: Capistrano 2 Rocks My World

All in a plugin

$ script/plugin install -x \http://svn.rubaidh.com/plugins/trunk/rubaidh_platform

Page 20: Capistrano 2 Rocks My World

“I checked out your slides, and they look great! Best of luck on your presentation.”

- Jamis Buck

Page 21: Capistrano 2 Rocks My World

What else do you want to know?