Skip to content

Commit

Permalink
Add build metadata to boxes and build artifacts.
Browse files Browse the repository at this point in the history
For each Packer template that is run via `bin/bento`, a JSON file of
build metadata will be written to the `builds/` directory, which looks
like the following (using the `ubuntu-14.100i386` template as an example):

    {
      "name": "ubuntu-14.10-i386",
      "version": "2.0.20150528211301",
      "build_timestamp": "20150528211301",
      "git_revision": "6b23dd8d8ff0fb9cc4473f510bc3c54f0b415d1b",
      "box_basename": "chef__ubuntu-14.10-i386-2.0.20150528211301.git.6b23dd8d8ff0fb9cc4473f510bc3c54f0b415d1b",
      "atlas_org": "chef",
      "arch": "32",
      "template": "ubuntu-14.10-i386",
      "md5": {
        "chef__ubuntu-14.10-i386-2.0.20150528211301.git.6b23dd8d8ff0fb9cc4473f510bc3c54f0b415d1b.parallels.box": "e3a18b096cddc73384f0912c3a65ebad",
        "chef__ubuntu-14.10-i386-2.0.20150528211301.git.6b23dd8d8ff0fb9cc4473f510bc3c54f0b415d1b.virtualbox.box": "106f2ca4e6da18663e7216a72dd62e56",
        "chef__ubuntu-14.10-i386-2.0.20150528211301.git.6b23dd8d8ff0fb9cc4473f510bc3c54f0b415d1b.vmware.box": "8990550bc2a0e2e7515ed3433ec54b46"
      },
      "sha256": {
        "chef__ubuntu-14.10-i386-2.0.20150528211301.git.6b23dd8d8ff0fb9cc4473f510bc3c54f0b415d1b.parallels.box": "0a0e3c9369de005a456f0cd7d94ba4d4b562d7231c11d9c5af8e40ef77131d3d",
        "chef__ubuntu-14.10-i386-2.0.20150528211301.git.6b23dd8d8ff0fb9cc4473f510bc3c54f0b415d1b.virtualbox.box": "0c23480a99294aea8f42daea2576a41820ec3bebb99a9d0a8ab72a3de1b24137",
        "chef__ubuntu-14.10-i386-2.0.20150528211301.git.6b23dd8d8ff0fb9cc4473f510bc3c54f0b415d1b.vmware.box": "9128b66ef4bae323a123fcdd0be5a598bb538f822295ab6bf043e7630a49b608"
      }
    }

In addition to the "sidecar" metadata file, a trimmed down version will
is added to each Vagrant box in `/etc/bento-metadata.json`. Using the
example above, here is what the file would look like:

    {
      "name": "ubuntu-14.10-i386",
      "version": "2.0.20150528211301",
      "build_timestamp": "20150528211301",
      "git_revision": "6b23dd8d8ff0fb9cc4473f510bc3c54f0b415d1b_dirty",
      "box_basename": "chef__ubuntu-14.10-i386-2.0.20150528211301.git.6b23dd8d8ff0fb9cc4473f510bc3c54f0b415d1b_dirty",
      "atlas_org": "chef",
      "arch": "32",
      "template": "ubuntu-14.10-i386"
    }

Also note that this changes the file naming scheme of the resulting box
artifacts in an effort to host multiple builds of the same templates in
one directory while maintaining enough information about the box within
the filename itself.

Using the same example as above, the VirtualBox provider box name is:

    chef__ubuntu-14.10-i386-2.0.20150528211301.git.6b23dd8d8ff0fb9cc4473f510bc3c54f0b415d1b.virtualbox.box

Which uses the following recipe to construct the filename:

* `atlas_org` value (default: `"chef"`)
* double underscore, which could be later interpreted as a slash (`/`)
  for an Atalas box name
* `name` value which may or may not equal the name of the template
  (captured as the `template` value)
* a dash
* `version` value, which removes the last digit in a version string and
  replaces it with the `build_timestamp` (a
  Year/Month/Day/Hour/Minute/Second format in UTC timezone)
