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 http://dev.rubyonrails.org/svn/rails/trunk/switchtower/ 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"
end
desc "Rollback to the release before the current release in production"
task :rollback do
  system "script/switchtower -vvvv -r config/deploy -a rollback"
end
desc "Enumerate all available deployment tasks"
task :show_deploy_tasks do
  system "script/switchtower -r config/deploy -a show_tasks"
end
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"
  end
  actions = ENV['ACTION'].split(",").map { |a| "-a #{a}" }.join(" ")
  system "script/switchtower -vvvv -r config/deploy #{actions}"
end

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)
  end

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
visudo

add the line

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

Comments

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 (akismet.com). If that's not acceptable for you, you can also reach me by other means.