Class: IscsiadmWrapper::Proc

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

Defined Under Namespace

Classes: ExternalFailure

Class Method Summary collapse

Class Method Details

.external(cmd) ⇒ Object

Return output of external command Yield each line or return entire output



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/iscsiadm/proc.rb', line 10

def external(cmd) #:nodoc:
  output = []
  IO.popen(cmd, "r") do |p|
    while line = p.gets
      if defined? yield 
        yield line
      else
        output << line 
      end
    end
  end
  stat = $?
  if stat.exited?
    if stat.exitstatus > 0
      raise ExternalFailure, "Fatal error, `#{cmd}` returned #{stat.exitstatus}"
    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"
  else
    output.join
  end
end