* a period
* the string `"git"`
* a period
* `git_revision` value, which will append `"_dirty"` if the current
  state of the git repository is not completely clean (i.e., there are
  uncommitted changes which happens in active development)
* a period
* the value of the `{{.Provider}}` Packer variable, being one of
  `"virtualbox"`, `"vmware"`, or `"parallels"`
* finished with `".box"`

Closes #364
  • Loading branch information
fnichol committed May 29, 2015
1 parent 6b23dd8 commit 875b3cf
Show file tree
Hide file tree
Showing 50 changed files with 801 additions and 127 deletions.
67 changes: 40 additions & 27 deletions bin/bento
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,22 @@ class BuildRunner
def start
banner("Starting build for templates: #{templates}")
time = Benchmark.measure do
templates.each do |template|
Tempfile.open("#{template}-metadata-var-file") do |md|
write_var_file(template, md)
md.close
packer(template, md.path)
write_metadata(template)
end
end
templates.each { |template| build_template(template) }
end
banner("Build finished in #{duration(time.real)}.")
end

def build_template(template)
Tempfile.open("#{template}-metadata.json") do |md_file|
Tempfile.open("#{template}-metadata-var-file") do |var_file|
write_box_metadata(template, md_file)
write_var_file(template, md_file.path, var_file)
packer(template, var_file.path)
write_final_metadata(template)
end
end
end

def packer(template, var_file)
cmd = packer_cmd(template, var_file)
banner("[#{template}] Running: '#{cmd.join(' ')}'")
Expand All @@ -179,7 +183,14 @@ class BuildRunner
cmd
end

def write_metadata(template)
def write_box_metadata(template, io)
md = BuildMetadata.new(template, build_timestamp).read

io.write(JSON.pretty_generate(md))
io.close
end

def write_final_metadata(template)
md = BuildMetadata.new(template, build_timestamp).read
path = File.join(File.dirname(__FILE__), "..", "builds")
filename = File.join(path, "#{md[:box_basename]}.metadata.json")
Expand All @@ -191,16 +202,17 @@ class BuildRunner
File.open(filename, "wb") { |file| file.write(JSON.pretty_generate(md)) }
end

def write_var_file(template, io)
def write_var_file(template, md_file, io)
md = BuildMetadata.new(template, build_timestamp).read

