0
Capistrano 2
Beyond Hassle-Free Deployment
          Luca Mearelli
       Pisa - Oct. 27, 2007
Rails is fun
Rails is fun
Rails is TOO MUCH fun
Deployment
System Administration
Rails : Web development
              =
Capistrano : Sys.administration
Capistrano basics

•SSH to the remote machines
•Posix compatible shell
•Same password or public key
•a bit of ruby knowled...
Capistrano basics

•Capfile (makefile, rakefile, ...)
•It’s ruby
 task :dump_db, :hosts => quot;database.spazidigitali.comquo...
role :sd_db, quot;database.spazidigitali.comquot;

task :dump_db do
  run quot;mysqldump -u deploy sd_production > /home/l...
set :gateway, quot;firewall.spazidigitali.comquot;
role :sd_db, quot;database.spazidigitali.comquot;

task :dump_db do
  r...
set :gateway, quot;firewall.spazidigitali.comquot;
role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidi...
set :gateway, quot;firewall.spazidigitali.comquot;
role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidi...
set :gateway, quot;firewall.spazidigitali.comquot;
role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidi...
set :gateway, quot;firewall.spazidigitali.comquot;
role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidi...
set :gateway, quot;firewall.spazidigitali.comquot;
role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidi...
set :gateway, quot;firewall.spazidigitali.comquot;
role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidi...
task :deploy do
  transaction do
    try_this
    then_do_that

  end
end

