-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMakefile
executable file
·130 lines (111 loc) · 3.86 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
APP_NAME = authlog_exporter
BRANCH_FULL=$(shell git rev-parse --abbrev-ref HEAD)
BRANCH=$(subst /,-,$(BRANCH_FULL))
GIT_REV=$(shell git describe --abbrev=7 --always)
SERVICE_CONF_DIR = /etc/systemd/system
HTTP_PORT = 9991
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
all: run-test docker-run-test
.PHONY: test
test:
@echo "Run tests for $(APP_NAME)"
TZ="Etc/UTC" go test -mod=vendor -timeout=60s -count 1 ./...
.PHONY: build
build:
@echo "Build $(APP_NAME)"
@make test
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -mod=vendor -trimpath -ldflags "-X main.version=$(BRANCH)-$(GIT_REV)" -o $(APP_NAME) $(APP_NAME).go
.PHONY: build-darwin
build-darwin:
@echo "Build $(APP_NAME)"
@make test
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -mod=vendor -trimpath -ldflags "-X main.version=$(BRANCH)-$(GIT_REV)" -o $(APP_NAME) $(APP_NAME).go
.PHONY: dist
dist:
- @mkdir -p dist
docker build -f Dockerfile.artifacts --progress=plain -t $(APP_NAME)_dist .
- @docker rm -f $(APP_NAME)_dist 2>/dev/null || exit 0
docker run -d --name=$(APP_NAME)_dist $(APP_NAME)_dist
docker cp $(APP_NAME)_dist:/artifacts dist/
docker rm -f $(APP_NAME)_dist
.PHONY: run-test
run-test:
@echo "Run $(APP_NAME) for test log: ./test/auth.log"
@make build
./$(APP_NAME) --auth.log ./test_data/auth.log &
$(call http-test)
pkill -f $(APP_NAME)
.PHONY: run-test-darwin
run-test-darwin:
@echo "Run $(APP_NAME) for test log: ./test/auth.log"
@make build-darwin
./$(APP_NAME) --auth.log ./test_data/auth.log &
$(call http-test)
pkill -f $(APP_NAME)
.PHONY: prepare-service
prepare-service:
@echo "Prepare config file $(APP_NAME).service for systemd"
cp $(ROOT_DIR)/$(APP_NAME).service.template $(ROOT_DIR)/$(APP_NAME).service
sed -i.bak "s|/usr/bin|$(ROOT_DIR)|g" $(APP_NAME).service
rm $(APP_NAME).service.bak
.PHONY: install-service
install-service:
@echo "Install $(APP_NAME) as systemd service"
$(call service-install)
.PHONY: remove-service
remove-service:
@echo "Delete $(APP_NAME) systemd service"
$(call service-remove)
.PHONY: docker-build
docker-build:
@echo "Build $(APP_NAME) docker container"
@echo "Version $(BRANCH)-$(GIT_REV)"
docker build --pull -f Dockerfile --build-arg REPO_BUILD_TAG=$(BRANCH)-$(GIT_REV) -t $(APP_NAME) .
.PHONY: docker-run
docker-run:
@echo "Run $(APP_NAME) docker container"
$(call run-container, /var/log/auth.log:/log/auth.log:ro)
.PHONY: docker-run-test
docker-run-test:
@echo "Run $(APP_NAME) docker container for test log: ./test/auth.log"
$(call run-container, $(PWD)/test_data/auth.log:/log/auth.log:ro)
$(call http-test)
@make docker-remove
.PHONY: docker-remove
docker-remove:
@echo "Stop and delete $(APP_NAME) docker container"
docker rm -f $(APP_NAME)
.PHONY: docker-build-test-db
docker-build-test-db:
@echo "Build custom Maxmind database for tests"
docker build -f test_data/Dockerfile.testdb --build-arg SCRIPT_PATH="test_data/create_test_database.pl" --progress=plain -t $(APP_NAME)_build_test_db .
- @docker rm -f $(APP_NAME)_build_test_db 2>/dev/null || exit 0
docker run -d --name=$(APP_NAME)_build_test_db $(APP_NAME)_build_test_db
docker cp $(APP_NAME)_build_test_db:/db/geolite2_test.mmdb test_data/
docker rm -f $(APP_NAME)_build_test_db
define service-install
cp $(ROOT_DIR)/$(APP_NAME).service $(SERVICE_CONF_DIR)/$(APP_NAME).service
systemctl daemon-reload
systemctl enable $(APP_NAME)
systemctl restart $(APP_NAME)
systemctl status $(APP_NAME)
endef
define service-remove
systemctl stop $(APP_NAME)
systemctl disable $(APP_NAME)
rm $(SERVICE_CONF_DIR)/$(APP_NAME).service
systemctl daemon-reload
systemctl reset-failed
endef
define run-container
docker run -d --restart=always \
--name $(APP_NAME) -p $(HTTP_PORT):9991 \
-v ${1} \
-u $(shell id -u):$(shell id -g) \
$(APP_NAME) \
--auth.log /log/auth.log
endef
define http-test
sleep 2
curl -s "http://localhost:9991/metrics"| grep "^authlog_events_total"
endef