Capistrano - Mike Clark
Posted by kev Fri, 23 Jun 2006 16:40:00 GMT
A big thanks to Jamis Buck who wrote Capistrano!
- Is deploying new releases of your app as effortless as breathing?
- Or is deployment choking the life out of your software?
- Capistrano (formerly Switchtower)
- noun - Software utility and framework that automates the deployment of web applications
- verb - to replace mundane deployment tasks with pure joy: “Stop wasting your time, just cap it already!”
- Capistrano automates mundane and annoyingly tedious deployment tasks.
- It scales!
- Getting your killer app deployed into production on launch day..
- Should be a few minutes of prep
- Fixing your first bug, and redeploying before anyone notices
- A few minutes of panic
- Deploying new releases to a cluster of machines, whenever you want, at the push of a button
- Priceless!
- Before we press on…
- We’re assuming you’ve already made the tough deployment decisions…
- Demo Assumptions
- Lighttpd, OSX, FastCGI, Subversion, One machine, Depot
- How do I install it?
gem install capistrano- Capify!
cap --apply-to /path/to/my/app MyAppName - Adds rake and recipe files
- Recipe
- Application Name
set :application, "depot"
- Repository
set :repository, "http://svn.yourhost.com/#{application}/trunk"
- Roles
- Used to define what roles which servers are playing.
rule :app, "app01.example.com", "app02.example.com"rule :web, "www01.example.com", "www02.example.com"rule :db, "db01.example.com", :primary => truerule :db, "db02.example.com"- web is for the webserver like lightty, :app is for handlers like fastcgi, db is for sql servers like mysql
- Deployment Root
- Where the deployed code goes
set :deploy_to, "/Library/Rails/#{application}"
- Need a deploy targer? Run once to create the deploy directories
cap setup- Demo
- Used
cap setupwhich created a skeleton directory structure.- Would have worked on a dozen servers identically if specified
- Used
cap updatewhich checked out a new copy of the app on the server- Releases directory now has timestamped subdirectory with the release
- current is a symlink to that release directory
- Here’s where it gets fun :)
- We update the code and run
cap deployand it goes off, checks out the new code and reloads the server. - We regret this.
cap rollback, it flips the symlink back to the last version and reloads the server.- Need some downtime?
- Disables web acces: puts up maintenance page
cap disable_web: puts up a maintenance screen and takes down your applicationcab enable_webto bring it back up.- You need this to be truly effortless when there’s a problem
- What’s the diff?
- What’s deployed vs what’s in repo
cap diff_from_last_deploy- It gets even better…
- Customizing and Extending (you know you want to)
Make a task
- Similar to
makeorrake - Task can define roles to use
- Can use
runto run commands on remote server - Can use variables inside tasks
- Can use multiple roles (app, web, and db)
- Chaining tasks easily:
task :status do which_ruby current_revision uptime end - Channels and Streams
tailthe logfile- Get a channel back which can be watched
Code run before or after a task
before_deploy…after_update_code…
Multiple Configurations
- It’s just ruby!
- Can do all sorts of conditionals
- If we’re in the production environment… else setup the dev environment
- But wait, there’s more
- Similar to
- Task Libraries
- Can reuse tasks across projects
- capistrano-ext - great example of how to write extension libraries
- Great example of how to write task libraries. Has
watch_loadtask.
- Great example of how to write task libraries. Has
- But this isn’t just a Rails or Ruby thing…
- It’s great at shuttling files along. Great for other systems as well.
- Remote Command and Control
- James Duncan Davidson has used it for numbers of servers in India as well as the US
- Capistrano just assumes…
- Remote servers talk POSIX
- Same deploy directory structure and password on each machine
- Can use public keys (and does by default)
- Web app uses FastCGI with Apache or LightTPD
- But can rewrite the restart task if you’d like for mongrel etc.
- As the number of machines and processes in your environment increases…
- you’re still typing exactly one command
- Can you deploy and roll back releases this easily and consistently on your project?
Q&A
Mike took questions from the crowd.
- How do you deal with permissions with multiple users?
- We use after hooks to deal with chown etc.
- Configurable option to use
sudo
- In these examples did your app server checkout from subversion?
- It did a checkout on my machine. You can configure it to do an export instead. The servers do need access to version control.
- If you’re always checking out the same code to the different servers.. is there ever a time you’re just checking out different files to servers with different roles?
- It needs all the code for migrations generally, but you’d override one of the tasks for a specific role.
- Sometimes I like to deploy alot but some are pretty big (30 or 40 megs of media files). Is there some way to not checkout each time?
- I’ve seen grassroots stuff to checkout once and rsync, but you can override the update task to deal with that.
- We’ve done that (someone in the crowd) that solves the problem.
- Who do I talk to about getting my tasks to capistrano into the mainline?
- You don’t. Publish task libraries and let the community get in if they’d like.
- Can I make capistrano prompt the user for which tag in subversion they’d like to use?
- Sure. You can specify the revision number and write ruby to prompt.
- How did you feel about Dave Thomas’s suggestions for changes to capistrano in his keynote?
- This isn’t the end goal. I think there will be a natural progression and “Capistrano II will” come out eventually.
- How does capistrano deal with database migrations?
- There is a task called
deploy_with_migrationswhich deals with it. This seems a bit scary, so I’d probably disable the web first, but the support is certainly there.
- There is a task called
- If you have a cluser of 10 machines do they go down in sequence?
- It’s done in parallel talking to each machine as best as possible.
- We’re in a Java shop, how can we use capistrano to tail logs and things like that?
- Capistrano could definitely move files (WAR files often) around for you. If you find yourself doing something manually in that setting, write your own tasks. You can always just write your own specific tasks and don’t have to use
cap deploy.
- Capistrano could definitely move files (WAR files often) around for you. If you find yourself doing something manually in that setting, write your own tasks. You can always just write your own specific tasks and don’t have to use
- When will there be a new version of “Project Automation”?
- Life gets in the way, so there’s nothing in the works.

