Class: Nissh::Session
- Inherits:
-
Object
- Object
- Nissh::Session
- Defined in:
- lib/nissh/session.rb
Defined Under Namespace
Classes: CommandExecutionFailed
Class Attribute Summary collapse
-
.logger ⇒ Object
Returns the value of attribute logger.
Instance Attribute Summary collapse
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#sudo_password ⇒ Object
Returns the value of attribute sudo_password.
Instance Method Summary collapse
- #execute!(commands, options = {}) ⇒ Object
- #execute_with_exception!(command, success_code = 0) ⇒ Object
- #execute_with_success!(command, success_code = 0) ⇒ Object
- #execute_with_timeout!(command, timeout = 30) ⇒ Object
-
#initialize(*args) ⇒ Session
constructor
A new instance of Session.
Constructor Details
#initialize(*args) ⇒ Session
Returns a new instance of Session.
16 17 18 19 20 21 22 |
# File 'lib/nissh/session.rb', line 16 def initialize(*args) if args.first.is_a?(Net::SSH::Connection::Session) @session = args.first else @session = Net::SSH.start(*args) end end |
Class Attribute Details
.logger ⇒ Object
Returns the value of attribute logger.
10 11 12 |
# File 'lib/nissh/session.rb', line 10 def logger @logger end |
Instance Attribute Details
#session ⇒ Object (readonly)
Returns the value of attribute session.
13 14 15 |
# File 'lib/nissh/session.rb', line 13 def session @session end |
#sudo_password ⇒ Object
Returns the value of attribute sudo_password.
14 15 16 |
# File 'lib/nissh/session.rb', line 14 def sudo_password @sudo_password end |
Instance Method Details
#execute!(commands, options = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/nissh/session.rb', line 24 def execute!(commands, = {}) unless commands.is_a?(Array) commands = [commands] end if [:sudo] commands = commands.map do |command| "sudo --stdin #{command}" end end command = commands.join(' && ') log :info, "\e[44;37m=> #{command}\e[0m" response = Nissh::Response.new response.command = command channel = @session.open_channel do |channel| channel.exec(command) do |_, success| raise CommandExecutionFailed, "Command \"#{command}\" was unable to execute" unless success channel.on_data do |_,data| response.stdout += data log :debug, data.gsub(/[\r]/, ''), :tab => 4 end channel.on_extended_data do |_,_,data| response.stderr += data.gsub(/\r/, '') log :warn, data, :tab => 4 if data =~ /^\[sudo\] password for/ password = [:sudo].is_a?(String) ? [:sudo] : self.sudo_password channel.send_data "#{password}\n" end end channel.on_request("exit-status") do |_,data| response.exit_code = data.read_long log :info, "\e[43;37m=> Exit status: #{response.exit_code}\e[0m" end channel.on_request("exit-signal") do |_, data| response.exit_signal = data.read_long end end end channel.wait response end |
#execute_with_exception!(command, success_code = 0) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/nissh/session.rb', line 92 def execute_with_exception!(command, success_code = 0) result = execute!(command) if result.exit_code == success_code result else raise CommandExecutionFailed, result.output end end |
#execute_with_success!(command, success_code = 0) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/nissh/session.rb', line 83 def execute_with_success!(command, success_code = 0) result = execute!(command) if result.success? result else false end end |
#execute_with_timeout!(command, timeout = 30) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/nissh/session.rb', line 71 def execute_with_timeout!(command, timeout = 30) Timeout.timeout(timeout) do execute!(command) end rescue Timeout::Error => e response = Nissh::Response.new response.exit_code = -255 response.stderr = "Command did not finish executing within the allowed #{timeout} seconds." response.command = command response end |