class NormalizeRunner

Attributes

build_timestamp[R]
debug[R]
override_version[R]
templates[R]

Public Class Methods

new(opts) click to toggle source
# File lib/bento/normalize.rb, line 9
def initialize(opts)
  @templates = opts.template_files
  @debug = opts.debug
  @modified = []
  @build_timestamp = Time.now.gmtime.strftime("%Y%m%d%H%M%S")
end

Public Instance Methods

start() click to toggle source
# File lib/bento/normalize.rb, line 16
def start
  banner("Normalizing for templates:")
  templates.each { |t| puts "- #{t}" }
  time = Benchmark.measure do
    templates.each do |file|
      dir, template = file.split("/")[0], file.split("/")[1]
      Dir.chdir dir
      validate(template)
      Dir.chdir("..")
    end
  end
  if !@modified.empty?
    info("")
    info("The following templates were modified:")
    @modified.sort.each { |template| info("  * #{template}") }
  end
  banner("Normalizing finished in #{duration(time.real)}.")
end

Private Instance Methods

checksum(file) click to toggle source
# File lib/bento/normalize.rb, line 37
def checksum(file)
  Digest::MD5.file(file).hexdigest
end
fix(template) click to toggle source
# File lib/bento/normalize.rb, line 41
def fix(template)
  file = "#{template}.json"

  banner("[#{template}] Fixing")
  original_checksum = checksum(file)
  output = `packer fix #{file}`
  raise "[#{template}] Error fixing, exited #{$?}" if $?.exitstatus != 0
  # preserve ampersands in shell commands,
  # see: https://github.com/mitchellh/packer/issues/784
  output.gsub!("\\u0026", "&")
  File.open(file, "wb") { |dest| dest.write(output) }
  fixed_checksum = checksum(file)

  if original_checksum == fixed_checksum
    puts("No changes made.")
  else
    warn("Template #{template} has been modified.")
    @modified << template
  end
end
packer_validate_cmd(template, var_file) click to toggle source
# File lib/bento/normalize.rb, line 62
def packer_validate_cmd(template, var_file)
  vars = "#{template}.variables.json"
  cmd = %W{packer validate -var-file=#{var_file} #{template}.json}
  cmd.insert(2, "-var-file=#{vars}") if File.exist?(vars)
  cmd
end
validate(template) click to toggle source
# File lib/bento/normalize.rb, line 69
def validate(template)
  for_packer_run_with(template) do |md_file, var_file|
    cmd = packer_validate_cmd(template, var_file.path)
    banner("[#{template}] Validating: '#{cmd.join(' ')}'")
    if debug
      banner("[#{template}] DEBUG: var_file(#{var_file.path}) is:")
      puts IO.read(var_file.path)
      banner("[#{template}] DEBUG: md_file(#{md_file.path}) is:")
      puts IO.read(md_file.path)
    end
    system(*cmd) || raise( "[#{template}] Error validating, exited #{$?}")
  end
end