packer-config

Gem Version

Build Status

A Ruby model that lets you build Packer configurations in Ruby.

Building the Packer JSON configurations in raw JSON can be quite an adventure. There's limited facilities for variable expansion and absolutely no support for nice things like comments. I decided it would just be easier to have an object model to build the Packer configurations in that would easily write to the correct JSON format. It also saved me having to remember the esoteric Packer syntax for referencing variables and whatnot in the JSON.

Bonus: you can really go to town with templates when it's all done in Ruby.

Installation

gem install packer-config

Use

require 'packer-config'

Requires

  • Packer version 1.0.0 or higher

Builders

The following Packer builders are currently implemented:

Communicators are supported as options on Builders in packer-config. The none, ssh, and winrm communicators are all available as is the docker communicator on the Docker-type builders. packer-config will raise an error if you try to use a Communicator type that isn't valid for the Builder.

Provisioners

The following Packer provisioners are currently implemented:

Post-Processors

The following Packer post-processors are currently implemented:

Examples

Packing a Vagrant Basebox from a CentOS ISO Using VirtualBox

This example is based on the integration test spec/integration/centos_vagrant_spec.rb. It produces a Vagrant Basebox that's provisionable with Chef and the packer config and provisioning is based on work done in the Bento project from the OpsCode crew.

CENTOS_VERSION = '7'

pconfig = Packer::Config.new "centos-#{CENTOS_VERSION}-vagrant.json"
pconfig.description "CentOS #{CENTOS_VERSION} VirtualBox Vagrant"
pconfig.add_variable 'mirror', 'http://mirrors.sonic.net/centos'
pconfig.add_variable 'mirror', 'http://ftp.pbone.net/pub/centos'
pconfig.add_variable 'my_version', '0.0.1'
pconfig.add_variable 'chef_version', 'latest'

builder = pconfig.add_builder Packer::Builder::VIRTUALBOX_ISO
builder.boot_command ["<tab> text ks=http://#{pconfig.macro.HTTPIP}:#{pconfig.macro.HTTPPort}/centos-#{CENTOS_VERSION}-ks.cfg<enter><wait>"]
builder.boot_wait '10s'
builder.disk_size 40_960
builder.guest_additions_path "VBoxGuestAdditions_#{pconfig.macro.Version}.iso"
builder.guest_os_type "RedHat_64"
builder.http_directory "scripts/kickstart"
builder.iso_checksum '38d5d51d9d100fd73df031ffd6bd8b1297ce24660dc8c13a3b8b4534a4bd291c'
builder.iso_checksum_type 'sha256'
builder.iso_url "#{pconfig.variable 'mirror'}/#{CENTOS_VERSION}/isos/x86_64/CentOS-7-x86_64-Minimal-1810.iso"
builder.output_directory "centos-#{CENTOS_VERSION}-x86_64-virtualbox"
builder.shutdown_command "echo 'vagrant'|sudo -S /sbin/halt -h -p"
builder.communicator "ssh"
builder.ssh_password "vagrant"
builder.ssh_port 22
builder.ssh_username "vagrant"
builder.ssh_timeout "10000s"
builder.vboxmanage [
  [
    "modifyvm",
    pconfig.macro.Name,
    "--memory",
    "480"
  ],
  [
    "modifyvm",
    pconfig.macro.Name,
    "--cpus",
    "1"
  ]
]
builder.virtualbox_version_file ".vbox_version"
builder.vm_name "packer-centos-#{CENTOS_VERSION}-x86_64"

provisioner = pconfig.add_provisioner Packer::Provisioner::FILE
provisioner.source 'scripts/hello.sh'
provisioner.destination '/home/vagrant/hello.sh'

provisioner = pconfig.add_provisioner Packer::Provisioner::SHELL
provisioner.scripts [
  'scripts/sshd.sh',
  'scripts/vagrant.sh'
]
provisioner.environment_vars [
  "CHEF_VERSION=#{pconfig.variable 'chef_version'}",
  "MY_CENTOS_VERSION=#{pconfig.variable 'my_version'}"
]
provisioner.execute_command "echo 'vagrant' | #{pconfig.macro.Vars} sudo -S -E bash '#{pconfig.macro.Path}'"

postprocessor = pconfig.add_postprocessor Packer::PostProcessor::VAGRANT
postprocessor.output File.join('builds', pconfig.macro.Provider, "centos-#{CENTOS_VERSION}-x86_64-#{pconfig.variable 'my_version'}.box")

pconfig.validate
pconfig.build

Development

Continuous Integration

I'm using Travis CI to build and test on every push to the public github repository. You can find the Travis CI page for this project here: https://travis-ci.org/ianchesal/packer-config/

Branching in Git

We release off the master branch. Please open your pull requests against develop. We were using git-flow but I've fallen out of love with its style in favor of more ad hoc branching and just keeping master clean for releases.

PLEASE OPEN ALL PULL REQUESTS AGAINST develop NOT master!

TODO Work

Please see TODO.md for the short list of big things I thought worth writing down.

Contact Me

Questions or comments about packer-config? Hit me up at [email protected].