Class: Prof::SshGateway
- Inherits:
-
Object
- Object
- Prof::SshGateway
- Defined in:
- lib/prof/ssh_gateway.rb
Instance Method Summary collapse
- #execute_on(host, cmd, options = {}) ⇒ Object
-
#initialize(gateway_host:, gateway_username:, gateway_password: nil, ssh_key: nil) ⇒ SshGateway
constructor
A new instance of SshGateway.
- #scp_from(host, remote_path, local_path, options = {}) ⇒ Object
- #scp_to(host, local_path, remote_path, options = {}) ⇒ Object
- #with_port_forwarded_to(remote_host, remote_port, &block) ⇒ Object
Constructor Details
#initialize(gateway_host:, gateway_username:, gateway_password: nil, ssh_key: nil) ⇒ SshGateway
Returns a new instance of SshGateway.
18 19 20 21 22 23 24 |
# File 'lib/prof/ssh_gateway.rb', line 18 def initialize(gateway_host:, gateway_username:, gateway_password: nil, ssh_key: nil) @gateway_host = gateway_host @gateway_username = gateway_username @gateway_password = gateway_password @ssh_key = ssh_key @forwards = {} end |
Instance Method Details
#execute_on(host, cmd, options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/prof/ssh_gateway.rb', line 26 def execute_on(host, cmd, = {}) user = .fetch(:user, 'vcap') password = .fetch(:password, 'c1oudc0w') run_as_root = .fetch(:root, false) discard_stderr = .fetch(:discard_stderr, false) cmd = "echo -e \"#{password}\\n\" | sudo -S #{cmd}" if run_as_root cmd << ' 2>/dev/null' if discard_stderr = { password: password, paranoid: false } if @ssh_key [:key_data] = [@ssh_key] else [:auth_methods] = [ 'password', 'publickey' ] end suppress_warnings do ssh_gateway.ssh( host, user, , ) do |ssh| ssh.exec!(cmd) end end end |
#scp_from(host, remote_path, local_path, options = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/prof/ssh_gateway.rb', line 68 def scp_from(host, remote_path, local_path, = {}) with_port_forwarded_to(host, 22) do |local_port| [:port] = local_port [:user] ||= 'vcap' [:password] ||= 'c1oudc0w' Net::SCP.start('127.0.0.1', .fetch(:user), ) do |scp| scp.download! remote_path, local_path end end end |
#scp_to(host, local_path, remote_path, options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/prof/ssh_gateway.rb', line 57 def scp_to(host, local_path, remote_path, = {}) with_port_forwarded_to(host, 22) do |local_port| [:port] = local_port [:user] ||= 'vcap' [:password] ||= 'c1oudc0w' Net::SCP.start('127.0.0.1', .fetch(:user), ) do |scp| scp.upload! local_path, remote_path end end end |
#with_port_forwarded_to(remote_host, remote_port, &block) ⇒ Object
79 80 81 |
# File 'lib/prof/ssh_gateway.rb', line 79 def with_port_forwarded_to(remote_host, remote_port, &block) ssh_gateway.open(remote_host, remote_port, &block) end |