io.write(JSON.pretty_generate(Hash.new.tap { |h|
h[:box_basename] = md[:box_basename]
h[:build_timestamp] = md[:build_timestamp]
h[:git_revision] = md[:git_revision]
h[:metadata] = JSON.pretty_generate(md)
h[:version] = md[:version]
io.write(JSON.pretty_generate({
box_basename: md[:box_basename],
build_timestamp: md[:build_timestamp],
git_revision: md[:git_revision],
metadata: md_file,
version: md[:version]
}))
io.close
end
end

Expand All @@ -212,8 +224,8 @@ class ChecksumMetadata

def read
{
:md5 => md5_checksums,
:sha256 => sha256_checksums,
md5: md5_checksums,
sha256: sha256_checksums
}
end

Expand Down Expand Up @@ -242,15 +254,16 @@ class BuildMetadata
end

def read
Hash.new.tap do |h|
h[:name] = name
h[:version] = version
h[:build_timestamp] = build_timestamp
h[:git_revision] = git_revision
h[:box_basename] = box_basename
h[:atlas_org] = atlas_org
h[:arch] = template_vars.fetch("arch", UNKNOWN)
end
{
name: name,
version: version,
build_timestamp: build_timestamp,
git_revision: git_revision,
box_basename: box_basename,
atlas_org: atlas_org,
arch: template_vars.fetch("arch", UNKNOWN),
template: template_vars.fetch("template", UNKNOWN),
}
end

private
Expand Down
18 changes: 16 additions & 2 deletions centos-5.11-i386.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,21 @@
],
"post-processors": [
{
"output": "builds/{{.Provider}}/opscode_centos-5.11-i386.box",
"output": "builds/{{user `box_basename`}}.{{.Provider}}.box",
"type": "vagrant"
}
],
"provisioners": [
{
"destination": "/tmp/bento-metadata.json",
"source": "{{user `metadata`}}",
"type": "file"
},
{
"environment_vars": [],
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
"scripts": [
"scripts/common/metadata.sh",
"scripts/common/vagrant.sh",
"scripts/common/sshd.sh",
"scripts/common/vmtools.sh",
Expand All @@ -121,7 +127,15 @@
}
],
"variables": {
"mirror": "http://mirrors.kernel.org/centos"
"arch": "32",
"box_basename": "centos-5.11-i386",
"build_timestamp": "{{isotime \"20060102150405\"}}",
"git_revision": "__unknown_git_revision__",
"metadata": "floppy/dummy_metadata.json",
"mirror": "http://mirrors.kernel.org/centos",
"name": "centos-5.11-i386",
"template": "centos-5.11-i386",
"version": "2.0.TIMESTAMP"
}
}

18 changes: 16 additions & 2 deletions centos-5.11-x86_64.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,21 @@
],
"post-processors": [
{
"output": "builds/{{.Provider}}/opscode_centos-5.11.box",
"output": "builds/{{user `box_basename`}}.{{.Provider}}.box",
"type": "vagrant"
}
],
"provisioners": [
{
"destination": "/tmp/bento-metadata.json",
"source": "{{user `metadata`}}",
"type": "file"
},
{
"environment_vars": [],
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
"scripts": [
"scripts/common/metadata.sh",
"scripts/common/vagrant.sh",
"scripts/common/sshd.sh",
"scripts/common/vmtools.sh",
Expand All @@ -121,7 +127,15 @@
}
],
"variables": {
"mirror": "http://mirrors.kernel.org/centos"
"arch": "64",
"box_basename": "centos-5.11",
"build_timestamp": "{{isotime \"20060102150405\"}}",
"git_revision": "__unknown_git_revision__",
"metadata": "floppy/dummy_metadata.json",
"mirror": "http://mirrors.kernel.org/centos",
"name": "centos-5.11",
"template": "centos-5.11-x86_64",
"version": "2.0.TIMESTAMP"
}
}

18 changes: 16 additions & 2 deletions centos-6.6-i386.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,21 @@
],
"post-processors": [
{
"output": "builds/{{.Provider}}/opscode_centos-6.6-i386.box",
"output": "builds/{{user `box_basename`}}.{{.Provider}}.box",
"type": "vagrant"
}
],
"provisioners": [
{
"destination": "/tmp/bento-metadata.json",
"source": "{{user `metadata`}}",
"type": "file"
},
{
"environment_vars": [],
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
"scripts": [
"scripts/common/metadata.sh",
"scripts/centos/fix-slow-dns.sh",
"scripts/common/sshd.sh",
"scripts/common/vagrant.sh",
Expand All @@ -122,7 +128,15 @@
}
],
"variables": {
"mirror": "http://mirrors.kernel.org/centos"
"arch": "32",
"box_basename": "centos-6.6-i386",
"build_timestamp": "{{isotime \"20060102150405\"}}",
"git_revision": "__unknown_git_revision__",
"metadata": "floppy/dummy_metadata.json",
"mirror": "http://mirrors.kernel.org/centos",
"name": "centos-6.6-i386",
"template": "centos-6.6-i386",
"version": "2.0.TIMESTAMP"
}
}

18 changes: 16 additions & 2 deletions centos-6.6-x86_64.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,21 @@
],
"post-processors": [
{
"output": "builds/{{.Provider}}/opscode_centos-6.6.box",
"output": "builds/{{user `box_basename`}}.{{.Provider}}.box",
"type": "vagrant"
}
],
"provisioners": [
{
"destination": "/tmp/bento-metadata.json",
"source": "{{user `metadata`}}",
"type": "file"
},
{
"environment_vars": [],
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
"scripts": [
"scripts/common/metadata.sh",
"scripts/centos/fix-slow-dns.sh",
"scripts/common/sshd.sh",
"scripts/common/vagrant.sh",
Expand All @@ -122,7 +128,15 @@
}
],
"variables": {
"mirror": "http://mirrors.kernel.org/centos"
"arch": "64",
"box_basename": "centos-6.6",
"build_timestamp": "{{isotime \"20060102150405\"}}",
"git_revision": "__unknown_git_revision__",
"metadata": "floppy/dummy_metadata.json",
"mirror": "http://mirrors.kernel.org/centos",
"name": "centos-6.6",
"template": "centos-6.6-x86_64",
"version": "2.0.TIMESTAMP"
}
}

