This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
368 lines (310 loc) · 13.7 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
PROJECT_NAME=fabric8-build
PACKAGE_NAME:=github.com/fabric8-services/$(PROJECT_NAME)
CUR_DIR=$(shell pwd)
TMP_PATH=$(CUR_DIR)/tmp
INSTALL_PREFIX=$(CUR_DIR)/bin
VENDOR_DIR=vendor
SOURCE_DIR ?= .
SOURCES := $(shell find $(SOURCE_DIR) -path $(SOURCE_DIR)/vendor -prune -o -name '*.go' -print)
DESIGN_DIR=design
DESIGNS := $(shell find $(SOURCE_DIR)/$(DESIGN_DIR) -path $(SOURCE_DIR)/vendor -prune -o -name '*.go' -print)
ALL_PKGS_EXCLUDE_PATTERN = 'vendor\|app\|tool\/cli\|design\|client\|test'
LDFLAGS=-ldflags "-X ${PACKAGE_NAME}/app.Commit=${COMMIT} -X ${PACKAGE_NAME}/app.BuildTime=${BUILD_TIME}"
# Paths common between OS
BINARY_SERVER_BIN=$(INSTALL_PREFIX)/fabric8-build
GOAGEN_BIN=$(VENDOR_DIR)/github.com/goadesign/goa/goagen/goagen
GO_BINDATA_DIR=$(VENDOR_DIR)/github.com/jteeuwen/go-bindata/go-bindata/
GO_BINDATA_BIN=$(GO_BINDATA_DIR)/go-bindata
FRESH_BIN=$(VENDOR_DIR)/github.com/chmouel/fresh/fresh
EXTRA_PATH=$(shell dirname $(GO_BINDATA_BIN))
GOCOV_BIN=$(VENDOR_DIR)/github.com/axw/gocov/gocov/gocov
GOCOVMERGE_BIN=$(VENDOR_DIR)/github.com/wadey/gocovmerge/gocovmerge
GOLINT_DIR=$(VENDOR_DIR)/github.com/golang/lint/golint
GOLINT_BIN=$(GOLINT_DIR)/golint
GOCYCLO_DIR=$(VENDOR_DIR)/github.com/fzipp/gocyclo
GOCYCLO_BIN=$(GOCYCLO_DIR)/gocyclo
GIT_BIN_NAME:=git
GO_BIN_NAME:=go
DEP_BIN_NAME:=dep
# by default use docker for compatibily and buildah/podman on Linux
CONTAINER_RUN := docker
# DB Container
DB_CONTAINER_NAME = db-build
DB_CONTAINER_PORT = 5433
DB_CONTAINER_IMAGE = registry.centos.org/postgresql/postgresql:9.6
# Auth
AUTH_CONTAINER_NAME = auth
AUTH_CONTAINER_PORT = 8089
AUTH_CONTAINER_IMAGE = quay.io/openshiftio/fabric8-services-fabric8-auth:16c631
# Env
ENV_CONTAINER_NAME = f8env
ENV_CONTAINER_IMAGE = quay.io/openshiftio/fabric8-services-fabric8-env:latest
# Wit
WIT_CONTAINER_NAME = f8wit
WIT_CONTAINER_IMAGE = quay.io/openshiftio/fabric8-services-fabric8-wit:latest
# By default reduce the amount of log output from tests, set this to debug and GO_TEST_VERBOSITY_FLAG to -v to increase it
F8_LOG_LEVEL ?= error
GO_TEST_VERBOSITY_FLAG =
# declares variable that are OS-sensitive
SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
include $(SELF_DIR)/.make/Makefile.lnx
endif
# This is a fix for a non-existing user in passwd file when running in a docker
# container and trying to clone repos of dependencies
GIT_COMMITTER_NAME ?= "Chmouel Boudjnah"
GIT_COMMITTER_EMAIL ?= "[email protected]"
export GIT_COMMITTER_NAME
export GIT_COMMITTER_EMAIL
COMMIT=$(shell git rev-parse HEAD 2>/dev/null)
GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no)
ifneq ($(GITUNTRACKEDCHANGES),)
COMMIT := $(COMMIT)-dirty
endif
BUILD_TIME=`date -u '+%Y-%m-%dT%H:%M:%SZ'`
.DEFAULT_GOAL := help
# Call this function with $(call log-info,"Your message")
define log-info =
@echo "INFO: $(1)"
endef
# -------------------------------------------------------------------
# Container build
# -------------------------------------------------------------------
BUILD_DIR = bin
REGISTRY_URI = quay.io
REGISTRY_NS = fabric8-services
REGISTRY_IMAGE = ${PROJECT_NAME}
ifeq ($(TARGET),rhel)
REGISTRY_URL_IMAGE := ${REGISTRY_URI}/openshiftio/rhel-${REGISTRY_NS}-${REGISTRY_IMAGE}
CONTAINERFILE := ./.make/Dockerfile.rhel
else
REGISTRY_URL_IMAGE := ${REGISTRY_URI}/openshiftio/${REGISTRY_NS}-${REGISTRY_IMAGE}
CONTAINERFILE := ./.make/Dockerfile
endif
$(BUILD_DIR):
mkdir $(BUILD_DIR)
.PHONY: build-linux $(BUILD_DIR)
build-linux: prebuild-check deps generate
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -v $(LDFLAGS) -o $(BUILD_DIR)/$(PROJECT_NAME)
.PHONY: image
image: clean-artifacts build-linux ## Build the container image
$(CONTAINER_RUN) build -t $(REGISTRY_URL_IMAGE) -f $(CONTAINERFILE) .
# -------------------------------------------------------------------
# Unittest
# -------------------------------------------------------------------
.PHONY: test-unit
test-unit: prebuild-check $(SOURCES) generate ## Runs the unit tests and WITHOUT producing coverage files for each package.
$(call log-info,"Running test: $@")
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN)))
F8_RESOURCE_UNIT_TEST=1 F8_RESOURCE_DATABASE=1 F8_DEVELOPER_MODE_ENABLED=1 \
F8_LOG_LEVEL=$(F8_LOG_LEVEL) \
go test $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)
.PHONY: coverage
coverage: prebuild-check deps $(SOURCES) ## Run coverage
$(call log-info,"Running coverage: $@")
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN)))
@cd $(VENDOR_DIR)/github.com/haya14busa/goverage && go build
F8_POSTGRES_PORT=$(DB_CONTAINER_PORT) \
F8_DEVELOPER_MODE_ENABLED=1 F8_RESOURCE_UNIT_TEST=1 \
F8_LOG_LEVEL=$(F8_LOG_LEVEL) F8_RESOURCE_DATABASE=1 \
./vendor/github.com/haya14busa/goverage/goverage -v -coverprofile=tmp/coverage.out $(TEST_PACKAGES)
sed -i~ -e '/\/sqlbindata.go:/d' -e '/\/main.go:/d' tmp/coverage.out
@go tool cover -func tmp/coverage.out
# -------------------------------------------------------------------
# help!
# -------------------------------------------------------------------
.PHONY: help
help: ## Prints this help
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort
# -------------------------------------------------------------------
# required tools
# -------------------------------------------------------------------
# Find all required tools:
GIT_BIN := $(shell command -v $(GIT_BIN_NAME) 2> /dev/null)
DEP_BIN_DIR := $(TMP_PATH)/bin
DEP_BIN := $(DEP_BIN_DIR)/$(DEP_BIN_NAME)
DEP_VERSION=v0.4.1
GO_BIN := $(shell command -v $(GO_BIN_NAME) 2> /dev/null)
$(INSTALL_PREFIX):
mkdir -p $(INSTALL_PREFIX)
$(TMP_PATH):
mkdir -p $(TMP_PATH)
.PHONY: prebuild-check
prebuild-check: $(TMP_PATH) $(INSTALL_PREFIX)
# Check that all tools where found
ifndef GIT_BIN
$(error The "$(GIT_BIN_NAME)" executable could not be found in your PATH)
endif
ifndef DEP_BIN
$(error The "$(DEP_BIN_NAME)" executable could not be found in your PATH)
endif
ifndef GO_BIN
$(error The "$(GO_BIN_NAME)" executable could not be found in your PATH)
endif
# -------------------------------------------------------------------
# deps
# -------------------------------------------------------------------
$(DEP_BIN_DIR):
mkdir -p $(DEP_BIN_DIR)
.PHONY: deps
deps: $(DEP_BIN) $(VENDOR_DIR) ## Download build dependencies.
# install dep in a the tmp/bin dir of the repo
$(DEP_BIN): $(DEP_BIN_DIR)
@echo "Installing 'dep' $(DEP_VERSION) at '$(DEP_BIN_DIR)'..."
mkdir -p $(DEP_BIN_DIR)
ifeq ($(UNAME_S),Darwin)
@curl -L -s https://github.com/golang/dep/releases/download/$(DEP_VERSION)/dep-darwin-amd64 -o $(DEP_BIN)
@cd $(DEP_BIN_DIR) && \
curl -L -s https://github.com/golang/dep/releases/download/$(DEP_VERSION)/dep-darwin-amd64.sha256 -o $(DEP_BIN_DIR)/dep-darwin-amd64.sha256 && \
echo "1544afdd4d543574ef8eabed343d683f7211202a65380f8b32035d07ce0c45ef dep" > dep-darwin-amd64.sha256 && \
shasum -a 256 --check dep-darwin-amd64.sha256
else
@curl -L -s https://github.com/golang/dep/releases/download/$(DEP_VERSION)/dep-linux-amd64 -o $(DEP_BIN)
@cd $(DEP_BIN_DIR) && \
echo "31144e465e52ffbc0035248a10ddea61a09bf28b00784fd3fdd9882c8cbb2315 dep" > dep-linux-amd64.sha256 && \
sha256sum -c dep-linux-amd64.sha256
endif
@chmod +x $(DEP_BIN)
$(VENDOR_DIR): Gopkg.toml
@echo "checking dependencies with $(DEP_BIN_NAME)"
@$(DEP_BIN) ensure -v
# -------------------------------------------------------------------
# Code format/check
# -------------------------------------------------------------------
GOFORMAT_FILES := $(shell find . -name '*.go' | grep -vEf .gofmt_exclude)
.PHONY: check-go-format
check-go-format: prebuild-check deps ## Exists with an error if there are files whose formatting differs from gofmt's
@gofmt -s -l ${GOFORMAT_FILES} 2>&1 \
| tee /tmp/gofmt-errors \
| read \
&& echo "ERROR: These files differ from gofmt's style (run 'make format-go-code' to fix this):" \
&& cat /tmp/gofmt-errors \
&& exit 1 \
|| true
# TODO(chmou): https://git.io/fxzkM
.PHONY: analyze-go-code
analyze-go-code: deps generate ## Run golangci analysis over the code.
$(info >>--- RESULTS: GOLANGCI CODE ANALYSIS ---<<)
@go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
@golangci-lint run
.PHONY: format-go-code
format-go-code: prebuild-check ## Formats any go file that differs from gofmt's style
@gofmt -s -l -w ${GOFORMAT_FILES}
# -------------------------------------------------------------------
# support for running in dev mode
# -------------------------------------------------------------------
$(FRESH_BIN): $(VENDOR_DIR)
cd $(VENDOR_DIR)/github.com/chmouel/fresh && go build -v
# -------------------------------------------------------------------
# support for generating goa code
# -------------------------------------------------------------------
$(GOAGEN_BIN): $(VENDOR_DIR)
cd $(VENDOR_DIR)/github.com/goadesign/goa/goagen && go build -v
# -------------------------------------------------------------------
# support for generating bindatas
# -------------------------------------------------------------------
$(GO_BINDATA_BIN): $(VENDOR_DIR)
cd $(VENDOR_DIR)/github.com/jteeuwen/go-bindata/go-bindata && go build -v
# -------------------------------------------------------------------
# clean
# -------------------------------------------------------------------
# For the global "clean" target all targets in this variable will be executed
CLEAN_TARGETS =
CLEAN_TARGETS += clean-artifacts
.PHONY: clean-artifacts
## Removes the ./bin directory.
clean-artifacts:
-rm -rf $(INSTALL_PREFIX)
CLEAN_TARGETS += clean-object-files
.PHONY: clean-object-files
## Runs go clean to remove any executables or other object files.
clean-object-files:
go clean ./...
CLEAN_TARGETS += clean-generated
.PHONY: clean-generated
## Removes all generated code.
clean-generated:
-rm -rf ./app
-rm -rf ./swagger/
-rm -f ./migration/sqlbindata.go
-rm -rf application/wit/witservice
-rm -rf application/env/envservice
CLEAN_TARGETS += clean-vendor
.PHONY: clean-vendor
## Removes the ./vendor directory.
clean-vendor:
-rm -rf $(VENDOR_DIR)
CLEAN_TARGETS += clean-tmp
.PHONY: clean-tmp
## Removes the ./vendor directory.
clean-tmp:
-rm -rf $(TMP_DIR)
# Keep this "clean" target here after all `clean-*` sub tasks
.PHONY: clean
clean: $(CLEAN_TARGETS) ## Runs all clean-* targets.
# -------------------------------------------------------------------
# run in dev mode
# -------------------------------------------------------------------
.PHONY: dev
dev: prebuild-check deps generate $(FRESH_BIN) ## run the server locally
F8_POSTGRES_PORT=$(DB_CONTAINER_PORT) F8_DEVELOPER_MODE_ENABLED=true $(FRESH_BIN)
# -------------------------------------------------------------------
# build the binary executable (to ship in prod)
# -------------------------------------------------------------------
.PHONY: build
build: prebuild-check deps generate ## Build the server
go build -v $(LDFLAGS) -o $(BINARY_SERVER_BIN)
# Pack all migration SQL files into a compilable Go file
migration/sqlbindata.go: $(GO_BINDATA_BIN) $(wildcard migration/sql-files/*.sql)
$(GO_BINDATA_BIN) \
-o migration/sqlbindata.go \
-pkg migration \
-prefix migration/sql-files \
-nocompress \
migration/sql-files
app/controllers.go: $(DESIGNS) $(GOAGEN_BIN) $(VENDOR_DIR)
$(GOAGEN_BIN) app -d ${PACKAGE_NAME}/${DESIGN_DIR}
$(GOAGEN_BIN) controller -d ${PACKAGE_NAME}/${DESIGN_DIR} -o controller/ --pkg controller --app-pkg ${PACKAGE_NAME}/app
$(GOAGEN_BIN) swagger -d ${PACKAGE_NAME}/${DESIGN_DIR}
$(GOAGEN_BIN) client -d github.com/fabric8-services/fabric8-wit/design --notool --pkg witservice -o application/wit
$(GOAGEN_BIN) client -d github.com/fabric8-services/fabric8-env/design --notool --pkg envservice -o application/env
$(GOAGEN_BIN) gen -d ${PACKAGE_NAME}/${DESIGN_DIR} --pkg-path=github.com/fabric8-services/fabric8-common/goasupport/status --out app
$(GOAGEN_BIN) gen -d ${PACKAGE_NAME}/${DESIGN_DIR} \
--pkg-path=github.com/fabric8-services/fabric8-common/goasupport/jsonapi_errors_helpers --out app
.PHONY: migrate-database
migrate-database: $(BINARY_SERVER_BIN) ## Compiles the server and runs the database migration with it
$(BINARY_SERVER_BIN) -migrateDatabase
.PHONY: generate
generate: app/controllers.go migration/sqlbindata.go ## Generate GOA sources. Only necessary after clean of if changed `design` folder.
.PHONY: regenerate
regenerate: clean-generated generate ## Runs the "clean-generated" and the "generate" target
.PHONY: print-env
print-env:
$(foreach var,$(.VARIABLES),$(info $(var)="$($(var))"))
# -----------------------------
# Run into a container for unitests
# -----------------------------
.PHONY: container-run
container-run: container-run-local-postgres ## Runs all the container images
.PHONY: container-run-local-postgres
container-run-local-postgres: container-clean-postgres ## Runs db in container
$(info >>--- Starting container $(DB_CONTAINER_NAME) ---<<)
@[[ "`$(CONTAINER_RUN) ps -q --filter 'name=$(DB_CONTAINER_NAME)'`xxx" == xxx ]] && \
$(CONTAINER_RUN) run --name $(DB_CONTAINER_NAME) -e POSTGRESQL_ADMIN_PASSWORD=`sed -n '/postgres.password/ { s/.*: //;p ;}' config.yaml` \
-d -p $(DB_CONTAINER_PORT):5432 $(DB_CONTAINER_IMAGE) >/dev/null
sleep 2 # sleep for a bit that it started
.PHONY: container-clean-postgres
container-clean-postgres:
$(info >>--- Stopping container $(DB_CONTAINER_NAME) ---<<)
@$(CONTAINER_RUN) rm -f $(DB_CONTAINER_NAME) 2>/dev/null || true
.PHONY: deploy-openshift-dev
deploy-openshift-dev: ## Deploy to an (already running) openshift environement
$(info >>-- Running the whole thing in openshift)
@./openshift/deploy-openshift-dev.sh
.PHONY: deploy-minishift
deploy-minishift-dev: build ## Deploy to a minishift environement
$(info >>-- Running in minishift)
eval `minishift docker-env` && eval `minishift oc-env` && \
make image && \
./openshift/deploy-openshift-dev.sh