Skip to content

Commit

Permalink
Merge pull request #361 from chef/normalize-bento-templates
Browse files Browse the repository at this point in the history
Normalize Bento templates
  • Loading branch information
fnichol committed May 26, 2015
2 parents 4f57b2f + e2d6c76 commit 7b0d85d
Show file tree
Hide file tree
Showing 49 changed files with 1,267 additions and 800 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ rvm:
before_install: wget --no-check-certificate https://dl.bintray.com/mitchellh/packer/packer_0.7.5_linux_amd64.zip && unzip -d packer packer_0.7.5_linux_amd64.zip
before_script: export PATH=$PATH:$PWD/packer

script: bundle exec thor packer:validate
script: ./bin/bento normalize

sudo: false
75 changes: 59 additions & 16 deletions bin/bento
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ Signal.trap("INT") { exit 1 }
$stdout.sync = true
$stderr.sync = true

require 'optparse'
require 'ostruct'
require 'benchmark'
require "benchmark"
require "digest"
require "optparse"
require "ostruct"

class Options

Expand All @@ -24,10 +25,10 @@ class Options
opts.banner = "Usage: #{NAME} [SUBCOMMAND [options]]"
opts.separator ""
opts.separator <<-COMMANDS.gsub(/^ {8}/, "")
build : build one or more templates
fix : fix one or more templates
help : prints this help message
list : list all templates in project
build : build one or more templates
help : prints this help message
list : list all templates in project
normalize : normalize one or more templates
COMMANDS
end

