Created
January 9, 2013 08:58
-
-
Save code-later/4491696 to your computer and use it in GitHub Desktop.
Open multiple SSH tunnels to forward resque-web instances via Capistrano.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Capistrano::Configuration.instance(:must_exist).load do | |
set :resque_local_start_port, 7070 | |
set :resque_remote_port, 8282 | |
def resque_local_ports | |
@resque_local_ports ||= Enumerator.new do |yielder| | |
current_port = fetch(:resque_local_start_port) | |
loop do | |
yielder.yield current_port | |
current_port += 1 | |
end | |
end | |
end | |
namespace :resque do | |
desc "Opens SSH tunnels to all resque-web instances" | |
task :web, :roles => :app do | |
puts | |
puts " ** Opening SSH tunnels to resque-web instances ..." | |
pids = [] | |
app_servers = find_servers(:roles => :app).each | |
loop do | |
next_server = app_servers.next | |
local_port = resque_local_ports.next | |
pid = spawn "ssh -L #{local_port}:127.0.0.1:#{resque_remote_port} #{user}@#{next_server} -NT 2>&1 /dev/null" | |
Process.detach(pid) | |
pids << pid | |
puts " ** Open your browser at http://localhost:#{local_port}" | |
end | |
puts " ** Pids of the SSH tunnels: #{pids.inspect}" | |
puts " ** Waiting for INT ..." | |
Signal.trap("INT") do | |
puts " ** Shutting down ..." | |
pids.each do |pid| | |
puts " ** Trying to kill #{pid} ..." | |
begin | |
Process.kill("QUIT", pid) | |
rescue | |
end | |
puts " * Process #{pid} killed!" | |
end | |
puts | |
puts " ** All done!" | |
puts | |
exit | |
end | |
while true | |
sleep 1 | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment