Class: Knife::Pkg::ShellCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/knife-pkg/shell_command.rb

Class Method Summary collapse

Class Method Details

.exec(cmd, session) ⇒ Object



21
22
23
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
# File 'lib/knife-pkg/shell_command.rb', line 21

def self.exec(cmd, session)

  stdout_data, stderr_data = "", ""
  exit_code, exit_signal = nil, nil
  session.open_channel do |channel|
    channel.exec(cmd) do |_, success|
      raise RuntimeError, "Command \"#{@cmd}\" could not be executed!" if !success

      channel.on_data do |_, data|
        stdout_data += data
      end

      channel.on_extended_data do |_,_,data|
        stderr_data += data
      end

      channel.on_request("exit-status") do |_,data|
        exit_code = data.read_long
      end

      channel.on_request("exit-signal") do |_, data|
        exit_signal = data.read_long
      end
    end
  end
  session.loop

  result = ShellCommandResult.new(cmd, stdout_data, stderr_data, exit_code.to_i)

  raise_error!(result) unless result.succeeded?

  return result
end

.raise_error!(result) ⇒ Object

Raises:

  • (RuntimeError)


55
56
57
# File 'lib/knife-pkg/shell_command.rb', line 55

def self.raise_error!(result)
  raise RuntimeError, "Command failed! #{result.to_s}" 
end