forked from GoogleCloudPlatform/cluster-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
227 lines (182 loc) · 7.64 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# PREAMBLE
MIN_PACKER_VERSION=1.6 # for building images
MIN_TERRAFORM_VERSION=1.0 # for deploying modules
MIN_GOLANG_VERSION=1.18 # for building ghpc
.PHONY: install install-user tests format add-google-license install-dev-deps \
warn-go-missing warn-terraform-missing warn-packer-missing \
warn-go-version warn-terraform-version warn-packer-version \
test-engine validate_configs packer-check \
terraform-format packer-format \
check-tflint check-pre-commit
ENG = ./cmd/... ./pkg/...
TERRAFORM_FOLDERS=$(shell find ./modules ./community/modules ./tools -type f -name "*.tf" -not -path '*/\.*' -exec dirname "{}" \; | sort -u)
PACKER_FOLDERS=$(shell find ./modules ./community/modules ./tools -type f -name "*.pkr.hcl" -not -path '*/\.*' -exec dirname "{}" \; | sort -u)
ifneq (, $(shell which git))
## GIT IS PRESENT
ifneq (,$(wildcard .git))
## GIT DIRECTORY EXISTS
GIT_TAG_VERSION=$(shell git tag --points-at HEAD)
GIT_BRANCH=$(shell git branch --show-current)
GIT_COMMIT_INFO=$(shell git describe --tags --dirty --long)
GIT_COMMIT_HASH=$(shell git rev-parse HEAD)
GIT_INITIAL_HASH=$(shell git rev-list --max-parents=0 HEAD)
endif
endif
# RULES MEANT TO BE USED DIRECTLY
ghpc: warn-go-version warn-terraform-version warn-packer-version $(shell find ./cmd ./pkg ghpc.go -type f)
$(info **************** building ghpc ************************)
@go build -ldflags="-X 'main.gitTagVersion=$(GIT_TAG_VERSION)' -X 'main.gitBranch=$(GIT_BRANCH)' -X 'main.gitCommitInfo=$(GIT_COMMIT_INFO)' -X 'main.gitCommitHash=$(GIT_COMMIT_HASH)' -X 'main.gitInitialHash=$(GIT_INITIAL_HASH)'" ghpc.go
install-user:
$(info ******** installing ghpc in ~/bin *********************)
mkdir -p ~/bin
install ./ghpc ~/bin
ifeq ($(shell id -u), 0)
install:
$(info ***** installing ghpc in /usr/local/bin ***************)
install ./ghpc /usr/local/bin
else
install: install-user
endif
tests: warn-terraform-version warn-packer-version test-engine validate_configs packer-check
format: warn-go-version warn-terraform-version warn-packer-version terraform-format packer-format
$(info **************** formatting go code *******************)
go fmt $(ENG)
install-dev-deps: warn-terraform-version warn-packer-version check-pre-commit check-tflint check-shellcheck
$(info *********** installing developer dependencies *********)
go install github.com/terraform-docs/terraform-docs@latest
go install golang.org/x/lint/golint@latest
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
go install github.com/go-critic/go-critic/cmd/gocritic@latest
go install github.com/google/addlicense@latest
go install mvdan.cc/sh/v3/cmd/shfmt@latest
go install golang.org/x/tools/cmd/goimports@latest
ifeq (, $(shell which addlicense))
add-google-license:
$(error "could not find addlicense in PATH, run: go install github.com/google/addlicense@latest")
else
add-google-license:
addlicense -c "Google LLC" -l apache .
endif
# RULES SUPPORTING THE ABOVE
test-engine: warn-go-missing
$(info **************** vetting go code **********************)
go vet $(ENG)
$(info **************** running ghpc unit tests **************)
go test -cover $(ENG) 2>&1 | perl tools/enforce_coverage.pl
ifeq (, $(shell which pre-commit))
check-pre-commit:
$(info WARNING: pre-commit not installed, visit https://pre-commit.com/ for installation instructions.)
else
check-pre-commit:
endif
ifeq (, $(shell which tflint))
check-tflint:
$(info WARNING: tflint not installed, visit https://github.com/terraform-linters/tflint#installation for installation instructions.)
else
check-tflint:
endif
ifeq (, $(shell which shellcheck))
check-shellcheck:
$(info WARNING: shellcheck not installed, visit https://github.com/koalaman/shellcheck#installing for installation instructions.)
else
check-shellcheck:
endif
###################################
# GO SECTION
ifeq (, $(shell which go))
## GO IS NOT PRESENT
warn-go-missing:
$(error ERROR: could not find go in PATH, visit: https://go.dev/doc/install)
warn-go-version: warn-go-missing
else
## GO IS PRESENT
warn-go-missing:
GO_VERSION_CHECK=$(shell expr `go version | cut -f 3 -d ' ' | cut -f 1 -d '-' | cut -c 3-` \>= $(MIN_GOLANG_VERSION))
ifneq ("$(GO_VERSION_CHECK)", "1")
warn-go-version:
$(warning WARNING: Go version must be greater than $(MIN_GOLANG_VERSION), update at https://go.dev/doc/install)
else
warn-go-version:
endif
endif
###################################
# TERRAFORM SECTION
ifeq (, $(shell which terraform))
## TERRAFORM IS NOT PRESENT
warn-terraform-missing:
$(warning WARNING: terraform not installed and deployments will not work in this machine, visit https://learn.hashicorp.com/tutorials/terraform/install-cli)
warn-terraform-version: warn-terraform-missing
terraform-format:
$(warning WARNING: not formatting terraform)
validate_configs:
$(error ERROR: validate configs requires terraform)
else
## TERRAFORM IS PRESENT
warn-terraform-missing:
TF_VERSION_CHECK=$(shell expr `terraform version | cut -f 2- -d ' ' | cut -c 2- | head -n1` \>= $(MIN_TERRAFORM_VERSION))
ifneq ("$(TF_VERSION_CHECK)", "1")
warn-terraform-version:
$(warning WARNING: terraform version must be greater than $(MIN_TERRAFORM_VERSION), update at https://learn.hashicorp.com/tutorials/terraform/install-cli)
else
warn-terraform-version:
endif
validate_configs: ghpc
$(info *********** running basic integration tests ***********)
tools/validate_configs/validate_configs.sh
terraform-format:
$(info *********** cleaning terraform files syntax and generating terraform documentation ***********)
@for folder in ${TERRAFORM_FOLDERS}; do \
echo "cleaning syntax for $${folder}";\
terraform fmt -list=true $${folder};\
done
@for folder in ${TERRAFORM_FOLDERS}; do \
terraform-docs markdown $${folder} --config .tfdocs-markdown.yaml;\
terraform-docs json $${folder} --config .tfdocs-json.yaml;\
done
endif
# END OF TERRAFORM SECTION
###################################
# PACKER SECTION
ifeq (, $(shell which packer))
## PACKER IS NOT PRESENT
warn-packer-missing:
$(warning WARNING: packer not installed, visit https://learn.hashicorp.com/tutorials/packer/get-started-install-cli)
warn-packer-version: warn-packer-missing
packer-check: warn-packer-missing
$(warning WARNING: packer not installed, not checking packer code)
packer-format: warn-packer-missing
$(warning WARNING: packer not installed, not formatting packer code)
else
## PACKER IS PRESENT
warn-packer-missing:
PK_VERSION_CHECK=$(shell expr `packer version | cut -f 2- -d ' ' | cut -c 2- | head -n1` \>= $(MIN_PACKER_VERSION))
ifneq ("$(PK_VERSION_CHECK)", "1")
### WRONG PACKER VERSION, MAY ALSO MEAN THE USER HAS SOME OTHER PACKER TOOL
warn-packer-version:
$(warning WARNING: packer version must be greater than $(MIN_PACKER_VERSION), update at https://learn.hashicorp.com/tutorials/packer/get-started-install-cli)
packer-check: warn-packer-version
$(warning WARNING: wrong packer version, not checking packer code)
packer-format: warn-packer-version
$(warning WARNING: wrong packer version, not formatting packer code)
else
### PACKER INSTALLED WITH THE RIGHT VERSION
warn-packer-version:
packer-check:
$(info **************** checking packer syntax ***************)
@for folder in ${PACKER_FOLDERS}; do \
echo "checking syntax for $${folder}"; \
packer fmt -check $${folder}; \
done
packer-format:
$(info **************** formatting packer files and generating packer documentation **************)
@for folder in ${PACKER_FOLDERS}; do \
echo -e "cleaning syntax for $${folder}\n";\
packer fmt $${folder};\
done
@for folder in ${PACKER_FOLDERS}; do \
terraform-docs markdown $${folder} --config .tfdocs-markdown.yaml;\
terraform-docs json $${folder} --config .tfdocs-json.yaml;\
done
endif
endif
# END OF PACKER SECTION