-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
145 lines (125 loc) · 4.27 KB
/
docker-entrypoint.sh
File metadata and controls
145 lines (125 loc) · 4.27 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
#!/bin/bash
set -e
# 函数:打印日志
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
# 函数:安装运行时依赖
install_runtime_dependencies() {
log "检查并安装运行时依赖..."
# 检查是否需要安装新的依赖
local requirements_file="requirements.txt"
local installed_packages_file="/tmp/installed_packages.txt"
# 如果requirements.txt存在且比已安装包列表新,则重新安装
if [ -f "$requirements_file" ]; then
if [ ! -f "$installed_packages_file" ] || [ "$requirements_file" -nt "$installed_packages_file" ]; then
log "发现新的依赖需求,开始安装..."
# 尝试使用sudo安装,如果失败则使用用户级安装
if command -v sudo >/dev/null 2>&1 && sudo -n true 2>/dev/null; then
log "尝试使用sudo安装依赖..."
sudo pip install --no-cache-dir -r "$requirements_file" 2>&1 | while read line; do
log "pip: $line"
done
INSTALL_RESULT=${PIPESTATUS[0]}
else
INSTALL_RESULT=1 # 设置为失败,触发用户级安装
fi
# 如果sudo安装失败,尝试用户级安装
if [ $INSTALL_RESULT -ne 0 ]; then
log "尝试用户级安装依赖..."
pip install --user --no-cache-dir -r "$requirements_file" 2>&1 | while read line; do
log "pip: $line"
done
# 确保用户级安装的包在PATH中
export PATH="$HOME/.local/bin:$PATH"
fi
# 单独安装腾讯云SDK(确保安装)
log "确保腾讯云SDK已安装..."
if ! pip list | grep -q "tencentcloud-sdk-python"; then
log "安装腾讯云SDK..."
pip install --user tencentcloud-sdk-python>=3.0.1200
else
log "腾讯云SDK已安装"
fi
# 记录安装时间
touch "$installed_packages_file"
log "依赖安装完成"
else
log "依赖已是最新版本,跳过安装"
fi
else
log "未找到 requirements.txt 文件"
fi
}
# 函数:检查必要的文件和目录
check_requirements() {
log "检查应用环境..."
# 检查配置文件
if [ ! -f "config.toml" ]; then
if [ -f "config.example.toml" ]; then
log "复制示例配置文件..."
cp config.example.toml config.toml
else
log "警告: 未找到配置文件"
fi
fi
# 检查必要的目录
for dir in "storage/temp" "storage/tasks" "storage/json" "storage/narration_scripts" "storage/drama_analysis"; do
if [ ! -d "$dir" ]; then
log "创建目录: $dir"
mkdir -p "$dir"
fi
done
# 安装运行时依赖
install_runtime_dependencies
log "环境检查完成"
}
# 函数:启动 WebUI
start_webui() {
log "启动 NarratoAI WebUI..."
# 检查端口是否可用
if command -v netstat >/dev/null 2>&1; then
if netstat -tuln | grep -q ":8501 "; then
log "警告: 端口 8501 已被占用"
fi
fi
# 启动 Streamlit 应用
exec streamlit run webui.py \
--server.address=0.0.0.0 \
--server.port=8501 \
--server.enableCORS=true \
--server.maxUploadSize=2048 \
--server.enableXsrfProtection=false \
--browser.gatherUsageStats=false \
--browser.serverAddress=0.0.0.0 \
--logger.level=info
}
# 主逻辑
log "NarratoAI Docker 容器启动中..."
# 检查环境
check_requirements
# 根据参数执行不同的命令
case "$1" in
"webui"|"")
start_webui
;;
"bash"|"sh")
log "启动交互式 shell..."
exec /bin/bash
;;
"health")
# 健康检查命令
log "执行健康检查..."
if curl -f http://localhost:8501/_stcore/health >/dev/null 2>&1; then
log "健康检查通过"
exit 0
else
log "健康检查失败"
exit 1
fi
;;
*)
log "执行自定义命令: $*"
exec "$@"
;;
esac