class Prof::SshGateway
Public Class Methods
new(gateway_host:, gateway_username:, gateway_password: nil, gateway_private_key: nil, ssh_key: nil)
click to toggle source
# File lib/prof/ssh_gateway.rb, line 18 def initialize(gateway_host:, gateway_username:, gateway_password: nil, gateway_private_key: nil, ssh_key: nil) @gateway_host = gateway_host @gateway_username = gateway_username @gateway_password = gateway_password @gateway_private_key = gateway_private_key @ssh_key = ssh_key @forwards = {} end
Public Instance Methods
execute_on(host, cmd, options = {})
click to toggle source
# File lib/prof/ssh_gateway.rb, line 27 def execute_on(host, cmd, options = {}) user = options.fetch(:user, 'vcap') password = options.fetch(:password, 'c1oudc0w') run_as_root = options.fetch(:root, false) discard_stderr = options.fetch(:discard_stderr, false) cmd = "echo -e \"#{password}\\n\" | sudo -S #{cmd}" if run_as_root cmd << ' 2>/dev/null' if discard_stderr ssh_gateway_options = { password: password, paranoid: false } if @ssh_key ssh_gateway_options[:key_data] = [@ssh_key] else ssh_gateway_options[:auth_methods] = [ 'password', 'publickey' ] end suppress_warnings do ssh_gateway.ssh( host, user, ssh_gateway_options, ) do |ssh| ssh.exec!(cmd) end end end
scp_from(host, remote_path, local_path, options = {})
click to toggle source
# File lib/prof/ssh_gateway.rb, line 69 def scp_from(host, remote_path, local_path, options = {}) with_port_forwarded_to(host, 22) do |local_port| options[:port] = local_port options[:user] ||= 'vcap' options[:password] ||= 'c1oudc0w' Net::SCP.start('127.0.0.1', options.fetch(:user), options) do |scp| scp.download! remote_path, local_path end end end
scp_to(host, local_path, remote_path, options = {})
click to toggle source
# File lib/prof/ssh_gateway.rb, line 58 def scp_to(host, local_path, remote_path, options = {}) with_port_forwarded_to(host, 22) do |local_port| options[:port] = local_port options[:user] ||= 'vcap' options[:password] ||= 'c1oudc0w' Net::SCP.start('127.0.0.1', options.fetch(:user), options) do |scp| scp.upload! local_path, remote_path end end end
with_port_forwarded_to(remote_host, remote_port, &block)
click to toggle source
# File lib/prof/ssh_gateway.rb, line 80 def with_port_forwarded_to(remote_host, remote_port, &block) ssh_gateway.open(remote_host, remote_port, &block) end
Private Instance Methods
gateway_host()
click to toggle source
# File lib/prof/ssh_gateway.rb, line 90 def gateway_host URI(@gateway_host).host || @gateway_host end
ssh_agent()
click to toggle source
# File lib/prof/ssh_gateway.rb, line 86 def ssh_agent @ssh_agent ||= Net::SSH::Authentication::Agent.connect end
ssh_gateway()
click to toggle source
# File lib/prof/ssh_gateway.rb, line 94 def ssh_gateway opts = { paranoid: false } if @gateway_private_key opts[:keys] = [@gateway_private_key] else opts[:password] = @gateway_password end @ssh_gateway ||= Net::SSH::Gateway.new( gateway_host, @gateway_username, opts ) rescue Net::SSH::AuthenticationFailed message = [ "Failed to connect to #{gateway_host}, with #{@gateway_username}:#{@gateway_password}.", "The ssh-agent has #{ssh_agent.identities.size} identities. Please either add a key, or correct password" ].join(' ') raise Net::SSH::AuthenticationFailed, message end
suppress_warnings() { || ... }
click to toggle source
# File lib/prof/ssh_gateway.rb, line 115 def suppress_warnings original_verbosity = $VERBOSE $VERBOSE = nil result = yield $VERBOSE = original_verbosity return result end