-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMakefile
More file actions
205 lines (166 loc) · 6.38 KB
/
Makefile
File metadata and controls
205 lines (166 loc) · 6.38 KB
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
PROJECT_VERSION := 0.2.2
DOCKER_REPO := synfinatic
PROJECT_NAME := netflow2ng
DIST_DIR ?= dist/
GOOS ?= $(shell uname -s | tr "[:upper:]" "[:lower:]")
ARCH ?= $(shell uname -m)
ifeq ($(ARCH),x86_64)
GOARCH := amd64
else
GOARCH := $(ARCH) # no idea if this works for other platforms....
endif
PROJECT_TAG := $(shell git describe --tags 2>/dev/null $(git rev-list --tags --max-count=1))
PROJECT_COMMIT := $(shell git rev-parse HEAD || echo "")
PROJECT_DELTA := $(shell DELTA_LINES=$$(git diff | wc -l); if [ $${DELTA_LINES} -ne 0 ]; then echo $${DELTA_LINES} ; else echo "''" ; fi)
BUILDINFOSDET ?=
PROGRAM_ARGS ?=
ifeq ($(PROJECT_TAG),)
PROJECT_TAG := NO-TAG
endif
ifeq ($(PROJECT_COMMIT),)
PROJECT_COMMIT := NO-CommitID
endif
ifeq ($(PROJECT_DELTA),)
PROJECT_DELTA :=
endif
VERSION_PKG := $(shell echo $(PROJECT_VERSION) | sed 's/^v//g')
LICENSE := MIT
URL := https://github.com/$(DOCKER_REPO)/$(PROJECT_NAME)
DESCRIPTION := NetFlow2ng: a NetFlow v9 collector for ntopng
BUILDINFOS := $(shell date +%FT%T%z)$(BUILDINFOSDET)
HOSTNAME := $(shell hostname)
LDFLAGS := -X "main.Version=$(PROJECT_VERSION)" -X "main.Delta=$(PROJECT_DELTA)" -X "main.Buildinfos=$(BUILDINFOS)" -X "main.Tag=$(PROJECT_TAG)" -X "main.CommitID=$(PROJECT_COMMIT)"
# default for current platform:
OUTPUT_NAME ?= $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)
ALL: netflow2ng
GOBFLAGS := -trimpath
LINUX_BIN := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-linux-amd64
LINUXARM64_BIN := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-linux-arm64
include help.mk
test: vet unittest lint ## Run important tests
precheck: test test-fmt test-tidy ## Run all tests that happen in a PR
clean:
rm -rf dist
clean-docker:
docker-compose -f docker-compose-pkg.yml rm -f
docker image rm netflow2ng_packager:v$(PROJECT_VERSION)
docker image rm $(DOCKER_REPO)/$(PROJECT_NAME):v$(PROJECT_VERSION)
clean-go:
go clean -i -r -cache -modcache
clean-proto:
rm -f proto/extended_flow.pb.go
netflow2ng: $(OUTPUT_NAME)
$(OUTPUT_NAME): .prepare protobuf
go build -ldflags='$(LDFLAGS)' -o $(OUTPUT_NAME) ./cmd/...
.go-mod-download:
go mod download
## Re-generate protobuf .pb.go. If you do this, be sure to edit the import path
# in the resulting extended_flow.pb.go to add 'v2' to the go package path.
# Ex: 'import pb "github.com/netsampler/goflow2/v2/pb
protobuf: proto/extended_flow.pb.go
ifeq ($(GOOS),darwin)
SEDFLAG := -i ''
else
SEDFLAG := -i
endif
proto/extended_flow.pb.go: proto/extended_flow.proto .go-mod-download
protoc --proto_path="$(shell go list -f '{{.Dir}}' -m github.com/netsampler/goflow2/v2)" \
--proto_path=./proto --go_out=./proto \
--go_opt=paths=source_relative \
extended_flow.proto
@echo "Modifying import path in extended_flow.pb.go to add 'v2' to the go package path."
sed $(SEDFLAG) 's|pb "github.com/netsampler/goflow2/pb"|pb "github.com/netsampler/goflow2/v2/pb"|g' $@
PHONY: docker-run
docker-run: ## Run docker container locally
docker run -it --rm \
-p 5556:5556/tcp \
-p 8080:8080/tcp \
-p 2055:2055/udp \
$(DOCKER_REPO)/$(PROJECT_NAME):v$(PROJECT_VERSION)
PHONY: docker
docker: ## Build docker images for Linux amd64/arm64
docker buildx build --platform linux/amd64,linux/arm64 \
-t $(DOCKER_REPO)/$(PROJECT_NAME):v$(PROJECT_VERSION) \
-t $(DOCKER_REPO)/$(PROJECT_NAME):latest \
-f Dockerfile .
docker-publish: ## Push docker images to hub.docker.com
docker push $(DOCKER_REPO)/$(PROJECT_NAME):v$(PROJECT_VERSION)
docker push $(DOCKER_REPO)/$(PROJECT_NAME):latest
.PHONY: unittest
unittest: ## Run go unit tests
go test -race -covermode=atomic -coverprofile=coverage.out ./...
.PHONY: vet
vet: # Go vet
@echo checking code is vetted...
go vet $(shell go list ./...)
.PHONY: test-race
test-race:
@echo testing code for races...
go test -race ./...
.PHONY: fmt
fmt: ## Format Go code
@go fmt cmd
.PHONY: test-fmt
test-fmt: fmt ## Test to make sure code if formatted correctly
@if test `git diff cmd | wc -l` -gt 0; then \
echo "Code changes detected when running 'go fmt':" ; \
git diff -Xfiles ; \
exit -1 ; \
fi
.PHONY: test-tidy
test-tidy: ## Test to make sure go.mod is tidy
@go mod tidy
@if test `git diff go.mod | wc -l` -gt 0; then \
echo "Need to run 'go mod tidy' to clean up go.mod" ; \
exit -1 ; \
fi
lint: ## Run golangci-lint
golangci-lint run
.PHONY: .prepare
.prepare:
mkdir -p $(DIST_DIR)
#.PHONY: package
#package: ## Build .deb and .rpm packages
# docker compose -f docker-compose-pkg.yml up
linux: $(LINUX_BIN) ## Build Linux/x86_64 binary
$(LINUX_BIN): $(wildcard */*.go) .prepare
GOARCH=amd64 GOOS=linux go build $(GOBFLAGS) -ldflags='$(LDFLAGS)' -o $(LINUX_BIN) ./cmd/...
@echo "Created: $(LINUX_BIN)"
linux-arm64: $(LINUXARM64_BIN) ## Build Linux/arm64 binary
$(LINUXARM64_BIN): $(wildcard */*.go) .prepare
GOARCH=arm64 GOOS=linux go build $(GOBFLAGS) -ldflags='$(LDFLAGS)' -o $(LINUXARM64_BIN) ./cmd/...
@echo "Created: $(LINUXARM64_BIN)"
.PHONY: package
package: ## Build deb/rpm packages
docker build -t netflow2ng-builder:latest -f Dockerfile.package .
docker run --rm \
-v $$(pwd)/dist:/root/dist \
-e VERSION=$(PROJECT_VERSION) \
-e UID=$$(id -u) \
-e GID=$$(id -g) \
netflow2ng-builder:latest
# These targets aren't for you.
.PHONY: .package-deb
.package-deb: $(OUTPUT_NAME)
fpm -s dir -t deb -n $(PROJECT_NAME) -v $(PROJECT_VERSION) \
--description "$(DESCRIPTION)" \
--url "$(URL)" \
--architecture $(ARCH) \
--license "$(LICENSE)" \
--deb-no-default-config-files \
--package $(DIST_DIR) \
$(OUTPUT_NAME)=/usr/bin/netflow2ng \
package/netflow2ng.service=/lib/systemd/system/netflow2ng.service \
package/netflow2ng.env=/etc/default/netflow2ng
.PHONY: .package-rpm
.package-rpm: $(OUTPUT_NAME)
fpm -s dir -t rpm -n $(PROJECT_NAME) -v $(PROJECT_VERSION) \
--description "$(DESCRIPTION)" \
--url "$(URL)" \
--architecture $(ARCH) \
--license "$(LICENSE)" \
--depends "zeromq" \
--package $(DIST_DIR) \
$(OUTPUT_NAME)=/usr/bin/netflow2ng \
package/netflow2ng.service=/lib/systemd/system/netflow2ng.service \
package/netflow2ng.env=/etc/default/netflow2ng