forked from appcelerator-archive/amp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
467 lines (387 loc) · 16.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
SHELL := /bin/bash
BASEDIR := $(shell echo $${PWD})
# =============================================================================
# BUILD MANAGEMENT
# Variables declared here are used by this Makefile *and* are exported to
# override default values used by supporting scripts in the hack directory
# =============================================================================
export UG := $(shell echo "$$(id -u):$$(id -g)")
export VERSION ?= $(shell cat VERSION)
export BUILD := $(shell git rev-parse HEAD | cut -c1-8)
export LDFLAGS := "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD) -s -w"
export OWNER := appcelerator
export REPO := github.com/$(OWNER)/amp
export GOOS := $(shell go env | grep GOOS | sed 's/"//g' | cut -c6-)
export GOARCH := $(shell go env | grep GOARCH | sed 's/"//g' | cut -c8-)
# =============================================================================
# COMMON DIRECTORIES
# =============================================================================
COMMONDIRS := pkg docker
CMDDIR := cmd
# =============================================================================
# DEFAULT TARGET
# =============================================================================
all: build
# =============================================================================
# PROTOC (PROTOCOL BUFFER COMPILER)
# Generate *.pb.go, *.pb.gw.go files in any non-excluded directory
# with *.proto files.
# =============================================================================
PROTODIRS := api cmd data tests $(COMMONDIRS)
# standard protobuf files
PROTOFILES := $(shell find $(PROTODIRS) -type f -name '*.proto')
PROTOTARGETS := $(PROTOFILES:.proto=.pb.go)
# grpc rest gateway protobuf files
PROTOGWFILES := $(shell find $(PROTODIRS) -type f -name '*.proto' -exec grep -l 'google.api.http' {} \;)
PROTOGWTARGETS := $(PROTOGWFILES:.proto=.pb.gw.go) $(PROTOGWFILES:.pb.gw.go=.swagger.json)
PROTOOPTS := -I$(GOPATH)/src/ \
-I $(GOPATH)/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
-I $(GOPATH)/src/github.com/grpc-ecosystem/grpc-gateway \
--go_out=plugins=grpc:$(GOPATH)/src/ \
--grpc-gateway_out=logtostderr=true:$(GOPATH)/src \
--swagger_out=logtostderr=true:$(GOPATH)/src/
PROTOALLTARGETS := $(PROTOTARGETS) $(PROTOGWTARGETS)
%.pb.go %.pb.gw.go %.swagger.json: %.proto
@echo $<
@protoc $(PROTOOPTS) $(GOPATH)/src/$(REPO)/$<
protoc: $(PROTOALLTARGETS)
.PHONY: clean-protoc
clean-protoc:
@find $(PROTODIRS) \( -name '*.pb.go' -o -name '*.pb.gw.go' -o -name '*.swagger.json' \) -type f -delete
# =============================================================================
# CLEAN
# =============================================================================
.PHONY: clean
clean: clean-protoc cleanall-cli clean-server clean-beat clean-agent clean-monit clean-plugin-local clean-plugin-aws clean-ampagent
# =============================================================================
# VENDOR
# =============================================================================
.PHONY: clean-vendor install-vendor
clean-vendor:
@rm -rf vendor
# not named `vendor` to avoid inopportune triggering
install-vendor:
@dep ensure -vendor-only
@dep prune
# =============================================================================
# BUILD
# =============================================================================
# When running in the amptools container, set DOCKER_CMD="sudo docker"
DOCKER_CMD ?= "docker"
build-base: protoc swagger build-server build-gateway build-beat build-agent build-monit
build-plugins: build-plugin-local build-plugin-aws build-ampagent
build: build-base build-plugins buildall-cli
# =============================================================================
# BUILD CLI (`amp`)
# =============================================================================
AMP := amp
AMPTARGET := bin/$(GOOS)/$(GOARCH)/$(AMP)
PLUGINDIRS := cluster/plugin/aws/plugin cluster/plugin/local/plugin
AMPDIRS := $(CMDDIR)/$(AMP) cli $(COMMONDIRS) $(PLUGINDIRS)
AMPSRC := $(shell find $(AMPDIRS) -type f -name '*.go')
AMPPKG := $(REPO)/$(CMDDIR)/$(AMP)
$(AMPTARGET): $(PROTOTARGETS) $(AMPSRC) VERSION vendor
@echo "Compiling $(AMP) source(s) ($(GOOS)/$(GOARCH))"
@echo $?
@GOOS=$(GOOS) GOARCH=$(GOARCH) hack/lbuild $(REPO)/bin $(AMP) $(AMPPKG) $(LDFLAGS)
@echo "bin/$(GOOS)/$(GOARCH)/$(AMP)"
# Warning: this only builds the CLI for the current OS, so when building under `ampmake`,
# the binary will be created under `bin/linux/amd64`.
build-cli: $(AMPTARGET)
.PHONY: rebuild-cli
rebuild-cli: clean-cli build-cli
.PHONY: rebuildall-cli
rebuildall-cli: cleanall-cli buildall-cli
.PHONY: clean-cli
clean-cli:
@rm -f $(AMPTARGET)
.PHONY: cleanall-cli
cleanall-cli:
# following fails in gogland shell
# @(shopt -s extglob; rm -f bin/*(darwin|linux|alpine)/amd64/amp)
@rm -f bin/darwin/amd64/amp bin/linux/amd64/amp bin/alpine/amd64/amp
# Build cross-compiled versions of the cli
buildall-cli: $(AMPTARGET) VERSION
@echo "cross-compiling $(AMP) cli for supported targets"
@hack/xbuild $(REPO)/bin $(AMP) $(REPO)/$(CMDDIR)/$(AMP) $(LDFLAGS)
# =============================================================================
# BUILD SERVER (`amplifier`)
# Saves binary to `cmd/amplifier/amplifier.alpine`,
# then builds `appcelerator/amplifier` image
# =============================================================================
AMPL := amplifier
AMPLBINARY=$(AMPL).alpine
AMPLTAG := $(VERSION)
AMPLIMG := appcelerator/$(AMPL):$(AMPLTAG)
AMPLTARGET := $(CMDDIR)/$(AMPL)/$(AMPLBINARY)
AMPLDIRS := $(CMDDIR)/$(AMPL) api data $(COMMONDIRS)
AMPLSRC := $(shell find $(AMPLDIRS) -type f -name '*.go')
AMPLPKG := $(REPO)/$(CMDDIR)/$(AMPL)
$(AMPLTARGET): $(PROTOTARGETS) $(AMPLSRC) VERSION vendor
@echo "Compiling $(AMPL) source(s):"
@echo $?
@hack/build4alpine $(REPO)/$(AMPLTARGET) $(AMPLPKG) $(LDFLAGS)
@echo "bin/$(GOOS)/$(GOARCH)/$(AMPL)"
build-server: $(AMPLTARGET)
@echo "build $(AMPLIMG)"
@$(DOCKER_CMD) build -t $(AMPLIMG) $(CMDDIR)/$(AMPL) || (rm -f $(AMPLTARGET); exit 1)
rebuild-server: clean-server build-server
.PHONY: clean-server
clean-server:
@rm -f $(AMPLTARGET)
@docker image rm $(AMPLIMG) 2>/dev/null || true
# =============================================================================
# BUILD GATEWAY (`gateway`)
# Saves binary to `cmd/gateway/gateway.alpine`,
# then builds `appcelerator/gateway` image
# =============================================================================
GW := gateway
GWBINARY=$(GW).alpine
GWTAG := $(VERSION)
GWIMG := appcelerator/$(GW):$(GWTAG)
GWTARGET := $(CMDDIR)/$(GW)/$(GWBINARY)
GWDIRS := $(CMDDIR)/$(GW) api data $(COMMONDIRS)
GWSRC := $(shell find $(GWDIRS) -type f -name '*.go')
GWPKG := $(REPO)/$(CMDDIR)/$(GW)
$(GWTARGET): $(PROTOTARGETS) $(GWSRC) VERSION vendor
@echo "Compiling $(GW) source(s):"
@echo $?
@hack/build4alpine $(REPO)/$(GWTARGET) $(GWPKG) $(LDFLAGS)
@echo "bin/$(GOOS)/$(GOARCH)/$(GW)"
build-gateway: $(GWTARGET)
@echo "build $(GWIMG)"
@$(DOCKER_CMD) build -t $(GWIMG) $(CMDDIR)/$(GW) || (rm -f $(GWTARGET); exit 1)
rebuild-gateway: clean-gateway build-gateway
.PHONY: clean-gateway
clean-gateway:
@rm -f $(GWTARGET)
@docker image rm $(GWIMG) 2>/dev/null || true
# =============================================================================
# BUILD monitoring (`promctl`)
# Saves binary to `cmd/monitoring/promctl.alpine`,
# then builds `appcelerator/amp-prometheus` image
# =============================================================================
MONIT := monitoring
MONITBINARY=promctl.alpine
MONITTAG := $(VERSION)
MONITIMG := appcelerator/amp-prometheus:$(MONITTAG)
MONITTARGET := $(MONIT)/$(MONITBINARY)
MONITDIRS := $(MONIT)/promctl $(COMMONDIRS)
MONITSRC := $(shell find $(MONITDIRS) -type f -name '*.go')
MONITPKG := $(REPO)/$(MONIT)/promctl
$(MONITTARGET): $(PROTOTARGETS) $(MONITSRC) VERSION vendor
@echo "Compiling $(MONIT) source(s):"
@echo $?
@hack/build4alpine $(REPO)/$(MONITTARGET) $(MONITPKG) $(LDFLAGS)
@echo "bin/$(GOOS)/$(GOARCH)/$(MONIT)"
build-monit: $(MONITTARGET)
@echo "build $(MONITIMG)"
@$(DOCKER_CMD) build -t $(MONITIMG) $(MONIT) || (rm -f $(MONITTARGET); exit 1)
rebuild-monit: clean-monit build-monit
.PHONY: clean-monit
clean-monit:
@rm -f $(MONITTARGET)
@docker image rm $(MONITIMG) 2>/dev/null || true
# =============================================================================
# BUILD BEAT (`ampbeat`)
# Saves binary to `cmd/ampbeat/ampbeat.alpine`,
# then builds `appcelerator/ampbeat` image
# =============================================================================
BEAT := ampbeat
BEATBINARY=$(BEAT).alpine
BEATTAG := $(VERSION)
BEATIMG := appcelerator/$(BEAT):$(BEATTAG)
BEATTARGET := $(CMDDIR)/$(BEAT)/$(BEATBINARY)
BEATDIRS := $(CMDDIR)/$(BEAT) api data $(COMMONDIRS)
BEATSRC := $(shell find $(BEATDIRS) -type f -name '*.go')
BEATPKG := $(REPO)/$(CMDDIR)/$(BEAT)
$(BEATTARGET): $(PROTOTARGETS) $(BEATSRC) VERSION vendor
@echo "Compiling $(BEAT) source(s):"
@echo $?
@hack/build4alpine $(REPO)/$(BEATTARGET) $(BEATPKG) $(LDFLAGS)
@echo "bin/$(GOOS)/$(GOARCH)/$(BEAT)"
build-beat: $(BEATTARGET)
@echo "build $(BEATIMG)"
@$(DOCKER_CMD) build -t $(BEATIMG) $(CMDDIR)/$(BEAT) || (rm -f $(BEATTARGET); exit 1)
rebuild-beat: clean-beat build-beat
.PHONY: clean-beat
clean-beat:
@rm -f $(BEATTARGET)
@docker image rm $(BEATIMG) 2>/dev/null || true
# =============================================================================
# BUILD AGENT (`agent`)
# Saves binary to `cmd/agent/agent.alpine`,
# then builds `appcelerator/agent` image
# =============================================================================
AGENT := agent
AGENTBINARY=$(AGENT).alpine
AGENTTAG := $(VERSION)
AGENTIMG := appcelerator/$(AGENT):$(AGENTTAG)
AGENTTARGET := $(CMDDIR)/$(AGENT)/$(AGENTBINARY)
AGENTDIRS := $(CMDDIR)/$(AGENT) agent api $(COMMONDIRS)
AGENTSRC := $(shell find $(AGENTDIRS) -type f -name '*.go')
AGENTPKG := $(REPO)/$(CMDDIR)/$(AGENT)
$(AGENTTARGET): $(PROTOTARGETS) $(AGENTSRC) VERSION vendor
@echo "Compiling $(AGENT) source(s):"
@echo $?
@hack/build4alpine $(REPO)/$(AGENTTARGET) $(AGENTPKG) $(LDFLAGS)
@echo "bin/$(GOOS)/$(GOARCH)/$(AGENT)"
build-agent: $(AGENTTARGET)
@echo "build $(AGENTIMG)"
@$(DOCKER_CMD) build -t $(AGENTIMG) $(CMDDIR)/$(AGENT) || (rm -f $(AGENTTARGET); exit 1)
rebuild-agent: clean-agent build-agent
.PHONY: clean-agent
clean-agent:
@rm -f $(AGENTTARGET)
@docker image rm $(AGENTIMG) 2>/dev/null || true
# =============================================================================
# CLUSTER PLUGINS
# =============================================================================
CPDIR := cluster/plugin
# =============================================================================
# BUILD AWS CLUSTER PLUGIN (`amp-aws`)
# Saves binary to `cluster/plugin/aws/aws.alpine`,
# then builds `appcelerator/amp-aws` image
# =============================================================================
CPAWS := amp-aws
CPAWSBINARY=$(CPAWS).alpine
CPAWSTAG := $(VERSION)
CPAWSIMG := appcelerator/$(CPAWS):$(CPAWSTAG)
CPAWSDIR := $(CPDIR)/aws
CPAWSDIRS := $(CPAWSDIR) $(COMMONDIRS)
CPAWSTARGET := $(CPAWSDIR)/$(CPAWSBINARY)
CPAWSSRC := $(shell find $(CPAWSDIRS) -type f -name '*.go')
CPAWSPKG := $(REPO)/$(CPAWSDIR)
$(CPAWSTARGET): $(PROTOTARGETS) $(CPAWSSRC) VERSION vendor
@echo "Compiling $(CPAWS) source(s):"
@echo $?
@hack/build4alpine $(REPO)/$(CPAWSTARGET) $(CPAWSPKG) $(LDFLAGS)
@echo "bin/$(GOOS)/$(GOARCH)/$(CPAWS)"
build-plugin-aws: $(CPAWSTARGET)
@echo "build $(CPAWSIMG)"
@$(DOCKER_CMD) build -t $(CPAWSIMG) $(CPAWSDIR) || (rm -f $(CPAWSTARGET); exit 1)
rebuild-cpaws: clean-plugin-aws build-plugin-aws
.PHONY: clean-plugin-aws
clean-plugin-aws:
@rm -f $(CPAWSTARGET)
@docker image rm $(CPAWSIMG) 2>/dev/null || true
# =============================================================================
# BUILD LOCAL CLUSTER PLUGIN (`amp-local`)
# Saves binary to `cluster/plugin/local/local.alpine`,
# then builds `appcelerator/amp-local` image
# =============================================================================
CPLOCAL := amp-local
CPLOCALBINARY=$(CPLOCAL).alpine
CPLOCALTAG := $(VERSION)
CPLOCALIMG := appcelerator/$(CPLOCAL):$(CPLOCALTAG)
CPLOCALDIR := $(CPDIR)/local
CPLOCALDIRS := $(CPLOCALDIR) $(COMMONDIRS)
CPLOCALTARGET := $(CPLOCALDIR)/$(CPLOCALBINARY)
CPLOCALSRC := $(shell find $(CPLOCALDIRS) -type f -name '*.go')
CPLOCALPKG := $(REPO)/$(CPLOCALDIR)
$(CPLOCALTARGET): $(PROTOTARGETS) $(CPLOCALSRC) VERSION vendor
@echo "Compiling $(CPLOCAL) source(s):"
@echo $?
@hack/build4alpine $(REPO)/$(CPLOCALTARGET) $(CPLOCALPKG) $(LDFLAGS)
@echo "bin/$(GOOS)/$(GOARCH)/$(CPLOCAL)"
build-plugin-local: $(CPLOCALTARGET)
@echo "build $(CPLOCALIMG)"
@$(DOCKER_CMD) build -t $(CPLOCALIMG) $(CPLOCALDIR) || (rm -f $(CPLOCALTARGET); exit 1)
rebuild-cplocal: clean-plugin-local build-plugin-local
.PHONY: clean-plugin-local
clean-plugin-local:
@rm -f $(CPLOCALTARGET)
@docker image rm $(CPLOCALIMG) 2>/dev/null || true
# =============================================================================
# BUILD AMPAGENT (`ampagent`)
# Saves binary to `cluster/ampagent/ampagent.alpine`,
# then builds `appcelerator/ampagent` image
# =============================================================================
AMPAGENT := ampagent
AMPAGENTBINARY=$(AMPAGENT).alpine
AMPAGENTTAG := $(VERSION)
AMPAGENTIMG := appcelerator/$(AMPAGENT):$(AMPAGENTTAG)
AMPAGENTDIR := cluster/$(AMPAGENT)
AMPAGENTDIRS := $(AMPAGENTDIR) $(COMMONDIRS)
AMPAGENTTARGET := $(AMPAGENTDIR)/$(AMPAGENTBINARY)
AMPAGENTSRC := $(shell find $(AMPAGENTDIRS) -type f -name '*.go')
AMPAGENTPKG := $(REPO)/$(AMPAGENTDIR)
$(AMPAGENTTARGET): $(PROTOTARGETS) $(AMPAGENTSRC) VERSION vendor
@echo "Compiling $(AMPAGENT) source(s):"
@echo $?
@hack/build4alpine $(REPO)/$(AMPAGENTTARGET) $(AMPAGENTPKG) $(LDFLAGS)
@echo "bin/$(GOOS)/$(GOARCH)/$(AMPAGENT)"
build-ampagent: $(AMPAGENTTARGET)
@echo "build $(AMPAGENTIMG)"
@$(DOCKER_CMD) build -t $(AMPAGENTIMG) $(AMPAGENTDIR) || (rm -f $(AMPAGENTTARGET); exit 1)
rebuild-ampagent: clean-ampagent build-ampagent
.PHONY: clean-ampagent
clean-ampagent:
@rm -f $(AMPAGENTTARGET)
@docker image rm $(AMPAGENTIMG) 2>/dev/null || true
# =============================================================================
# Swagger combine
# =============================================================================
swagger: protoc
@rm -f swagger.json
@node swagger-combine.js | jq . > swagger.json
# =============================================================================
# Quality checks
# =============================================================================
CHECKDIRS := agent api cli cluster cmd data monitoring tests $(COMMONDIRS)
CHECKSRCS := $(shell find $(CHECKDIRS) -type f \( -name '*.go' -and -not -name '*.pb.go' -and -not -name '*.pb.gw.go' \))
# format and simplify if possible (https://golang.org/cmd/gofmt/#hdr-The_simplify_command)
.PHONY: fmt
fmt:
@goimports -l $(CHECKDIRS) && goimports -w $(CHECKDIRS)
@gofmt -s -l -w $(CHECKSRCS)
.PHONY: lint
lint:
@echo "running lint checks - this will take a while..."
@gometalinter --deadline=10m --concurrency=1 --enable-gc --vendor --exclude=vendor --exclude=\.pb\.go \
--sort=path --aggregate \
--disable-all \
--enable=deadcode \
--enable=errcheck \
--enable=gas \
--enable=goconst \
--enable=gofmt \
--enable=goimports \
--enable=golint \
--enable=gosimple \
--enable=ineffassign \
--enable=interfacer \
--enable=staticcheck \
--enable=structcheck \
--enable=test \
--enable=unconvert \
--enable=unparam \
--enable=unused \
--enable=varcheck \
--enable=vet \
--enable=vetshadow \
$(CHECKDIRS)
.PHONY: lint-fast
lint-fast:
@echo "running subset of lint checks in fast mode"
@gometalinter \
--vendored-linters \
--fast --deadline=10m --concurrency=1 --enable-gc --vendor --exclude=vendor --exclude=\.pb\.go \
--disable=gotype \
$(CHECKDIRS)
# =============================================================================
# Misc
# =============================================================================
# Display all the Makefile rules
.PHONY: rules
rules:
@hack/print-make-rules
# Display pertinent environment variables
.PHONY: env
env:
@echo "GOOS=$(GOOS)"
@echo "GOARCH=$(GOARCH)"
# =============================================================================
# Run check before submitting a pull request!
# =============================================================================
check: fmt buildall lint-fast