forked from elastic/terraform-provider-elasticstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
229 lines (170 loc) · 7.67 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
228
229
.DEFAULT_GOAL = help
SHELL := /bin/bash
VERSION ?= 0.5.0
NAME = elasticstack
BINARY = terraform-provider-${NAME}
MARCH = "$$(go env GOOS)_$$(go env GOARCH)"
ACCTEST_PARALLELISM ?= 10
ACCTEST_TIMEOUT = 120m
ACCTEST_COUNT = 1
TEST ?= ./...
GOVERSION ?= 1.19
STACK_VERSION ?= 8.0.0
ELASTICSEARCH_NAME ?= terraform-elasticstack-es
ELASTICSEARCH_ENDPOINTS ?= http://$(ELASTICSEARCH_NAME):9200
ELASTICSEARCH_USERNAME ?= elastic
ELASTICSEARCH_PASSWORD ?= password
ELASTICSEARCH_NETWORK ?= elasticstack-network
ELASTICSEARCH_MEM ?= 1024m
KIBANA_NAME ?= terraform-elasticstack-kb
KIBANA_ENDPOINT ?= http://$(KIBANA_NAME):5601
KIBANA_SYSTEM_USERNAME ?= kibana_system
KIBANA_SYSTEM_PASSWORD ?= password
SOURCE_LOCATION ?= $(shell pwd)
export GOBIN = $(shell pwd)/bin
$(GOBIN): ## create bin/ in the current directory
mkdir -p $(GOBIN)
## Downloads all the Golang dependencies.
vendor:
@ go mod download
.PHONY: build-ci
build-ci: ## build the terraform provider
go build -o ${BINARY}
.PHONY: build
build: lint build-ci ## build the terraform provider
.PHONY: testacc
testacc: ## Run acceptance tests
TF_ACC=1 go test -v ./... -count $(ACCTEST_COUNT) -parallel $(ACCTEST_PARALLELISM) $(TESTARGS) -timeout $(ACCTEST_TIMEOUT)
.PHONY: test
test: ## Run unit tests
go test -v $(TEST) $(TESTARGS) -timeout=5m -parallel=4
# Retry command - first argumment is how many attempts are required, second argument is the command to run
# Backoff starts with 1 second and double with next itteration
retry = until [ $$(if [ -z "$$attempt" ]; then echo -n "0"; else echo -n "$$attempt"; fi) -ge $(1) ]; do \
backoff=$$(if [ -z "$$backoff" ]; then echo "1"; else echo "$$backoff"; fi); \
sleep $$backoff; \
$(2) && break; \
attempt=$$((attempt + 1)); \
backoff=$$((backoff * 2)); \
done
.PHONY: docker-testacc
docker-testacc: docker-elasticsearch docker-kibana ## Run acceptance tests in the docker container
@ docker run --rm \
-e ELASTICSEARCH_ENDPOINTS="$(ELASTICSEARCH_ENDPOINTS)" \
-e KIBANA_ENDPOINT="$(KIBANA_ENDPOINT)" \
-e ELASTICSEARCH_USERNAME="$(ELASTICSEARCH_USERNAME)" \
-e ELASTICSEARCH_PASSWORD="$(ELASTICSEARCH_PASSWORD)" \
--network $(ELASTICSEARCH_NETWORK) \
-w "/provider" \
-v "$(SOURCE_LOCATION):/provider" \
golang:$(GOVERSION) make testacc
.PHONY: docker-elasticsearch
docker-elasticsearch: docker-network ## Start Elasticsearch single node cluster in docker container
@ $(call retry, 5, if ! docker ps --format '{{.Names}}' | grep -w $(ELASTICSEARCH_NAME) > /dev/null 2>&1 ; then \
docker run -d \
--memory $(ELASTICSEARCH_MEM) \
-p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e "xpack.security.enabled=true" \
-e "repositories.url.allowed_urls=https://example.com/*" \
-e "path.repo=/tmp" \
-e ELASTIC_PASSWORD=$(ELASTICSEARCH_PASSWORD) \
--name $(ELASTICSEARCH_NAME) \
--network $(ELASTICSEARCH_NETWORK) \
docker.elastic.co/elasticsearch/elasticsearch:$(STACK_VERSION); \
fi)
.PHONY: docker-kibana
docker-kibana: docker-network docker-elasticsearch set-kibana-password ## Start Kibana node in docker container
@ $(call retry, 5, if ! docker ps --format '{{.Names}}' | grep -w $(KIBANA_NAME) > /dev/null 2>&1 ; then \
docker run -d \
-p 5601:5601 \
-e SERVER_NAME=kibana \
-e ELASTICSEARCH_HOSTS=$(ELASTICSEARCH_ENDPOINTS) \
-e ELASTICSEARCH_USERNAME=$(KIBANA_SYSTEM_USERNAME) \
-e ELASTICSEARCH_PASSWORD=$(KIBANA_SYSTEM_PASSWORD) \
-e "logging.root.level=debug" \
--name $(KIBANA_NAME) \
--network $(ELASTICSEARCH_NETWORK) \
docker.elastic.co/kibana/kibana:$(STACK_VERSION); \
fi)
.PHONY: docker-network
docker-network: ## Create a dedicated network for ES and test runs
@ if ! docker network ls --format '{{.Name}}' | grep -w $(ELASTICSEARCH_NETWORK) > /dev/null 2>&1 ; then \
docker network create $(ELASTICSEARCH_NETWORK); \
fi
.PHONY: set-kibana-password
set-kibana-password: ## Sets the ES KIBANA_SYSTEM_USERNAME's password to KIBANA_SYSTEM_PASSWORD. This expects Elasticsearch to be available at localhost:9200
@ $(call retry, 10, curl -X POST -u $(ELASTICSEARCH_USERNAME):$(ELASTICSEARCH_PASSWORD) -H "Content-Type: application/json" http://localhost:9200/_security/user/$(KIBANA_SYSTEM_USERNAME)/_password -d "{\"password\":\"$(KIBANA_SYSTEM_PASSWORD)\"}" | grep -q "^{}")
.PHONY: docker-clean
docker-clean: ## Try to remove provisioned nodes and assigned network
@ docker rm -f $(ELASTICSEARCH_NAME) $(KIBANA_NAME) || true
@ docker network rm $(ELASTICSEARCH_NETWORK) || true
.PHONY: docs-generate
docs-generate: tools ## Generate documentation for the provider
@ $(GOBIN)/tfplugindocs
.PHONY: gen
gen: docs-generate ## Generate the code and documentation
@ go generate ./...
.PHONY: clean
clean: ## Remove built binary
rm -f ${BINARY}
.PHONY: install
install: build ## Install built provider into the local terraform cache
mkdir -p ~/.terraform.d/plugins/registry.terraform.io/elastic/${NAME}/${VERSION}/${MARCH}
mv ${BINARY} ~/.terraform.d/plugins/registry.terraform.io/elastic/${NAME}/${VERSION}/${MARCH}
.PHONY: tools
tools: $(GOBIN) ## Install useful tools for linting, docs generation and development
@ cd tools && go install github.com/client9/misspell/cmd/misspell
@ cd tools && go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
@ cd tools && go install github.com/golangci/golangci-lint/cmd/golangci-lint
@ cd tools && go install github.com/goreleaser/goreleaser
.PHONY: misspell
misspell:
@ $(GOBIN)/misspell -error -source go ./internal/
@ $(GOBIN)/misspell -error -source text ./templates/
.PHONY: golangci-lint
golangci-lint:
@ $(GOBIN)/golangci-lint run --max-same-issues=0 --timeout=300s $(GOLANGCIFLAGS) ./internal/...
.PHONY: lint
lint: setup misspell golangci-lint check-fmt check-docs ## Run lints to check the spelling and common go patterns
.PHONY: fmt
fmt: ## Format code
go fmt ./...
terraform fmt --recursive
.PHONY:check-fmt
check-fmt: fmt ## Check if code is formatted
@if [ "`git status --porcelain `" ]; then \
echo "Unformatted files were detected. Please run 'make fmt' to format code, and commit the changes" && echo `git status --porcelain docs/` && exit 1; \
fi
.PHONY: check-docs
check-docs: docs-generate ## Check uncommitted changes on docs
@if [ "`git status --porcelain docs/`" ]; then \
echo "Uncommitted changes were detected in the docs folder. Please run 'make docs-generate' to autogenerate the docs, and commit the changes" && echo `git status --porcelain docs/` && exit 1; \
fi
.PHONY: setup
setup: tools ## Setup the dev environment
.PHONY: release-snapshot
release-snapshot: tools ## Make local-only test release to see if it works using "release" command
@ $(GOBIN)/goreleaser release --snapshot --rm-dist
.PHONY: release-no-publish
release-no-publish: tools check-sign-release ## Make a release without publishing artifacts
@ $(GOBIN)/goreleaser release --skip-publish --skip-announce --skip-validate
.PHONY: release
release: tools check-sign-release check-publish-release ## Build, sign, and upload your release
@ $(GOBIN)/goreleaser release --rm-dist
.PHONY: check-sign-release
check-sign-release:
ifndef GPG_FINGERPRINT
$(error GPG_FINGERPRINT is undefined, but required for signing the release)
endif
.PHONY: check-publish-release
check-publish-release:
ifndef GITHUB_TOKEN
$(error GITHUB_TOKEN is undefined, but required to make build and upload the released artifacts)
endif
.PHONY: release-notes
release-notes: ## greps UNRELEASED notes from the CHANGELOG
@ awk '/## \[Unreleased\]/{flag=1;next}/## \[.*\] - /{flag=0}flag' CHANGELOG.md
.PHONY: help
help: ## this help
@ awk 'BEGIN {FS = ":.*##"; printf "Usage: make \033[36m<target>\033[0m\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-10s\033[0m\t%s\n", $$1, $$2 }' $(MAKEFILE_LIST) | column -s$$'\t' -t