Permalink
Browse files

Hop's are starting to work. next is just cleanup, boilerplate, and

documenation
  • Loading branch information...
1 parent 64d83ad commit 30d4c1ee9142d75e393579d32808769bab4635f9 @vbatts vbatts committed Jan 27, 2011
Showing with 89 additions and 26 deletions.
  1. +31 −11 lib/rye/box.rb
  2. +32 −13 lib/rye/hop.rb
  3. +1 −2 rye.gemspec
  4. +25 −0 test_hop.rb
View
@@ -199,16 +199,20 @@ def ssh_config_options(host)
# h2 = Rye::Hop.new "host2"
# h1.via_hop h2
#
- def via_hop(*hops)
- hops = hops.flatten.compact
- if hops.first.nil?
+ def via_hop(*args)
+ args = args.flatten.compact
+ if args.first.nil?
return @rye_via
- elsif hops.first.is_a?(Rye::Hop)
- @rye_via = hops.first
- elsif hops.first.is_a?(String)
- hop = hops.shift
- if hops.first.is_a?(Hash)
- @rye_via = Rye::Hop.new(hop, hops.first)
+ elsif args.first.is_a?(Rye::Hop)
+ @rye_via = args.first
+ elsif args.first.is_a?(String)
+ hop = args.shift
+ if args.first.is_a?(Hash)
+ @rye_via = Rye::Hop.new(hop, args.first.merge(
+ :debug => @rye_debug,
+ :info => @rye_info,
+ :error => @rye_error)
+ )
else
@rye_via = Rye::Hop.new(hop)
end
@@ -641,12 +645,24 @@ def connect(reconnect=true)
raise Rye::NoHost unless @rye_host
return if @rye_ssh && !reconnect
disconnect if @rye_ssh
- debug "Opening connection to #{@rye_host} as #{@rye_user}"
+ if @rye_via
+ debug "Opening connection to #{@rye_host} as #{@rye_user}, via #{@rye_via.host}"
+ else
+ debug "Opening connection to #{@rye_host} as #{@rye_user}"
+ end
highline = HighLine.new # Used for password prompt
retried = 0
@rye_opts[:keys].compact! # A quick fix in Windows. TODO: Why is there a nil?
begin
- @rye_ssh = Net::SSH.start(@rye_host, @rye_user, @rye_opts || {})
+ if @rye_via
+ # tell the +Rye::Hop+ what and where to setup,
+ # it returns the local port used
+ @rye_localport = @rye_via.fetch_port(@rye_host, @rye_opts[:port].nil? ? 22 : @rye_opts[:port] )
+ debug "fetched localport #{@rye_localport}"
+ @rye_ssh = Net::SSH.start("localhost", @rye_user, @rye_opts.merge(:port => @rye_localport) || {})
+ else
+ @rye_ssh = Net::SSH.start(@rye_host, @rye_user, @rye_opts || {})
+ end
rescue Net::SSH::HostKeyMismatch => ex
STDERR.puts ex.message
print "\a" if @rye_info # Ring the bell
@@ -692,6 +708,10 @@ def disconnect
end
debug "Closing connection to #{@rye_ssh.host}"
@rye_ssh.close
+ if @rye_via
+ debug "disconnecting Hop #{@rye_via.host}"
+ @rye_via.disconnect
+ end
rescue SystemCallError, Timeout::Error => ex
error "Disconnect timeout"
rescue Interrupt
View
@@ -1,5 +1,7 @@
# vim : set sw=2 ts=2 :
+require 'socket'
+
module Rye
DEBUG = false unless defined?(Rye::DEBUG)
@@ -123,11 +125,11 @@ def initialize(host, opts={})
# From: capistrano/lib/capistrano/cli.rb
STDOUT.sync = true # so that Net::SSH prompts show up
+ debug "starting the port forward thread"
+ port_loop
+
debug "ssh-agent info: #{Rye.sshagent_info.inspect}"
debug @rye_opts.inspect
-
- port_loop()
-
end
# * +hops+ Rye::Hop objects will be added directly
@@ -168,8 +170,11 @@ def fetch_port(host, port = 22, localport = nil)
else
port_used = localport
end
+ # i would like to check if the port and host
+ # are already an active_locals forward, but that
+ # info does not get returned, and trusting the localport
+ # is not enough information, so lets just set up a new one
@rye_ssh.forward.local(port_used, host, port)
- port_loop unless @thread.alive?
return port_used
end
@@ -228,7 +233,7 @@ def connect(reconnect=true)
return if @rye_ssh && !reconnect
disconnect if @rye_ssh
if @rye_via
- debug "Opening connection to #{@rye_host} as #{@rye_user}, via #{@rye_via}"
+ debug "Opening connection to #{@rye_host} as #{@rye_user}, via #{@rye_via.host}"
else
debug "Opening connection to #{@rye_host} as #{@rye_user}"
end
@@ -237,11 +242,10 @@ def connect(reconnect=true)
@rye_opts[:keys].compact! # A quick fix in Windows. TODO: Why is there a nil?
begin
if @rye_via
- # XXX
- # need a method to to the Hop to create the forward,
- # and return the localport to use here
- # fetch_port()
- @rye_ssh = Net::SSH.start(localhost, @rye_user, @rye_opts || {})
+ # tell the +Rye::Hop+ what and where to setup,
+ # it returns the local port used
+ @rye_localport = @rye_via.fetch_port(@rye_host, @rye_opts[:port].nil? ? 22 : @rye_opts[:port] )
+ @rye_ssh = Net::SSH.start("localhost", @rye_user, @rye_opts.merge(:port => @rye_localport) || {})
else
@rye_ssh = Net::SSH.start(@rye_host, @rye_user, @rye_opts || {})
end
@@ -291,14 +295,20 @@ def remove_hops!
def disconnect
return unless @rye_ssh && !@rye_ssh.closed?
begin
+ debug "killing port_loop @thread"
+ @thread.kill
if @rye_ssh.busy?;
info "Is something still running? (ctrl-C to exit)"
Timeout::timeout(10) do
@rye_ssh.loop(0.3) { @rye_ssh.busy?; }
end
end
debug "Closing connection to #{@rye_ssh.host}"
- @rye_ssh.shutdown!
+ @rye_ssh.close
+ if @rye_via
+ debug "disconnecting Hop #{@rye_via.host}"
+ @rye_via.disconnect
+ end
rescue SystemCallError, Timeout::Error => ex
error "Disconnect timeout"
rescue Interrupt
@@ -341,7 +351,7 @@ def port_loop
connect unless @rye_ssh
@active = true
@thread = Thread.new do
- while @active
+ while true
@rye_ssh.process(0.1)
end
end
@@ -352,7 +362,16 @@ def next_port
port = @next_port
@next_port -= 1
@next_port = MAX_PORT if @next_port < MIN_PORT
- port
+ # check if the port is in use, if so get the next_port
+ begin
+ TCPSocket.new '127.0.0.1', port
+ rescue Errno::EADDRINUSE
+ next_port()
+ rescue Errno::ECONNREFUSED
+ port
+ else
+ next_port()
+ end
end
def debug(msg="unknown debug msg"); @rye_debug.puts msg if @rye_debug; end
View
@@ -1,7 +1,7 @@
@spec = Gem::Specification.new do |s|
s.name = "rye"
s.rubyforge_project = "rye"
- s.version = "0.9.2"
+ s.version = "0.9.2_vb"
s.summary = "Rye: Safely run SSH commands on a bunch of machines at the same time (from Ruby)."
s.description = s.summary
s.author = "Delano Mandelbaum"
@@ -16,7 +16,6 @@
s.add_dependency 'highline', '>= 1.5.1'
s.add_dependency 'net-ssh', '>= 2.0.13'
s.add_dependency 'net-scp', '>= 1.0.2'
- s.add_dependency 'net-ssh-gateway'
#s.add_dependency 'net-ssh-multi'
# = EXECUTABLES =
View
@@ -0,0 +1,25 @@
+$: << File.expand_path(Dir.pwd + "/lib")
+$:.reverse!
+
+require 'rubygems'
+require 'rye'
+begin
+require 'perftools'
+rescue LoadError => ex
+ puts ex.message
+ puts "$> gem install perftools.rb"
+ exit 1
+end
+
+data_file = Dir.pwd + "/profile.data"
+pdf_file = Dir.pwd + "/profile.pdf"
+text_file = Dir.pwd + "/profile.txt"
+
+PerfTools::CpuProfiler.start(data_file) do
+ lo0 = Rye::Hop.new "localhost" # set some real host names
+ lo1 = Rye::Box.new "localhost", :via => lo0 # set some real host names
+ puts lo1.uptime
+end
+
+system("pprof.rb --pdf #{data_file} > #{pdf_file}")
+system("pprof.rb --text #{data_file} > #{text_file}")

0 comments on commit 30d4c1e

Please sign in to comment.