-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Makefile
377 lines (301 loc) · 13.2 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
TIMESTAMP ?= $(shell date +%s)
# ensure that we use the same timestamps in sub-makes. We've seen cases where
# these can vary by 1 second
export TIMESTAMP
ifneq ($(PRIVATE),)
REPOSITORY_SUFFIX:=-private
endif
SERVICES_SEPARATOR=,
SERVICES_TO_BUILD?=auth,blob,data,migrations,prescription,task,tools
SERVICES_TO_BUILD:=$(subst $(SERVICES_SEPARATOR), ,$(SERVICES_TO_BUILD))
ROOT_DIRECTORY:=$(realpath $(dir $(realpath $(lastword $(MAKEFILE_LIST)))))
REPOSITORY_GOPATH:=$(word 1, $(subst :, ,$(GOPATH)))
REPOSITORY_PACKAGE:=github.com/tidepool-org/platform
REPOSITORY_NAME:=$(notdir $(REPOSITORY_PACKAGE))$(REPOSITORY_SUFFIX)
BIN_DIRECTORY := ${ROOT_DIRECTORY}/_bin
PATH := ${PATH}:${BIN_DIRECTORY}
VERSION_BASE:=platform
VERSION_SHORT_COMMIT:=$(shell git rev-parse --short HEAD || echo "dev")
VERSION_FULL_COMMIT:=$(shell git rev-parse HEAD || echo "dev")
VERSION_PACKAGE:=$(REPOSITORY_PACKAGE)/application
GO_BUILD_FLAGS:=-buildvcs=false
GO_LD_FLAGS:=-ldflags '-X $(VERSION_PACKAGE).VersionBase=$(VERSION_BASE) -X $(VERSION_PACKAGE).VersionShortCommit=$(VERSION_SHORT_COMMIT) -X $(VERSION_PACKAGE).VersionFullCommit=$(VERSION_FULL_COMMIT)'
FIND_MAIN_CMD:=find . -path './$(BUILD)*' -not -path './.gvm_local/*' -not -path './vendor/*' -name '*.go' -not -name '*_test.go' -type f -exec egrep -l '^\s*func\s+main\s*(\s*)' {} \;
TRANSFORM_GO_BUILD_CMD:=sed 's|\.\(.*\)\(/[^/]*\)/[^/]*|_bin\1\2\2 .\1\2/.|'
GO_BUILD_CMD:=go build $(GO_BUILD_FLAGS) $(GO_LD_FLAGS) -o
GINKGO_FLAGS += --require-suite --poll-progress-after=10s --poll-progress-interval=20s -r
GINKGO_CI_WATCH_FLAGS += --randomize-all --succinct --fail-on-pending --cover --trace --race
GINKGO_CI_FLAGS += $(GINKGO_CI_WATCH_FLAGS) --randomize-suites --keep-going
GOTEST_PKGS ?= ./...
GOTEST_FLAGS ?=
TIMING_CMD ?=
ifdef TRAVIS_BRANCH
ifdef TRAVIS_COMMIT
DOCKER:=true
endif
endif
ifeq ($(TRAVIS_BRANCH),master)
ifeq ($(TRAVIS_PULL_REQUEST_BRANCH),)
DOCKER:=true
endif
else ifdef TRAVIS_TAG
DOCKER:=true
endif
ifdef DOCKER_FILE
SERVICE_NAME:=$(patsubst .%,%,$(suffix $(DOCKER_FILE)))
ifneq ($(filter $(SERVICE_NAME),$(SERVICES_TO_BUILD)),)
BUILD_SERVICE:=true
endif
DOCKER_REPOSITORY:=tidepool/$(REPOSITORY_NAME)-$(SERVICE_NAME)
endif
default: test
tmp:
@mkdir -p $(ROOT_DIRECTORY)/_tmp
bindir:
@mkdir -p $(ROOT_DIRECTORY)/_bin
CompileDaemon:
ifeq ($(shell which CompileDaemon),)
cd vendor/github.com/githubnemo/CompileDaemon && go install -mod=vendor .
endif
esc:
ifeq ($(shell which esc),)
cd vendor/github.com/mjibson/esc && go install -mod=vendor .
endif
mockgen:
ifeq ($(shell which mockgen),)
go install github.com/golang/mock/[email protected]
endif
ginkgo:
ifeq ($(shell which ginkgo),)
cd vendor/github.com/onsi/ginkgo/v2/ginkgo && go install -mod=vendor .
endif
goimports:
ifeq ($(shell which goimports),)
cd vendor/golang.org/x/tools/cmd/goimports && go install -mod=vendor .
endif
golint:
ifeq ($(shell which golint),)
cd vendor/golang.org/x/lint/golint && go install -mod=vendor .
endif
buildable: export GOBIN = ${BIN_DIRECTORY}
buildable: bindir CompileDaemon esc ginkgo goimports golint
generate: esc mockgen
@echo "go generate ./..."
@cd $(ROOT_DIRECTORY) && go generate ./...
ci-generate: generate format-write-changed imports-write-changed
@cd $(ROOT_DIRECTORY) && \
O=`git diff` && [ "$${O}" = "" ] || (echo "$${O}" && exit 1)
format:
@echo "gofmt -d -e -s"
@cd $(ROOT_DIRECTORY) && \
O=`find . -not -path './.gvm_local/*' -not -path './vendor/*' -name '*.go' -type f -exec gofmt -d -e -s {} \; 2>&1` && \
[ -z "$${O}" ] || (echo "$${O}" && exit 1)
format-write:
@echo "gofmt -e -s -w"
@cd $(ROOT_DIRECTORY) && \
O=`find . -not -path './.gvm_local/*' -not -path './vendor/*' -name '*.go' -type f -exec gofmt -e -s -w {} \; 2>&1` && \
[ -z "$${O}" ] || (echo "$${O}" && exit 1)
format-write-changed:
@cd $(ROOT_DIRECTORY) && \
git diff --name-only | grep '\.go$$' | xargs -I{} gofmt -e -s -w {}
imports: goimports
@echo "goimports -d -e -local 'github.com/tidepool-org/platform'"
@cd $(ROOT_DIRECTORY) && \
O=`find . -not -path './.gvm_local/*' -not -path './vendor/*' -not -path '**/test/mock.go' -not -name '*mock.go' -not -name '**_gen.go' -name '*.go' -type f -exec goimports -d -e -local 'github.com/tidepool-org/platform' {} \; 2>&1` && \
[ -z "$${O}" ] || (echo "$${O}" && exit 1)
imports-write: goimports
@echo "goimports -e -w -local 'github.com/tidepool-org/platform'"
@cd $(ROOT_DIRECTORY) && \
O=`find . -not -path './.gvm_local/*' -not -path './vendor/*' -name '*.go' -type f -exec goimports -e -w -local 'github.com/tidepool-org/platform' {} \; 2>&1` && \
[ -z "$${O}" ] || (echo "$${O}" && exit 1)
imports-write-changed: goimports
@cd $(ROOT_DIRECTORY) && \
git diff --name-only | grep '\.go$$' | xargs -I{} goimports -e -w -local 'github.com/tidepool-org/platform' {}
vet: tmp
@echo "go vet"
cd $(ROOT_DIRECTORY) && \
go vet ./... > _tmp/govet.out 2>&1 || \
(diff .govetignore _tmp/govet.out && exit 1)
vet-ignore:
@cd $(ROOT_DIRECTORY) && cp _tmp/govet.out .govetignore
lint: golint tmp
@echo "golint"
@cd $(ROOT_DIRECTORY) && \
find . -not -path './.gvm_local/*' -not -path './vendor/*' -name '*.go' -type f | sort -d | xargs -I {} golint {} | grep -v 'exported.*should have comment.*or be unexported' 2> _tmp/golint.out > _tmp/golint.out || [ $${?} == 1 ] && \
diff .golintignore _tmp/golint.out || \
exit 0
lint-ignore:
@cd $(ROOT_DIRECTORY) && cp _tmp/golint.out .golintignore
pre-build: format imports vet
# pre-build: format imports vet lint
build-list:
@cd $(ROOT_DIRECTORY) && $(FIND_MAIN_CMD)
build:
@echo "go build $(BUILD)"
@cd $(ROOT_DIRECTORY) && $(FIND_MAIN_CMD) | $(TRANSFORM_GO_BUILD_CMD) | while read LINE; do $(GO_BUILD_CMD) $${LINE}; done
build-watch: CompileDaemon
@cd $(ROOT_DIRECTORY) && BUILD=$(BUILD) CompileDaemon -build-dir='.' -build='make build' -color -directory='.' -exclude-dir='.git' -exclude='*_test.go' -include='Makefile' -recursive=true
ci-build: pre-build build
ci-build-watch: CompileDaemon
@cd $(ROOT_DIRECTORY) && BUILD=$(BUILD) CompileDaemon -build-dir='.' -build='make ci-build' -color -directory='.' -exclude-dir='.git' -include='Makefile' -recursive=true
service-build:
ifdef SERVICE
@$(MAKE) build BUILD=$${SERVICE}
endif
service-start: CompileDaemon tmp
ifdef SERVICE
@cd $(ROOT_DIRECTORY) && BUILD=$(SERVICE) CompileDaemon -build-dir='.' -build='make build' -command='_bin/$(SERVICE)/$(notdir $(SERVICE))' -directory='_tmp' -pattern='^$$' -include='$(subst /,.,$(SERVICE)).restart' -recursive=false -log-prefix=false -graceful-kill=true -graceful-timeout=60
endif
service-debug: CompileDaemon tmp
ifdef SERVICE
ifdef DEBUG_PORT
@cd $(ROOT_DIRECTORY) && BUILD=$(SERVICE) CompileDaemon -build-dir='.' -build='make build' -command='dlv exec --headless --log --listen=:$(DEBUG_PORT) --api-version=2 _bin/$(SERVICE)/$(notdir $(SERVICE))' -directory='_tmp' -pattern='^$$' -include='$(subst /,.,$(SERVICE)).restart' -recursive=false -log-prefix=false -graceful-kill=true -graceful-timeout=60
endif
endif
service-restart: tmp
ifdef SERVICE
@cd $(ROOT_DIRECTORY) && date +'%Y-%m-%dT%H:%M:%S%z' > _tmp/$(subst /,.,$(SERVICE)).restart
endif
service-restart-all:
@cd $(ROOT_DIRECTORY) && for SERVICE in $(shell ls -1 services) ; do $(MAKE) service-restart SERVICE="services/$${SERVICE}"; done
@cd $(ROOT_DIRECTORY) && for SERVICE in migrations tools; do $(MAKE) service-restart SERVICE="$${SERVICE}"; done
test: go-test
ginkgo-test: ginkgo
@echo "ginkgo $(GINKGO_FLAGS) $(TEST)"
@cd $(ROOT_DIRECTORY) && . ./env.test.sh && ginkgo $(GINKGO_FLAGS) $(TEST)
test-until-failure: ginkgo
@echo "ginkgo $(GINKGO_FLAGS) -untilItFails $(TEST)"
@cd $(ROOT_DIRECTORY) && . ./env.test.sh && ginkgo $(GINKGO_FLAGS) -untilItFails $(TEST)
test-watch: ginkgo
@echo "ginkgo watch $(GINKGO_FLAGS) $(TEST)"
@cd $(ROOT_DIRECTORY) && . ./env.test.sh && ginkgo watch $(GINKGO_FLAGS) $(TEST)
ci-test: ginkgo
@echo "ginkgo $(GINKGO_FLAGS) $(GINKGO_CI_FLAGS) $(TEST)"
@cd $(ROOT_DIRECTORY) && . ./env.test.sh && $(TIMING_CMD) ginkgo $(GINKGO_FLAGS) $(GINKGO_CI_FLAGS) $(TEST)
ci-test-until-failure: ginkgo
@echo "ginkgo $(GINKGO_FLAGS) $(GINKGO_CI_FLAGS) -untilItFails $(TEST)"
@cd $(ROOT_DIRECTORY) && . ./env.test.sh && ginkgo $(GINKGO_FLAGS) $(GINKGO_CI_FLAGS) -untilItFails $(TEST)
ci-test-watch: ginkgo
@echo "ginkgo watch $(GINKGO_FLAGS) $(GINKGO_CI_WATCH_FLAGS) $(TEST)"
@cd $(ROOT_DIRECTORY) && . ./env.test.sh && ginkgo watch $(GINKGO_FLAGS) $(GINKGO_CI_WATCH_FLAGS) $(TEST)
go-test:
. ./env.test.sh && $(TIMING_CMD) go test $(GOTEST_FLAGS) $(GOTEST_PKGS)
go-ci-test: GOTEST_FLAGS += -count=1 -race -shuffle=on -cover
go-ci-test: GOTEST_PKGS = ./...
go-ci-test: go-test
deploy: clean-deploy deploy-services deploy-migrations deploy-tools
deploy-services:
ifdef TRAVIS_TAG
@cd $(ROOT_DIRECTORY) && for SERVICE in $(shell ls -1 _bin/services); do $(MAKE) bundle-deploy DEPLOY=$${SERVICE} SOURCE=services/$${SERVICE}; done
endif
deploy-migrations:
ifdef TRAVIS_TAG
@$(MAKE) bundle-deploy DEPLOY=migrations SOURCE=migrations
endif
deploy-tools:
ifdef TRAVIS_TAG
@$(MAKE) bundle-deploy DEPLOY=tools SOURCE=tools
endif
ci-deploy: deploy
bundle-deploy:
ifdef DEPLOY
ifdef TRAVIS_TAG
@cd $(ROOT_DIRECTORY) && \
DEPLOY_TAG=$(DEPLOY)-$(TRAVIS_TAG) && \
DEPLOY_DIR=deploy/$(DEPLOY)/$${DEPLOY_TAG} && \
mkdir -p $${DEPLOY_DIR}/_bin/$(SOURCE) && \
cp -R _bin/$(SOURCE)/* $${DEPLOY_DIR}/_bin/$(SOURCE)/ && \
find $(SOURCE) -type f -name 'README.md' -exec cp {} $${DEPLOY_DIR}/_bin/{} \; && \
cp $(SOURCE)/start.sh $${DEPLOY_DIR}/ && \
tar -c -z -f $${DEPLOY_DIR}.tar.gz -C deploy/$(DEPLOY)/ $${DEPLOY_TAG}
endif
endif
docker:
ifdef DOCKER
@echo "$(DOCKER_PASSWORD)" | docker login --username "$(DOCKER_USERNAME)" --password-stdin
@cd $(ROOT_DIRECTORY) && for DOCKER_FILE in $(shell ls -1 Dockerfile.*); do $(MAKE) docker-build DOCKER_FILE="$${DOCKER_FILE}" TIMESTAMP="$(TIMESTAMP)";done
@cd $(ROOT_DIRECTORY) && for DOCKER_FILE in $(shell ls -1 Dockerfile.*); do $(MAKE) docker-push DOCKER_FILE="$${DOCKER_FILE}" TIMESTAMP="$(TIMESTAMP)";done
endif
docker-build:
ifdef DOCKER
ifdef DOCKER_FILE
ifdef BUILD_SERVICE
docker build --tag $(DOCKER_REPOSITORY):development --target=development --file "$(DOCKER_FILE)" .
docker build --tag $(DOCKER_REPOSITORY) --file "$(DOCKER_FILE)" .
ifdef TRAVIS_BRANCH
ifdef TRAVIS_COMMIT
ifdef TRAVIS_PULL_REQUEST_BRANCH
docker tag $(DOCKER_REPOSITORY) $(DOCKER_REPOSITORY):PR-$(subst /,-,$(TRAVIS_BRANCH))-$(TRAVIS_COMMIT)
docker tag $(DOCKER_REPOSITORY) $(DOCKER_REPOSITORY):PR-$(subst /,-,$(TRAVIS_BRANCH))-$(TRAVIS_COMMIT)-$(TIMESTAMP)
else
docker tag $(DOCKER_REPOSITORY) $(DOCKER_REPOSITORY):$(subst /,-,$(TRAVIS_BRANCH))-$(TRAVIS_COMMIT)
docker tag $(DOCKER_REPOSITORY) $(DOCKER_REPOSITORY):$(subst /,-,$(TRAVIS_BRANCH))-latest
docker tag $(DOCKER_REPOSITORY) $(DOCKER_REPOSITORY):$(subst /,-,$(TRAVIS_BRANCH))-$(TRAVIS_COMMIT)-$(TIMESTAMP)
endif
endif
endif
ifdef TRAVIS_TAG
docker tag $(DOCKER_REPOSITORY) $(DOCKER_REPOSITORY):$(TRAVIS_TAG:v%=%)
endif
else
@echo skipping $(DOCKER_FILE)
endif
endif
endif
docker-push:
ifdef DOCKER
ifdef BUILD_SERVICE
@echo "DOCKER_REPOSITORY = $(DOCKER_REPOSITORY)"
@echo "TRAVIS_BRANCH = $(TRAVIS_BRANCH)"
@echo "TRAVIS_PULL_REQUEST_BRANCH = $(TRAVIS_PULL_REQUEST_BRANCH)"
@echo "TRAVIS_COMMIT = $(TRAVIS_COMMIT)"
@echo "TRAVIS_TAG= $(TRAVIS_TAG)"
ifdef DOCKER_REPOSITORY
ifeq ($(TRAVIS_BRANCH),master)
ifeq ($(TRAVIS_PULL_REQUEST_BRANCH),)
docker push $(DOCKER_REPOSITORY)
endif
endif
ifdef TRAVIS_BRANCH
ifdef TRAVIS_COMMIT
ifdef TRAVIS_PULL_REQUEST_BRANCH
docker push $(DOCKER_REPOSITORY):PR-$(subst /,-,$(TRAVIS_BRANCH))-$(TRAVIS_COMMIT)
docker push $(DOCKER_REPOSITORY):PR-$(subst /,-,$(TRAVIS_BRANCH))-$(TRAVIS_COMMIT)-$(TIMESTAMP)
else
docker push $(DOCKER_REPOSITORY):$(subst /,-,$(TRAVIS_BRANCH))-$(TRAVIS_COMMIT)
docker push $(DOCKER_REPOSITORY):$(subst /,-,$(TRAVIS_BRANCH))-latest
docker push $(DOCKER_REPOSITORY):$(subst /,-,$(TRAVIS_BRANCH))-$(TRAVIS_COMMIT)-$(TIMESTAMP)
endif
endif
endif
ifdef TRAVIS_TAG
docker push $(DOCKER_REPOSITORY):$(TRAVIS_TAG:v%=%)
endif
endif
endif
endif
ci-docker: docker
clean: clean-bin clean-cover clean-debug clean-deploy clean-test
@cd $(ROOT_DIRECTORY) && rm -rf _tmp
clean-bin:
@cd $(ROOT_DIRECTORY) && rm -rf _bin _log
clean-cover:
@cd $(ROOT_DIRECTORY) && find . -type f -name "*.coverprofile" -o -name "coverprofile.out" -delete
clean-debug:
@cd $(ROOT_DIRECTORY) && find . -type f -name "debug" -o -name "__debug_bin*" -delete
clean-deploy:
@cd $(ROOT_DIRECTORY) && rm -rf deploy
clean-test:
@cd $(ROOT_DIRECTORY) && find . -type f -name "*.test" -o -name "*.report" -delete
clean-all: clean
pre-commit: format imports vet
# pre-commit: format imports vet lint
gopath-implode:
cd $(REPOSITORY_GOPATH) && rm -rf bin pkg && find src -not -path "src/$(REPOSITORY_PACKAGE)/*" -type f -delete && find src -not -path "src/$(REPOSITORY_PACKAGE)/*" -type d -empty -delete
.PHONY: default tmp bindir CompileDaemon esc ginkgo goimports golint buildable \
format format-write imports vet vet-ignore lint lint-ignore pre-build build-list build ci-build \
service-build service-start service-restart service-restart-all test test-watch ci-test c-test-watch \
deploy deploy-services deploy-migrations deploy-tools ci-deploy bundle-deploy \
docker docker-build docker-push ci-docker \
clean clean-bin clean-cover clean-debug clean-deploy clean-all pre-commit \
gopath-implode go-test go-ci-test