Skip to content

Commit 941d024

Browse files
authored
CI-159 Update Makefile (#6)
1 parent c02241c commit 941d024

File tree

5 files changed

+33
-213
lines changed

5 files changed

+33
-213
lines changed

.gitignore

+9-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ override.tf.json
2525
#
2626
# !example_override.tf
2727

28+
# Ignore terratest configuration
29+
terratest.tf.json
30+
2831
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
2932
# example: *tfplan*
3033

31-
# IDE files
32-
**/.idea/*
34+
# Intellij
35+
.idea/
36+
*.iml
37+
38+
# Ignore makefile
39+
ops-makefile

Makefile

+23-207
Original file line numberDiff line numberDiff line change
@@ -2,212 +2,28 @@ ifneq (,)
22
.error This Makefile requires GNU Make.
33
endif
44

5-
.PHONY: help gen lint test _gen-main _gen-examples _gen-modules _lint_files _lint_fmt _pull-tf _pull-tf-docs
6-
7-
CURRENT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
8-
TF_EXAMPLES = $(sort $(dir $(wildcard $(CURRENT_DIR)examples/*/)))
9-
TF_MODULES = $(sort $(dir $(wildcard $(CURRENT_DIR)modules/*/)))
10-
11-
TF_VERSION = light
12-
TF_DOCS_VERSION = 0.9.1
13-
14-
# Adjust your delimiter here or overwrite via make arguments
15-
DELIM_START = <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
16-
DELIM_CLOSE = <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
17-
18-
help:
19-
@echo "gen Generate terraform-docs output and replace in all README.md's"
20-
@echo "lint Static source code analysis"
21-
@echo "test Integration tests"
22-
23-
gen: _pull-tf-docs
24-
@echo "################################################################################"
25-
@echo "# Terraform-docs generate"
26-
@echo "################################################################################"
27-
@$(MAKE) --no-print-directory _gen-main
28-
@$(MAKE) --no-print-directory _gen-examples
29-
@$(MAKE) --no-print-directory _gen-modules
30-
31-
lint: _pull-tf
32-
@$(MAKE) --no-print-directory _lint_files
33-
@$(MAKE) --no-print-directory _lint_fmt
34-
35-
test: _pull-tf
36-
@$(foreach example,\
37-
$(TF_EXAMPLES),\
38-
DOCKER_PATH="/t/examples/$(notdir $(patsubst %/,%,$(example)))"; \
39-
echo "################################################################################"; \
40-
echo "# examples/$$( basename $${DOCKER_PATH} )"; \
41-
echo "################################################################################"; \
42-
echo; \
43-
echo "------------------------------------------------------------"; \
44-
echo "# Terraform init"; \
45-
echo "------------------------------------------------------------"; \
46-
if docker run -it --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" hashicorp/terraform:$(TF_VERSION) \
47-
init \
48-
-verify-plugins=true \
49-
-lock=false \
50-
-upgrade=true \
51-
-reconfigure \
52-
-input=false \
53-
-get-plugins=true \
54-
-get=true \
55-
.; then \
56-
echo "OK"; \
57-
else \
58-
echo "Failed"; \
59-
docker run -it --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" --entrypoint=rm hashicorp/terraform:$(TF_VERSION) -rf .terraform/ || true; \
60-
exit 1; \
61-
fi; \
62-
echo; \
63-
echo "------------------------------------------------------------"; \
64-
echo "# Terraform validate"; \
65-
echo "------------------------------------------------------------"; \
66-
if docker run -it --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" hashicorp/terraform:$(TF_VERSION) \
67-
validate \
68-
$(ARGS) \
69-
.; then \
70-
echo "OK"; \
71-
docker run -it --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" --entrypoint=rm hashicorp/terraform:$(TF_VERSION) -rf .terraform/ || true; \
72-
else \
73-
echo "Failed"; \
74-
docker run -it --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" --entrypoint=rm hashicorp/terraform:$(TF_VERSION) -rf .terraform/ || true; \
75-
exit 1; \
76-
fi; \
77-
echo; \
78-
)
79-
80-
_gen-main:
81-
@echo "------------------------------------------------------------"
82-
@echo "# Main module"
83-
@echo "------------------------------------------------------------"
84-
@if docker run --rm \
85-
-v $(CURRENT_DIR):/data \
86-
-e DELIM_START='$(DELIM_START)' \
87-
-e DELIM_CLOSE='$(DELIM_CLOSE)' \
88-
cytopia/terraform-docs:$(TF_DOCS_VERSION) \
89-
terraform-docs-replace-012 --sort-inputs-by-required --with-aggregate-type-defaults md README.md; then \
90-
echo "OK"; \
91-
else \
92-
echo "Failed"; \
93-
exit 1; \
94-
fi
95-
96-
_gen-examples:
97-
@$(foreach example,\
98-
$(TF_EXAMPLES),\
99-
DOCKER_PATH="examples/$(notdir $(patsubst %/,%,$(example)))"; \
100-
echo "------------------------------------------------------------"; \
101-
echo "# $${DOCKER_PATH}"; \
102-
echo "------------------------------------------------------------"; \
103-
if docker run --rm \
104-
-v $(CURRENT_DIR):/data \
105-
--workdir "/data/$${DOCKER_PATH}" \
106-
-e DELIM_START='$(DELIM_START)' \
107-
-e DELIM_CLOSE='$(DELIM_CLOSE)' \
108-
cytopia/terraform-docs:$(TF_DOCS_VERSION) \
109-
terraform-docs-replace-012 --sort-inputs-by-required --with-aggregate-type-defaults md README.md; then \
110-
echo "OK"; \
111-
else \
112-
echo "Failed"; \
113-
exit 1; \
114-
fi; \
115-
)
116-
117-
_gen-modules:
118-
@$(foreach module,\
119-
$(TF_MODULES),\
120-
DOCKER_PATH="modules/$(notdir $(patsubst %/,%,$(module)))"; \
121-
echo "------------------------------------------------------------"; \
122-
echo "# $${DOCKER_PATH}"; \
123-
echo "------------------------------------------------------------"; \
124-
if docker run --rm \
125-
-v $(CURRENT_DIR):/data \
126-
-e DELIM_START='$(DELIM_START)' \
127-
-e DELIM_CLOSE='$(DELIM_CLOSE)' \
128-
cytopia/terraform-docs:$(TF_DOCS_VERSION) \
129-
terraform-docs-replace-012 --sort-inputs-by-required --with-aggregate-type-defaults md $${DOCKER_PATH}/README.md; then \
130-
echo "OK"; \
131-
else \
132-
echo "Failed"; \
133-
exit 1; \
134-
fi; \
135-
)
136-
137-
_lint_files:
138-
@# Lint all non-binary files for trailing spaces
139-
@echo "################################################################################"
140-
@echo "# Lint files"
141-
@echo "################################################################################"
142-
@echo
143-
@echo "------------------------------------------------------------"
144-
@echo "# Trailing spaces"
145-
@echo "------------------------------------------------------------"
146-
find . -type f -not \( -path "*/.git/*" -o -path "*/.github/*" -o -path "*/.terraform/*" -o -path "*/.idea/*"\) -print0 \
147-
| xargs -0 -n1 grep -Il '' \
148-
| tr '\n' '\0' \
149-
| xargs -0 -n1 \
150-
sh -c 'if [ -f "$${1}" ]; then if LC_ALL=C grep --color=always -inHE "^.*[[:blank:]]+$$" "$${1}";then false; else true; fi; fi' --
151-
@echo
152-
@echo "------------------------------------------------------------"
153-
@echo "# Windows line feeds (CRLF)"
154-
@echo "------------------------------------------------------------"
155-
find . -type f -not \( -path "*/.git/*" -o -path "*/.github/*" -o -path "*/.terraform/*" -o -path "*/.idea/*"\) -print0 \
156-
| xargs -0 -n1 grep -Il '' \
157-
| tr '\n' '\0' \
158-
| xargs -0 -n1 \
159-
sh -c 'if [ -f "$${1}" ]; then if file "$${1}" | grep --color=always -E "[[:space:]]CRLF[[:space:]].*line"; then false; else true; fi; fi' --
160-
@echo
161-
@echo "------------------------------------------------------------"
162-
@echo "# Single trailing newline"
163-
@echo "------------------------------------------------------------"
164-
find . -type f -not \( -path "*/.git/*" -o -path "*/.github/*" -o -path "*/.terraform/*" -o -path "*/.idea/*"\) -print0 \
165-
| xargs -0 -n1 grep -Il '' \
166-
| tr '\n' '\0' \
167-
| xargs -0 -n1 \
168-
sh -c 'if [ -f "$${1}" ]; then if ! (tail -c 1 "$${1}" | grep -Eq "^$$" && tail -c 2 "$${1}" | grep -Eqv "^$$"); then echo "$${1}"; false; else true; fi; fi' --
169-
@echo
170-
171-
_lint_fmt:
172-
@# Lint all Terraform files
173-
@echo "################################################################################"
174-
@echo "# Terraform fmt"
175-
@echo "################################################################################"
176-
@echo
177-
@echo "------------------------------------------------------------"
178-
@echo "# *.tf files"
179-
@echo "------------------------------------------------------------"
180-
@if docker run --rm -v "$(CURRENT_DIR):/t:ro" --workdir "/t" hashicorp/terraform:$(TF_VERSION) \
181-
fmt -check=true -diff=true -write=false -list=true /t; then \
182-
echo "OK"; \
183-
else \
184-
echo "Failed"; \
185-
exit 1; \
186-
fi;
187-
@echo "------------------------------------------------------------"
188-
@echo "# tfsec"
189-
@echo "------------------------------------------------------------"
190-
@if docker run --rm -v "$(CURRENT_DIR):/workdir:ro" --workdir "/workdir" tamr-docker.jfrog.io/ops/tfsec:v0.19.0 . ; then \
191-
echo "OK"; \
192-
else \
193-
echo "Failed"; \
194-
exit 1; \
195-
fi;
196-
@echo
197-
@echo "------------------------------------------------------------"
198-
@echo "# *.tfvars files"
199-
@echo "------------------------------------------------------------"
200-
@if docker run --rm --entrypoint=/bin/sh -v "$(CURRENT_DIR)/terraform:/t:ro" hashicorp/terraform:$(TF_VERSION) \
201-
-c "find . -name '*.tfvars' -type f -print0 | xargs -0 -n1 terraform fmt -check=true -write=false -diff=true -list=true"; then \
202-
echo "OK"; \
203-
else \
204-
echo "Failed"; \
205-
exit 1; \
206-
fi;
207-
@echo
5+
GIT_PREFIX = [email protected]
6+
7+
MAKEFILE_DIR = ops-makefile
8+
MAKEFILE_TAG = master
9+
MAKEFILE_REPO = Datatamer/ops-makefile.git
10+
11+
export BUILD_PATH ?= $(shell 'pwd')
12+
13+
ifneq ($(shell grep $(MAKEFILE_REPO) $(BUILD_PATH)/.git/config),)
14+
include $(BUILD_PATH)/Makefile.*
15+
include $(BUILD_PATH)/modules/*/Makefile*
16+
else ifeq (,$(firstword $(wildcard $(MAKEFILE_DIR))))
17+
$(shell git clone -c advice.detachedHead=false --depth=1 -b $(MAKEFILE_TAG) $(GIT_PREFIX):$(MAKEFILE_REPO))
18+
include $(BUILD_PATH)/$(MAKEFILE_DIR)/Makefile.*
19+
include $(BUILD_PATH)/$(MAKEFILE_DIR)/modules/*/Makefile*
20+
else
21+
UPDATE := $(shell git -C ops-makefile pull)
22+
include $(BUILD_PATH)/$(MAKEFILE_DIR)/Makefile.*
23+
include $(BUILD_PATH)/$(MAKEFILE_DIR)/modules/*/Makefile*
24+
endif
20825

209-
_pull-tf:
210-
docker pull hashicorp/terraform:$(TF_VERSION)
26+
EXCLUDE := $(filter-out $(MAKEFILE_DIR)/Makefile, $(wildcard $(MAKEFILE_DIR)/*))
21127

212-
_pull-tf-docs:
213-
docker pull cytopia/terraform-docs:$(TF_DOCS_VERSION)
28+
clean:
29+
rm -rf $(MAKEFILE_DIR)

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module "rds_postgres" {
1111
postgres_name = "example_rds_postgres"
1212
parameter_group_name = "example-rds-postgres-pg"
1313
identifier_prefix = "example-rds-"
14-
1514
username = "exampleUsername"
1615
password = "examplePassword"
1716

examples/minimal/variables.tf

-2
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@ variable "pg_password" {
2222
type = string
2323
description = "Password for postgres"
2424
}
25-
26-

modules/rds-postgres-sg/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module "rds_sg" {
1111
security_group_name = "examplerdssecuritygroup"
1212
additional_cidrs = ["1.2.3.4/32"]
1313
}
14-
```
14+
```
1515

1616
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
1717
## Requirements

0 commit comments

Comments
 (0)