Class: WatchmonkeyCli::SshConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/watchmonkey_cli/ssh_connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(id, opts = {}, &initializer) ⇒ SshConnection

Returns a new instance of SshConnection.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 3

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 Method Details

#close!Object



46
47
48
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 46

def close!
  @ssh.try(:close) rescue false
end

#connectionObject



42
43
44
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 42

def connection
  sync { @ssh ||= Net::SSH.start(nil, nil, @opts) }
end

#exec(cmd, chomp = true) ⇒ Object



35
36
37
38
39
40
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 35

def exec cmd, chomp = true
  sync do
    res = connection.exec!(cmd)
    chomp ? res.chomp : res
  end
end

#nameObject



27
28
29
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 27

def name
  "ssh:#{@id}"
end

#sync(&block) ⇒ Object



31
32
33
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 31

def sync &block
  @mutex.synchronize(&block)
end

#to_sObject



23
24
25
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 23

def to_s
  "#<WatchmonkeyCli::SshConnection:#{@id}>"
end