-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinfra.sh
More file actions
519 lines (444 loc) · 12.4 KB
/
Copy pathinfra.sh
File metadata and controls
519 lines (444 loc) · 12.4 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#!/usr/bin/env bash
set -u
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RUNTIME_DIR="${ROOT_DIR}/.runtime"
PID_DIR="${RUNTIME_DIR}/pids"
LOG_DIR="${RUNTIME_DIR}/logs"
mkdir -p "${PID_DIR}" "${LOG_DIR}"
MINIO_DIR="/path/to/minio/data"
MINIO_API_PORT="9000"
MINIO_CONSOLE_PORT="9001"
MINIO_CMD="./minio server data/ --console-address :${MINIO_CONSOLE_PORT}"
KAFKA_DIR="/path/to/kafka"
KAFKA_PORT="9092"
KAFKA_CMD="./start-kafka.sh"
ELASTICSEARCH_DIR="/path/to/elasticsearch"
ELASTICSEARCH_PORT="9200"
ELASTICSEARCH_SCHEME="${ELASTICSEARCH_SCHEME:-https}"
ELASTICSEARCH_CMD='ES_JAVA_OPTS="-Xms500M -Xmx500M" ./bin/elasticsearch'
SERVICES=("minio" "kafka" "elasticsearch")
service_dir() {
case "$1" in
minio) echo "${MINIO_DIR}" ;;
kafka) echo "${KAFKA_DIR}" ;;
elasticsearch) echo "${ELASTICSEARCH_DIR}" ;;
*) return 1 ;;
esac
}
service_cmd() {
case "$1" in
minio) echo "${MINIO_CMD}" ;;
kafka) echo "${KAFKA_CMD}" ;;
elasticsearch) echo "${ELASTICSEARCH_CMD}" ;;
*) return 1 ;;
esac
}
pid_file() {
echo "${PID_DIR}/$1.pid"
}
log_file() {
echo "${LOG_DIR}/$1.log"
}
service_port() {
case "$1" in
minio) echo "${MINIO_API_PORT}" ;;
kafka) echo "${KAFKA_PORT}" ;;
elasticsearch) echo "${ELASTICSEARCH_PORT}" ;;
*) return 1 ;;
esac
}
service_aux_port() {
case "$1" in
minio) echo "${MINIO_CONSOLE_PORT}" ;;
*) return 1 ;;
esac
}
service_pattern() {
case "$1" in
minio) echo 'minio server data/' ;;
kafka) echo 'kafka\.Kafka|start-kafka\.sh' ;;
elasticsearch) echo 'org\.elasticsearch\.bootstrap\.Elasticsearch|org\.elasticsearch\.server|jdk\.module\.main=org\.elasticsearch\.server|elasticsearch-8\.10\.0' ;;
*) return 1 ;;
esac
}
discover_pid() {
local service="$1"
local pattern
pattern="$(service_pattern "${service}")" || return 1
local pid
pid="$(pgrep -f "${pattern}" | head -n 1)"
if [ -n "${pid}" ]; then
echo "${pid}"
return 0
fi
return 1
}
discover_pids() {
local service="$1"
local pattern
pattern="$(service_pattern "${service}")" || return 1
pgrep -f "${pattern}" || true
}
port_is_listening() {
local port="$1"
if lsof -nP -iTCP:"${port}" -sTCP:LISTEN > /dev/null 2>&1; then
return 0
fi
return 1
}
http_ok() {
local url="$1"
if curl --silent --fail --max-time 2 "${url}" > /dev/null 2>&1; then
return 0
fi
return 1
}
java_major_version() {
local java_bin="$1"
local version_line version
version_line="$("${java_bin}" -version 2>&1 | head -n 1)"
version="$(printf '%s\n' "${version_line}" | sed -E 's/.*version "([^"]+)".*/\1/')"
if [[ "${version}" == 1.* ]]; then
echo "${version#1.}" | cut -d. -f1
return 0
fi
echo "${version}" | cut -d. -f1
}
detect_java_home() {
local project_java_version project_java_home
if [ -f "${ROOT_DIR}/.java-version" ] && command -v jenv > /dev/null 2>&1; then
project_java_version="$(tr -d '[:space:]' < "${ROOT_DIR}/.java-version")"
if [ -n "${project_java_version}" ]; then
project_java_home="$(jenv prefix "${project_java_version}" 2>/dev/null || true)"
if [ -n "${project_java_home}" ] && [ -x "${project_java_home}/bin/java" ]; then
echo "${project_java_home}"
return 0
fi
fi
fi
if [ -n "${JAVA_HOME:-}" ] && [ -x "${JAVA_HOME}/bin/java" ]; then
local current_major
current_major="$(java_major_version "${JAVA_HOME}/bin/java")"
if [ "${current_major}" -ge 17 ]; then
echo "${JAVA_HOME}"
return 0
fi
fi
if command -v /usr/libexec/java_home > /dev/null 2>&1; then
local detected_home
detected_home="$(/usr/libexec/java_home -v 17+ 2>/dev/null || true)"
if [ -n "${detected_home}" ] && [ -x "${detected_home}/bin/java" ]; then
echo "${detected_home}"
return 0
fi
fi
return 1
}
elastic_http_status() {
local scheme="$1"
curl \
--silent \
--output /dev/null \
--write-out '%{http_code}' \
--max-time 2 \
--insecure \
"${scheme}://127.0.0.1:${ELASTICSEARCH_PORT}" 2>/dev/null || true
}
elastic_health_check() {
local status
if [ "${ELASTICSEARCH_SCHEME}" = "https" ]; then
status="$(elastic_http_status "https")"
case "${status}" in
200|401|403) return 0 ;;
esac
status="$(elastic_http_status "http")"
case "${status}" in
200|401|403) return 0 ;;
esac
port_is_listening "${ELASTICSEARCH_PORT}" && return 0
return 1
fi
status="$(elastic_http_status "http")"
case "${status}" in
200|401|403) return 0 ;;
esac
status="$(elastic_http_status "https")"
case "${status}" in
200|401|403) return 0 ;;
esac
port_is_listening "${ELASTICSEARCH_PORT}" && return 0
return 1
}
http_health_check() {
local service="$1"
case "${service}" in
minio)
http_ok "http://127.0.0.1:${MINIO_API_PORT}/minio/health/live" || return 1
http_ok "http://127.0.0.1:${MINIO_CONSOLE_PORT}" || return 1
;;
elasticsearch)
elastic_health_check || return 1
;;
kafka)
port_is_listening "${KAFKA_PORT}" || return 1
;;
*)
return 1
;;
esac
return 0
}
wait_for_service() {
local service="$1"
local i
for i in $(seq 1 30); do
if http_health_check "${service}"; then
return 0
fi
sleep 1
done
return 1
}
is_running() {
local service="$1"
if http_health_check "${service}"; then
local discovered_pid
discovered_pid="$(discover_pid "${service}" || true)"
if [ -n "${discovered_pid}" ]; then
echo "${discovered_pid}" > "$(pid_file "${service}")"
fi
return 0
fi
local pid_path
pid_path="$(pid_file "${service}")"
if [ ! -f "${pid_path}" ]; then
return 1
fi
local pid
pid="$(cat "${pid_path}")"
if [ -z "${pid}" ]; then
return 1
fi
if ps -p "${pid}" > /dev/null 2>&1; then
return 0
fi
local discovered_pid
discovered_pid="$(discover_pid "${service}" || true)"
if [ -n "${discovered_pid}" ]; then
echo "${discovered_pid}" > "${pid_path}"
return 0
fi
rm -f "${pid_path}"
return 1
}
validate_service() {
local service="$1"
local dir
dir="$(service_dir "${service}")" || {
echo "不支持的服务: ${service}"
return 1
}
if [ ! -d "${dir}" ]; then
echo "${service} 目录不存在: ${dir}"
return 1
fi
if [ "${service}" = "elasticsearch" ] && ! detect_java_home > /dev/null; then
echo "启动 Elasticsearch 需要 Java 17+,但当前未找到可用的 JDK"
return 1
fi
return 0
}
start_service() {
local service="$1"
validate_service "${service}" || return 1
if is_running "${service}"; then
echo "${service} 已在运行,PID: $(cat "$(pid_file "${service}")")"
return 0
fi
local dir cmd log_path pid_path
dir="$(service_dir "${service}")"
cmd="$(service_cmd "${service}")"
log_path="$(log_file "${service}")"
pid_path="$(pid_file "${service}")"
echo "启动 ${service}..."
(
cd "${dir}" || exit 1
if [ "${service}" = "elasticsearch" ]; then
export ES_JAVA_HOME
ES_JAVA_HOME="$(detect_java_home)" || exit 1
fi
if command -v setsid > /dev/null 2>&1; then
nohup setsid bash -lc "${cmd}" >> "${log_path}" 2>&1 < /dev/null &
else
nohup bash -lc "${cmd}" >> "${log_path}" 2>&1 < /dev/null &
fi
echo $! > "${pid_path}"
)
if wait_for_service "${service}"; then
local discovered_pid
discovered_pid="$(discover_pid "${service}" || true)"
if [ -n "${discovered_pid}" ]; then
echo "${discovered_pid}" > "${pid_path}"
fi
fi
if is_running "${service}"; then
echo "${service} 启动成功,PID: $(cat "${pid_path}")"
echo "日志: ${log_path}"
return 0
fi
echo "${service} 启动失败,请检查日志: ${log_path}"
return 1
}
stop_service() {
local service="$1"
local pid_path
pid_path="$(pid_file "${service}")"
if ! is_running "${service}"; then
echo "${service} 未运行"
return 0
fi
local pid
pid="$(cat "${pid_path}")"
if [ -z "${pid}" ]; then
pid="$(discover_pid "${service}" || true)"
fi
if [ -z "${pid}" ]; then
echo "${service} 未运行"
rm -f "${pid_path}"
return 0
fi
local pids
pids="$(discover_pids "${service}")"
if [ -z "${pids}" ]; then
pids="${pid}"
fi
echo "停止 ${service} (PID: ${pids//$'\n'/ })..."
while IFS= read -r current_pid; do
[ -n "${current_pid}" ] || continue
kill "${current_pid}" 2>/dev/null || true
done <<< "${pids}"
for _ in 1 2 3 4 5; do
if ! is_running "${service}"; then
rm -f "${pid_path}"
echo "${service} 已停止"
return 0
fi
sleep 1
done
echo "${service} 未在预期时间内退出,执行强制停止"
while IFS= read -r current_pid; do
[ -n "${current_pid}" ] || continue
kill -9 "${current_pid}" 2>/dev/null || true
done <<< "${pids}"
rm -f "${pid_path}"
echo "${service} 已强制停止"
}
status_service() {
local service="$1"
if is_running "${service}"; then
local pid port aux_port
pid="$(cat "$(pid_file "${service}")" 2>/dev/null || true)"
port="$(service_port "${service}" || true)"
aux_port="$(service_aux_port "${service}" || true)"
if [ -n "${aux_port}" ]; then
echo "${service}: 运行中 (PID: ${pid:-unknown}, Port: ${port:-unknown}, Extra: ${aux_port})"
else
echo "${service}: 运行中 (PID: ${pid:-unknown}, Port: ${port:-unknown})"
fi
else
echo "${service}: 未运行"
fi
}
logs_service() {
local service="$1"
local log_path
log_path="$(log_file "${service}")"
if [ ! -f "${log_path}" ]; then
echo "${service} 暂无日志: ${log_path}"
return 1
fi
tail -f "${log_path}"
}
show_urls() {
cat <<EOF
MinIO API: http://127.0.0.1:${MINIO_API_PORT}
MinIO Console: http://127.0.0.1:${MINIO_CONSOLE_PORT}
Kafka Broker: 127.0.0.1:${KAFKA_PORT}
Elasticsearch: ${ELASTICSEARCH_SCHEME}://127.0.0.1:${ELASTICSEARCH_PORT}
EOF
}
run_for_services() {
local action="$1"
shift
local selected_services=()
if [ "$#" -eq 0 ]; then
selected_services=("${SERVICES[@]}")
else
selected_services=("$@")
fi
local service
for service in "${selected_services[@]}"; do
case "${action}" in
start) start_service "${service}" ;;
stop) stop_service "${service}" ;;
status) status_service "${service}" ;;
restart)
stop_service "${service}"
start_service "${service}"
;;
*) return 1 ;;
esac
echo ""
done
}
show_help() {
cat <<'EOF'
用法:
./infra.sh start [service...]
./infra.sh stop [service...]
./infra.sh restart [service...]
./infra.sh status [service...]
./infra.sh logs <service>
./infra.sh urls
可选服务:
minio
kafka
elasticsearch
示例:
./infra.sh start
./infra.sh start minio kafka
./infra.sh stop elasticsearch
./infra.sh status
./infra.sh logs kafka
./infra.sh urls
EOF
}
main() {
local command="${1:-}"
case "${command}" in
start|stop|restart|status)
shift
run_for_services "${command}" "$@"
;;
logs)
if [ $# -lt 2 ]; then
echo "请指定要查看日志的服务"
exit 1
fi
logs_service "$2"
;;
urls)
show_urls
;;
-h|--help|help|"")
show_help
;;
*)
echo "不支持的命令: ${command}"
echo ""
show_help
exit 1
;;
esac
}
main "$@"