Skip to content

Commit e1da025

Browse files
authored
Merge pull request #8 from alexkonkin/feature/vm_module_add_ci_pipeline
changes for concourse ci integration
2 parents 495cf54 + 1c725a4 commit e1da025

File tree

14 files changed

+188
-97
lines changed

14 files changed

+188
-97
lines changed

Makefile

Lines changed: 64 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,54 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# Please note that this file was generated from [terraform-google-module-template](https://github.com/terraform-google-modules/terraform-google-module-template).
16+
# Please make sure to contribute relevant changes upstream!
17+
1518
# Make will use bash instead of sh
1619
SHELL := /usr/bin/env bash
1720

1821
# Docker build config variables
19-
CREDENTIALS_PATH ?= /cft/workdir/credentials.json
20-
DOCKER_ORG := gcr.io/cloud-foundation-cicd
21-
DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 0.11.10_216.0.0_1.19.1_0.1.10
22-
DOCKER_REPO_BASE_KITCHEN_TERRAFORM := ${DOCKER_ORG}/cft/kitchen-terraform:${DOCKER_TAG_BASE_KITCHEN_TERRAFORM}
23-
DOCKER_TAG_KITCHEN_TERRAFORM ?= ${DOCKER_TAG_BASE_KITCHEN_TERRAFORM}
24-
DOCKER_IMAGE_KITCHEN_TERRAFORM := cft/kitchen_terraform
22+
CREDENTIALS_PATH ?= /cft/workdir/credentials.json
23+
DOCKER_ORG := gcr.io/cloud-foundation-cicd
24+
DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 1.2.0
25+
DOCKER_REPO_BASE_KITCHEN_TERRAFORM := ${DOCKER_ORG}/cft/kitchen-terraform:${DOCKER_TAG_BASE_KITCHEN_TERRAFORM}
26+
27+
28+
# All is the first target in the file so it will get picked up when you just run 'make' on its own
29+
.PHONY: all
30+
all: check generate_docs
2531

26-
all: check_shell check_python check_golang check_terraform check_docker check_base_files test_check_headers check_headers check_trailing_whitespace generate_docs ## Run all linters and update documentation
32+
# Run all available linters
33+
.PHONY: check
34+
check: check_shell check_python check_golang check_terraform check_docker check_base_files test_check_headers check_headers check_trailing_whitespace
2735

2836
# The .PHONY directive tells make that this isn't a real target and so
2937
# the presence of a file named 'check_shell' won't cause this target to stop
3038
# working
3139
.PHONY: check_shell
32-
check_shell: ## Lint shell scripts
40+
check_shell:
3341
@source test/make.sh && check_shell
3442

3543
.PHONY: check_python
36-
check_python: ## Lint Python source files
44+
check_python:
3745
@source test/make.sh && check_python
3846

3947
.PHONY: check_golang
40-
check_golang: ## Lint Go source files
48+
check_golang:
4149
@source test/make.sh && golang
4250

4351
.PHONY: check_terraform
4452
check_terraform:
45-
@source ## Lint Terraform source files
53+
@source test/make.sh && check_terraform
4654

4755
.PHONY: check_docker
48-
check_docker: ## Lint Dockerfiles
56+
check_docker:
4957
@source test/make.sh && docker
5058

5159
.PHONY: check_base_files
5260
check_base_files:
5361
@source test/make.sh && basefiles
5462

55-
.PHONY: check_shebangs
56-
check_shebangs: ## Check that scripts have correct shebangs
57-
@source test/make.sh && check_bash
58-
5963
.PHONY: check_trailing_whitespace
6064
check_trailing_whitespace:
6165
@source test/make.sh && check_trailing_whitespace
@@ -65,91 +69,86 @@ test_check_headers:
6569
@echo "Testing the validity of the header check"
6670
@python test/test_verify_boilerplate.py
6771

72+
#.PHONY: check_headers
73+
#check_headers:
74+
# @source test/make.sh && check_headers
6875
.PHONY: check_headers
6976
check_headers: ## Check that source files have appropriate boilerplate
7077
@echo "Checking file headers"
7178
@python test/verify_boilerplate.py
7279

80+
7381
# Integration tests
7482
.PHONY: test_integration
75-
test_integration: ## Run integration tests
76-
bundle install
77-
bundle exec kitchen create
78-
bundle exec kitchen converge
79-
bundle exec kitchen converge
80-
bundle exec kitchen verify
81-
bundle exec kitchen destroy
83+
test_integration:
84+
test/ci_integration.sh
8285

8386
.PHONY: generate_docs
84-
generate_docs: ## Update README documentation for Terraform variables and outputs
87+
generate_docs:
8588
@source test/make.sh && generate_docs
8689

