Capistrano Tutorial

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

5 comments

Comments 1 - 5 of 5 previous next Post a comment

  • + Anksta Anksta 4 months ago
    what crap??
  • + guestb150e58 guestb150e58 11 months ago
    The slideshow presented is for MySQL Storage Engines NOT capistrano as the title implies.
  • + gisellas gisellas 2 years ago
    Slideshowis incorrect (capistrano tutorial)
  • + guestb00611 guestb00611 2 years ago
    Man! This tutorial is awesome!
  • + Kendog Kendog 2 years ago
    1. I couldn’t view the slideshow

    2. I couldn’t download it even after creating an account
Post a comment
Embed Video
Edit your comment Cancel

1 Favorite

Capistrano Tutorial - Presentation Transcript

  1. Harnessing Capistrano Jamis Buck, 37signals Portland, Oregon: RailsConf 2007
  2. Online http://presentations.jamisbuck.org/railsconf2007
  3. A deployment tool
  4. And so much more!
  5. Ad-hoc monitoring • Query all servers in your cluster at once • uptime • uname • ps • df • tail • grep • Query status of all DB slaves
  6. Server maintenance • Editing NFS mountpoints • Creating symlinks • pkg_add $HOME/imagemagick.tbz • Keeping config files up-to-date and consistent
  7. Troubleshooting • 37signals Highrise: cap dump_email • clear stubborn caches (cap uncache_page_limit FOR=xyz) • grep • access logs • rails logs • mail logs
  8. “I wish I had cap back when I was managing hundreds of boxes at AOL. I had a bunch of hacked-together perl and shell scripts that did a tiny fraction of what cap does.” — Mark Imbriaco, System Administrator, 37signals
  9. “If you ever teach a yodeling class, probably the hardest thing is to keep the students from just trying to yodel right off. You see, we build to that.” — Jack Handey, SNL
  10. Installation • 1.4.1: capistrano gem install • 2.0 preview: gem install net-ssh gem install net-sftp gem install highline gem install -s http://gems.rubyonrails.com capistrano
  11. The Basics • What does Capistrano do? • What does it need to know? • What assumptions does it make? • SSH access • POSIX target • public keys or identical passwords
  12. Capfile • Makefile :: Make Rakefile :: Rake Capfile :: Cap • define servers, tasks, settings • Ruby DSL (rake-like, but NOT rake- compatible)
  13. A Simple Example $ cat Capfile role :logs, \"capistrano.demo\" task :grep_maillog do run \"grep jamis /var/log/maillog\" end
  14. demo: 001-grep
  15. Gateways $ cat Capfile set :gateway, \"capistrano.demo\" role :logs, \"web1\" task :tail_uploadlog do run \"tail $HOME/log/upload.log\" end
  16. demo: 002-gateway
  17. Multiple Servers $ cat Capfile set :gateway, \"capistrano.demo\" role :logs, \"web1\", \"web2\" task :tail_uploadlog do run \"tail $HOME/log/upload.log\" end
  18. demo: 003-multiple-servers
  19. Multiple Roles $ cat Capfile set :gateway, \"capistrano.demo\" role :web, \"web1\", \"web2\" role :app, \"app1\", \"app2\" task :tail_uploadlog, :roles => :web do run \"tail $HOME/log/upload.log\" end task :tail_crashlog, :roles => :app do run \"tail $HOME/log/fastcgi.crash.log\" end
  20. demo: 004-multiple-roles
  21. Documenting Tasks $ cat Capfile set :gateway, \"capistrano.demo\" role :web, \"web1\", \"web2\" role :app, \"app1\", \"app2\" desc \"Shows the tail of the upload log.\" task :tail_uploadlog, :roles => :web do run \"tail $HOME/log/upload.log\" end desc \"Shows the tail of the fastcgi crash log.\" task :tail_crashlog, :roles => :app do run \"tail $HOME/log/fastcgi.crash.log\" end
  22. demo: 005-desc
  23. cap -e invoke • cap invoke COMMAND=’ruby -v’ • cap invoke ROLES=app \\ COMMAND='gem list rake | grep ^rake' • script-friendly • doesn’t even require a Capfile! • cap invoke -s gateway=capistrano.demo HOSTS=app1,app2 COMMAND=uname
  24. demo: 006-invoke
  25. Nice, but... • Needs to reconnect on each invocation • Lots of typing • If you need to run several commands in sequence, there is a better way!
  26. cap -e shell • Caches connections • Execute both tasks and commands • Scope invocations by role, or by host • Still experimental (but highly useful!)
  27. demo: 007-shell
  28. Namespaces $ cat Capfile set :gateway, \"capistrano.demo\" role :web, \"web1\", \"web2\" role :app, \"app1\", \"app2\" namespace :tail do desc \"Shows the tail of the upload log.\" task :uploadlog, :roles => :web do run \"tail $HOME/log/upload.log\" end desc \"Shows the tail of the fastcgi crash log.\" task :crashlog, :roles => :app do run \"tail $HOME/log/fastcgi.crash.log\" end end
  29. demo: 008-namespace
  30. Default Tasks namespace :tail do task :default do # invoke as “cap tail” end end
  31. Variables set :prompt, false set(:log_file) do if prompt Capistrano::CLI.ui.ask( \"Which log file should we tail: \") else \"upload.log\" end end task :tail_log do run \"tail $HOME/log/#{log_file}\" end
  32. demo: 009-variables
  33. cap -s • -s foo=bar • Sets a variable after all recipes are loaded • Can be given multiple times • -S foo=bar (to set before recipes are loaded)
  34. Transactions task :first do transaction do second third end end task :second do on_rollback { system “rm -f second” } puts “creating `second’” system “touch second” end task :third do on_rollback { system “rm -f third” } puts “creating `third’” system “touch third” abort “!!! something went wrong !!!” end
  35. Loading Files load_paths << “config/deploy” case ENV[’STAGE’] when “production” then load “production” when “staging” then load “staging” else abort “unknown stage: #{ENV[’STAGE’]}” end
  36. cap -f file • Loads the given Capistano recipe file • Will not autoload Capfile if -f is given • Use -F to force Capfile to be loaded, too • Can specify multiple -f switches • cap -f deploy -f upgrade -T
  37. cap -vvv -q • “-v” is “verbose” • may be given multiple times • default is “-vvv” (maximum verbosity) • “-q” is minimal verbosity (only critical output is displayed)
  38. Fundamentally... That’s it!
  39. In practice... Deployment
  40. Assumptions • Source code management • Web application (Rails) • Production environment is all ready (DB, web server, etc.) • Standalone FCGI listeners
  41. capify . • minimal Capfile • basic config/deploy.rb
  42. capify ➠ Capfile $ cat Capfile load 'deploy' if respond_to?(:namespace) load 'config/deploy'
  43. capify ➠ config/deploy $ cat config/deploy.rb set :application, \"set your application name here\" set :repository, \"set your repository location here\" # If you aren't deploying to /u/apps/#{application} on the # target servers (which is the default), you can specify the # actual location via the :deploy_to variable: # set :deploy_to, \"/var/www/#{application}\" # If you aren't using Subversion to manage your source code, # specify your SCM below: # set :scm, :subversion role :app, \"your app-server here\" role :web, \"your web-server here\" role :db, \"your db-server here\", :primary => true
  44. Fill in the blanks $ cat config/deploy.rb set :application, \"pepcake\" set :repository, \"http://svn.jamisbuck.org/projects/pepcake/\" set :deploy_to, \"$HOME/rails-apps/#{application}\" role :app, \"comox.textdrive.com\" role :web, \"comox.textdrive.com\" role :db, \"comox.textdrive.com\", :primary => true
  45. demo: 011-deploy:setup
  46. Deployment Tree :deploy_to current releases 20070502124420 shared 20070507080815 20070512173751 log pids system
  47. Check Dependencies • Do the necessary directories exist? • Are they readable? writable? • Is Subversion installed? • Etc.
  48. demo: 012-deploy:check
  49. Custom Dependencies • Local vs. Remote • Is a particular gem installed? • depend :remote, :gem, ‘tzinfo’, ‘>=0.3.3’ • Does a directory exist? • depend :remote, :directory, “/path/to/dir” • Is a file writable? • depend :remote, :writable, “/dir/file.log” • Is a particular command present in the PATH? • depend :local, :command, “convert” • depend :remote, :command, “iconv”
  50. script/spin • Tells Capistrano how to start (”spin up”) your application • Many cases can just invoke script/process/ spawner • Called by the “deploy:start” task, to start your application
  51. script/spin (simple) $ cat script/spin /u/apps/backpack/current/script/process/spawner fcgi \\ -p 9000 \\ -i 5 \\ -r 5
  52. script/spin (less simple) $ cat script/spin SPAWN=/usr/local/bin/spawn-fcgi ROOT=$HOME/rails-apps/pepcake CURRENT=$ROOT/current SHARED=$ROOT/shared RAILS_ENV=production $SPAWN \\ -f $CURRENT/public/dispatch.fcgi \\ -s $SHARED/sockets/dispatch.0.socket \\ -P $SHARED/pids/dispatch.0.pid
  53. script/spin alternatives • Process supervision (init, svscan, runit, etc.) • Override deploy:start (mongrel_cluster, etc.)
  54. sudo • Used (by default) by a few tasks: • deploy:start • deploy:restart • deploy:cleanup • deploy:stop • Override by setting :use_sudo to false
  55. deploy:cold • Deploys your source code (deploy:update) • Applies pending migrations (deploy:migrate) • Starts your application (deploy:start)
  56. demo: 013-deploy:cold
  57. What next? • cap deploy • cap deploy:rollback • cap deploy:migrations • cap deploy:web:disable • cap deploy:web:enable • cap deploy:cleanup
  58. Deployment Strategies • checkout (default) • export • copy (with checkout or export) • remote cache • build your own!
  59. :deploy_via set :deploy_via, :copy set :copy_strategy, :export set :copy_compression, :bzip2
  60. Not using Subversion? • bazaar (ng) • darcs • perforce • mercurial (coming soon) • git (coming soon) • others (write your own?)
  61. :scm set :scm, :darcs set :scm_command, “/opt/bin/darcs” set :scm_username, “bob” set :local_scm_command, “...”
  62. Useful Variables :ssh_options -- a hash of options to send to Net::SSH :application -- name of the application being deployed :repository -- SCM-specific location of the repository :scm -- name of SCM to use (:subversion) :deploy_via -- name of strategy to use (:checkout) :deploy_to -- path to deploy app to (”/u/apps/#{application}”) :revision -- the revision to deploy (”HEAD”) :use_sudo -- whether or not to use sudo (true) :group_writable -- whether releases should be g+w (true) :rake -- location of the `rake’ utility (”rake”) :current_path -- path to the `current’ symlink :release_path -- path of the release being deployed :shared_path -- path to the `shared’ directory
  63. Task “Helpers” run(cmd, opts={}) -- execute the given command on the remote servers sudo(...) -- same as run(), but uses sudo to execute the cmd transaction -- calls rollback blocks if an error occurs on_rollback -- saves a proc to be called if an error occurs put(data, path) -- stores the given data to the remote servers get(remote, local) -- retrieves the given file stream(...) -- same as run(), but omits the prefix on output capture(...) -- same as run(), but returns output as a string connect!(opts={}) -- forces open connections to servers immediately execute_on_servers -- connects to servers and yields the list current_task -- a reference to the task that is executing execute_task(task) -- executes a specific task object find_task(name) -- looks up a task, by name find_servers(opts={}) -- finds all matching servers
  64. “Advanced” Techniques
  65. Extending Tasks before :deploy, :run_tests after “deploy:update_code”, :copy_database_yml task :run_tests do system “rake” or abort “tests failed” end task :copy_database_yml do put File.read(”database.yml.prod”), “#{release_path}/config/database.yml” end
  66. Event Callbacks # generally... on :event, :callback, :only => %w(tasks here) # :event may be :load, :exit, :start, :finish, # :begin, :after # specifically... on :load, :display_banner task :display_banner do puts “=====================================” puts “Welcome, stranger!” puts “=====================================” end
  67. Custom Callbacks task :backup, :roles => :db do trigger :notify, current_task # ... end on :notify, :email_people on :notify, :im_people, :except => :backup task :email_people do # ... end task :im_people do # ... end
  68. Staging Environments STAGES = %w(staging production) STAGES.each do |name| desc “Set the target stage to `#{name}'.” task(name) do set :stage, name.to_sym load \"config/deploy/#{stage}\" end end on :start, :except => STAGES do if !exists?(:stage) warn \"Defaulting to `#{STAGES.first}'\" find_and_execute_task(STAGES.first) end end
  69. Upgrading to Capistrano 2.0
  70. capify .
  71. Gotchas • 3rd party extensions, libraries • Overridden or extended tasks (namespaces, changed task names) • Different variables (svn -> scm_command, etc.)
  72. “compat” mode • provides tasks with the old names that delegate to the new names • cap -Ff compat ... • load “compat” • mostly useful to help learn the new task names
  73. demo: 014-compat
  74. cap -Ff upgrade upgrade:revisions
  75. Any Questions? http://www.capify.org

+ vishnuvishnu, 3 years ago

custom

10305 views, 1 favs, 3 embeds more stats

Jamis Buck, 37signals
Portland, Oregon: RailsConf more

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 10305
    • 10251 on SlideShare
    • 54 from embeds
  • Comments 5
  • Favorites 1
  • Downloads 221
Most viewed embeds
  • 51 views on http://www.railsdeployment.com
  • 2 views on http://natyuken.slideshow.com
  • 1 views on http://feeds.feedburner.com

more

All embeds
  • 51 views on http://www.railsdeployment.com
  • 2 views on http://natyuken.slideshow.com
  • 1 views on http://feeds.feedburner.com

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories