Class: WatchmonkeyCli::SshConnection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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
24
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 5

def initialize(id, opts = {}, &initializer)
  @id = id
  @established = false

  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

#optsObject (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



58
59
60
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 58

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

#connectionObject



49
50
51
52
53
54
55
56
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 49

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

#established?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/watchmonkey_cli/ssh_connection.rb', line 34

def established?
  @established
end

#exec(cmd, chomp = true) ⇒ Object



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

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

#nameObject



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

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

#sync(&block) ⇒ Object



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

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

#to_sObject



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

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