Class: HackyHAL::DeviceControllers::GenericSsh
- Defined in:
- lib/hacky_hal/device_controllers/generic_ssh.rb
Direct Known Subclasses
Constant Summary collapse
- MAX_COMMAND_RETRIES =
1
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#ssh_options ⇒ Object
readonly
Returns the value of attribute ssh_options.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Attributes included from Options
Instance Method Summary collapse
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #exec(command) ⇒ Object
-
#initialize(options) ⇒ GenericSsh
constructor
A new instance of GenericSsh.
Methods inherited from Base
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() super() ensure_option(:host) ensure_option(:user) @host = [:host] @user = [:user] @ssh_options = [:ssh_options] || {} end |
Instance Attribute Details
#host ⇒ Object (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_options ⇒ Object (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 end |
#user ⇒ Object (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
#connect ⇒ Object
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, ) rescue SocketError, Net::SSH::Exception, Errno::EHOSTUNREACH => e log("Failed to connect: #{e.class.name} - #{e.}", :warn) end |
#connected? ⇒ Boolean
52 53 54 |
# File 'lib/hacky_hal/device_controllers/generic_ssh.rb', line 52 def connected? @ssh && !@ssh.closed? end |
#disconnect ⇒ Object
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.}", :warn) disconnect if retries < MAX_COMMAND_RETRIES log("Retrying last command", :warn) retries += 1 retry end end out end |