Capistrano and CakePHP in Perfect Harmony
A comment on my post about Joel Moss’s Cake Migrations asked for more information about how I got Capistrano and CakePHP working together. So, here’s a quick blurb on what I had to do. Note that I’m assuming your familiar with Capistrano already; if that’s not the case, just read the excellent Capistrano documentation — it provides a much better introduction than I could hope to.
The biggest problem I found was that Capistrano thinks it has to kill and relaunch the web server on every deployment. I use Cake in concert with Apache, so restarting the web server is completely unnecessary. The second biggest problem was, ironically, Migrations; Capistrano is set up to use Rails migrations and Cake doesn’t have those. (And I haven’t sat down and updated my Capistrano configuration to use Joel’s migrations yet.) Luckily, both these problems are easily solved by just overriding the associated task with a stub.
Once the tasks that would fail have been removed, all that’s left is to modify the :update_code task so that it sets permissions properly on the Cake tmp directory and links the Cake cache and logs directories into the Capistrano shared folder.
# Override the Rails-specific tasks and make them stubs
task :restart, :roles => :app do
end
task :migrate, :roles => :db, :only => { :primary => true } do
end
task :spinner, :roles => :app do
end
# Rewrite deploy and setup to use Cake-appropriate shared folders
desc <<-DESC
Update all servers with the latest release of the
source code. All this does is do a checkout (as
defined by the selected scm module).
DESC
task :update_code, :roles => [:app, :db, :web] do
on_rollback { delete release_path, :recursive => true }
source.checkout(self)
run <<-CMD
rm -rf #{release_path}/app/tmp/logs #{release_path}/app/tmp/cache &&
ln -nfs #{shared_path}/app/tmp/logs #{release_path}/logs &&
ln -nfs #{shared_path}/app/tmp/cache #{release_path}/cache
CMD
end
desc "Set up the expected application directory structure on all boxes"
task :setup, :roles => [:app, :db, :web] do
run <<-CMD
mkdir -p -m 775 #{releases_path} #{shared_path}/cache &&
mkdir -p -m 777 #{shared_path}/logs
CMD
end

