Class: IscsiadmWrapper::External

Inherits:
Object
  • Object
show all
Defined in:
lib/iscsiadm/external.rb

Defined Under Namespace

Classes: ExternalFailure

Class Method Summary collapse

Class Method Details

.cmd(cmd) ⇒ Object

Execute a command, returning the resulting String of standard output.

The block is optional. If given, it will be invoked for each line of output.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/iscsiadm/external.rb', line 13

def cmd(cmd)
  output = []
  error = nil
  stat = Open4::popen4(cmd) do |pid, stdin, stdout, stderr|
    while line = stdout.gets
      output << line 
    end
    error = stderr.read.strip
  end
  if stat.exited?
    if stat.exitstatus > 0
      raise ExternalFailure, "Fatal error, `#{cmd}` returned #{stat.exitstatus} with '#{error}'"
    end
  elsif stat.signaled?
    raise ExternalFailure, "Fatal error, `#{cmd}` got signal #{stat.termsig} and terminated"
  elsif stat.stopped?
    raise ExternalFailure, "Fatal error, `#{cmd}` got signal #{stat.stopsig} and is stopped"
  end

  if block_given?
    return output.each { |l| yield l }
  else 
    return output.join
  end
end