This tutorial shows you how to use Packer to build custom bootable images on
the Catalyst Cloud. Packer is an open source tool developed by Hashicorp for
creating machine images for multiple platforms from a single source
configuration.

Packer makes use of builders and provisioners to create custom bootable
images.

Packer supports a number of builders for different target platforms
including Amazon EC2 AMI images, VirtualBox and VMware. When building images
for the Catalyst Cloud you will be using the OpenStack builder.

Provisioners provide a way to configure a base image such that a new custom
image can be created. Many provisioners are available, including shell
provisoners and provisioners that use DevOps tools like Ansible, Puppet and
Chef.

Templates are JSON files that configure the builders and provisioners that
you will use to create our custom image.

In this example, you will create a basic template that can be invoked with the
packerbuild command. It will create an instance in the Catalyst cloud, and
once the instance is running, copy a script to it and run the script using SSH.
Once the script has finished running, it will create a new Catalyst Cloud image
that includes the changes you have made. Once this process is complete, it will
clean up after itself so that only the new image remains.

In this example, you will be using the shell provisioner to update the packages
on an Ubuntu 16.04 machine to the latest versions. You will then build a
golang application called ssllabs-scan from source.

At this time, due to a known issue in the Catalyst Cloud, it is not possible
to deploy a Windows image using Packer directly from the publicly available
Windows image.

In order to overcome this limitation, it is necessary to deploy a new
temporary Windows instance in the Catalyst Cloud. When launching this
instance, you need to say Yes to CreateNewVolume when selecting
the InstanceSource.

Once the image has booted succesfully, take a snapshot of it. This new
snapshot can now be used as the source image for your Packer build. It is
not necessary to keep the temporary Windows instance once the snapshot has
been successfully taken.

It is possible to use Packer to create custom Windows images. This requires
some changes in approach as the tools and connection details are those typical
of Windows technologies.

The first change is in the builders section of the packer build file. Here
you need to add the settings to specify the connection type and the credentials
to use on this connection.

Below is an example of the new communicator settings. These make use of the
Windows Remote Management feature. This uses the WS-Management Protocol, which
is based on SOAP (Simple Object Access Protocol).

Setting "communicator" to "winrm" is mandatory in order for this to
work as expected. The username is required, but it does not have to be
Administrator, though for a Windows instance it makes sense to have a known
administration account.

The other important change is the creation of a userdata script that is run
by the builders section of the build file. The purpose of this userdata
section is to configure the WinRM access and define the user so that Packer is
able to connect to the instance once it has been created.

The reference to the userdata script needs to be added to the builders section
and provide the location of the script that needs to be run.

"builders": [{
...
"user_data_file": "./userdata_setup.ps1",
...
}],

The userdata itself is a Windows command-line/PowerShell script that configures
various settings required to allow remote connectivity via WinRM.