Class: Singel::PackerExecutor

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

Overview

executes packer commands against template object

Instance Method Summary collapse

Constructor Details

#initialize(template, builders) ⇒ PackerExecutor

Returns a new instance of PackerExecutor.



22
23
24
25
26
27
# File 'lib/executor.rb', line 22

def initialize(template, builders)
  @template = template
  @file_path = template.path
  @template_builders = template.builders
  @specified_builders = builders
end

Instance Method Details

#buildObject



43
44
45
46
# File 'lib/executor.rb', line 43

def build
  puts "Building #{File.basename(@file_path, '.json')}:".to_green
  stream_cmd("packer build #{build_options} #{@file_path}")
end

#build_optionsObject

specify the builders if any were passed



49
50
51
# File 'lib/executor.rb', line 49

def build_options
  "-only=#{@specified_builders.join(',')}" unless @specified_builders.empty?
end

#exit_errorObject

throw and error message and exit with and error status



69
70
71
72
# File 'lib/executor.rb', line 69

def exit_error
  puts "An error occured during the packer run.\nSee the above output for more details.".to_red
  exit!
end

#listObject

print out the builders for this template



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/executor.rb', line 30

def list
  puts File.basename(@file_path, '.json') + ':'

  builders = @template.builders_hash
  if builders.empty?
    puts '- No builders found'.indent.to_red
  else
    builders.each_pair do |name, type|
      puts "- #{name} (type: #{type})".indent
    end
  end
end

#stream_cmd(command) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/executor.rb', line 53

def stream_cmd(command)
  PTY.spawn(command) do |r, _w, pid|
    begin
      r.each { |line| print line; }
    rescue Errno::EIO
      # this is an expected behavior when an app is done sending output
    ensure
      Process.wait(pid)
    end
  end
  exit_error unless $CHILD_STATUS == 0
rescue PTY::ChildExited
  exit_error unless $CHILD_STATUS == 0
end