18 changes: 16 additions & 2 deletions centos-7.1-x86_64.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,21 @@
],
"post-processors": [
{
"output": "builds/{{.Provider}}/opscode_centos-7.1.box",
"output": "builds/{{user `box_basename`}}.{{.Provider}}.box",
"type": "vagrant"
}
],
"provisioners": [
{
"destination": "/tmp/bento-metadata.json",
"source": "{{user `metadata`}}",
"type": "file"
},
{
"environment_vars": [],
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
"scripts": [
"scripts/common/metadata.sh",
"scripts/centos/fix-slow-dns.sh",
"scripts/common/sshd.sh",
"scripts/common/vagrant.sh",
Expand All @@ -122,7 +128,15 @@
}
],
"variables": {
"mirror": "http://mirrors.kernel.org/centos"
"arch": "64",
"box_basename": "centos-7.1",
"build_timestamp": "{{isotime \"20060102150405\"}}",
"git_revision": "__unknown_git_revision__",
"metadata": "floppy/dummy_metadata.json",
"mirror": "http://mirrors.kernel.org/centos",
"name": "centos-7.1",
"template": "centos-7.1-x86_64",
"version": "2.0.TIMESTAMP"
}
}

18 changes: 16 additions & 2 deletions debian-6.0.10-amd64.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,21 @@
],
"post-processors": [
{
"output": "builds/{{.Provider}}/opscode_debian-6.0.10.box",
"output": "builds/{{user `box_basename`}}.{{.Provider}}.box",
"type": "vagrant"
}
],
"provisioners": [
{
"destination": "/tmp/bento-metadata.json",
"source": "{{user `metadata`}}",
"type": "file"
},
{
"environment_vars": [],
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
"scripts": [
"scripts/common/metadata.sh",
"scripts/debian/update.sh",
"scripts/common/sshd.sh",
"scripts/debian/networking.sh",
Expand All @@ -166,7 +172,15 @@
}
],
"variables": {
"mirror": "http://cdimage.debian.org/cdimage/archive"
"arch": "64",
"box_basename": "debian-6.0.10",
"build_timestamp": "{{isotime \"20060102150405\"}}",
"git_revision": "__unknown_git_revision__",
"metadata": "floppy/dummy_metadata.json",
"mirror": "http://cdimage.debian.org/cdimage/archive",
"name": "debian-6.0.10",
"template": "debian-6.0.10-amd64",
"version": "2.0.TIMESTAMP"
}
}

18 changes: 16 additions & 2 deletions debian-6.0.10-i386.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,21 @@
],
"post-processors": [
{
"output": "builds/{{.Provider}}/opscode_debian-6.0.10-i386.box",
"output": "builds/{{user `box_basename`}}.{{.Provider}}.box",
"type": "vagrant"
}
],
"provisioners": [
{
"destination": "/tmp/bento-metadata.json",
"source": "{{user `metadata`}}",
"type": "file"
},
{
"environment_vars": [],
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
"scripts": [
"scripts/common/metadata.sh",
"scripts/debian/update.sh",
"scripts/common/sshd.sh",
"scripts/debian/networking.sh",
Expand All @@ -166,7 +172,15 @@
}
],
"variables": {
"mirror": "http://cdimage.debian.org/cdimage/archive"
"arch": "32",
"box_basename": "debian-6.0.10-i386",
"build_timestamp": "{{isotime \"20060102150405\"}}",
"git_revision": "__unknown_git_revision__",
"metadata": "floppy/dummy_metadata.json",
"mirror": "http://cdimage.debian.org/cdimage/archive",
"name": "debian-6.0.10-i386",
"template": "debian-6.0.10-i386",
"version": "2.0.TIMESTAMP"
}
}

Loading

0 comments on commit 875b3cf

Please sign in to comment.