task :try_this do
  on_rollback { run quot;do_c...
on :event, :callback, :only => %w(tasks_to_do)
# :load, :exit, :start, :finish, :begin, :after


trigger :notify, current_...
load_paths << quot;config/deployquot;
case ENV['STAGE']
when quot;productionquot; then
  load quot;productionquot;
when qu...
before :deploy, :run_tests
after quot;deploy:update_codequot;, :link_database_yml
task :run_tests do
  system quot;rakequo...
Cap
•cap -T	
•cap <some task>
•cap invoke COMMAND=quot;df -hquot;
•cap invoke COMMAND=quot;df -hquot; ROLES=sd_db
•cap inv...
Capify
  capify .




•Creates a Capfile
•Creates a deploy “configuration” file
Cap
•cap deploy:setup       •cap deploy
•cap deploy:check       •cap deploy:rollback
•cap deploy:cold        •cap deploy:m...
deploy:setup
•<deploy_to>
 •current
 •releases
  •200706060120
  •200702020815
 •shared
  •log
  •pids
  •system
deploy:check


depend   :remote, :gem, 'rpdf', '>=1.1.4'
depend   :remote, :directory, quot;/tmp/railsquot;
depend   :remo...
deploy:cold

•copies the code - cap deploy:update
•migrates the database - cap deploy:migrate
•starts the service - cap de...
Variables
•:ssh_options         •:use_sudo
•:application         •:group_writable
•:repository          •:rake
•:scm      ...
Deployment strategies

•Checkout (classic capistrano)
•Remote cache (svn up)
•Export
•Copy (via checkout or export, with c...
Example*
 *sorta real
That’s all folks


•Luca Mearelli
•http://spazidigitali.com
•lucamea@gmail.com
Capistrano2
Upcoming SlideShare
Loading in...5
×

Capistrano2

1,043

Published on

An introduction to deploying rails applications with capistrano I gave at RailsToItaly in 2007

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,043
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
34
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Transcript of "Capistrano2"

  1. 1. Capistrano 2 Beyond Hassle-Free Deployment Luca Mearelli Pisa - Oct. 27, 2007
  2. 2. Rails is fun
  3. 3. Rails is fun Rails is TOO MUCH fun
  4. 4. Deployment
  5. 5. System Administration
  6. 6. Rails : Web development = Capistrano : Sys.administration
  7. 7. Capistrano basics •SSH to the remote machines •Posix compatible shell •Same password or public key •a bit of ruby knowledge •works from a command line
  8. 8. Capistrano basics •Capfile (makefile, rakefile, ...) •It’s ruby task :dump_db, :hosts => quot;database.spazidigitali.comquot; do run quot;mysqldump -u deploy sd_production > /home/luca/dumps/db.sqlquot; end cap dump_db
  9. 9. role :sd_db, quot;database.spazidigitali.comquot; task :dump_db do run quot;mysqldump -u deploy sd_production > /home/luca/dumps/db.sqlquot; end task :free_space do run quot;df -hquot; end cap dump_db
  10. 10. set :gateway, quot;firewall.spazidigitali.comquot; role :sd_db, quot;database.spazidigitali.comquot; task :dump_db do run quot;mysqldump -u deploy sd_production > /home/luca/dumps/db.sqlquot; end task :free_space do run quot;df -hquot; end cap dump_db
  11. 11. set :gateway, quot;firewall.spazidigitali.comquot; role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidigitali.comquot; task :dump_db do run quot;mysqldump -u deploy sd_production > /home/luca/dumps/db.sqlquot; end task :free_space do run quot;df -hquot; end cap dump_db
  12. 12. set :gateway, quot;firewall.spazidigitali.comquot; role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidigitali.comquot; role :sd_app, quot;mongrels.spazidigitali.comquot; task :dump_db, :roles=>:sd_db do run quot;mysqldump -u deploy sd_production > /home/luca/dumps/db.sqlquot; end task :free_space, :roles=>[:sd_db, :sd_app ] do run quot;df -hquot; end cap dump_db
  13. 13. set :gateway, quot;firewall.spazidigitali.comquot; role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidigitali.comquot; role :sd_app, quot;mongrels.spazidigitali.comquot; namespace :spazidig do desc quot;Dumping our DBsquot; task :dump_db, :roles=>:sd_db do run quot;mysqldump -u deploy sd_production > /home/luca/dumps/db.sqlquot; end desc quot;Checking free spacequot; task :free_space, :roles=>[:sd_db, :sd_app ]do run quot;df -hquot; end end cap spazidig:dump_db
  14. 14. set :gateway, quot;firewall.spazidigitali.comquot; role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidigitali.comquot; role :sd_app, quot;mongrels.spazidigitali.comquot; namespace :spazidig do desc quot;What??quot; task :default do run quot;uname -aquot; end desc quot;Dumping our DBsquot; task :dump_db, :roles=>:sd_db do run quot;mysqldump -u deploy sd_production > /home/luca/dumps/db.sqlquot; end desc quot;Checking free spacequot; task :free_space, :roles=>[:sd_db, :sd_app ]do run quot;df -hquot; end end cap spazidig spazidig:dump_db cap dump_db cap spazidig:free_space
  15. 15. set :gateway, quot;firewall.spazidigitali.comquot; role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidigitali.comquot; role :sd_app, quot;mongrels.spazidigitali.comquot; set :destination, quot;db.sqlquot; namespace :spazidig do desc quot;Dumping our DBsquot; task :dump_db, :roles=>:sd_db do run quot;mysqldump -u deploy sd_production > /home/luca/dumps/#{destination}.sqlquot; end end cap spazidig:dump_db
  16. 16. set :gateway, quot;firewall.spazidigitali.comquot; role :sd_db, quot;database.spazidigitali.comquot;, quot;replica.spazidigitali.comquot; role :sd_app, quot;mongrels.spazidigitali.comquot; set :destination, quot;db.sqlquot; set(:database) do Capistrano::CLI.ui.ask quot;Which database?: quot; end namespace :spazidig do desc quot;Dumping our DBsquot; task :dump_db, :roles=>:sd_db do run quot;mysqldump -u deploy #{database} > /home/luca/dumps/#{destination}.sqlquot; end end cap spazidig:dump_db
  17. 17. task :deploy do transaction do try_this then_do_that end end task :try_this do on_rollback { run quot;do_cleanupquot; } #... end task :then_do_that do on_rollback { run quot;do_super_cleanupquot; } #... end
  18. 18. on :event, :callback, :only => %w(tasks_to_do) # :load, :exit, :start, :finish, :begin, :after trigger :notify, current_task on :notify, :callback
  19. 19. load_paths << quot;config/deployquot; case ENV['STAGE'] when quot;productionquot; then load quot;productionquot; when quot;stagingquot; then load quot;stagingquot; else abort quot;unknown stage: #{ENV['STAGE']}quot; end
  20. 20. before :deploy, :run_tests after quot;deploy:update_codequot;, :link_database_yml task :run_tests do system quot;rakequot; or abort quot;tests failedquot; end task :link_database_yml do run quot;ln -s #{release_path}/config/database.yml #{shared_path}/database.ymlquot; end
  21. 21. Cap •cap -T •cap <some task> •cap invoke COMMAND=quot;df -hquot; •cap invoke COMMAND=quot;df -hquot; ROLES=sd_db •cap invoke COMMAND=quot;df -hquot; HOSTS=www.spazidigitali.com •cap shell
  22. 22. Capify capify . •Creates a Capfile •Creates a deploy “configuration” file
  23. 23. Cap •cap deploy:setup •cap deploy •cap deploy:check •cap deploy:rollback •cap deploy:cold •cap deploy:migrations •cap deploy:update •cap deploy:web:disable •cap deploy:migrate •cap deploy:web:enable •cap deploy:start •cap deploy:cleanup
  24. 24. deploy:setup •<deploy_to> •current •releases •200706060120 •200702020815 •shared •log •pids •system
  25. 25. deploy:check depend :remote, :gem, 'rpdf', '>=1.1.4' depend :remote, :directory, quot;/tmp/railsquot; depend :remote, :writable, quot;/dir/file.logquot; depend :local, :command, quot;gemquot; depend :remote, :command, quot;mysqldumpquot;
  26. 26. deploy:cold •copies the code - cap deploy:update •migrates the database - cap deploy:migrate •starts the service - cap deploy:start
  27. 27. Variables •:ssh_options •:use_sudo •:application •:group_writable •:repository •:rake •:scm •:current_path •:deploy_via •:release_path •:deploy_to •:shared_path •:revision
  28. 28. Deployment strategies •Checkout (classic capistrano) •Remote cache (svn up) •Export •Copy (via checkout or export, with compression)
  29. 29. Example* *sorta real
  30. 30. That’s all folks •Luca Mearelli •http://spazidigitali.com •lucamea@gmail.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×