class ImageBuilder::Backends::Packer

Generic class doc comment

Attributes

builders[RW]
description[RW]
min_packer_version[RW]
post_processors[RW]
provisioners[RW]
variables[RW]

Public Class Methods

new() click to toggle source
# File lib/image_builder/backends/packer.rb, line 21
def initialize
  @builders = []
  @post_processors = []
  @provisioners = []
  @variables = {}
end
packer_path(path) click to toggle source
# File lib/image_builder/backends/packer.rb, line 28
def self.packer_path(path)
  @@packer_exe = ::File.expand_path(path)
end
packer_path=(path) click to toggle source
# File lib/image_builder/backends/packer.rb, line 32
def self.packer_path=(path)
  packer_path(path)
end

Public Instance Methods

add_builder(builder_obj) click to toggle source

rubocop:disable Metrics/LineLength

# File lib/image_builder/backends/packer.rb, line 37
def add_builder(builder_obj)
  fail ArgumentError, 'Invalid builder object, does not respond to packer_hash()' unless builder_obj.respond_to?(:packer_hash)
  @builders << builder_obj.packer_hash
end
add_post_processor(post_obj) click to toggle source
# File lib/image_builder/backends/packer.rb, line 42
def add_post_processor(post_obj)
  fail ArgumentError, 'Invalid post-processor object, does not respond to packer_hash()' unless post_obj.respond_to?(:packer_hash)
  @post_processors << post_obj.packer_hash
end
add_provisioner(provisioner_obj) click to toggle source
# File lib/image_builder/backends/packer.rb, line 47
def add_provisioner(provisioner_obj)
  fail ArgumentError, 'Invalid provisioner object, does not respond to packer_hash()' unless provisioner_obj.respond_to?(:packer_hash)
  @provisioners << provisioner_obj.packer_hash
end
add_variable(name, value = '') click to toggle source

rubocop:enable Metrics/LineLength

# File lib/image_builder/backends/packer.rb, line 53
def add_variable(name, value = '')
  @variables[name] = value
end
build(args = []) click to toggle source
# File lib/image_builder/backends/packer.rb, line 57
def build(args = [])
  validate

  cmd = [@@packer_exe, 'build'].concat(args)
  cmd << '-'

  if args.include?('-debug')
    json_obj = JSON.parse(to_json)
    puts 'DEBUG: PACKER_JSON ='
    puts JSON.pretty_generate(json_obj)
  end

  # Some provisioners may take a long time to run without giving output (applying OS updates via Chef
  # to CentOS6, and AMI copying, comes to mind), so extend the timeout to 1.5 hours.
  shellout = Mixlib::ShellOut.new(cmd, input: to_json, timeout: 5400)
  shellout.live_stdout = STDOUT
  shellout.live_stderr = STDERR
  shellout.run_command
  shellout.error!

  true
end
to_json() click to toggle source
# File lib/image_builder/backends/packer.rb, line 94
def to_json
  config = {}

  attr_to_hash(config, :description)
  attr_to_hash(config, :min_packer_version)
  attr_to_hash(config, :variables)
  attr_to_hash(config, :builders, true)
  attr_to_hash(config, :provisioners)

  # Special handling for post-processors, since the packer config name uses '-', and ruby doesn't
  # like those (you know....subtraction operator, and all that)
  config['post-processors'] = @post_processors unless @post_processors.nil? || @post_processors.empty?

  config.to_json
end
validate(args = []) click to toggle source
# File lib/image_builder/backends/packer.rb, line 80
def validate(args = [])
  cmd = [@@packer_exe, 'validate'].concat(args)
  cmd << '-'

  shellout = Mixlib::ShellOut.new(cmd, input: to_json)
  shellout.inspect
  shellout.run_command
  shellout.error!

  shellout.stdout

  true
end