Skip to content
This repository
Browse code

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

documenation
  • Loading branch information...
commit 30d4c1ee9142d75e393579d32808769bab4635f9 1 parent 64d83ad
Vincent Batts vbatts authored

Showing 4 changed files with 89 additions and 26 deletions. Show diff stats Hide diff stats

  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
42 lib/rye/box.rb
@@ -199,16 +199,20 @@ def ssh_config_options(host)
199 199 # h2 = Rye::Hop.new "host2"
200 200 # h1.via_hop h2
201 201 #
202   - def via_hop(*hops)
203   - hops = hops.flatten.compact
204   - if hops.first.nil?
  202 + def via_hop(*args)
  203 + args = args.flatten.compact
  204 + if args.first.nil?
205 205 return @rye_via
206   - elsif hops.first.is_a?(Rye::Hop)
207   - @rye_via = hops.first
208   - elsif hops.first.is_a?(String)
209   - hop = hops.shift
210   - if hops.first.is_a?(Hash)
211   - @rye_via = Rye::Hop.new(hop, hops.first)
  206 + elsif args.first.is_a?(Rye::Hop)
  207 + @rye_via = args.first
  208 + elsif args.first.is_a?(String)
  209 + hop = args.shift
  210 + if args.first.is_a?(Hash)
  211 + @rye_via = Rye::Hop.new(hop, args.first.merge(
  212 + :debug => @rye_debug,
  213 + :info => @rye_info,
  214 + :error => @rye_error)
  215 + )
212 216 else
213 217 @rye_via = Rye::Hop.new(hop)
214 218 end
@@ -641,12 +645,24 @@ def connect(reconnect=true)
641 645 raise Rye::NoHost unless @rye_host
642 646 return if @rye_ssh && !reconnect
643 647 disconnect if @rye_ssh
644   - debug "Opening connection to #{@rye_host} as #{@rye_user}"
  648 + if @rye_via
  649 + debug "Opening connection to #{@rye_host} as #{@rye_user}, via #{@rye_via.host}"
  650 + else
  651 + debug "Opening connection to #{@rye_host} as #{@rye_user}"
  652 + end
645 653 highline = HighLine.new # Used for password prompt
646 654 retried = 0
647 655 @rye_opts[:keys].compact! # A quick fix in Windows. TODO: Why is there a nil?
648 656 begin
649   - @rye_ssh = Net::SSH.start(@rye_host, @rye_user, @rye_opts || {})
  657 + if @rye_via
  658 + # tell the +Rye::Hop+ what and where to setup,
  659 + # it returns the local port used
  660 + @rye_localport = @rye_via.fetch_port(@rye_host, @rye_opts[:port].nil? ? 22 : @rye_opts[:port] )
  661 + debug "fetched localport #{@rye_localport}"
  662 + @rye_ssh = Net::SSH.start("localhost", @rye_user, @rye_opts.merge(:port => @rye_localport) || {})
  663 + else
  664 + @rye_ssh = Net::SSH.start(@rye_host, @rye_user, @rye_opts || {})
  665 + end
650 666 rescue Net::SSH::HostKeyMismatch => ex
651 667 STDERR.puts ex.message
652 668 print "\a" if @rye_info # Ring the bell
@@ -692,6 +708,10 @@ def disconnect
692 708 end
693 709 debug "Closing connection to #{@rye_ssh.host}"
694 710 @rye_ssh.close
  711 + if @rye_via
  712 + debug "disconnecting Hop #{@rye_via.host}"
  713 + @rye_via.disconnect
  714 + end
695 715 rescue SystemCallError, Timeout::Error => ex
696 716 error "Disconnect timeout"
697 717 rescue Interrupt
45 lib/rye/hop.rb
... ... @@ -1,5 +1,7 @@
1 1 # vim : set sw=2 ts=2 :
2 2
  3 +require 'socket'
  4 +
3 5 module Rye
4 6 DEBUG = false unless defined?(Rye::DEBUG)
5 7
@@ -123,11 +125,11 @@ def initialize(host, opts={})
123 125 # From: capistrano/lib/capistrano/cli.rb
124 126 STDOUT.sync = true # so that Net::SSH prompts show up
125 127
  128 + debug "starting the port forward thread"
  129 + port_loop
  130 +
126 131 debug "ssh-agent info: #{Rye.sshagent_info.inspect}"
127 132 debug @rye_opts.inspect
128   -
129   - port_loop()
130   -
131 133 end
132 134
133 135 # * +hops+ Rye::Hop objects will be added directly
@@ -168,8 +170,11 @@ def fetch_port(host, port = 22, localport = nil)
168 170 else
169 171 port_used = localport
170 172 end
  173 + # i would like to check if the port and host
  174 + # are already an active_locals forward, but that
  175 + # info does not get returned, and trusting the localport
  176 + # is not enough information, so lets just set up a new one
171 177 @rye_ssh.forward.local(port_used, host, port)
172   - port_loop unless @thread.alive?
173 178 return port_used
174 179 end
175 180
@@ -228,7 +233,7 @@ def connect(reconnect=true)
228 233 return if @rye_ssh && !reconnect
229 234 disconnect if @rye_ssh
230 235 if @rye_via
231   - debug "Opening connection to #{@rye_host} as #{@rye_user}, via #{@rye_via}"
  236 + debug "Opening connection to #{@rye_host} as #{@rye_user}, via #{@rye_via.host}"
