Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize Bento templates #361

Merged
merged 3 commits into from
May 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should deprecate/warn but not remove the 'fix' CLI command in favor of making it an alias of 'normalize' at least for a little while. I may be being more conservative than is needed though. I feel like as small as it is, it is technically a 'breaking' change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, the bin/bento code has only been in master about a week (and still not documented). I could add an alias here, but was hoping it was under (or not) used.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh gotcha - cool!

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