Skip to content

Commit

Permalink
Merge pull request #964 from gruntwork-io/support-packer-init
Browse files Browse the repository at this point in the history
Support for packer v1.7.0
  • Loading branch information
bwhaley authored Jul 28, 2021
2 parents ce41ae4 + 950fddf commit bd89ef2
Show file tree
Hide file tree
Showing 23 changed files with 403 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ env: &env
MODULE_GCP_CI_VERSION: v0.1.1
TERRAFORM_VERSION: 1.0.3
TERRAGRUNT_VERSION: v0.28.24
PACKER_VERSION: 1.6.6
PACKER_VERSION: 1.7.4
GO_VERSION: 1.16.3
GO111MODULE: auto
K8S_VERSION: v1.15.0 # Same as EKS
Expand Down
13 changes: 8 additions & 5 deletions examples/packer-basic-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ Terratest. For slightly more complicated, real-world examples of Packer template




## Building the Packer template manually

## Installation steps
1. Sign up for [AWS](https://aws.amazon.com/).
1. Configure your AWS credentials using one of the [supported methods for AWS CLI
tools](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html), such as setting the
`AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables.
1. Install [Packer](https://www.packer.io/) and make sure it's on your `PATH`.
1. Run `packer build build.json`.

## Building the Packer template manually (Packer >= 1.7.0)
1. Run `packer init build.pkr.hcl`. # Use build-gcp.pkr.hcl if using GCP
1. Run `packer build build.pkr.hcl`. # Use build-gcp.pkr.hcl if using GCP

## Building the Packer template manually (Packer < 1.7.0)
1. Run `packer build build.json`.



Expand Down Expand Up @@ -61,7 +64,7 @@ Terratest. For slightly more complicated, real-world examples of Packer template
1. Sign up for [OCI](https://cloud.oracle.com/cloud-infrastructure).
1. Configure your OCI credentials via [CLI Configuration
Information](https://docs.cloud.oracle.com/iaas/Content/API/Concepts/sdkconfig.htm).
1. Create [VCN](https://docs.cloud.oracle.com/iaas/Content/GSG/Tasks/creatingnetwork.htm) and subnet
1. Create [VCN](https://docs.cloud.oracle.com/iaas/Content/GSG/Tasks/creatingnetwork.htm) and subnet
resources in your tenancy (a.k.a. a root compartment).
1. (Optional) Create `TF_VAR_pass_phrase` environment property with the pass phrase for decrypting of the OCI [API signing
key](https://docs.cloud.oracle.com/iaas/Content/API/Concepts/apisigningkey.htm) (can be omitted
Expand Down
40 changes: 40 additions & 0 deletions examples/packer-basic-example/build-gcp.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
packer {
required_plugins {
googlecompute = {
version = ">=v1.0.0"
source = "github.com/hashicorp/googlecompute"
}
}
}

variable "gcp_project_id" {
type = string
default = ""
}

variable "gcp_zone" {
type = string
default = "us-central1-a"
}

source "googlecompute" "ubuntu-bionic" {
image_family = "terratest"
image_name = "terratest-packer-example-${formatdate("YYYYMMDD-hhmm", timestamp())}"
project_id = var.gcp_project_id
source_image_family = "ubuntu-1804-lts"
ssh_username = "ubuntu"
zone = var.gcp_zone
}


build {
sources = [
"source.googlecompute.ubuntu-bionic"
]

provisioner "shell" {
inline = ["sudo DEBIAN_FRONTEND=noninteractive apt-get update", "sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y"]
pause_before = "30s"
}

}
61 changes: 0 additions & 61 deletions examples/packer-basic-example/build.json

This file was deleted.

98 changes: 98 additions & 0 deletions examples/packer-basic-example/build.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
packer {
required_plugins {
amazon = {
version = ">=v1.0.0"
source = "github.com/hashicorp/amazon"
}
oracle = {
version = ">=v1.0.0"
source = "github.com/hashicorp/oracle"
}
}
}

variable "ami_base_name" {
type = string
default = ""
}

variable "aws_region" {
type = string
default = "us-east-1"
}

variable "instance_type" {
type = string
default = "t2.micro"
}

variable "oci_availability_domain" {
type = string
default = ""
}

variable "oci_base_image_ocid" {
type = string
default = ""
}

variable "oci_compartment_ocid" {
type = string
default = ""
}

variable "oci_pass_phrase" {
type = string
default = ""
}

variable "oci_subnet_ocid" {
type = string
default = ""
}

data "amazon-ami" "ubuntu-xenial" {
filters = {
architecture = "x86_64"
"block-device-mapping.volume-type" = "gp2"
name = "*ubuntu-xenial-16.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"]
region = var.aws_region
}

source "amazon-ebs" "ubuntu-example" {
ami_description = "An example of how to create a custom AMI on top of Ubuntu"
ami_name = "${var.ami_base_name}-terratest-packer-example"
encrypt_boot = false
instance_type = var.instance_type
region = var.aws_region
source_ami = data.amazon-ami.ubuntu-xenial.id
ssh_username = "ubuntu"
}

source "oracle-oci" "oracle-example" {
availability_domain = var.oci_availability_domain
base_image_ocid = var.oci_base_image_ocid
compartment_ocid = var.oci_compartment_ocid
image_name = "terratest-packer-example-${formatdate("YYYYMMDD-hhmm", timestamp())}"
pass_phrase = var.oci_pass_phrase
shape = "VM.Standard2.1"
ssh_username = "ubuntu"
subnet_ocid = var.oci_subnet_ocid
}

build {
sources = [
"source.amazon-ebs.ubuntu-example",
"source.oracle-oci.oracle-example"
]

provisioner "shell" {
inline = ["sudo DEBIAN_FRONTEND=noninteractive apt-get update", "sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y"]
pause_before = "30s"
}
}
18 changes: 13 additions & 5 deletions examples/packer-docker-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ The Docker-based tests in this folder are in some sense "unit tests" for the Pac
[terraform-packer-example](/examples/terraform-packer-example).


## Installation steps
1. Install [Packer](https://www.packer.io/) and make sure it's on your `PATH`.
1. Install [Docker](https://www.docker.com/) and make sure it's on your `PATH`.


## Building a Docker image for local testing
## Building a Docker image for local testing (Packer >= 1.7.0)
1. Run `packer init build.pkr.hcl`.
1. Run `packer build build.pkr.hcl`.

1. Install [Packer](https://www.packer.io/) and make sure it's on your `PATH`.
1. Install [Docker](https://www.docker.com/) and make sure it's on your `PATH`.
1. Run `packer build -only=ubuntu-docker build.json`.

## Building a Docker image for local testing (Packer < 1.7.0)
1. Run `packer build build.json`.


## Run the container
1. Run `docker-compose up`.
1. You should now be able to access the sample web app at http://localhost:8080

Expand Down Expand Up @@ -53,4 +61,4 @@ The Docker-based tests in this folder are in some sense "unit tests" for the Pac

## Running automated tests in AWS against this Packer template

See [terraform-packer-example](/examples/terraform-packer-example).
See [terraform-packer-example](/examples/terraform-packer-example).
76 changes: 76 additions & 0 deletions examples/packer-docker-example/build.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
packer {
required_plugins {
amazon = {
version = ">=v1.0.0"
source = "github.com/hashicorp/amazon"
}
}
}

variable "ami_name_base" {
type = string
default = "terratest-packer-docker-example"
}

variable "aws_region" {
type = string
default = "us-east-1"
}

variable "instance_type" {
type = string
default = "t2.micro"
}

data "amazon-ami" "aws" {
filters = {
architecture = "x86_64"
"block-device-mapping.volume-type" = "gp2"
name = "*ubuntu-xenial-16.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"]
region = var.aws_region
}

source "amazon-ebs" "ubuntu-ami" {
ami_description = "An example of how to create a custom AMI with a simple web app on top of Ubuntu"
ami_name = "${var.ami_name_base}-${formatdate("YYYYMMDD-hhmm", timestamp())}"
encrypt_boot = false
instance_type = var.instance_type
region = var.aws_region
source_ami = data.amazon-ami.aws.id
ssh_username = "ubuntu"
}

source "docker" "ubuntu-docker" {
changes = ["ENTRYPOINT [\"\"]"]
commit = true
image = "gruntwork/ubuntu-test:16.04"
}

build {
sources = ["source.amazon-ebs.ubuntu-ami", "source.docker.ubuntu-docker"]

provisioner "shell" {
inline = ["echo 'Sleeping for a few seconds to give Ubuntu time to boot up'", "sleep 30"]
only = ["amazon-ebs.ubuntu-ami"]
}

provisioner "file" {
destination = "/tmp/packer-docker-example"
source = path.root
}

provisioner "shell" {
inline = ["/tmp/packer-docker-example/configure-sinatra-app.sh"]
}

post-processor "docker-tag" {
only = ["docker.ubuntu-docker"]
repository = "gruntwork/packer-docker-example"
tag = ["latest"]
}
}
2 changes: 1 addition & 1 deletion examples/packer-docker-example/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
version: '3'
services:
web_app:
# The name we use for the Docker image in build.json
# The name we use for the Docker image in build.json (or build.pkr.hcl)
image: gruntwork/packer-docker-example

# Run the sample web app on port 8080
Expand Down
18 changes: 12 additions & 6 deletions examples/packer-hello-world-example/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
# Packer "Hello, World" Example

This folder contains the simplest possible Packer template—one that builds a Docker image with a text file that says
"Hello, World"!—to demonstrate how you can use Terratest to write automated tests for your Packer templates.
"Hello, World"!—to demonstrate how you can use Terratest to write automated tests for your Packer templates.

Check out [test/packer_hello_world_example_test.go](/test/packer_hello_world_example_test.go) to see how you can write
automated tests for this simple template.


## Installation steps
1. Install [Packer](https://www.packer.io/) and make sure it's on your `PATH`.
1. Install [Docker](https://www.docker.com/) and make sure it's on your `PATH`.


## Building the Packer template
## Building the Packer template (Packer >= 1.7.0)
1. Run `packer init build.pkr.hcl`.
1. Run `packer build build.pkr.hcl`.

1. Install [Packer](https://www.packer.io/) and make sure it's on your `PATH`.
1. Install [Docker](https://www.docker.com/) and make sure it's on your `PATH`.

## Building the Packer template (Packer < 1.7.0)
1. Run `packer build build.json`.
1. Run `docker run -it --rm gruntwork/packer-hello-world-example cat /test.txt`.
1. You should see the text "Hello, World!"


## Run Docker
1. Run `docker run -it --rm gruntwork/packer-hello-world-example cat /test.txt`.
1. You should see the text "Hello, World!"


## Running automated tests against the Packer template
Expand Down
Loading

0 comments on commit bd89ef2

Please sign in to comment.