Add Packer support for building Vagrant base boxes
authorDaniel Farrell <dfarrell@redhat.com>
Fri, 5 Jun 2015 05:28:37 +0000 (01:28 -0400)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 5 Jun 2015 11:07:51 +0000 (11:07 +0000)
Now able to generate a valid base box that Vagrant
can log into. There's an issue with VBox Guest
Editions that's preventing shared dirs from mounting
properly, but `vagrant up` and `vagrant ssh` both
work.

Trello card tracking this TODO: https://goo.gl/J36ljc

Change-Id: I6bf0338a764256b3c9e378d3a1b17b801621deab
Signed-off-by: Daniel Farrell <dfarrell@redhat.com>
packaging/packer/.gitignore
packaging/packer/README.markdown
packaging/packer/centos.json
packaging/packer/centos_kickstart.cfg
packaging/packer/config_vagrant.sh [new file with mode: 0644]

index ccbbe78fcdc156d95f515b8161a73f909a82c494..f90c3c5e99d30e323279199b5b8035da9a6dbfff 100644 (file)
@@ -1,6 +1,8 @@
 # Vim tmp files
 .*sw?
 
-# Packer output dirs
+# Packer output
 output-virtualbox-iso/
 packer_cache/
+*.box
+Vagrantfile
index 389cb9e5c3c5a516f01253aa905a064bcce29e6f..0c075765613304eda00c7b9aa7cecb852721a520 100644 (file)
@@ -4,15 +4,50 @@
 
 OpenDaylight's current use-case for Packer is to build an ODL Vagrant base box, provisioned using upstream deployment/config tools. Reusing the logic of our configuration management tools nicely standardizes our deployment story, enabling good code reuse and separation of responsibilities. Providing an upstream Vagrant base box prevents all ODL consumers from duplicating ODL download, install and configuration logic, data transfer and computation in every Vagrantfile we create.
 
-## Building ODL Images
+We currently only support CentOS 7, output as a VirtualBox machine image, not packaged into a Vagrant box. Additional development is ongoing.
+
+## Building an ODL Vagrant Base Box
 
-After [installing Packer][2], build our ODL image with:
+After [installing Packer][2], build our ODL Vagrant base box with:
 
 ```
 [~/integration/packaging/packer]$ packer build centos.json
 ```
 
-We currently only support CentOS 7, output as a VirtualBox machine image, not packaged into a Vagrant box. Additional development is ongoing.
+This will download and verify a fresh CentOS 7.1 Minimal ISO, use a Kickstart template to automate a minimal install against a VirtualBox host, run a post-install shell provisioner to configure the VM for use as a Vagrant base box and finally export, compress and package it as a Vagrant base box.
+
+```
+<snip>
+Build 'virtualbox-iso' finished.
+
+==> Builds finished. The artifacts of successful builds are:
+--> virtualbox-iso: 'virtualbox' provider box: opendaylight-centos-7.1.box
+[~/integration/packaging/packer]$ ls -lh opendaylight-centos-7.1.box
+-rw-rw-r--. 1 daniel daniel 532M Jun  5 00:32 opendaylight-centos-7.1.box
+```
+
+Import the local box into Vagrant with:
+
+```
+[~/integration/packaging/packer]$ vagrant box add --name "opendaylight" opendaylight-centos-7.1.box --force
+==> box: Adding box 'opendaylight' (v0) for provider:
+    box: Downloading: file:///home/daniel/integration/packaging/packer/opendaylight-centos-7.1.box
+==> box: Successfully added box 'opendaylight' (v0) for 'virtualbox'!
+```
+
+To connect to your new box, you'll need a trivial Vagrantfile:
+
+```
+[~/integration/packaging/packer]$ vagrant init -m opendaylight
+A `Vagrantfile` has been placed in this directory.<snip>
+[~/integration/packaging/packer]$ cat Vagrantfile
+Vagrant.configure(2) do |config|
+  config.vm.box = "opendaylight"
+end
+[~/integration/packaging/packer]$ vagrant ssh
+<snip>
+```
+
 
 [1]: https://www.packer.io/
 [2]: https://www.packer.io/intro/getting-started/setup.html
index b13772d92f3d5f8190c33abe1090fdeea749c4fd..56cac9a4dc66dedef581ffd0e9712e2a8019fdf9 100644 (file)
         ],
       "iso_checksum": "7cf1ac8da13f54d6be41e3ccf228dc5bb35792f515642755ff4780d5714d4278",
       "iso_checksum_type": "sha256",
+      "headless": "false",
       "ssh_username": "vagrant",
       "ssh_password": "vagrant",
-      "shutdown_command": "echo 'vagrant' | sudo -S shutdown -P now",
+      "shutdown_command": "sudo shutdown -P now",
       "http_directory": ".",
       "boot_command": [
         "<tab>text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos_kickstart.cfg<enter>"
+      ],
+      "vboxmanage": [
+        [
+          "modifyvm",
+          "{{ .Name }}",
+          "--memory",
+          "1024"
+        ],
+        [
+          "modifyvm",
+          "{{ .Name }}",
+          "--cpus",
+          "2"
+        ]
       ]
     }
+  ],
+  "provisioners": [
+    {
+      "type": "shell",
+      "script": "config_vagrant.sh"
+    }
+  ],
+  "post-processors": [
+    {
+      "type": "vagrant",
+      "compression_level": "9",
+      "output": "opendaylight-centos-7.1.box"
+    }
   ]
 }
index bb686771a7c8a0f6ef43727119a0cedbea88283c..96680644482fdcdd4c622984a8338ba662ce2591 100644 (file)
@@ -44,15 +44,24 @@ reboot
 %end
 
 %post
-  # Create vagrant user and group, give them correct permissions
-  groupadd vagrant
-  useradd vagrant -g vagrant -G wheel
-  echo "vagrant" | passwd --stdin vagrant
-  echo "vagrant        ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers.d/vagrant
+  # In the VM output, this is the "Running post-installation scripts" section.
+  # Unfortunately, it produces no output. The `yum update` step may give the
+  # appearance that the build is hung, as it's slow, uses little CPU and
+  # doesn't transfer much data during the package index building step.
 
   # Update all packages
   yum update -y
 
   # Clear yum cache to save space
   yum clean all
+
+  # Create vagrant user and group, give them correct permissions
+  # This needs to happen before Packer tries to SSH in and poweroff the VM,
+  # (at the end of the builder step), so it must be done here (in the
+  # builder step), not in an external script called by a Packer shell
+  # provisioner (after the builder step).
+  groupadd vagrant
+  useradd vagrant -g vagrant -G wheel
+  echo "vagrant" | passwd --stdin vagrant
+  echo "vagrant        ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers.d/vagrant
 %end
diff --git a/packaging/packer/config_vagrant.sh b/packaging/packer/config_vagrant.sh
new file mode 100644 (file)
index 0000000..edfed44
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/env bash
+# Called by Packer to config a CentOS 7 machine for use as a Vagrant base box
+
+# Echo commands as they are run
+set -x
+
+# Install SSH key used by Vagrant for all interactions that connect to the box
+install -v -o vagrant -g vagrant -m 0700 -d /home/vagrant/.ssh
+curl -o /home/vagrant/.ssh/authorized_keys -kL "https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub"
+chmod 600 /home/vagrant/.ssh/authorized_keys
+chown -R vagrant:vagrant /home/vagrant/.ssh
+
+# Vagrant requires the ability to run sudo without a tty
+# Else, `vagrant up` will fail with: "sorry, you must have a tty to run sudo"
+sudo sed -i "s/^.*requiretty/#Defaults requiretty/" /etc/sudoers