Class: ConnectableServer
- Inherits:
-
Object
- Object
- ConnectableServer
- Defined in:
- lib/rbvppc/connectable_server.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
Instance Method Summary collapse
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #execute_cmd(command) ⇒ Object
-
#initialize(hostname, username, options) ⇒ ConnectableServer
constructor
A new instance of ConnectableServer.
-
#toggle_debug ⇒ Object
Debug attribute toggle for childen classes to employ for logging purposes.
Constructor Details
#initialize(hostname, username, options) ⇒ ConnectableServer
Returns a new instance of ConnectableServer.
23 24 25 26 27 28 29 |
# File 'lib/rbvppc/connectable_server.rb', line 23 def initialize(hostname, username, ) [:protocol] = :ssh if !.has_key? :protocol [:port] = 22 if ([:protocol] == :ssh) && (!.has_key? :port) @hostname, @username, @options = hostname, username, @session = nil @debug ||= true end |
Instance Attribute Details
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
21 22 23 |
# File 'lib/rbvppc/connectable_server.rb', line 21 def debug @debug end |
Instance Method Details
#connect ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rbvppc/connectable_server.rb', line 41 def connect if (@options[:key] && !@options[:password]) then @session = Net::SSH.start(@hostname, @username, :key => @options[:key], :port => @options[:port]) elsif (@options[:key] && @options[:password]) @session = Net::SSH.start(@hostname, @username, :key => @options[:key], :passphrase => @options[:password], :port => @options[:port]) elsif (@options[:key_data] && @options[:passphrase]) @session = Net::SSH.start(@hostname, @username, :keys => @options[:keys], :key_data => @options[:key_data], :keys_only => @options[:keys_only], :passphrase => @options[:passphrase], :port => @options[:port]) else @session = Net::SSH.start(@hostname, @username, :password => @options[:password], :port => @options[:port]) end true end |
#connected? ⇒ Boolean
31 32 33 |
# File 'lib/rbvppc/connectable_server.rb', line 31 def connected? ! @session.nil? end |
#disconnect ⇒ Object
88 89 90 91 |
# File 'lib/rbvppc/connectable_server.rb', line 88 def disconnect @session.close @session = nil end |
#execute_cmd(command) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rbvppc/connectable_server.rb', line 54 def execute_cmd(command) raise StandardError.new("No connection has been established") if !connected? stdout_data = "" stderr_data = "" exit_code = nil exit_signal = nil @session.open_channel do |channel| channel.exec(command) do |ch, success| unless success abort "FAILED: couldn't execute command (@session.channel.exec)" end channel.on_data do |ch,data| stdout_data+=data end channel.on_extended_data do |ch,type,data| stderr_data+=data end channel.on_request("exit-status") do |ch,data| exit_code = data.read_long end channel.on_request("exit-signal") do |ch, data| exit_signal = data.read_long end end end @session.loop raise CommandFailure.new(stderr_data, exit_code, exit_signal) if exit_code != 0 return stdout_data # {:stdout => stdout_data, :stderr => stderr_data, :exit_code => exit_code, :exit_signal => exit_signal} end |
#toggle_debug ⇒ Object
Debug attribute toggle for childen classes to employ for logging purposes
37 38 39 |
# File 'lib/rbvppc/connectable_server.rb', line 37 def toggle_debug @debug ^= true end |