Home > Development, Linux > easy capistrano remote invocation

easy capistrano remote invocation

May 6th, 2009

Today I was coding just for fun … and wrote my own Capistrano script for deployment, during this I have found great way to invoke remote tasks.

The method is easy, add following code to your config/deploy.rb file:

set :sudo_call, ''
desc 'makes remote/rake calls to be executed with sudo'
task :use_sudo do
  set :sudo_call, 'sudo'
end

desc 'run rake task'
task :rake do
  ARGV.values_at(Range.new(ARGV.index('rake')+1,-1)).each do |task|
    run "cd #{current_path}; #{sudo_call} RAILS_ENV=production rake #{task}"
  end
  exit(0)
end

desc 'run remote command'
task :remote do
  command=ARGV.values_at(Range.new(ARGV.index('remote')+1,-1))
  run "cd #{current_path}; #{sudo_call} RAILS_ENV=production #{command*' '}"
  exit(0)
end

desc 'run specified rails code on server'
task :runner do
  command=ARGV.values_at(Range.new(ARGV.index('runner')+1,-1))
  run "cd #{current_path}; RAILS_ENV=production script/runner '#{command*' '}'"
  exit(0)
end

Now try your new tool with following commands:

cap rake db:migrate
cap use_sudo rake db:migrate
cap remote "tail -n 10 log/production.log"
cap use_sudo remote cat /etc/passwd
cap runner p User.all
cap runner "User.all.each{ |u| p u }"

In the third call I have used parentheses to hide “-n” form Capistrano, because it is its parameter, to see whole list of Capistrano parameters call it with “cap –help”. For the last command I have used parentheses again because now it contained bash special characters.

Did You liked this post or maybe not, vote on it at dzone.

Categories: Development, Linux Tags: , , , ,
Comments are closed.