-
Notifications
You must be signed in to change notification settings - Fork 72
/
Makefile
243 lines (203 loc) · 7.81 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
PROJECT = prometheus_rabbitmq_exporter
PROJECT_DESCRIPTION = RabbitMQ Prometheus.io metrics exporter
RABBITMQ_MINOR_VERSION = 3.7
# PROJECT_VERSION gets set in rabbitmq-components.mk to RABBITMQ_VERSION
RABBITMQ_VERSION = $(RABBITMQ_MINOR_VERSION).9.1
EZ = $(PROJECT)-$(PROJECT_VERSION)
PROJECT_APP_EXTRA_KEYS = {maintainers, ["Ilya Khaprov"]}, \
{licenses, ["MIT"]}, \
{links, [{"Github", "https://github.com/deadtrickster/prometheus_rabbitmq_exporter"}]}
ACCEPT_VERSION = 0.3.5
dep_accept = hex $(ACCEPT_VERSION)
PROMETHEUS_VERSION = 4.3.0
dep_prometheus = hex $(PROMETHEUS_VERSION)
PROMETHEUS_COWBOY_VERSION = 0.1.7
dep_prometheus_cowboy = hex $(PROMETHEUS_COWBOY_VERSION)
PROMETHEUS_HTTPD_VERSION = 2.1.10
dep_prometheus_httpd = hex $(PROMETHEUS_HTTPD_VERSION)
# prometheus_process_collector will fail to build in otp-*-build-context, skipping
ifneq ($(DOCKER_BUILD_CONTEXT),1)
PROMETHEUS_PROCESS_COLLECTOR_VERSION = 1.4.3
dep_prometheus_process_collector = hex $(PROMETHEUS_PROCESS_COLLECTOR_VERSION)
endif
DEPS = rabbit rabbitmq_management \
prometheus prometheus_cowboy prometheus_httpd
# We do not want these deps defined as applications in app
# prometheus_process_collector will fail to build in otp-*-build-context, skipping
ifneq ($(DOCKER_BUILD_CONTEXT),1)
BUILD_DEPS = rabbit_common rabbitmq_management_agent accept prometheus_process_collector
else
BUILD_DEPS = rabbit_common rabbitmq_management_agent accept
endif
DEP_EARLY_PLUGINS = rabbit_common/mk/rabbitmq-early-plugin.mk
DEP_PLUGINS = rabbit_common/mk/rabbitmq-plugin.mk
# FIXME: Use erlang.mk patched for RabbitMQ, while waiting for PRs to be
# reviewed and merged.
ERLANG_MK_REPO = https://github.com/rabbitmq/erlang.mk.git
ERLANG_MK_COMMIT = rabbitmq-tmp
RABBITMQ_BRANCH ?= v$(RABBITMQ_MINOR_VERSION).x
RABBITMQ_CURRENT_FETCH_URL ?= https://github.com/rabbitmq/
REBAR := $(CURDIR)/rebar3
REBAR_VERSION := 3.9.0
$(REBAR):
@wget --output-document $(REBAR) https://github.com/erlang/rebar3/releases/download/$(REBAR_VERSION)/rebar3 && \
chmod +x $(REBAR) && \
$(REBAR) --version | grep $(REBAR_VERSION)
include rabbitmq-components.mk
include erlang.mk
.PHONY: distclean
distclean::
@rm -fr tmp
DOCKER_IMAGE_VERSION = $(RABBITMQ_MINOR_VERSION)
DOCKER_BASE_IMAGE ?= rabbitmq:$(DOCKER_IMAGE_VERSION)-management
define BUILD_DOCKER_IMAGE
docker build \
--pull \
--build-arg FROM_IMAGE=$(DOCKER_BASE_IMAGE) \
--build-arg PROMETHEUS_RABBITMQ_EXPORTER_VERSION=$(PROJECT_VERSION) \
--tag deadtrickster/rabbitmq_prometheus:$(DOCKER_IMAGE_VERSION) .
endef
.PHONY: docker_build
docker_build:
@$(BUILD_DOCKER_IMAGE)
.PHONY: docker_build_alpine
docker_build_alpine: DOCKER_IMAGE_VERSION = 3.7-alpine
docker_build_alpine: DOCKER_BASE_IMAGE = rabbitmq:3.7-management-alpine
docker_build_alpine: docker_build
define PUSH_DOCKER_IMAGE
docker push deadtrickster/rabbitmq_prometheus:$(DOCKER_IMAGE_VERSION)
endef
.PHONY: docker_push
docker_push:
@$(PUSH_DOCKER_IMAGE)
.PHONY: docker_push_alpine
docker_push_alpine: DOCKER_IMAGE_VERSION = 3.7-alpine
docker_push_alpine: docker_push
define RUN_DOCKER_IMAGE
docker run --interactive --tty --publish=15672:15672 \
deadtrickster/rabbitmq_prometheus:$(DOCKER_IMAGE_VERSION)
endef
.PHONY: docker_run
docker_run:
@$(RUN_DOCKER_IMAGE)
.PHONY: docker_run_alpine
docker_run_alpine: DOCKER_IMAGE_VERSION = 3.7-alpine
docker_run_alpine: docker_run
.PHONY: up
up: $(abspath .)+up $(DEPS:%=$(DEPS_DIR)/%+up) $(BUILD_DEPS:%=$(DEPS_DIR)/%+up)
@:
%+up: fetch-deps
$(exec_verbose) cd $*; \
git fetch -p && \
if [ '$(RABBITMQ_BRANCH)' ]; then \
git checkout $(RABBITMQ_BRANCH) || : ; \
fi && \
if git symbolic-ref -q HEAD >/dev/null; then \
branch=$$(git symbolic-ref --short HEAD); \
remote=$$(git config branch.$$branch.remote); \
merge=$$(git config branch.$$branch.merge | sed 's,refs/heads/,,'); \
if [ "$$remote" -a "$$merge" ]; then \
git merge --ff-only "$$remote/$$merge"; \
fi; \
fi && \
echo
REBAR_DEPS_DIR := _build/default/lib
$(REBAR_DEPS_DIR): up
@mkdir -p _build/default && \
ln -sfnv $(CURDIR)/deps $(REBAR_DEPS_DIR)
tmp:
@mkdir -p tmp
tmp/accept-$(ACCEPT_VERSION).ez: $(REBAR) $(REBAR_DEPS_DIR) tmp
@cd $(REBAR_DEPS_DIR)/accept && \
$(REBAR) archive && \
mv accept-$(ACCEPT_VERSION).ez $(CURDIR)/tmp/
tmp/prometheus-$(PROMETHEUS_VERSION).ez: $(REBAR) $(REBAR_DEPS_DIR) tmp
@cd $(REBAR_DEPS_DIR)/prometheus && \
$(REBAR) archive && \
mv prometheus-$(PROMETHEUS_VERSION).ez $(CURDIR)/tmp/
tmp/prometheus_cowboy-$(PROMETHEUS_COWBOY_VERSION).ez: $(REBAR) $(REBAR_DEPS_DIR) tmp
@cd $(REBAR_DEPS_DIR)/prometheus_cowboy && \
$(REBAR) archive && \
mv prometheus_cowboy-$(PROMETHEUS_COWBOY_VERSION).ez $(CURDIR)/tmp/
tmp/prometheus_httpd-$(PROMETHEUS_HTTPD_VERSION).ez: $(REBAR) $(REBAR_DEPS_DIR) tmp
@cd $(REBAR_DEPS_DIR)/prometheus_httpd && \
$(REBAR) archive && \
mv prometheus_httpd-$(PROMETHEUS_HTTPD_VERSION).ez $(CURDIR)/tmp/
# prometheus_process_collector will fail to build in otp-*-build-context, skipping
ifneq ($(DOCKER_BUILD_CONTEXT),1)
tmp/prometheus_process_collector-$(PROMETHEUS_PROCESS_COLLECTOR_VERSION).ez: $(REBAR) $(REBAR_DEPS_DIR) tmp
@cd $(REBAR_DEPS_DIR)/prometheus_process_collector && \
$(REBAR) archive && \
mv prometheus_process_collector-$(PROMETHEUS_PROCESS_COLLECTOR_VERSION).ez $(CURDIR)/tmp
endif
tmp/$(EZ).ez: up app tmp
@rm -fr $(EZ) && mkdir $(EZ) && \
cp -r ebin include priv $(EZ) && \
rm -f $(EZ).ez && \
zip --move --recurse-paths --test $(EZ).ez $(EZ) && \
mv $(EZ).ez tmp/
.PHONY: ezs
ezs:: tmp/accept-$(ACCEPT_VERSION).ez
ezs:: tmp/prometheus-$(PROMETHEUS_VERSION).ez
ezs:: tmp/prometheus_cowboy-$(PROMETHEUS_COWBOY_VERSION).ez
ezs:: tmp/prometheus_httpd-$(PROMETHEUS_HTTPD_VERSION).ez
# prometheus_process_collector will fail to build in otp-*-build-context, skipping
ifneq ($(DOCKER_BUILD_CONTEXT),1)
ezs:: tmp/prometheus_process_collector-$(PROMETHEUS_PROCESS_COLLECTOR_VERSION).ez
endif
ezs:: tmp/$(EZ).ez
define RUN_DOCKER_TEST_IMAGE
docker run \
--tty \
--detach \
--name test_prometheus_rabbitmq_exporter \
--publish 15672:15672 \
deadtrickster/rabbitmq_prometheus:$(DOCKER_IMAGE_VERSION)
endef
define ENSURE_RABBIT_IN_DOCKER_IS_RUNNING
docker exec test_prometheus_rabbitmq_exporter \
bash -c "while sleep 1; do rabbitmq-diagnostics check_port_listener 15672 2>/dev/null && break; done"
endef
define CLEAN_DOCKER_TEST_IMAGE
docker rm --force test_prometheus_rabbitmq_exporter
endef
define VERIFY_METRICS_API
curl --silent --verbose --fail localhost:15672/api/metrics
endef
.PHONY: test
test: ezs docker_build
@$(CLEAN_DOCKER_TEST_IMAGE) ; \
$(RUN_DOCKER_TEST_IMAGE) && \
$(ENSURE_RABBIT_IN_DOCKER_IS_RUNNING) && \
$(VERIFY_METRICS_API) && \
$(CLEAN_DOCKER_TEST_IMAGE)
# This plugin is currently expected to work with RabbitMQ v3.7.9 and above:
# https://github.com/deadtrickster/prometheus_rabbitmq_exporter#versioning
#
# The OTP version that ships in rabbitmq:3.7.9 Docker image is v21.2.5:
# docker run -it --rm rabbitmq:3.7.9 -- cat /usr/local/lib/erlang/releases/21/OTP_VERSION
# 21.2.5
#
# We are creating an Erlang/OTP build context which has the same version as RabbitMQ v3.7.9 Docker image
# The goal is to produce ezs that are compatible with RabbitMQ v3.7.9 Docker image
MIN_SUPPORTED_OTP_VERSION := 21.2.5
.PHONY: otp-$(MIN_SUPPORTED_OTP_VERSION)-build-context
otp-$(MIN_SUPPORTED_OTP_VERSION)-build-context:
@docker run --tty --interactive --rm --name build_prometheus_rabbitmq_export \
--env LANG=C.UTF-8 \
--env LANGUAGE=C.UTF-8 \
--env LC_ALL=C.UTF-8 \
--env DOCKER_BUILD_CONTEXT=1 \
--volume $(CURDIR):/prometheus_rabbitmq_exporter \
--workdir /prometheus_rabbitmq_exporter \
erlang:$(MIN_SUPPORTED_OTP_VERSION) bash
# make /usr/local/bin/elixir
# apt update && apt install zip
# make ezs
tmp/elixir: tmp
@cd tmp && \
cd elixir || git clone https://github.com/elixir-lang/elixir.git; \
git checkout v1.8
/usr/local/bin/elixir: tmp/elixir
@cd tmp/elixir && \
make clean install