Overriding Capistrano

Posted by nick.stielau on September 07, 2007

Capistrano rocks for deployment, but I found myself wanting to override some spefic parts of the gem. I realize that I am a little late on the scene, as Capistrano 2.0 is out and about, but this solved some of my problems.

Specifically, I wanted to customize the SVN checkout to use sudo, have sudo default to specific user, and add a deployment message to the revisions log. This is all easy to do without editing the capistrano gem. I looked around the gem source and found the code that I wanted to override, and then pulled it out and tweaked it.

In deploy.rb

RUBY:
  1. class ::Capistrano::SCM::Subversion
  2.   alias_method :non_sudo_checkout, :checkout
  3.   def checkout(actor)
  4.     op = 'export'
  5.     command = "sudo -u USER #{svn} #{op} -q --config-dir /home/USER/.subversion -r#{configuration.revision} #{configuration.repository} #{actor.release_path} &&"
  6.    run_checkout(actor, command, &svn_stream_handler(actor))
  7.   end
  8. end
  9.  
  10. class ::Capistrano::Actor
  11.   alias_method :sudo_with_optional_user, :sudo
  12.   def sudo(command, options={}, &block)
  13.     sudo_with_optional_user(command, {:as =>; 'SUDO_USER'}.merge(options), &block)
  14.   end
  15. end
  16.  
  17.  
  18. class ::Capistrano::SCM::Base
  19.   alias_method :logging_commands_without_message, :logging_commands
  20.   def logging_commands(directory = nil)
  21.     log = "#{configuration.deploy_to}/revisions.log"
  22.     "(test -e #{log} || (touch #{log} && chmod 666 #{log})) && " +
  23.     "echo `date +\"%Y-%m-%d %H:%M:%S\"` $USER #{configuration.revision} #{directory} '#{configuration.message}'>> #{log};"
  24.   end
  25. end