Class: Packer::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-packer-plugin/patches/runner.rb

Defined Under Namespace

Classes: CommandExecutionError

Class Method Summary collapse

Class Method Details

.run!(*args, quiet: false) ⇒ Object



10
11
12
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
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vagrant-packer-plugin/patches/runner.rb', line 10

def self.run!(*args, quiet: false)
  cmd = Shellwords.shelljoin(args.flatten)

  debug = cmd.include? '-debug'

  status = 0
  stdout = ''
  stderr = ''
  if quiet && !debug
    # Run without streaming std* to any screen
    stdout, stderr, status = Open3.capture3(cmd)
  else
    # Run but stream as well as capture stdout to the screen
    # see: http://stackoverflow.com/a/1162850/83386
    Open3.popen3(cmd) do |std_in, std_out, std_err, thread|
      # read each stream from a new thread
      Thread.new do
        until (raw = std_out.getc).nil? do
          stdout << raw
          $stdout.write "#{raw}"
        end
      end
      Thread.new do
        until (raw_line = std_err.gets).nil? do
          stderr << raw_line
        end
      end

      Thread.new do
        std_in.puts $stdin.gets while thread.alive?
      end

      thread.join # don't exit until the external process is done
      status = thread.value
    end
  end
  raise CommandExecutionError.new(stderr) unless status == 0
  stdout
end