-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMakefile
218 lines (172 loc) · 5.59 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
#!/usr/bin/make -f
PROJECT := host-metering
RPMNAME := host-metering
VERSION := $(shell grep "Version" version/version.go | awk -F '"' '{print $$2}')
NEXT_VERSION ?=
SHORT_COMMIT ?= $(shell git rev-parse --short=8 HEAD)
AUTORELEASE ?= "git$(shell date "+%Y%m%d%H%M")G$(SHORT_COMMIT)"
UBI_VERSION ?= 7
DISTDIR ?= $(CURDIR)/dist
RPMTOPDIR := $(DISTDIR)/rpmbuild
GO := go
TESTDIR := $(CURDIR)/test
DASHBOARD_URL = http://localhost:9090/graph?g0.expr=system_cpu_logical_count&g0.tab=0&g0.range_input=1m
MOCKS_DIR = $(TESTDIR)/../mocks
# Test
.PHONY: test
test: vendor
@echo "Running the unit tests..."
PATH=$(MOCKS_DIR):$(PATH) \
$(GO) test -v \
-coverprofile=coverage.out \
-covermode=atomic \
-coverpkg=./... \
./...
@echo "Calculating the coverage..."
$(GO) tool cover -html=coverage.out -o coverage.html
$(GO) tool cover -func=coverage.out -o coverage.txt
@cat coverage.txt
# Non-verbose failfast variant of unit tests
.PHONY: test-ff
test-ff: vendor
@echo "Running the unit tests..."
PATH=$(MOCKS_DIR):$(PATH) \
$(GO) test -failfast ./...
# Build
.PHONY: build
build:
@echo "Building the project..."
$(GO) build -o $(DISTDIR)/$(PROJECT)
.PHONY: build-selinux
build-selinux:
@echo "Building SELinux policy..."
cd contrib/selinux && \
make -f /usr/share/selinux/devel/Makefile $(PROJECT).pp || exit
# Functional testing (manual or automatic)
.PHONY: container-env-setup
container-env-setup:
ifndef IS_IN_CONTAINER
CONTAINER_TARGET = prometheus
PROMETHEUS_ADDRESS = http://localhost:9090/api/v1/write
else
CONTAINER_TARGET =
PROMETHEUS_ADDRESS = http://prometheus:9090/api/v1/write
endif
.PHONY: test-daemon
test-daemon: cert build container-env-setup $(CONTAINER_TARGET)
@echo "Running $(PROJECT) in daemon mode..."
PATH=$(MOCKS_DIR):$(PATH) \
HOST_METERING_WRITE_URL=$(PROMETHEUS_ADDRESS) \
$(DISTDIR)/$(PROJECT) --config .devcontainer/host-metering.conf daemon
cert: mocks/consumer/cert.pem mocks/consumer/key.pem
mocks/consumer/cert.pem mocks/consumer/key.pem:
@echo "Generating test certificates..."
cd mocks && ./create-cert.sh
# Containers
.PHONY: podman-containers
podman-containers:
podman build --no-cache -t devcontainer_host-metering --build-arg UBI_VERSION=$(UBI_VERSION) -f .devcontainer/Dockerfile .
.PHONY: prometheus
prometheus: podman-containers
@echo "See the dashboard at: ${DASHBOARD_URL}"
podman-compose -f .devcontainer/docker-compose.yml up -d prometheus
.PHONY: prometheus-stop
prometheus-stop:
podman-compose -f .devcontainer/docker-compose.yml stop prometheus
.PHONY: podman-%
podman-%:
podman-compose -f .devcontainer/docker-compose.yml run -u root host-metering bash -c "cd /workspace/host-metering && make $(subst podman-,,$@)"
.PHONY: promtool-check-config
promtool-check-config:
podman-compose -f .devcontainer/docker-compose.yml up promtool
.PHONY: clean-pod
clean-pod:
podman-compose -f .devcontainer/docker-compose.yml down
# Release
.PHONY: version
version:
@echo $(VERSION)
.PHONY: version-update
version-update:
@test -n "$(NEXT_VERSION)" || (echo "NEXT_VERSION is not set"; exit 1)
@echo "Updating the version to $(NEXT_VERSION)..."
sed -i "s/Version = \".*\"/Version = \"$(NEXT_VERSION)\"/" version/version.go
.PHONY: distdir
distdir:
@echo "Creating the destination directory..."
mkdir -p $(DISTDIR)
# Ensure vendor was run at least once
vendor:
$(MAKE) force-vendor
# refresh vendor cache
.PHONY: force-vendor
force-vendor:
@echo "Downloading go dependencies..."
$(GO) mod tidy && $(GO) mod vendor
.PHONY: tarball
tarball: distdir force-vendor
@echo "Creating a tarball with the source code..."
git archive \
--format="tar" \
--prefix=$(PROJECT)-$(VERSION)/ \
--output $(DISTDIR)/$(PROJECT)-$(VERSION).tar \
HEAD
@echo "Adding go dependencies to the tarball..."
tar --append \
--transform="s/^\./$(PROJECT)-$(VERSION)/" \
--file $(DISTDIR)/$(PROJECT)-$(VERSION).tar \
./vendor
@echo "Compressing the tarball..."
gzip -f $(DISTDIR)/$(PROJECT)-$(VERSION).tar
# RPM build
.PHONY: rpm/spec
rpm/spec:
sed "s/#VERSION#/${VERSION}/" contrib/rpm/host-metering.spec.in > contrib/rpm/$(RPMNAME).spec
sed "s/#AUTORELEASE#/${AUTORELEASE}/" -i contrib/rpm/$(RPMNAME).spec
.PHONY: rpm/srpm
rpm/srpm: tarball rpm/spec
mkdir -p $(RPMTOPDIR)/SOURCES
cp $(DISTDIR)/$(PROJECT)-$(VERSION).tar.gz $(RPMTOPDIR)/SOURCES/
rm -rf $(RPMTOPDIR)/SRPMS/*
rpmbuild --define '_topdir $(RPMTOPDIR)' -bs contrib/rpm/$(RPMNAME).spec
.PHONY: rpm
rpm: rpm/srpm
rpmbuild --define '_topdir $(RPMTOPDIR)' -bb contrib/rpm/$(RPMNAME).spec
.PHONY: rpm/mock
rpm/mock: rpm/srpm
mkdir -p $(DISTDIR)/mock7
mock -r contrib/rpm/epel-7-x86_64-custom.cfg \
--resultdir=$(DISTDIR)/mock7/ \
--rebuild $(RPMTOPDIR)/SRPMS/$(shell ls -1 $(RPMTOPDIR)/SRPMS)
.PHONY: rpm/mock-8
rpm/mock-8: rpm/srpm
mkdir -p $(DISTDIR)/mock8
mock -r centos-stream-8-x86_64 \
--resultdir=$(DISTDIR)/mock8/ \
--rebuild $(RPMTOPDIR)/SRPMS/$(shell ls -1 $(RPMTOPDIR)/SRPMS)
.PHONY: rpm/mock-9
rpm/mock-9: rpm/srpm
mkdir -p $(DISTDIR)/mock9
mock -r centos-stream-9-x86_64 \
--resultdir=$(DISTDIR)/mock9 \
--rebuild $(RPMTOPDIR)/SRPMS/$(shell ls -1 $(RPMTOPDIR)/SRPMS)
# Clean
.PHONY: clean
clean:
@echo "Cleaning the project..."
rm -rf $(DISTDIR)
rm -rf $(CURDIR)/vendor
rm -rf $(CURDIR)/coverage.out
rm -rf $(CURDIR)/coverage.html
rm -rf $(CURDIR)/coverage.txt
rm -rf $(CURDIR)/$(PROJECT)
rm -rf $(CURDIR)/contrib/selinux/tmp
rm -rf $(CURDIR)/contrib/selinux/*.pp
rm -rf $(MOCKS_DIR)/cpumetrics
rm -rf $(MOCKS_DIR)/consumer
.PHONY: clean-node
clean-node:
@echo "Cleaning the node..."
rm -rf $(CURDIR)/node_modules
.PHONY: clean-all
clean-all: clean clean-pod clean-node