Skip to content

Commit 0d1ae20

Browse files
committed
fix(docker): 增强配置生成脚本的调试和错误处理能力
- 在 generate-config.sh 中添加详细的调试日志输出 - 添加目标目录存在性检查和自动创建 - 显示所有 VITE_* 环境变量用于调试 - 验证配置文件生成结果并输出预览 - 修复字符串拼接问题,避免 \n 显示异常 - 在 start-services.sh 中添加脚本可执行性检查 - 添加 config.js 生成验证和手动重试机制 - 改进错误提示和日志输出格式 关联: #186
1 parent 89adcce commit 0d1ae20

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

docker/generate-config.sh

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,27 @@
33
# 配置文件路径
44
CONFIG_FILE="/usr/share/nginx/html/config.js"
55

6+
echo "========================================="
67
echo "开始生成运行时配置文件..."
8+
echo "目标文件: $CONFIG_FILE"
9+
echo "========================================="
10+
11+
# 检查目标目录是否存在
12+
TARGET_DIR=$(dirname "$CONFIG_FILE")
13+
if [ ! -d "$TARGET_DIR" ]; then
14+
echo "ERROR: 目标目录不存在: $TARGET_DIR"
15+
mkdir -p "$TARGET_DIR" || echo "ERROR: 无法创建目录"
16+
fi
717

818
# 构建包含全部 VITE_* 变量的运行时配置(同时注入带前缀与不带前缀的键)
919
CONFIG_BODY=""
1020
COUNT=0
1121

22+
# 显示所有VITE_*环境变量用于调试
23+
echo "扫描VITE_*环境变量..."
24+
env | grep '^VITE_' || echo "未找到任何VITE_*变量"
25+
echo "========================================="
26+
1227
# 遍历所有以 VITE_ 开头的环境变量
1328
for var in $(env | grep '^VITE_[A-Za-z0-9_]*=' | cut -d= -f1 | sort); do
1429
value=$(printenv "$var" 2>/dev/null)
@@ -20,9 +35,11 @@ for var in $(env | grep '^VITE_[A-Za-z0-9_]*=' | cut -d= -f1 | sort); do
2035

2136
# 追加属性:无前缀副本 与 带前缀副本
2237
if [ -n "$CONFIG_BODY" ]; then
23-
CONFIG_BODY="$CONFIG_BODY,\n"
38+
CONFIG_BODY="${CONFIG_BODY},
39+
"
2440
fi
25-
CONFIG_BODY="$CONFIG_BODY $no_prefix_key: \"$escaped_value\",\n $var: \"$escaped_value\""
41+
CONFIG_BODY="${CONFIG_BODY} ${no_prefix_key}: \"${escaped_value}\",
42+
${var}: \"${escaped_value}\""
2643

2744
COUNT=$((COUNT + 1))
2845
echo "Found VITE var: $var"
@@ -39,5 +56,20 @@ ${CONFIG_BODY}
3956
console.log("运行时配置已加载,共注入 ${COUNT} 个 VITE_* 变量(双份键)");
4057
EOF
4158

59+
echo "========================================="
4260
echo "配置文件已生成: $CONFIG_FILE"
4361
echo "已注入 VITE_* 变量数量: $COUNT"
62+
echo "========================================="
63+
64+
# 验证文件是否成功生成
65+
if [ -f "$CONFIG_FILE" ]; then
66+
echo "✅ 配置文件生成成功"
67+
echo "文件大小: $(wc -c < "$CONFIG_FILE") bytes"
68+
echo "前10行内容:"
69+
head -n 10 "$CONFIG_FILE"
70+
else
71+
echo "❌ ERROR: 配置文件生成失败!"
72+
exit 1
73+
fi
74+
75+
echo "========================================="

docker/start-services.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,26 @@ echo "Nginx configuration updated with NGINX_PORT=${NGINX_PORT}"
1212
# 运行原有的nginx初始化脚本
1313
echo "Running nginx initialization scripts..."
1414
for script in /docker-entrypoint.d/*.sh; do
15-
if [ -f "$script" ]; then
15+
if [ -f "$script" ] && [ -x "$script" ]; then
1616
echo "Running $script"
17-
sh "$script"
17+
sh "$script" || echo "WARNING: $script failed with exit code $?"
18+
elif [ -f "$script" ]; then
19+
echo "WARNING: $script is not executable, attempting to run anyway..."
20+
sh "$script" || echo "WARNING: $script failed with exit code $?"
1821
fi
1922
done
2023

24+
# 验证config.js是否已生成
25+
if [ -f "/usr/share/nginx/html/config.js" ]; then
26+
echo "✅ config.js generated successfully"
27+
echo "Content preview:"
28+
head -n 5 /usr/share/nginx/html/config.js
29+
else
30+
echo "❌ ERROR: config.js was not generated!"
31+
echo "Attempting manual generation..."
32+
sh /docker-entrypoint.d/40-generate-config.sh || echo "Manual generation failed"
33+
fi
34+
2135
echo "Starting services with supervisor..."
2236
echo "MCP Server will run on port: ${MCP_HTTP_PORT}"
2337
echo "MCP Server log level: ${MCP_LOG_LEVEL}"

0 commit comments

Comments
 (0)