Sunday, July 22, 2007

Naughty Capistrano

Use at your own risk. Code responsibly. This is a little code recipe you probably shouldn't follow. It's a Capistrano recipe which defines a little

cap deploy:my_naughty_app

command. When you run this recipe, you'll deploy your new code and restart your Mongrel cluster, after first clearing your image and HTML caches.

So far, not actually so naughty. Seems like a regular law-abiding citizen.

The naughtiness starts when we get to the Unix command handling. First of all, the cache clearing is done with an explicit rm -rf. There's a Ruby rm_rf method somewhere in Capistrano that you should use instead, but I got lazy. Secondly, and more dangerously, the code uses a little helper method to enable you to go ahead with your recipe even if a Unix command within it fails. The danger there is putting something crucial in the failproofed section. (Bad thing, don't do it.)

The usefulness is that you could conceivably deploy right after clearing your cache, or conceivably start a Mongrel cluster on a new server that's never been running before. Depending how frequently you're doing that, and how many other people besides yourself are using your Cap recipes - and how fully they understand your backend system - it's a real practical concern.

The standard method for invoking commands on remote servers in Cap is invoke_command. But invoke_command throws an exception on failure, bringing the whole deployment to a halt. The helper method in the recipe gives you the option to reserve that level of response for mission-critical Unix commands. (It also demonstrates that since Capistrano recipes are written in Ruby, helper methods are easy and useful to add.)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.