Class: MSPRelease::Exec

Inherits:
Object
  • Object
show all
Defined in:
lib/msp_release/exec.rb

Defined Under Namespace

Modules: Helpers Classes: UnexpectedExitStatus

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Exec

Returns a new instance of Exec.



43
44
45
# File 'lib/msp_release/exec.rb', line 43

def initialize(options={})
  @name   = options.fetch(:name,   nil)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



41
42
43
# File 'lib/msp_release/exec.rb', line 41

def name
  @name
end

Class Method Details

.exec(command, options = {}) ⇒ Object



37
38
39
# File 'lib/msp_release/exec.rb', line 37

def self.exec(command, options={})
  new(options).exec(command, options)
end

Instance Method Details

#exec(command, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/msp_release/exec.rb', line 63

def exec(command, options={})

  expected = to_expected_array(options.fetch(:status, 0))

  @last_stdout = ""
  @last_stderr = ""
  @last_output = ""

  output = options[:output]

  output_semaphore = Mutex.new

  start = name.nil? ? '' : "#{name}: "

  status = POpen4::popen4(command) do |stdout, stderr, stdin, pid|
    t1 = Thread.new do
      stdout.each_line do |line|
        @last_stdout += line
        output_semaphore.synchronize { @last_output += line }
        msg = "#{start}#{line}"
        (output && !options[:quiet]) ? output.puts(msg) : LOG.debug(msg)
      end
    end

    t2 = Thread.new do
      stderr.each_line do |line|
        @last_stderr += line
        output_semaphore.synchronize { @last_output += line }
        msg = "#{start}#{line}"
        (output && !options[:quiet]) ? output.puts(msg) : LOG.error(msg)
      end
    end

    t1.join
    t2.join
  end

  @last_exitstatus = status.exitstatus

  unless expected.nil? || expected.include?(status.exitstatus)
    raise UnexpectedExitStatus.new(expected, status.exitstatus, command, @last_output, @last_stdout, @last_stderr)
  end

  @last_stdout
end

#last_exitstatusObject



59
60
61
# File 'lib/msp_release/exec.rb', line 59

def last_exitstatus
  @last_exitstatus
end

#last_outputObject



55
56
57
# File 'lib/msp_release/exec.rb', line 55

def last_output
  @last_output
end

#last_stderrObject



51
52
53
# File 'lib/msp_release/exec.rb', line 51

def last_stderr
  @last_stderr
end

#last_stdoutObject



47
48
49
# File 'lib/msp_release/exec.rb', line 47

def last_stdout
  @last_stdout
end