-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathotb-dev.sh
More file actions
executable file
·558 lines (495 loc) · 17.8 KB
/
otb-dev.sh
File metadata and controls
executable file
·558 lines (495 loc) · 17.8 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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#!/bin/bash
# OTB Development Docker Container Management Script
# 基于 docker-compose 管理开发容器
set -e
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
print_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
print_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# 配置变量
IMAGE_NAME_CONFIGURED="otb-dev-centos:7"
IMAGE_NAME_BASE="otb-dev-centos:7-base"
CONTAINER_NAME="otb-dev-centos7"
# 显示状态
show_status() {
print_info "OTB Development Containers Status:"
echo ""
docker compose ps
}
# 显示帮助
show_usage() {
cat << EOF
Usage: $0 [COMMAND] [OPTIONS]
Commands:
up [MODE] 启动容器
MODE: configured - 使用预配置镜像 (默认)
base - 使用基础镜像,需自己 configure
down 停止并删除容器
start 启动已存在的容器
stop 停止容器
restart 重启容器
build [MODE] 构建镜像
MODE: configured - 构建预配置镜像 (默认)
base - 构建基础镜像
rebuild [MODE] 重新构建镜像 (--no-cache)
MODE: configured - 构建预配置镜像 (默认)
base - 构建基础镜像
logs 查看日志
status 显示容器状态
enter 进入容器(自动切换到源码目录)
export [MODE] 导出镜像 (gzip)
MODE: auto - 自动检测 (默认)
configured - 导出预配置镜像
base - 导出基础镜像
export-zstd [MODE] 导出镜像 (zstd, 更快)
MODE: auto - 自动检测 (默认)
configured - 导出预配置镜像
base - 导出基础镜像
import 导入镜像 (gzip,自动检测文件)
import-zstd 导入镜像 (zstd,自动检测文件)
help 显示帮助
Examples:
$0 up # 启动容器(预配置模式)
$0 up base # 启动容器(基础模式)
$0 build configured # 构建预配置镜像
$0 build base # 构建基础镜像
$0 rebuild # 重新构建镜像(预配置模式)
$0 enter # 进入容器
$0 export # 导出镜像(自动检测)
$0 export configured # 导出预配置镜像
$0 down # 停止并删除容器
镜像说明:
- configured 模式: otb-dev-centos:7 (已执行 configure)
- base 模式: otb-dev-centos:7-base (未执行 configure)
EOF
}
# 生成 Banner 脚本
generate_banner_script() {
local mode=$1
cat > /tmp/otb-banner.sh << 'BANNER_EOF'
#!/bin/bash
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m'
echo ""
echo -e "${BOLD}OpenTenBase Development Environment${NC}"
echo "==================================="
echo ""
BANNER_EOF
if [ "$mode" = "configured" ]; then
cat >> /tmp/otb-banner.sh << 'BANNER_EOF'
echo -e "${GREEN}✓${NC} 模式: ${BOLD}预配置镜像${NC} (已完成 configure)"
echo -e "${GREEN}✓${NC} 源码路径: ${YELLOW}\$SOURCECODE_PATH${NC}"
echo -e "${GREEN}✓${NC} 安装路径: ${YELLOW}\$INSTALL_PATH${NC}"
echo ""
echo -e "${BOLD}下一步操作:${NC}"
echo -e " ${BLUE}1.${NC} 编译: ${YELLOW}make -j\$(nproc)${NC}"
echo -e " ${BLUE}2.${NC} 安装: ${YELLOW}make install${NC}"
echo -e " ${BLUE}3.${NC} 初始化集群: ${YELLOW}opentenbase_ctl init_cluster${NC}"
BANNER_EOF
else
cat >> /tmp/otb-banner.sh << 'BANNER_EOF'
echo -e "${YELLOW}⚠${NC} 模式: ${BOLD}基础镜像${NC} (需要手动 configure)"
echo -e "${GREEN}✓${NC} 源码路径: ${YELLOW}\$SOURCECODE_PATH${NC}"
echo -e "${GREEN}✓${NC} 安装路径: ${YELLOW}\$INSTALL_PATH${NC}"
echo ""
echo -e "${BOLD}下一步操作:${NC}"
echo -e " ${BLUE}1.${NC} Configure:"
echo -e " ${YELLOW}./configure --prefix=\$INSTALL_PATH/opentenbase_bin_v5.0 \\"
echo -e " --enable-user-switch --with-libxml \\"
echo -e " --disable-license --with-openssl \\"
echo -e " --with-ossp-uuid CFLAGS=\"-g\""
echo -e " ${BLUE}2.${NC} 编译: ${YELLOW}make -j\$(nproc)${NC}"
echo -e " ${BLUE}3.${NC} 安装: ${YELLOW}make install${NC}"
BANNER_EOF
fi
cat >> /tmp/otb-banner.sh << 'BANNER_EOF'
echo ""
echo -e "${BOLD}有用的命令:${NC}"
echo -e " • 查看 GCC 版本: ${YELLOW}gcc --version${NC}"
echo -e " • 查看环境变量: ${YELLOW}env | grep -E 'SOURCECODE|INSTALL|PG'${NC}"
echo ""
BANNER_EOF
chmod +x /tmp/otb-banner.sh
}
# 进入容器 (以 opentenbase 用户身份,切换到源码目录)
enter_container() {
# 检查容器是否运行
if ! docker compose ps | grep -q "Up"; then
print_error "容器未运行,请先执行: $0 up"
exit 1
fi
# 获取当前运行的镜像判断模式
local current_image=$(docker inspect ${CONTAINER_NAME} 2>/dev/null | grep -oP '"Image":\s*"sha256:\K[^"]+' | head -1)
local mode="configured"
# 判断是否是基础镜像(没有configure过的)
if docker exec ${CONTAINER_NAME} test ! -f /data/opentenbase/OpenTenBase/config.status 2>/dev/null; then
mode="base"
fi
# 生成 banner 脚本
generate_banner_script "$mode"
# 复制 banner 脚本到容器
docker cp /tmp/otb-banner.sh ${CONTAINER_NAME}:/tmp/otb-banner.sh
print_info "进入容器 (opentenbase 用户, 源码目录)..."
# 进入容器并显示 banner
docker compose exec -u opentenbase -w /data/opentenbase/OpenTenBase centos bash -c '
/tmp/otb-banner.sh
exec bash
'
# 清理临时文件
rm -f /tmp/otb-banner.sh
}
# 启动容器
start_container() {
local mode="${1:-configured}"
shift || true
if [ "$mode" = "base" ]; then
print_info "启动容器(基础模式 - 需要手动 configure)..."
print_warn "此模式下,您需要自己执行 configure"
RUN_CONFIGURE=false IMAGE_TAG=${IMAGE_NAME_BASE} docker compose up -d --build "$@"
elif [ "$mode" = "configured" ]; then
print_info "启动容器(预配置模式 - 已完成 configure)..."
RUN_CONFIGURE=true IMAGE_TAG=${IMAGE_NAME_CONFIGURED} docker compose up -d --build "$@"
else
print_error "未知模式: $mode. 请使用 'configured' 或 'base'"
exit 1
fi
show_status
echo ""
print_info "容器已启动,使用 '$0 enter' 进入容器"
}
# 构建镜像
build_image() {
local mode="${1:-configured}"
shift || true
if [ "$mode" = "base" ]; then
print_info "构建镜像(基础模式 - 不执行 configure)..."
RUN_CONFIGURE=false IMAGE_TAG=${IMAGE_NAME_BASE} docker compose build "$@"
print_info "镜像已构建: ${IMAGE_NAME_BASE}"
elif [ "$mode" = "configured" ]; then
print_info "构建镜像(预配置模式 - 执行 configure)..."
RUN_CONFIGURE=true IMAGE_TAG=${IMAGE_NAME_CONFIGURED} docker compose build "$@"
print_info "镜像已构建: ${IMAGE_NAME_CONFIGURED}"
else
print_error "未知模式: $mode. 请使用 'configured' 或 'base'"
exit 1
fi
}
# 重新构建镜像
rebuild_image() {
local mode="${1:-configured}"
shift || true
if [ "$mode" = "base" ]; then
print_info "重新构建镜像(基础模式 - 不执行 configure)..."
RUN_CONFIGURE=false IMAGE_TAG=${IMAGE_NAME_BASE} docker compose build --no-cache "$@"
print_info "镜像已构建: ${IMAGE_NAME_BASE}"
elif [ "$mode" = "configured" ]; then
print_info "重新构建镜像(预配置模式 - 执行 configure)..."
RUN_CONFIGURE=true IMAGE_TAG=${IMAGE_NAME_CONFIGURED} docker compose build --no-cache "$@"
print_info "镜像已构建: ${IMAGE_NAME_CONFIGURED}"
else
print_error "未知模式: $mode. 请使用 'configured' 或 'base'"
exit 1
fi
}
# 镜像名称
EXPORT_FILE_CONFIGURED="otb-dev-centos-configured.tar"
EXPORT_FILE_BASE="otb-dev-centos-base.tar"
# 导出镜像 (gzip)
export_image() {
local mode="${1:-auto}"
# 自动检测存在哪个镜像
local has_configured=false
local has_base=false
if docker image inspect ${IMAGE_NAME_CONFIGURED} &> /dev/null; then
has_configured=true
fi
if docker image inspect ${IMAGE_NAME_BASE} &> /dev/null; then
has_base=true
fi
# 如果两个都不存在
if [ "$has_configured" = false ] && [ "$has_base" = false ]; then
print_error "没有找到任何镜像"
print_info "请先构建镜像: $0 build [configured|base]"
exit 1
fi
# 如果mode是auto,根据存在的镜像自动选择
if [ "$mode" = "auto" ]; then
if [ "$has_configured" = true ] && [ "$has_base" = false ]; then
mode="configured"
elif [ "$has_configured" = false ] && [ "$has_base" = true ]; then
mode="base"
else
# 两个都存在,提示用户选择
print_warn "检测到两个镜像都存在:"
echo " 1. ${IMAGE_NAME_CONFIGURED} (预配置版)"
echo " 2. ${IMAGE_NAME_BASE} (基础版)"
print_info "请指定要导出的版本: $0 export [configured|base]"
exit 1
fi
fi
# 根据模式导出
if [ "$mode" = "configured" ]; then
if [ "$has_configured" = false ]; then
print_error "镜像 ${IMAGE_NAME_CONFIGURED} 不存在"
print_info "请先构建: $0 build configured"
exit 1
fi
print_info "导出镜像 ${IMAGE_NAME_CONFIGURED} (gzip)..."
docker save ${IMAGE_NAME_CONFIGURED} | gzip > ${EXPORT_FILE_CONFIGURED}.gz
print_info "导出完成: ${EXPORT_FILE_CONFIGURED}.gz"
ls -lh ${EXPORT_FILE_CONFIGURED}.gz
elif [ "$mode" = "base" ]; then
if [ "$has_base" = false ]; then
print_error "镜像 ${IMAGE_NAME_BASE} 不存在"
print_info "请先构建: $0 build base"
exit 1
fi
print_info "导出镜像 ${IMAGE_NAME_BASE} (gzip)..."
docker save ${IMAGE_NAME_BASE} | gzip > ${EXPORT_FILE_BASE}.gz
print_info "导出完成: ${EXPORT_FILE_BASE}.gz"
ls -lh ${EXPORT_FILE_BASE}.gz
else
print_error "未知模式: $mode"
exit 1
fi
}
# 导出镜像 (zstd)
export_image_zstd() {
if ! command -v zstd &> /dev/null; then
print_error "zstd 未安装,请先安装: yum install -y zstd 或 apt install -y zstd"
exit 1
fi
local mode="${1:-auto}"
# 自动检测存在哪个镜像
local has_configured=false
local has_base=false
if docker image inspect ${IMAGE_NAME_CONFIGURED} &> /dev/null; then
has_configured=true
fi
if docker image inspect ${IMAGE_NAME_BASE} &> /dev/null; then
has_base=true
fi
# 如果两个都不存在
if [ "$has_configured" = false ] && [ "$has_base" = false ]; then
print_error "没有找到任何镜像"
print_info "请先构建镜像: $0 build [configured|base]"
exit 1
fi
# 如果mode是auto,根据存在的镜像自动选择
if [ "$mode" = "auto" ]; then
if [ "$has_configured" = true ] && [ "$has_base" = false ]; then
mode="configured"
elif [ "$has_configured" = false ] && [ "$has_base" = true ]; then
mode="base"
else
# 两个都存在,提示用户选择
print_warn "检测到两个镜像都存在:"
echo " 1. ${IMAGE_NAME_CONFIGURED} (预配置版)"
echo " 2. ${IMAGE_NAME_BASE} (基础版)"
print_info "请指定要导出的版本: $0 export-zstd [configured|base]"
exit 1
fi
fi
# 根据模式导出
if [ "$mode" = "configured" ]; then
if [ "$has_configured" = false ]; then
print_error "镜像 ${IMAGE_NAME_CONFIGURED} 不存在"
print_info "请先构建: $0 build configured"
exit 1
fi
print_info "导出镜像 ${IMAGE_NAME_CONFIGURED} (zstd)..."
docker save ${IMAGE_NAME_CONFIGURED} | zstd -T0 > ${EXPORT_FILE_CONFIGURED}.zst
print_info "导出完成: ${EXPORT_FILE_CONFIGURED}.zst"
ls -lh ${EXPORT_FILE_CONFIGURED}.zst
elif [ "$mode" = "base" ]; then
if [ "$has_base" = false ]; then
print_error "镜像 ${IMAGE_NAME_BASE} 不存在"
print_info "请先构建: $0 build base"
exit 1
fi
print_info "导出镜像 ${IMAGE_NAME_BASE} (zstd)..."
docker save ${IMAGE_NAME_BASE} | zstd -T0 > ${EXPORT_FILE_BASE}.zst
print_info "导出完成: ${EXPORT_FILE_BASE}.zst"
ls -lh ${EXPORT_FILE_BASE}.zst
else
print_error "未知模式: $mode"
exit 1
fi
}
# 导入镜像 (gzip)
import_image() {
# 检查哪个文件存在
local has_configured=false
local has_base=false
if [ -f "${EXPORT_FILE_CONFIGURED}.gz" ]; then
has_configured=true
fi
if [ -f "${EXPORT_FILE_BASE}.gz" ]; then
has_base=true
fi
if [ "$has_configured" = false ] && [ "$has_base" = false ]; then
print_error "没有找到导出文件"
print_info "期望文件: ${EXPORT_FILE_CONFIGURED}.gz 或 ${EXPORT_FILE_BASE}.gz"
exit 1
fi
# 导入找到的文件
if [ "$has_configured" = true ]; then
print_info "导入镜像 (gzip): ${EXPORT_FILE_CONFIGURED}.gz"
gunzip -c ${EXPORT_FILE_CONFIGURED}.gz | docker load
print_info "导入完成: ${IMAGE_NAME_CONFIGURED}"
fi
if [ "$has_base" = true ]; then
print_info "导入镜像 (gzip): ${EXPORT_FILE_BASE}.gz"
gunzip -c ${EXPORT_FILE_BASE}.gz | docker load
print_info "导入完成: ${IMAGE_NAME_BASE}"
fi
}
# 导入镜像 (zstd)
import_image_zstd() {
if ! command -v zstd &> /dev/null; then
print_error "zstd 未安装,请先安装: yum install -y zstd 或 apt install -y zstd"
exit 1
fi
# 检查哪个文件存在
local has_configured=false
local has_base=false
if [ -f "${EXPORT_FILE_CONFIGURED}.zst" ]; then
has_configured=true
fi
if [ -f "${EXPORT_FILE_BASE}.zst" ]; then
has_base=true
fi
if [ "$has_configured" = false ] && [ "$has_base" = false ]; then
print_error "没有找到导出文件"
print_info "期望文件: ${EXPORT_FILE_CONFIGURED}.zst 或 ${EXPORT_FILE_BASE}.zst"
exit 1
fi
# 导入找到的文件
if [ "$has_configured" = true ]; then
print_info "导入镜像 (zstd): ${EXPORT_FILE_CONFIGURED}.zst"
zstd -d -c ${EXPORT_FILE_CONFIGURED}.zst | docker load
print_info "导入完成: ${IMAGE_NAME_CONFIGURED}"
fi
if [ "$has_base" = true ]; then
print_info "导入镜像 (zstd): ${EXPORT_FILE_BASE}.zst"
zstd -d -c ${EXPORT_FILE_BASE}.zst | docker load
print_info "导入完成: ${IMAGE_NAME_BASE}"
fi
}
# 主逻辑
main() {
if [ $# -eq 0 ]; then
show_usage
exit 0
fi
case "$1" in
up)
shift
# 检查是否指定了模式参数
local mode="configured"
if [ "$1" = "base" ] || [ "$1" = "configured" ]; then
mode="$1"
shift
fi
start_container "$mode" "$@"
;;
down)
shift
print_info "停止并删除容器..."
docker compose down "$@"
;;
start)
shift
print_info "启动容器..."
docker compose start "$@"
;;
stop)
shift
print_info "停止容器..."
docker compose stop "$@"
;;
restart)
shift
print_info "重启容器..."
docker compose restart "$@"
;;
build)
shift
# 检查是否指定了模式参数
local mode="configured"
if [ "$1" = "base" ] || [ "$1" = "configured" ]; then
mode="$1"
shift
fi
build_image "$mode" "$@"
;;
rebuild)
shift
# 检查是否指定了模式参数
local mode="configured"
if [ "$1" = "base" ] || [ "$1" = "configured" ]; then
mode="$1"
shift
fi
rebuild_image "$mode" "$@"
;;
logs)
shift
docker compose logs -f "$@"
;;
status|ps)
show_status
;;
enter|exec)
enter_container
;;
export)
shift
# 检查是否指定了模式参数
local mode="auto"
if [ "$1" = "base" ] || [ "$1" = "configured" ]; then
mode="$1"
shift
fi
export_image "$mode"
;;
export-zstd)
shift
# 检查是否指定了模式参数
local mode="auto"
if [ "$1" = "base" ] || [ "$1" = "configured" ]; then
mode="$1"
shift
fi
export_image_zstd "$mode"
;;
import)
import_image
;;
import-zstd)
import_image_zstd
;;
help|--help|-h)
show_usage
;;
*)
# 直接传递给 docker compose
docker compose "$@"
;;
esac
}
main "$@"