Expand Down Expand Up @@ -69,10 +70,10 @@ class Options
},
argv: templates_argv_proc
},
fix: {
class: FixRunner,
normalize: {
class: NormalizeRunner,
parser: OptionParser.new { |opts|
opts.banner = "Usage: #{NAME} fix TEMPLATE[ TEMPLATE ...]"
opts.banner = "Usage: #{NAME} normalize TEMPLATE[ TEMPLATE ...]"
},
argv: templates_argv_proc
},
Expand Down Expand Up @@ -112,6 +113,14 @@ module Common
puts "==> #{msg}"
end

def info(msg)
puts " #{msg}"
end

def warn(msg)
puts ">>> #{msg}"
end

def duration(total)
total = 0 if total.nil?
minutes = (total / 60).to_i
Expand Down Expand Up @@ -165,28 +174,62 @@ class BuildRunner
end
end

class FixRunner
class NormalizeRunner

include Common

attr_reader :templates

def initialize(opts)
@templates = opts.templates
@modified = []
end

def start
banner("Fixing for templates: #{templates}")
banner("Normalizing for templates: #{templates}")
time = Benchmark.measure do
templates.each { |template| fix(template) }
templates.each do |template|
validate(template)
fix(template)
end
end
if !@modified.empty?
info("")
info("The following templates were modified:")
@modified.sort.each { |template| info(" * #{template}")}
end
banner("Fixing finished in #{duration(time.real)}.")
banner("Normalizing finished in #{duration(time.real)}.")
end

def fix(template)
output = %x{packer fix #{template}.json}
file = "#{template}.json"

banner("[#{template}] Fixing")
original_checksum = checksum(file)
output = %x{packer fix #{file}}
raise "[#{template}] Error fixing, exited #{$?}" if $?.exitstatus != 0
File.open("#{template}.json", "wb") { |file| file.write(output) }
# 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

def validate(template)
cmd = %W[packer validate #{template}.json]
banner("[#{template}] Validating")
system(*cmd) or raise "[#{template}] Error validating, exited #{$?}"
end

def checksum(file)
Digest::MD5.file(file).hexdigest
end
end

Expand Down
15 changes: 7 additions & 8 deletions centos-5.11-i386.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,13 @@
],
"boot_wait": "10s",
"disk_size": 40960,
"parallels_tools_flavor": "lin",
"guest_os_type": "centos",
"http_directory": "http",
"iso_checksum": "ec3a89b4edc8e391d13c1ecce4e6ce3917719e39",
"iso_checksum_type": "sha1",
"iso_url": "{{user `mirror`}}/5.11/isos/i386/CentOS-5.11-i386-bin-DVD-1of2.iso",
"output_directory": "packer-centos-5.11-i386-parallels",
"shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
"ssh_password": "vagrant",
"ssh_port": 22,
"ssh_username": "vagrant",
"ssh_wait_timeout": "10000s",
"type": "parallels-iso",
"parallels_tools_flavor": "lin",
"prlctl": [
[
"set",
Expand All @@ -97,9 +91,14 @@
]
],
"prlctl_version_file": ".prlctl_version",
"shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
"ssh_password": "vagrant",
"ssh_port": 22,
"ssh_username": "vagrant",
"ssh_wait_timeout": "10000s",
"type": "parallels-iso",
"vm_name": "packer-centos-5.11-i386"
}

],
"post-processors": [
{
Expand Down
16 changes: 7 additions & 9 deletions centos-5.11-x86_64.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,13 @@
],
"boot_wait": "10s",
"disk_size": 40960,
"parallels_tools_flavor": "lin",
"guest_os_type": "centos",
"http_directory": "http",
"iso_checksum": "ebd77f2fdfac8da04f31c508905cf52aa62937cc",
"iso_checksum_type": "sha1",
"iso_url": "{{user `mirror`}}/5.11/isos/x86_64/CentOS-5.11-x86_64-bin-DVD-1of2.iso",
"output_directory": "packer-centos-5.11-x86_64-parallels",
"shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
"ssh_password": "vagrant",
"ssh_port": 22,
"ssh_username": "vagrant",
"ssh_wait_timeout": "10000s",
"type": "parallels-iso",
"parallels_tools_flavor": "lin",
"prlctl": [
[
"set",
Expand All @@ -97,10 +91,14 @@
]
],
"prlctl_version_file": ".prlctl_version",
"shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
"ssh_password": "vagrant",
"ssh_port": 22,
"ssh_username": "vagrant",
"ssh_wait_timeout": "10000s",
"type": "parallels-iso",
"vm_name": "packer-centos-5.11-x86_64"
}


],
"post-processors": [
{
Expand Down
14 changes: 7 additions & 7 deletions centos-6.6-i386.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,13 @@
],
"boot_wait": "10s",
"disk_size": 40960,
"parallels_tools_flavor": "lin",
"guest_os_type": "centos",
"http_directory": "http",
"iso_checksum": "d16aa4a8e6f71fb01fcc26d8ae0e3443ed514c8e",
"iso_checksum_type": "sha1",
"iso_url": "{{user `mirror`}}/6.6/isos/i386/CentOS-6.6-i386-bin-DVD1.iso",
"output_directory": "packer-centos-6.6-i386-parallels",
"shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
"ssh_password": "vagrant",
"ssh_port": 22,
"ssh_username": "vagrant",
"ssh_wait_timeout": "10000s",
"type": "parallels-iso",
"parallels_tools_flavor": "lin",
"prlctl": [
[
"set",
Expand All @@ -97,6 +91,12 @@
]
],
"prlctl_version_file": ".prlctl_version",
"shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
"ssh_password": "vagrant",
"ssh_port": 22,
"ssh_username": "vagrant",
"ssh_wait_timeout": "10000s",
"type": "parallels-iso",
"vm_name": "packer-centos-6.6-i386"
}
],
Expand Down
15 changes: 7 additions & 8 deletions centos-6.6-x86_64.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,13 @@
],
"boot_wait": "10s",
"disk_size": 40960,
"parallels_tools_flavor": "lin",
"guest_os_type": "centos",
"http_directory": "http",
"iso_checksum": "08be09fd7276822bd3468af8f96198279ffc41f0",
"iso_checksum_type": "sha1",
"iso_url": "{{user `mirror`}}/6.6/isos/x86_64/CentOS-6.6-x86_64-bin-DVD1.iso",
"output_directory": "packer-centos-6.6-x86_64-parallels",
"shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
"ssh_password": "vagrant",
"ssh_port": 22,
"ssh_username": "vagrant",
"ssh_wait_timeout": "10000s",
"type": "parallels-iso",
"parallels_tools_flavor": "lin",
"prlctl": [
[
"set",
Expand All @@ -97,9 +91,14 @@
]
],
"prlctl_version_file": ".prlctl_version",
"shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
"ssh_password": "vagrant",
"ssh_port": 22,
"ssh_username": "vagrant",
"ssh_wait_timeout": "10000s",
"type": "parallels-iso",
"vm_name": "packer-centos-6.6-x86_64"
}

],
"post-processors": [
{
Expand Down
15 changes: 8 additions & 7 deletions centos-7.1-x86_64.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,13 @@
],
"boot_wait": "10s",
"disk_size": 40960,
"parallels_tools_flavor": "lin",
"guest_os_type": "centos",
"http_directory": "http",
"iso_checksum": "85bcf62462fb678adc0cec159bf8b39ab5515404bc3828c432f743a1b0b30157",
"iso_checksum_type": "sha256",
"iso_url": "{{user `mirror`}}/7.1.1503/isos/x86_64/CentOS-7-x86_64-DVD-1503-01.iso",
"output_directory": "packer-centos-7.1-x86_64-parallels",
"shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
"ssh_password": "vagrant",
"ssh_port": 22,
"ssh_username": "vagrant",
"ssh_wait_timeout": "10000s",
"type": "parallels-iso",
"parallels_tools_flavor": "lin",
"prlctl": [
[
"set",
Expand All @@ -97,6 +91,12 @@
]
],
"prlctl_version_file": ".prlctl_version",
"shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
"ssh_password": "vagrant",
"ssh_port": 22,
"ssh_username": "vagrant",
"ssh_wait_timeout": "10000s",
"type": "parallels-iso",
"vm_name": "packer-centos-7.1-x86_64"
}
],
Expand Down Expand Up @@ -129,3 +129,4 @@
"mirror": "http://mirrors.kernel.org/centos"
}
}

Loading

0 comments on commit 7b0d85d

Please sign in to comment.