Capistrano

2,657 views
2,601 views

Published on

Walk through on how to deploy applications via capistrano

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,657
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
21
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Capistrano

  1. 1. Capistrano by Jason Noble http://jasonnoble.org
  2. 2. Getting Started <ul><li>http://github.com/jasonnoble/capistrano-demo/ </li></ul><ul><li>Click fork </li></ul><ul><li>Click “Your Clone URL” </li></ul><ul><li>Paste into a terminal window </li></ul><ul><li>Two branches in this repo </li></ul><ul><ul><li>master (base shopping cart, no capistrano changes) </li></ul></ul><ul><ul><li>working_capistrano_deploys </li></ul></ul>
  3. 3. Quick Demo of App <ul><li>Create/Migrate databases </li></ul><ul><ul><li>rake db:create:all </li></ul></ul><ul><ul><li>rake db:migrate </li></ul></ul><ul><li>script/server </li></ul><ul><li>http://localhost:3000/store </li></ul><ul><li>http://localhost:3000/admin </li></ul><ul><ul><li>admin/admin </li></ul></ul><ul><li>http://localhost:3000/users </li></ul>
  4. 4. Server preperation <ul><li>Install Git (1.5.5.6) </li></ul><ul><li>Install Rake gem (0.8.7) </li></ul><ul><li>Install Rails gem (2.3.5) </li></ul><ul><li>Install mongrel_cluster gem (1.0.5) </li></ul><ul><li>Mysql server / Mysql gem </li></ul>
  5. 5. Local Preparation <ul><li>sudo gem install capistrano </li></ul><ul><ul><li>Latest is 2.5.10 (as of 11/2009) </li></ul></ul><ul><li>capify . </li></ul><ul><ul><li>Creates ./Capfile </li></ul></ul><ul><ul><li>Creates ./config/deploy.rb </li></ul></ul>
  6. 6. config/deploy.rb <ul><li>set :application, “shopping_cart” </li></ul><ul><li>set :repository, git@github.com: jasonnoble /capistrano-demo.git </li></ul><ul><li>set :scm, :git </li></ul><ul><li>set :user, “ jasonn ” </li></ul><ul><li>default_run_options[:pty] = true </li></ul><ul><ul><li>Enables password entry for git </li></ul></ul><ul><li>set :branch, ”master” </li></ul><ul><li>set :deploy_to, &quot;/var/www/#{user}/#{application}” </li></ul><ul><li>set :deploy_via, :remote_cache </li></ul>
  7. 7. config/deploy.rb cont. <ul><li>role :web, “ 192.168.255.54 ” </li></ul><ul><li>role :app, “ 192.168.255.54 ” </li></ul><ul><li>role :db, “ 192.168.255.54 ”, :primary => true </li></ul><ul><li>set :mongrel_conf, “#{current_path}/config/mongrel_cluster.yml” </li></ul><ul><li>set :use_sudo, false </li></ul>
  8. 8. config/deploy.rb cont. <ul><li>namespace :deploy do </li></ul><ul><li>namespace :mongrel do </li></ul><ul><li>[:stop, :start, :restart ].each do |t| </li></ul><ul><li>desc &quot;#{t.to_s.capitalize} the mongrel appserver&quot; </li></ul><ul><li>task t, :roles => :app do </li></ul><ul><li>invoke_command &quot;mongrel_rails cluster::#{t.to_s} -C #{mongrel_conf}&quot;, :via => run_method </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>desc &quot;Customer restart task for mongrel cluster&quot; </li></ul><ul><li>task :restart, :roles => :app, :except => { :no_release => true } do </li></ul><ul><li>deploy.mongrel.restart </li></ul><ul><li>end </li></ul><ul><li>desc &quot;Customer start task for mongrel cluster&quot; </li></ul><ul><li>task :start, :roles => :app, :except => { :no_release => true } do </li></ul><ul><li>deploy.mongrel.start </li></ul><ul><li>end </li></ul><ul><li>desc &quot;Customer stop task for mongrel cluster&quot; </li></ul><ul><li>task :stop, :roles => :app, :except => { :no_release => true } do </li></ul><ul><li>deploy.mongrel.stop </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
  9. 9. config/mongrel_cluster.yml <ul><li>--- </li></ul><ul><li>cwd: /var/www/rails/ jasonn /shopping_cart/current </li></ul><ul><li>log_file: log/mongrel.log </li></ul><ul><li>port: &quot; 7010 ” </li></ul><ul><li>environment: production </li></ul><ul><li>address: 0.0.0.0 </li></ul><ul><li>pid_file: tmp/pids/mongrel.pid </li></ul><ul><li>servers: 2 </li></ul>
  10. 10. config/database.yml <ul><li>Production setting, change </li></ul><ul><li>socket: /tmp/mysql.sock </li></ul><ul><li>to </li></ul><ul><li>socket: /var/lib/mysql/mysql.sock </li></ul><ul><li>Modify the database name to be unique </li></ul>
  11. 11. Setup SSH keys on the server <ul><li>scp ~/.ssh/id_dsa.pub 192.168.255.54:~/ </li></ul><ul><li>ssh [email_address] </li></ul><ul><ul><li>Login with your password </li></ul></ul><ul><li>mkdir –p ~/.ssh </li></ul><ul><li>chmod 755 ~/.ssh </li></ul><ul><li>mv ~/id_dsa.pub .ssh/authorized_keys </li></ul><ul><li>Logout. When you log back in it should not ask for a password. </li></ul><ul><li>While logged in, run the following: </li></ul><ul><ul><li>ssh github.com </li></ul></ul><ul><ul><ul><li>This adds the GitHub host to your “known hosts” file </li></ul></ul></ul><ul><ul><ul><li>Will prevent later errors </li></ul></ul></ul>
  12. 12. Add Deploy SSH key to Github <ul><li>On your server: </li></ul><ul><ul><li>ssh-keygen –d </li></ul></ul><ul><ul><li>cat ~/.ssh/id_dsa.pub </li></ul></ul><ul><ul><ul><li>Copy </li></ul></ul></ul><ul><li>http://github.com/jasonnoble/capistrano-demo/edit </li></ul><ul><ul><li>Click Add another deploy key </li></ul></ul><ul><ul><li>Paste your key into the box </li></ul></ul>
  13. 13. cap -T <ul><li>Capistrano uses the “cap” command </li></ul><ul><li>Similar to Rake, has tasks available </li></ul><ul><li>cap –T </li></ul><ul><ul><li>cap deploy # Deploys your project. </li></ul></ul><ul><ul><li>cap deploy:check # Test deployment dependencies. </li></ul></ul><ul><ul><li>cap deploy:cleanup # Clean up old releases. </li></ul></ul>
  14. 14. cap deploy:setup <ul><li>This makes a bunch of directories that capistrano needs to work </li></ul><ul><li>/var/www/rails/ username /app_name </li></ul><ul><ul><li>shared </li></ul></ul><ul><ul><li>releases </li></ul></ul><ul><li>Sets up directory ownership properly </li></ul>
  15. 15. cap deploy:check <ul><li>Verifies required directories are there </li></ul><ul><li>Tests SSH connectivity </li></ul><ul><li>Verifies some minimal gems are installed </li></ul>
  16. 16. Git installed in custom dir <ul><li>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) </li></ul><ul><li>Add the following to config/deploy.rb </li></ul><ul><ul><li>set :scm_command, &quot;/path/to/git/bin/git&quot; </li></ul></ul><ul><ul><li>set :local_scm_command, &quot;git&quot; </li></ul></ul>
  17. 17. cap deploy:cold <ul><li>First time deploy method </li></ul><ul><li>This should error out when trying to use the database (we haven’t created it!) </li></ul><ul><li>Login to the server </li></ul><ul><ul><li>RAILS_ENV=production rake db:create </li></ul></ul><ul><li>Logout and run cap deploy:cold again </li></ul>
  18. 18. Your site is live! <ul><li>http://192.168.255.54:7010/ </li></ul><ul><ul><li>Use the mongrel port you specified in config/mongrel_cluster.yml </li></ul></ul>
  19. 19. Make a change to the rails app <ul><li>Change the text in one of the views </li></ul><ul><li>commit it via git </li></ul><ul><li>cap deploy </li></ul><ul><ul><li>We use deploy instead of deploy:cold because it’s already been deployed once </li></ul></ul>
  20. 20. Questions?

×