Class: SimpleGate
- Inherits:
-
Object
- Object
- SimpleGate
- Defined in:
- lib/simple_gate.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ SimpleGate
constructor
A new instance of SimpleGate.
-
#through(*gateways) ⇒ Object
Most of the code was taken from Capistrano and then changed to work outside of Capistrano.
-
#through_to(*gateways) ⇒ Object
Connect through a list of gateways to the real server.
- #verbose? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ SimpleGate
Returns a new instance of SimpleGate.
8 9 10 11 12 |
# File 'lib/simple_gate.rb', line 8 def initialize(={}) @options = { :verbose => false }.merge() end |
Instance Method Details
#through(*gateways) ⇒ Object
Most of the code was taken from Capistrano and then changed to work outside of Capistrano.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/simple_gate.rb', line 36 def through(*gateways) Thread.abort_on_exception = true @open_connections ||= [] @gateways = gateways.flatten.collect { |g| ServerDefinition.find(g) } tunnel = @gateways[0].connection_info do |host, user, | puts "Setting up tunnel #{@gateways[0]}" if verbose? gw = Net::SSH::Gateway.new(host, user, ) @open_connections << gw gw end @gateway = (@gateways[1..-1]).inject(tunnel) do |tunnel, destination| puts "Connecting to #{destination}" if verbose? tunnel_port = tunnel.open(destination.host, (destination.port || 22)) = {:user => destination.user, :port => tunnel_port, :password => destination.password} local_host = ServerDefinition.new("127.0.0.1", ) local_host.connection_info do |host, user, | puts "Connecting using local info #{local_host}" if verbose? gw = Net::SSH::Gateway.new(host, user, ) @open_connections << gw gw end end yield(@gateway) ensure while g = @open_connections.pop g.shutdown! end end |
#through_to(*gateways) ⇒ Object
Connect through a list of gateways to the real server. Treats the last ‘gateway’ as the real server and the others as gateways. Needs at least one real gateway and a real server.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/simple_gate.rb', line 21 def through_to(*gateways) # :yields: ssh session gateways = gateways.flatten raise ArgumentError.new("Need at least 2 servers") if gateways.size < 2 target = ServerDefinition.find(gateways.pop) through(gateways) do |gate| target.connection_info do |host, user, | gate.ssh(host, user, ) do |session| yield(session) end end end end |
#verbose? ⇒ Boolean
14 15 16 |
# File 'lib/simple_gate.rb', line 14 def verbose? @options[:verbose] end |