From daac9842550036a533c4e8e0ec71477361df86e1 Mon Sep 17 00:00:00 2001 From: Mike McGirr Date: Fri, 25 May 2018 14:45:33 -0700 Subject: [PATCH 1/8] Add example for the packer-vpc module. --- examples/terraform-vpc/Makefile | 30 +++++++++++++++++++++++++ examples/terraform-vpc/README.md | 33 +++++++++++++++++++++++++++ examples/terraform-vpc/main.tf | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 examples/terraform-vpc/Makefile create mode 100644 examples/terraform-vpc/README.md create mode 100644 examples/terraform-vpc/main.tf diff --git a/examples/terraform-vpc/Makefile b/examples/terraform-vpc/Makefile new file mode 100644 index 00000000..f841e591 --- /dev/null +++ b/examples/terraform-vpc/Makefile @@ -0,0 +1,30 @@ +# Hardcoding value of 3 minutes when we check if the plan file is stale +STALE_PLAN_FILE := `find "tf.out" -mmin -3 | grep -q tf.out` + +## Check if tf.out is stale (Older than 2 minutes) +check-plan-file: + @if ! ${STALE_PLAN_FILE} ; then \ + echo "ERROR: Stale tf.out plan file (older than 3 minutes)!"; \ + exit 1; \ + fi + +## Runs terraform get and terraform init for env +init: + @terraform get + @terraform init + +## terraform plan (makes everything) +plan: + @terraform plan -out=tf.out + +## terraform apply +apply: check-plan-file + @terraform apply tf.out + +# clean up terrform and any other files +clean: + @terraform destroy + @rm -f tf.out + @rm -f terraform.tfvars + @rm -f terraform.*.backup + @rm -f terraform.tfstate diff --git a/examples/terraform-vpc/README.md b/examples/terraform-vpc/README.md new file mode 100644 index 00000000..99d9fc72 --- /dev/null +++ b/examples/terraform-vpc/README.md @@ -0,0 +1,33 @@ +# Example to test the packer-vpc module. + +An example to demonstrate the [packer-vpc](https://github.com/fpco/terraform-aws-foundation/tree/master/modules/packer-vpc) module. +This will use the `packer-vpc` module to create a simple, isolated VPC on AWS which can be dedicated to building AMI's with `packer`. + + +## Using the Example + +### Environment creation and deployment + +To use this example set up AWS credentials and then run the commands in the +following order: + +``` +make init +make plan +make apply +``` + +Or simply run the following: + +``` +make init && make plan && make apply +``` + +### Destruction + +To destroy the test environment and other resources created by the example run the following command: + +``` +make clean +``` + diff --git a/examples/terraform-vpc/main.tf b/examples/terraform-vpc/main.tf new file mode 100644 index 00000000..8bf32079 --- /dev/null +++ b/examples/terraform-vpc/main.tf @@ -0,0 +1,38 @@ +variable "region" { + default = "us-west-1" + description = "The AWS region to deploy to" +} + +provider "aws" { + region = "${var.region}" +} + +module "vpc" { + source = "../../modules/packer-vpc" + region = "${var.region}" +} + +output "region" { + value = "${var.region}" + description = "region" +} + +output "vpc_id" { + value = "${module.vpc.vpc_id}" + description = "VPC ID" +} + +output "subnet_id" { + value = "${module.vpc.subnet_id}" + description = "Subnet ID" +} + +output "trusty_ami_id" { + value = "${module.vpc.trusty_ami_id}" + description = "ID of latest trusty AMI" +} + +output "xenial_ami_id" { + value = "${module.vpc.xenial_ami_id}" + description = "ID of latest xenial AMI" +} From 7083325c78df9dccff27fd0303efd91b2204f2b4 Mon Sep 17 00:00:00 2001 From: Mike McGirr Date: Fri, 25 May 2018 18:11:42 -0700 Subject: [PATCH 2/8] Update scripts for gitlab-ci. This is to add linting of the s3-full-access-policy module example. --- scripts/ci/tfinit.sh | 8 +++++++- scripts/ci/tflint.sh | 11 +++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/scripts/ci/tfinit.sh b/scripts/ci/tfinit.sh index 2bd95cad..1c7d84fa 100755 --- a/scripts/ci/tfinit.sh +++ b/scripts/ci/tfinit.sh @@ -1,4 +1,4 @@ -#/bin/bash +#!/usr/bin/env bash set -o nounset set -o pipefail @@ -9,3 +9,9 @@ pushd "$(dirname $(basename "${0}"))/tests" > /dev/null terraform init 1> /dev/null popd > /dev/null + +pushd "$(dirname $(basename "${0}"))/examples/s3-full-access-policy" > /dev/null + +terraform init 1> /dev/null + +popd > /dev/null diff --git a/scripts/ci/tflint.sh b/scripts/ci/tflint.sh index beb0e728..4a3f8451 100755 --- a/scripts/ci/tflint.sh +++ b/scripts/ci/tflint.sh @@ -1,4 +1,4 @@ -#/bin/bash +#!/usr/bin/env bash set -o nounset set -o pipefail @@ -7,6 +7,13 @@ set -o errexit pushd "$(dirname "$(basename "${0}")")/tests" > /dev/null terraform init 1> /dev/null 2>&1 || true -tflint --error-with-issues +tflint --error-with-issues + +popd > /dev/null + +pushd "$(dirname $(basename "${0}"))/examples/s3-full-access-policy" > /dev/null + +terraform init 1> /dev/null 2>&1 || true +tflint --error-with-issues popd > /dev/null From 41193446e73970cd044b1a9e5528be9a7d198c79 Mon Sep 17 00:00:00 2001 From: Mike McGirr Date: Fri, 25 May 2018 21:19:15 -0700 Subject: [PATCH 3/8] Add initial scripting to build and test resources with the s3-full-access-policy module. This may not have the AWS credentials to build/deploy resources with terraform yet. --- .gitlab-ci.yml | 4 ++++ examples/s3-full-access-policy/Makefile | 2 +- scripts/ci/tftest.sh | 13 +++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100755 scripts/ci/tftest.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d799db29..2b60ffb1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,3 +5,7 @@ lint: - scripts/ci/tfinit.sh - scripts/ci/tflint.sh # - scripts/ci/undeclared-variables.sh + +test: + script: + - script/ci/tftest.sh diff --git a/examples/s3-full-access-policy/Makefile b/examples/s3-full-access-policy/Makefile index ba843465..85dcfdb0 100644 --- a/examples/s3-full-access-policy/Makefile +++ b/examples/s3-full-access-policy/Makefile @@ -33,7 +33,7 @@ test: # clean up terrform and any other files clean: - @terraform destroy + @terraform destroy -auto-approve @rm -f tf.out @rm -f terraform.tfvars @rm -f terraform.*.backup diff --git a/scripts/ci/tftest.sh b/scripts/ci/tftest.sh new file mode 100755 index 00000000..be59ad0e --- /dev/null +++ b/scripts/ci/tftest.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -o nounset +set -o pipefail +set -o errexit + +pushd "$(dirname $(basename "${0}"))/examples/s3-full-access-policy" > /dev/null + +make init && make plan && make apply && make output 1> /dev/null +make test 1> /dev/null +make clean 1> /dev/null + +popd > /dev/null From 2cc61772af0be908088f96393ecaa31f66b3edce Mon Sep 17 00:00:00 2001 From: Mike McGirr Date: Fri, 25 May 2018 21:33:38 -0700 Subject: [PATCH 4/8] Separate CI scripts for s3-full-access-policy module example. This should help route out which one is failing on the gitlab pipeline. --- .gitlab-ci.yml | 4 +++- scripts/ci/{tftest.sh => examples/tfbuild.sh} | 2 -- scripts/ci/examples/tfcleanup.sh | 11 +++++++++++ scripts/ci/examples/tftest.sh | 11 +++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) rename scripts/ci/{tftest.sh => examples/tfbuild.sh} (83%) create mode 100755 scripts/ci/examples/tfcleanup.sh create mode 100755 scripts/ci/examples/tftest.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2b60ffb1..37616094 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,4 +8,6 @@ lint: test: script: - - script/ci/tftest.sh + - script/ci/examples/tfbuild.sh + - script/ci/examples/tftest.sh + - script/ci/examples/tfclean.sh diff --git a/scripts/ci/tftest.sh b/scripts/ci/examples/tfbuild.sh similarity index 83% rename from scripts/ci/tftest.sh rename to scripts/ci/examples/tfbuild.sh index be59ad0e..80d89467 100755 --- a/scripts/ci/tftest.sh +++ b/scripts/ci/examples/tfbuild.sh @@ -7,7 +7,5 @@ set -o errexit pushd "$(dirname $(basename "${0}"))/examples/s3-full-access-policy" > /dev/null make init && make plan && make apply && make output 1> /dev/null -make test 1> /dev/null -make clean 1> /dev/null popd > /dev/null diff --git a/scripts/ci/examples/tfcleanup.sh b/scripts/ci/examples/tfcleanup.sh new file mode 100755 index 00000000..64cdaec8 --- /dev/null +++ b/scripts/ci/examples/tfcleanup.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -o nounset +set -o pipefail +set -o errexit + +pushd "$(dirname $(basename "${0}"))/examples/s3-full-access-policy" > /dev/null + +make clean 1> /dev/null + +popd > /dev/null diff --git a/scripts/ci/examples/tftest.sh b/scripts/ci/examples/tftest.sh new file mode 100755 index 00000000..2206b21a --- /dev/null +++ b/scripts/ci/examples/tftest.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -o nounset +set -o pipefail +set -o errexit + +pushd "$(dirname $(basename "${0}"))/examples/s3-full-access-policy" > /dev/null + +make test 1> /dev/null + +popd > /dev/null From 53c00ce5de626f7f220d26794c9fd9b04abd9d40 Mon Sep 17 00:00:00 2001 From: Mike McGirr Date: Fri, 25 May 2018 21:37:31 -0700 Subject: [PATCH 5/8] Fix typos in .gitlab-ci.yml --- .gitlab-ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 37616094..d5c09bbe 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,6 +8,6 @@ lint: test: script: - - script/ci/examples/tfbuild.sh - - script/ci/examples/tftest.sh - - script/ci/examples/tfclean.sh + - scripts/ci/examples/tfbuild.sh + - scripts/ci/examples/tftest.sh + - scripts/ci/examples/tfclean.sh From 5d0667dff7cae071f8c4077c3a2be7650af42ccf Mon Sep 17 00:00:00 2001 From: Mike McGirr Date: Sun, 14 Jul 2019 20:44:20 -0700 Subject: [PATCH 6/8] Add manual trigger to gitlab ci for s3-full-policy test. --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d5c09bbe..d1ec58b6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -11,3 +11,4 @@ test: - scripts/ci/examples/tfbuild.sh - scripts/ci/examples/tftest.sh - scripts/ci/examples/tfclean.sh + when: manual From c44776f2bc65da94b6e0af574337745b5e156106 Mon Sep 17 00:00:00 2001 From: Mike McGirr Date: Sun, 14 Jul 2019 20:56:34 -0700 Subject: [PATCH 7/8] Update the changelog based on issue 144 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db9655d5..1b3fe656 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ * `gitlab-ha`: Update `Makefile` * `vpc-gateway`: correct module path reference * `legacy`: drop deprecated example - +* `s3-full-access-policy`: setup CI for building and running tests # v0.8.2 From 4b8e11c4500bddbb1942cd7910e4eb588dfac2e3 Mon Sep 17 00:00:00 2001 From: Mike McGirr Date: Mon, 15 Jul 2019 18:58:40 -0700 Subject: [PATCH 8/8] Fix typo --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d1ec58b6..c2ba2288 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,5 +10,5 @@ test: script: - scripts/ci/examples/tfbuild.sh - scripts/ci/examples/tftest.sh - - scripts/ci/examples/tfclean.sh + - scripts/ci/examples/tfcleanup.sh when: manual