Repository navigation
Expand file tree
/
Copy pathRun.mk
More file actions
373 lines (259 loc) · 10.9 KB
/
Copy pathRun.mk
File metadata and controls
373 lines (259 loc) · 10.9 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
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
369
370
371
372
# KyberBench
# Copyright (c) 2025-2026, Kyber Development Team, all right reserved.
#
BENCH_ACTION_LIST := run rund exec ssh start stop rm rmi ip
BENCH_CONTAINER_RUN := $(patsubst %,run_%,$(BENCH_IMAGE_TAG))
BENCH_CONTAINER_RUND := $(patsubst %,rund_%,$(BENCH_IMAGE_TAG))
BENCH_CONTAINER_EXEC := $(patsubst %,exec_%,$(BENCH_IMAGE_TAG))
BENCH_CONTAINER_SSH := $(patsubst %,ssh_%,$(BENCH_IMAGE_TAG))
BENCH_CONTAINER_START := $(patsubst %,start_%,$(BENCH_IMAGE_TAG))
BENCH_CONTAINER_STOP := $(patsubst %,stop_%,$(BENCH_IMAGE_TAG))
BENCH_CONTAINER_RM := $(patsubst %,rm_%,$(BENCH_IMAGE_TAG))
BENCH_CONTAINER_RMI := $(patsubst %,rmi_%,$(BENCH_IMAGE_TAG))
BENCH_CONTAINER_IP := $(patsubst %,ip_%,$(BENCH_IMAGE_TAG))
BENCH_CONTAINER_IMAGE := $(patsubst %,bench_%,$(BENCH_IMAGE_TAG))
.PHONY : $(BENCH_CONTAINER_IMAGE) $(BENCH_CONTAINER_RUN) $(BENCH_CONTAINER_RUND) $(BENCH_CONTAINER_EXEC) $(BENCH_CONTAINER_SSH) \
$(BENCH_CONTAINER_START) $(BENCH_CONTAINER_STOP) $(BENCH_CONTAINER_RM) $(BENCH_CONTAINER_RMI) $(BENCH_CONTAINER_IP)
###############################################################################
# Default Action
$(BENCH_ACTION_LIST) :
$(Q)$(MAKE) $@_$(BENCH_IMG_DEFAULT)
###############################################################################
# bench_self_id
# return current container self id
define bench_self_id
$(shell cat /proc/self/mountinfo | awk '$$5=="/etc/resolv.conf" {print $$4}' | awk -F "/" '{print $$(NF-1)}')
endef
# bench_host_temp_root
# return current container temp root directory path in host
define bench_host_temp_root
$(shell docker inspect $(call bench_self_id) -f "{{.GraphDriver.Data.MergedDir}}")
endef
# bench_host_mount_root
# $(1) path in current container
# return mount path in host
define bench_host_mount_root
$(shell docker inspect $(call bench_self_id) -f '{{range .Mounts}}{{if eq .Destination "$(1)" }}{{printf "%s" .Source}}{{end}}{{end}}')
endef
# bench_host_map_root
# $(1) path in current container
# return map root in host
define bench_host_map_root
$(if $(call bench_host_mount_root,$(1)),$(call bench_host_mount_root,$(1)),$(call bench_host_temp_root)$(1))
endef
# bench_host_map_path
# $(1) path in current container
# return map path in host
define bench_host_map_path
$(if $(BENCH_CI_ENABLE),$(call bench_host_map_root,$(BENCH_CI_ENABLE))/$(patsubst $(BENCH_CI_ENABLE)%,%,$(1)),$(if $(call is_in_docker),$(1),$(call bench_host_map_root,$(1))))
endef
# bench_daemon_name
# $(1) docker image tag
define bench_daemon_name
$(shell echo $(BENCH_NAME) | tr '/' '_')_$(1)_$(shell whoami)_$(shell echo $(BENCH_ROOT_PATH) | md5sum | cut -f1 -d " " | head -c6)
endef
# bench_daemon_id
# $(1) docker image tag
define bench_daemon_id
$(shell docker ps -a | awk '$$NF=="$(call bench_daemon_name,$(1))" {print $$1}' | head -n1)
endef
# bench_image_id
# $(1) docker image tag
define bench_image_id
$(shell docker images --format table | awk '$$1=="$(BENCH_NAME)" && $$2=="$(1)" {print $$3}')
endef
#$(shell docker images 2>/dev/null | awk '$$1=="$(BENCH_NAME):$(1)" {print $$2}')
#$(shell docker images | awk '$$1=="$(BENCH_NAME)" && $$2=="$(1)" {print $$3}')
###############################################################################
# Run Arguments
BENCH_RUN_ARGS := $(BENCH_RUN_OPTS)
# For device and kernel
BENCH_RUN_ARGS += --privileged
BENCH_RUN_ARGS += -v /dev:/dev
# Docker In Docker Arguments
BENCH_RUN_ARGS += -e BENCH_RUNNING_NESTED=1
BENCH_RUN_ARGS += -v /etc/localtime:/etc/localtime:ro
BENCH_RUN_ARGS += -v /var/run/docker.sock:/var/run/docker.sock
# Proxy Arguments
BENCH_RUN_ARGS += \
-e BENCH_ENV_EXPORT_http_proxy="$(http_proxy)" \
-e BENCH_ENV_EXPORT_https_proxy="$(https_proxy)" \
-e BENCH_ENV_EXPORT_no_proxy="localhost,127.0.0.1"
# X11 Forward Arguments
BENCH_RUN_ARGS += \
-e DISPLAY=$${DISPLAY} \
-v /tmp/.X11-unix:/tmp/.X11-unix
# Terminal Arguments
BENCH_RUN_ARGS += $(BENCH_TERM_OPTS)
# Debug Arguments
BENCH_RUN_ARGS += -e BENCH_RUN_DEBUG=$(BENCH_RUN_DEBUG)
# Image User Arguments
BENCH_RUN_ARGS += \
-e BENCH_IMG_GROUP=$(BENCH_IMG_GROUP) \
-e BENCH_IMG_USER=$(BENCH_IMG_USER) \
-e BENCH_IMG_GID=$(BENCH_IMG_GID) \
-e BENCH_IMG_UID=$(BENCH_IMG_UID)
ifneq ($(BENCH_INITRC_DISABLE),1)
BENCH_RUN_ARGS += \
-u $(BENCH_IMG_UID):$(BENCH_IMG_GID)
endif
# Container User Arguments
BENCH_RUN_ARGS += \
-e BENCH_RUN_GROUP=$(BENCH_RUN_GROUP) \
-e BENCH_RUN_USER=$(BENCH_RUN_USER) \
-e BENCH_RUN_GID=$(BENCH_RUN_GID) \
-e BENCH_RUN_UID=$(BENCH_RUN_UID)
# Initrc Arguments
BENCH_RUN_ARGS += \
-e BENCH_INITRC=$(BENCH_INITRC) \
-e BENCH_INITCFG=$(BENCH_INITCFG)
# Authorization Arguments
ifeq ($(call is_in_docker),)
ifeq ($(BENCH_ROOT_PATH),$(call file_dst,$(USER_MAP_WS)))
ifneq ($(call file_is_exist,$(BENCH_ROOT_PATH)/$(BENCH_AUTH_FILE)),)
$(error Container Authorization File Not Exist !)
endif
endif # ($(BENCH_ROOT_PATH),$(call file_dst,$(USER_MAP_WS)))
else # ($(call is_in_docker),)
ifneq ($(call file_is_exist,$(BENCH_ROOT_PATH)/$(BENCH_AUTH_FILE)),)
$(warning Container Authorization File Not Exist !)
$(shell touch $(BENCH_ROOT_PATH)/$(BENCH_AUTH_FILE))
endif
endif # ($(call is_in_docker),)
BENCH_RUN_ARGS += \
-e BENCH_AUTH_FILE=$(BENCH_AUTH_FILE)
# Mount Arguments
ifeq ($(BENCH_RUNNING_NESTED),1)
BENCH_RUN_ARGS += \
--mount type=bind,source=$(call bench_host_map_path,/etc/$(BENCH_AUTH_FILE)),destination=/etc/$(BENCH_AUTH_FILE)
else # ($(BENCH_RUNNING_NESTED),1)
BENCH_RUN_ARGS += \
--mount type=bind,source=$(call bench_host_map_path,$(BENCH_ROOT_PATH)/$(BENCH_AUTH_FILE)),destination=/etc/$(BENCH_AUTH_FILE)
endif # ($(BENCH_RUNNING_NESTED),1)
# Work Directory Arguments
BENCH_RUN_ARGS += \
-e BENCH_WORK_PATH=$(call file_dst,$(USER_MAP_WS)) \
--mount type=bind,source=$(call bench_host_map_path,$(call file_src,$(USER_MAP_WS))),destination=$(call file_dst,$(USER_MAP_WS))
# SSH Arguments
BENCH_RUN_ARGS += \
-e BENCH_SSH_MAP_KEY=$(BENCH_SSH_MAP_KEY)
ifneq ($(BENCH_SSH_MAP_KEY),0)
BENCH_RUN_ARGS += \
-v ~/.ssh/id_rsa:/home/$(BENCH_IMG_USER)/.ssh/id_rsa \
-v ~/.ssh/id_rsa.pub:/home/$(BENCH_IMG_USER)/.ssh/id_rsa.pub
endif
###############################################################################
# Run targets
# define bench_run_plat
# $(1) docker image tag
# return platform
define bench_run_plat
$(shell x=$$(echo $(filter %$(1),$(BOARD_LONG_LIST)) | awk -F "_" '{print $$1}'); [ -z $$x ] && echo $(BUILD_PLATFORM_DEFAULT) || echo $${x})
endef
# define bench_run_board
# $(1) running platform
# $(2) docker image tag
# return board
define bench_run_board
$(shell [ $(1) = $(BUILD_PLATFORM_DEFAULT) ] && echo $(BUILD_BOARD_DEFAULT) || echo $(2))
endef
# define bench_run_env
# $(1) docker image tag
# return env set scripts
define bench_run_env
export BENCH_RUN_PLAT=$(call bench_run_plat,$(1)) && \
export BENCH_RUN_BOARD=$$([ $${BENCH_RUN_PLAT} = $(BUILD_PLATFORM_DEFAULT) ] && echo $(BUILD_BOARD_DEFAULT) || echo $(1))
endef
# define bench_run_cmd
# $(1) docker image tag
# $(2) run cmd
define bench_run_cmd
$(call bench_run_env,$(1)) && \
docker run --rm \
-e BENCH_RUN_PLAT=$${BENCH_RUN_PLAT} \
-e BENCH_RUN_BOARD=$${BENCH_RUN_BOARD} \
-e BENCH_IMAGE_NAME=$(1) \
$(BENCH_CMD_ARGS) \
$(BENCH_RUN_ARGS) \
$(BENCH_NAME):$(1) \
$(if $(2), sh -c $(2)); \
exit 0
endef
# define bench_rund_cmd
# $(1) docker image tag
define bench_rund_cmd
$(call bench_run_env,$(1)) && \
docker run -d --restart=always \
--name $(call bench_daemon_name,$(1)) \
-e BENCH_RUN_PLAT=$${BENCH_RUN_PLAT} \
-e BENCH_RUN_BOARD=$${BENCH_RUN_BOARD} \
-e BENCH_IMAGE_NAME=$(1) \
$(BENCH_CMD_ARGS) \
$(BENCH_RUN_ARGS) \
$(BENCH_NAME):$(1)
endef
ifneq ($(BENCH_INITRC_DISABLE),1)
BENCH_RUN_SCRIPT := "USER_RUN_CMD=$(if $(USER_RUN_CMD),\"$(call string_escape_double_quotes,$(call string_escape_double_quotes,$(USER_RUN_CMD))))\" . $(BENCH_INITRC)"
else
BENCH_RUN_SCRIPT := "$(if $(USER_RUN_CMD),$(USER_RUN_CMD),/bin/bash)"
endif
#$(warning USER_RUN_CMD=$(USER_RUN_CMD))
#$(error BENCH_RUN_SCRIPT=$(BENCH_RUN_SCRIPT))
ifeq ($(BENCH_IMG_FROM),remote)
$(BENCH_CONTAINER_IMAGE) : bench_% : pull_%
else # ($(BENCH_IMG_FROM),remote)
ifeq ($(BENCH_IMG_FROM),local)
$(BENCH_CONTAINER_IMAGE) : bench_% : build_%
else # ($(BENCH_IMG_FROM),local)
$(error Invalid Image Type : $(BENCH_IMG_FROM))
endif # ($(BENCH_IMG_FROM),local)
endif # ($(BENCH_IMG_FROM),remote)
$(BENCH_CONTAINER_RUN) : run_% : bench_%
$(Q)$(call bench_run_cmd,$(subst bench_,,$<),$(BENCH_RUN_SCRIPT))
$(BENCH_CONTAINER_RUND) : rund_% : bench_%
$(Q)if [ -z $(BENCH_RUNNING_NESTED) ]; then $(if $(call bench_daemon_id,$(subst bench_,,$<)),$(call xprint,$(RED),Container \"$(BENCH_NAME)_$(subst bench_,,$<)\" Has Been Started),$(call bench_rund_cmd,$(subst bench_,,$<))); fi
###############################################################################
# bench_start
# $(1) docker image tag
define bench_start
$(if $(call bench_daemon_id,$(1)),docker start $(call bench_daemon_name,$(1)) > /dev/null && $(ECHO) "start $(call bench_daemon_name,$(1)) ok")
endef
$(BENCH_CONTAINER_START) : start_% : bench_%
$(Q)$(call bench_start,$(subst bench_,,$<))
###############################################################################
# bench_stop
# $(1) docker image tag
define bench_stop
$(if $(call bench_daemon_id,$(1)),ssh-keygen -f "$${HOME}/.ssh/known_hosts" -R "$(call bench_ip,$(1))" > /dev/null 2>&1;docker stop -t0 $(call bench_daemon_name,$(1)) > /dev/null && $(ECHO) "stop $(call bench_daemon_name,$(1)) ok";)
endef
$(BENCH_CONTAINER_STOP) :
$(Q)$(call bench_stop,$(patsubst stop_%,%,$@))
###############################################################################
# bench_rm
# $(1) docker image tag
define bench_rm
$(if $(call bench_daemon_id,$(1)),docker rm $(call bench_daemon_name,$(1)) > /dev/null && $(ECHO) "remove $(call bench_daemon_name,$(1)) ok")
endef
$(BENCH_CONTAINER_RM) : rm_% : stop_%
$(Q)$(call bench_rm,$(patsubst rm_%,%,$@))
###############################################################################
# bench_rmi
# $(1) docker image tag
define bench_rmi
$(if $(call bench_image_id,$(1)),docker rmi $(BENCH_NAME):$(1) > /dev/null && $(ECHO) "remove $(BENCH_NAME):$(1) ok")
endef
$(BENCH_CONTAINER_RMI) : rmi_% : rm_%
$(Q)$(call bench_rmi,$(patsubst rmi_%,%,$@))
###############################################################################
# bench_ip
# $(1) docker image tag
define bench_ip
$(shell docker exec $(BENCH_TERM_OPTS) $(call bench_daemon_name,$(1)) ip addr show eth0 | tr "/" " " | awk '$$1=="inet" {print $$2}')
endef
$(BENCH_CONTAINER_IP) : ip_% : bench_%
$(Q)echo -n $(call bench_ip,$(subst bench_,,$<))
###############################################################################
$(BENCH_CONTAINER_EXEC) : exec_% : bench_%
$(Q)-docker exec $(BENCH_TERM_OPTS) $(call bench_daemon_id,$(subst bench_,,$<)) sh -c $(BENCH_RUN_SCRIPT); exit 0
$(BENCH_CONTAINER_SSH) : ssh_% : bench_%
$(Q)-ssh -AY -t $(BENCH_RUN_USER)@$(call bench_ip,$(subst bench_,,$<)) "LC_ALL=en_US.UTF-8 exec dbus-run-session -- bash -l"; exit 0