232 237 else
233 238 debug "Opening connection to #{@rye_host} as #{@rye_user}"
234 239 end
@@ -237,11 +242,10 @@ def connect(reconnect=true)
237 242 @rye_opts[:keys].compact! # A quick fix in Windows. TODO: Why is there a nil?
238 243 begin
239 244 if @rye_via
240   - # XXX
241   - # need a method to to the Hop to create the forward,
242   - # and return the localport to use here
243   - # fetch_port()
244   - @rye_ssh = Net::SSH.start(localhost, @rye_user, @rye_opts || {})
  245 + # tell the +Rye::Hop+ what and where to setup,
  246 + # it returns the local port used
  247 + @rye_localport = @rye_via.fetch_port(@rye_host, @rye_opts[:port].nil? ? 22 : @rye_opts[:port] )
  248 + @rye_ssh = Net::SSH.start("localhost", @rye_user, @rye_opts.merge(:port => @rye_localport) || {})
245 249 else
246 250 @rye_ssh = Net::SSH.start(@rye_host, @rye_user, @rye_opts || {})
247 251 end
@@ -291,6 +295,8 @@ def remove_hops!
291 295 def disconnect
292 296 return unless @rye_ssh && !@rye_ssh.closed?
293 297 begin
  298 + debug "killing port_loop @thread"
  299 + @thread.kill
294 300 if @rye_ssh.busy?;
295 301 info "Is something still running? (ctrl-C to exit)"
296 302 Timeout::timeout(10) do
@@ -298,7 +304,11 @@ def disconnect
298 304 end
299 305 end
300 306 debug "Closing connection to #{@rye_ssh.host}"
301   - @rye_ssh.shutdown!
  307 + @rye_ssh.close
  308 + if @rye_via
  309 + debug "disconnecting Hop #{@rye_via.host}"
  310 + @rye_via.disconnect
  311 + end
302 312 rescue SystemCallError, Timeout::Error => ex
303 313 error "Disconnect timeout"
304 314 rescue Interrupt
@@ -341,7 +351,7 @@ def port_loop
341 351 connect unless @rye_ssh
342 352 @active = true
343 353 @thread = Thread.new do
344   - while @active
  354 + while true
345 355 @rye_ssh.process(0.1)
346 356 end
347 357 end
@@ -352,7 +362,16 @@ def next_port
352 362 port = @next_port
353 363 @next_port -= 1
354 364 @next_port = MAX_PORT if @next_port < MIN_PORT
355   - port
  365 + # check if the port is in use, if so get the next_port
  366 + begin
  367 + TCPSocket.new '127.0.0.1', port
  368 + rescue Errno::EADDRINUSE
  369 + next_port()
  370 + rescue Errno::ECONNREFUSED
  371 + port
  372 + else
  373 + next_port()
  374 + end
356 375 end
357 376
358 377 def debug(msg="unknown debug msg"); @rye_debug.puts msg if @rye_debug; end
3  rye.gemspec
... ... @@ -1,7 +1,7 @@
1 1 @spec = Gem::Specification.new do |s|
2 2 s.name = "rye"
3 3 s.rubyforge_project = "rye"
4   - s.version = "0.9.2"
  4 + s.version = "0.9.2_vb"
5 5 s.summary = "Rye: Safely run SSH commands on a bunch of machines at the same time (from Ruby)."
6 6 s.description = s.summary
7 7 s.author = "Delano Mandelbaum"
@@ -16,7 +16,6 @@
16 16 s.add_dependency 'highline', '>= 1.5.1'
17 17 s.add_dependency 'net-ssh', '>= 2.0.13'
18 18 s.add_dependency 'net-scp', '>= 1.0.2'
19   - s.add_dependency 'net-ssh-gateway'
20 19 #s.add_dependency 'net-ssh-multi'
21 20
22 21 # = EXECUTABLES =
25 test_hop.rb
... ... @@ -0,0 +1,25 @@
  1 +$: << File.expand_path(Dir.pwd + "/lib")
  2 +$:.reverse!
  3 +
  4 +require 'rubygems'
  5 +require 'rye'
  6 +begin
  7 +require 'perftools'
  8 +rescue LoadError => ex
  9 + puts ex.message
  10 + puts "$> gem install perftools.rb"
  11 + exit 1
  12 +end
  13 +
  14 +data_file = Dir.pwd + "/profile.data"
  15 +pdf_file = Dir.pwd + "/profile.pdf"
  16 +text_file = Dir.pwd + "/profile.txt"
  17 +
  18 +PerfTools::CpuProfiler.start(data_file) do
  19 + lo0 = Rye::Hop.new "localhost" # set some real host names
  20 + lo1 = Rye::Box.new "localhost", :via => lo0 # set some real host names
  21 + puts lo1.uptime
  22 +end
  23 +
  24 +system("pprof.rb --pdf #{data_file} > #{pdf_file}")
  25 +system("pprof.rb --text #{data_file} > #{text_file}")

0 comments on commit 30d4c1e

Please sign in to comment.
Something went wrong with that request. Please try again.