Class: HackyHAL::DeviceControllers::GenericSsh

Inherits:
Base
  • Object
show all
Defined in:
lib/hacky_hal/device_controllers/generic_ssh.rb

Direct Known Subclasses

LinuxComputer, OsxComputer

Constant Summary collapse

MAX_COMMAND_RETRIES =
1

Instance Attribute Summary collapse

Attributes included from Options

#options

Instance Method Summary collapse

Methods inherited from Base

#log

Methods included from Options

#[]

Constructor Details

#initialize(options) ⇒ GenericSsh

Returns a new instance of GenericSsh.



11
12
13
14
15
16
17
18
19
20
# File 'lib/hacky_hal/device_controllers/generic_ssh.rb', line 11

def initialize(options)
  super(options)

  ensure_option(:host)
  ensure_option(:user)

  @host = options[:host]
  @user = options[:user]
  @ssh_options = options[:ssh_options] || {}
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/hacky_hal/device_controllers/generic_ssh.rb', line 9

def host
  @host
end

#ssh_optionsObject (readonly)

Returns the value of attribute ssh_options.



9
10
11
# File 'lib/hacky_hal/device_controllers/generic_ssh.rb', line 9

def ssh_options
  @ssh_options
end

#userObject (readonly)

Returns the value of attribute user.



9
10
11
# File 'lib/hacky_hal/device_controllers/generic_ssh.rb', line 9

def user
  @user
end

Instance Method Details

#connectObject



45
46
47
48
49
50
# File 'lib/hacky_hal/device_controllers/generic_ssh.rb', line 45

def connect
  disconnect if @ssh
  @ssh = Net::SSH.start(host, user, ssh_options)
rescue SocketError, Net::SSH::Exception, Errno::EHOSTUNREACH => e
  log("Failed to connect: #{e.class.name} - #{e.message}", :warn)
end

#connected?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/hacky_hal/device_controllers/generic_ssh.rb', line 52

def connected?
  !!(@ssh && !@ssh.closed?)
end

#disconnectObject



56
57
58
59
60
61
# File 'lib/hacky_hal/device_controllers/generic_ssh.rb', line 56

def disconnect
  @ssh.close if connected?
rescue Net::SSH::Disconnect
ensure
  @ssh = nil
end

#exec(command) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hacky_hal/device_controllers/generic_ssh.rb', line 22

def exec(command)
  out = nil
  retries = 0

  begin
    connect unless connected?
    log("Command: #{command.inspect}", :debug)
    out = ssh_exec(command)
    log("Output: #{out.inspect}", :debug)
  rescue Net::SSH::Disconnect, EOFError  => e
    log("Command failed: #{e.class.name} - #{e.message}", :warn)
    disconnect

    if retries < MAX_COMMAND_RETRIES
      log("Retrying last command", :warn)
      retries += 1
      retry
    end
  end

  out
end