Class: WatchmonkeyCli::SshConnection
- Inherits:
-
Object
- Object
- WatchmonkeyCli::SshConnection
- Defined in:
- lib/watchmonkey_cli/ssh_connection.rb
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #close! ⇒ Object
- #connection ⇒ Object
- #exec(cmd, chomp = true) ⇒ Object
-
#initialize(id, opts = {}, &initializer) ⇒ SshConnection
constructor
A new instance of SshConnection.
- #name ⇒ Object
- #sync(&block) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(id, opts = {}, &initializer) ⇒ SshConnection
Returns a new instance of SshConnection.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 5 def initialize(id, opts = {}, &initializer) @id = id if opts.is_a?(String) u, h = opts.split("@", 2) opts = { user: u, host_name: h } elsif opts[:host].is_a?(String) u, h = opts[:host].split("@", 2) opts = opts.merge(user: u, host_name: h) opts.delete(:host) end # net/ssh options @opts = { config: false, }.merge(opts) @mutex = Monitor.new initializer.try(:call, @opts) end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
3 4 5 |
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 3 def opts @opts end |
Instance Method Details
#close! ⇒ Object
48 49 50 |
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 48 def close! @ssh.try(:close) rescue false end |
#connection ⇒ Object
44 45 46 |
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 44 def connection sync { @ssh ||= Net::SSH.start(nil, nil, @opts) } end |
#exec(cmd, chomp = true) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 37 def exec cmd, chomp = true sync do res = connection.exec!(cmd) chomp ? res.chomp : res end end |
#name ⇒ Object
29 30 31 |
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 29 def name "ssh:#{@id}" end |
#sync(&block) ⇒ Object
33 34 35 |
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 33 def sync &block @mutex.synchronize(&block) end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 25 def to_s "#<WatchmonkeyCli::SshConnection:#{@id}>" end |