87-
.PHONY: release-new-version
88-
release-new-version:
89-
@source helpers/release-new-version.sh
90-
91-
# Build Docker
92-
.PHONY: docker_build_kitchen_terraform
93-
docker_build_kitchen_terraform:
94-
docker build -f build/docker/kitchen_terraform/Dockerfile \
95-
--build-arg BASE_IMAGE=${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
96-
-t ${DOCKER_IMAGE_KITCHEN_TERRAFORM}:${DOCKER_TAG_KITCHEN_TERRAFORM} .
97-
98-
# Push Docker image
99-
.PHONY: docker_push_kitchen_terraform
100-
docker_push_kitchen_terraform:
101-
docker tag ${DOCKER_IMAGE_KITCHEN_TERRAFORM}:${DOCKER_TAG_KITCHEN_TERRAFORM} ${DOCKER_ORG}/${DOCKER_IMAGE_KITCHEN_TERRAFORM}:${DOCKER_TAG_KITCHEN_TERRAFORM}
102-
docker push ${DOCKER_ORG}/${DOCKER_IMAGE_KITCHEN_TERRAFORM}:${DOCKER_TAG_KITCHEN_TERRAFORM}
90+
# Versioning
91+
.PHONY: version
92+
version:
93+
@source helpers/version-repo.sh
10394

10495
# Run docker
10596
.PHONY: docker_run
106-
docker_run: ## Launch a shell within the Docker test environment
97+
docker_run:
10798
docker run --rm -it \
108-
-e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \
99+
-e PROJECT_ID \
100+
-e SERVICE_ACCOUNT_JSON \
109101
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
110102
-v $(CURDIR):/cft/workdir \
111-
${DOCKER_IMAGE_KITCHEN_TERRAFORM}:${DOCKER_TAG_KITCHEN_TERRAFORM} \
112-
/bin/bash
103+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
104+
/bin/bash -c "source test/ci_integration.sh && setup_environment && exec /bin/bash"
113105

114106
.PHONY: docker_create
115-
docker_create: docker_build_kitchen_terraform
107+
docker_create:
116108
docker run --rm -it \
117-
-e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \
109+
-e PROJECT_ID \
110+
-e SERVICE_ACCOUNT_JSON \
118111
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
119112
-v $(CURDIR):/cft/workdir \
120-
${DOCKER_IMAGE_KITCHEN_TERRAFORM}:${DOCKER_TAG_KITCHEN_TERRAFORM} \
121-
/bin/bash -c "kitchen create"
113+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
114+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen create"
122115

123116
.PHONY: docker_converge
124-
docker_converge: ## Run `kitchen converge` within the Docker test environment
117+
docker_converge:
125118
docker run --rm -it \
126-
-e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \
119+
-e PROJECT_ID \
120+
-e SERVICE_ACCOUNT_JSON \
127121
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
128122
-v $(CURDIR):/cft/workdir \
129-
${DOCKER_IMAGE_KITCHEN_TERRAFORM}:${DOCKER_TAG_KITCHEN_TERRAFORM} \
130-
/bin/bash -c "kitchen converge && kitchen converge"
123+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
124+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen converge"
131125

132126
.PHONY: docker_verify
133-
docker_verify: ## Run `kitchen verify` within the Docker test environment
127+
docker_verify:
134128
docker run --rm -it \
135-
-e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \
129+
-e PROJECT_ID \
130+
-e SERVICE_ACCOUNT_JSON \
136131
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
137132
-v $(CURDIR):/cft/workdir \
138-
${DOCKER_IMAGE_KITCHEN_TERRAFORM}:${DOCKER_TAG_KITCHEN_TERRAFORM} \
139-
/bin/bash -c "kitchen verify"
133+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
134+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen verify"
140135

141136
.PHONY: docker_destroy
142-
docker_destroy: ## Run `kitchen destroy` within the Docker test environment
137+
docker_destroy:
143138
docker run --rm -it \
144-
-e CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=${CREDENTIALS_PATH} \
139+
-e PROJECT_ID \
140+
-e SERVICE_ACCOUNT_JSON \
145141
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
146142
-v $(CURDIR):/cft/workdir \
147-
${DOCKER_IMAGE_KITCHEN_TERRAFORM}:${DOCKER_TAG_KITCHEN_TERRAFORM} \
148-
/bin/bash -c "kitchen destroy"
143+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
144+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen destroy"
149145

150146
.PHONY: test_integration_docker
151-
test_integration_docker: docker_create docker_converge docker_verify docker_destroy ## Run a full integration test cycle
152-
@echo "Running test-kitchen tests in docker"
153-
154-
help: ## Prints help for targets with comments
155-
@grep -E '^[a-zA-Z._-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
147+
test_integration_docker:
148+
docker run --rm -it \
149+
-e PROJECT_ID \
150+
-e SERVICE_ACCOUNT_JSON \
151+
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
152+
-v $(CURDIR):/cft/workdir \
153+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
154+
make test_integration

examples/mig/full/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ variable "subnetwork" {
3737
default = ""
3838
}
3939

40+
variable "project_id" {
41+
description = "The GCP project to use for integration tests"
42+
default = ""
43+
}
44+
45+
variable "credentials_path" {
46+
description = "The path to the GCP credentials JSON file"
47+
default = ""
48+
}
49+
4050
variable "subnetwork_project" {
4151
description = "The project that subnetwork belongs to"
4252
default = ""
@@ -241,3 +251,7 @@ variable "autoscaling_lb" {
241251
type = "list"
242252
default = []
243253
}
254+
255+
variable "autoscaling_enabled" {
256+
description = "Creates an autoscaler for the managed instance group"
257+
}

examples/umig/full/outputs.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,3 @@ output "umig_self_links" {
2323
description = "List of self-links for unmanaged instance groups"
2424
value = "${module.umig.self_links}"
2525
}
26-
27-
output "mig_self_link" {
28-
description = "Self-link for managed instance group"
29-
value = "${module.mig.self_link}"
30-
}

examples/umig/full/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
# Common
1919
#########
2020

21+
variable "project_id" {
22+
description = "The GCP project to use for integration tests"
23+
}
24+
25+
variable "credentials_path" {
26+
description = "The path to the GCP credentials JSON file"
27+
default = ""
28+
}
29+
2130
variable "hostname" {
2231
description = "Hostname prefix for instances."
2332
default = "default"

modules/compute_instance/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Compute Instance
22

3-
This module is used to create compute instances (and only compute instances) using
3+
This module is used to create compute instances (and only compute instances) using
44
[google_compute_instance_from_template](https://www.terraform.io/docs/providers/google/r/compute_instance_from_template.html), with no instance groups.
55

66
## Usage

modules/instance_template/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# instance_template
22

33
This submodule allows you to create an `google_compute_instance_template`
4-
resource, which is used as the basis for the other instance, managed, and
4+
resource, which is used as the basis for the other instance, managed, and
55
unmanaged instance groups submodules.
66

77
## Usage

modules/umig/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Unmanaged Instance Group (UMIG)
22

3-
This module is used to create compute instances using
3+
This module is used to create compute instances using
44
[google_compute_instance_from_template](https://www.terraform.io/docs/providers/google/r/compute_instance_from_template.html), and [google_compute_instance_groups](https://www.terraform.io/docs/providers/google/r/compute_instance_group.html).
55

66
## Usage

test/ci_integration.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2018 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
# Always clean up.
19+
DELETE_AT_EXIT="$(mktemp -d)"
20+
finish() {
21+
echo 'BEGIN: finish() trap handler' >&2
22+
kitchen destroy "$SUITE"
23+
[[ -d "${DELETE_AT_EXIT}" ]] && rm -rf "${DELETE_AT_EXIT}"
24+
echo 'END: finish() trap handler' >&2
25+
}
26+
27+
# Map the input parameters provided by Concourse CI, or whatever mechanism is
28+
# running the tests to Terraform input variables. Also setup credentials for
29+
# use with kitchen-terraform, inspec, and gcloud.
30+
setup_environment() {
31+
local tmpfile
32+
tmpfile="$(mktemp)"
33+
echo "${SERVICE_ACCOUNT_JSON}" > "${tmpfile}"
34+
35+
cat "${tmpfile}" > test/fixtures/shared/credentials.json
36+
37+
client_email="$(jq -r .client_email "${tmpfile}")"
38+
# gcloud variables
39+
export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE="${tmpfile}"
40+
# Application default credentials (Terraform google provider and inspec-gcp)
41+
export GOOGLE_APPLICATION_CREDENTIALS="${tmpfile}"
42+
43+
# Terraform variables
44+
export TF_VAR_project_id="$PROJECT_ID"
45+
export TF_VAR_region="${REGION:-us-central1}"
46+
export TF_VAR_service_account='{email="'$client_email'", scopes = ["cloud-platform"]}'
47+
export TF_VAR_credentials_path_relative="../../shared/credentials.json"
48+
}
49+
50+
main() {
51+
export SUITE="${SUITE:-}"
52+
53+
set -eu
54+
# Setup trap handler to auto-cleanup
55+
export TMPDIR="${DELETE_AT_EXIT}"
56+
trap finish EXIT
57+
58+
# Setup environment variables
59+
setup_environment
60+
set -x
61+
62+
# Execute the test lifecycle
63+
kitchen create "$SUITE"
64+
kitchen converge "$SUITE"
65+
kitchen verify "$SUITE"
66+
}
67+
68+
# if script is being executed and not sourced.
69+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
70+
main "$@"
71+
fi
72+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../shared/terraform.tfvars
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../shared/terraform.tfvars

0 commit comments

Comments
 (0)