Jens Krämer

Playing with SwitchTower

 |  ruby, rails

Notes about integrating SwitchTower into an existing rails 0.13.1 project. Probably not necessary if using a newer rails versions, since these have SwitchTower already integrated.

Be sure to read the SwitchTower documentation

Install SwitchTower

sudo gem install net-ssh
svn co switchtower
rake gem
sudo gem install pkg/switchtower-0.8.0.gem

Integrate SwitchTower into Rakefile

These tasks were taken from a recent Rakefile template from rails svn.

desc "Push the latest revision into production using the release manager"
task :deploy do
  system "script/switchtower -vvvv -r config/deploy -a deploy"
desc "Rollback to the release before the current release in production"
task :rollback do
  system "script/switchtower -vvvv -r config/deploy -a rollback"
desc "Enumerate all available deployment tasks"
task :show_deploy_tasks do
  system "script/switchtower -r config/deploy -a show_tasks"
desc "Execute a specific action using the release manager"
task :remote_exec do
  unless ENV['ACTION']
    raise "Please specify an action (or comma separated list of actions) via the ACTION environment variable"
  actions = ENV['ACTION'].split(",").map { |a| "-a #{a}" }.join(" ")
  system "script/switchtower -vvvv -r config/deploy #{actions}"

Link the switchtower executable to where the Rakefile tasks expect it:

cd script/
ln -s /usr/bin/switchtower .

Support for non-standard ssh ports

Updated. Now the hack isn’t that dirty anymore :-).

Edit /usr/lib/ruby/gems/1.8/gems/switchtower-0.8.0/lib/switchtower/actor.rb and change the connect_to method of class DefaultConnectionFactory like this:

  def connect_to(server)
    port = ENV['SSH_PORT'] || 22
    SSH.connect(server, @config, port)

Now simply append SSH_PORT=1234 to your switchtower rake calls, if your ssh server listens to port 1234. Or, if you’re lazy like me, add the following to your Rakefile:

ENV['SSH_PORT'] = '4148'

setup remote dir

rake remote_exec ACTION=setup

allow server restart for normal user

apt-get install sudo

add the line

jk      ALL = NOPASSWD: /etc/init.d/apache2


You can use Markdown here.

For the sake of spam checking any data you submit, including your IP address, will be transferred to the US based Akismet web service ( If that's not acceptable for you, you can also reach me by other means.