-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy.sh
More file actions
423 lines (354 loc) · 10.6 KB
/
deploy.sh
File metadata and controls
423 lines (354 loc) · 10.6 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
#!/bin/bash
# ============================================================
# M-Team Helper 一键部署脚本
# 支持系统: Ubuntu/Debian, CentOS/RHEL, Fedora
# 功能: 自动安装依赖、部署应用、配置服务、设置开机自启
# ============================================================
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 配置变量
REPO_URL="https://github.com/spellyaohui/M-Team-Helper.git"
INSTALL_DIR="/opt/mteam-helper"
SERVICE_NAME="mteam-helper"
APP_PORT=8001
PYTHON_VERSION="3.10"
NODE_VERSION="20"
# 日志函数
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_step() {
echo -e "${BLUE}[STEP]${NC} $1"
}
# 检查是否为 root 用户
check_root() {
if [ "$EUID" -ne 0 ]; then
log_error "请使用 root 用户运行此脚本"
log_info "使用: sudo bash deploy.sh"
exit 1
fi
}
# 检测系统类型
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
VERSION=$VERSION_ID
elif [ -f /etc/redhat-release ]; then
OS="centos"
else
log_error "不支持的操作系统"
exit 1
fi
log_info "检测到系统: $OS $VERSION"
}
# 安装基础依赖
install_base_deps() {
log_step "安装基础依赖..."
case $OS in
ubuntu|debian)
apt-get update
apt-get install -y curl wget git build-essential software-properties-common
;;
centos|rhel|rocky|almalinux)
yum install -y curl wget git gcc gcc-c++ make
;;
fedora)
dnf install -y curl wget git gcc gcc-c++ make
;;
*)
log_error "不支持的系统: $OS"
exit 1
;;
esac
}
# 安装 Python
install_python() {
log_step "安装 Python ${PYTHON_VERSION}..."
# 检查是否已安装
if command -v python3 &> /dev/null; then
CURRENT_VERSION=$(python3 --version 2>&1 | awk '{print $2}' | cut -d. -f1,2)
log_info "当前 Python 版本: $CURRENT_VERSION"
if [[ "$CURRENT_VERSION" == "3.10" || "$CURRENT_VERSION" == "3.11" || "$CURRENT_VERSION" == "3.12" ]]; then
log_info "Python 版本满足要求,跳过安装"
return
fi
fi
case $OS in
ubuntu|debian)
add-apt-repository -y ppa:deadsnakes/ppa 2>/dev/null || true
apt-get update
apt-get install -y python3.10 python3.10-venv python3.10-dev python3-pip
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 2>/dev/null || true
;;
centos|rhel|rocky|almalinux)
yum install -y python3 python3-pip python3-devel
;;
fedora)
dnf install -y python3 python3-pip python3-devel
;;
esac
# 安装 pip
python3 -m pip install --upgrade pip 2>/dev/null || curl -sS https://bootstrap.pypa.io/get-pip.py | python3
}
# 安装 Node.js
install_nodejs() {
log_step "安装 Node.js ${NODE_VERSION}..."
# 检查是否已安装
if command -v node &> /dev/null; then
CURRENT_VERSION=$(node --version | cut -d. -f1 | tr -d 'v')
log_info "当前 Node.js 版本: v$CURRENT_VERSION"
if [ "$CURRENT_VERSION" -ge 18 ]; then
log_info "Node.js 版本满足要求,跳过安装"
return
fi
fi
# 使用 NodeSource 安装
curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x | bash - 2>/dev/null || \
curl -fsSL https://rpm.nodesource.com/setup_${NODE_VERSION}.x | bash - 2>/dev/null || true
case $OS in
ubuntu|debian)
apt-get install -y nodejs
;;
centos|rhel|rocky|almalinux|fedora)
yum install -y nodejs || dnf install -y nodejs
;;
esac
# 安装 npm(如果没有)
if ! command -v npm &> /dev/null; then
log_warn "npm 未安装,尝试单独安装..."
case $OS in
ubuntu|debian)
apt-get install -y npm
;;
*)
yum install -y npm || dnf install -y npm
;;
esac
fi
}
# 克隆或更新仓库
clone_repo() {
log_step "获取项目代码..."
if [ -d "$INSTALL_DIR" ]; then
log_info "项目目录已存在,更新代码..."
cd "$INSTALL_DIR"
git fetch origin
git reset --hard origin/main
else
log_info "克隆项目仓库..."
git clone "$REPO_URL" "$INSTALL_DIR"
fi
cd "$INSTALL_DIR"
log_info "当前版本: $(git log --oneline -1)"
}
# 配置前端(构建)
setup_frontend() {
log_step "构建前端..."
cd "$INSTALL_DIR/frontend"
# 安装依赖
log_info "安装前端依赖..."
npm install
# 构建生产版本
log_info "构建前端生产版本..."
npm run build
}
# 配置后端
setup_backend() {
log_step "配置后端服务..."
cd "$INSTALL_DIR/backend"
# 创建虚拟环境
log_info "创建 Python 虚拟环境..."
python3 -m venv venv
source venv/bin/activate
# 安装依赖
log_info "安装 Python 依赖..."
pip install --upgrade pip
pip install -r requirements.txt
# 创建数据目录
mkdir -p data/torrents
# 创建 .env 文件(如果不存在)
if [ ! -f .env ]; then
log_info "创建配置文件..."
cat > .env << EOF
# M-Team 网站地址
MTEAM_BASE_URL=https://api.m-team.cc
# 数据库配置
DATABASE_URL=sqlite:///./data/mteam.db
# 定时任务间隔(秒)
REFRESH_INTERVAL=300
# 调试模式
DEBUG=false
EOF
fi
deactivate
}
# 创建 systemd 服务
create_systemd_service() {
log_step "创建系统服务..."
cat > /etc/systemd/system/${SERVICE_NAME}.service << EOF
[Unit]
Description=M-Team Helper Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=${INSTALL_DIR}/backend
Environment="PATH=${INSTALL_DIR}/backend/venv/bin"
ExecStart=${INSTALL_DIR}/backend/venv/bin/python main.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
# 重新加载 systemd
systemctl daemon-reload
}
# 启动服务
start_services() {
log_step "启动服务..."
# 启用并启动服务
systemctl enable ${SERVICE_NAME}
systemctl start ${SERVICE_NAME}
# 等待服务启动
sleep 3
# 检查服务状态
log_info "检查服务状态..."
systemctl status ${SERVICE_NAME} --no-pager || true
}
# 配置防火墙
configure_firewall() {
log_step "配置防火墙..."
# 检测防火墙类型
if command -v ufw &> /dev/null; then
ufw allow ${APP_PORT}/tcp 2>/dev/null || true
log_info "已配置 UFW 防火墙,开放端口 ${APP_PORT}"
elif command -v firewall-cmd &> /dev/null; then
firewall-cmd --permanent --add-port=${APP_PORT}/tcp 2>/dev/null || true
firewall-cmd --reload 2>/dev/null || true
log_info "已配置 firewalld 防火墙,开放端口 ${APP_PORT}"
else
log_warn "未检测到防火墙,跳过配置"
fi
}
# 显示安装信息
show_info() {
# 获取服务器 IP
SERVER_IP=$(hostname -I | awk '{print $1}')
echo ""
echo "============================================================"
echo -e "${GREEN}M-Team Helper 部署完成!${NC}"
echo "============================================================"
echo ""
echo -e "访问地址: ${BLUE}http://${SERVER_IP}:${APP_PORT}${NC}"
echo -e "API 文档: ${BLUE}http://${SERVER_IP}:${APP_PORT}/docs${NC}"
echo ""
echo "常用命令:"
echo " 查看状态: systemctl status ${SERVICE_NAME}"
echo " 查看日志: journalctl -u ${SERVICE_NAME} -f"
echo " 重启服务: systemctl restart ${SERVICE_NAME}"
echo " 停止服务: systemctl stop ${SERVICE_NAME}"
echo " 启动服务: systemctl start ${SERVICE_NAME}"
echo ""
echo "安装目录: ${INSTALL_DIR}"
echo "配置文件: ${INSTALL_DIR}/backend/.env"
echo "数据目录: ${INSTALL_DIR}/backend/data/"
echo ""
echo "卸载命令: sudo bash ${INSTALL_DIR}/deploy.sh uninstall"
echo ""
echo "============================================================"
}
# 卸载函数
uninstall() {
log_step "卸载 M-Team Helper..."
# 停止并禁用服务
systemctl stop ${SERVICE_NAME} 2>/dev/null || true
systemctl disable ${SERVICE_NAME} 2>/dev/null || true
# 删除服务文件
rm -f /etc/systemd/system/${SERVICE_NAME}.service
systemctl daemon-reload
# 询问是否删除数据
read -p "是否删除数据目录?(y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
rm -rf "$INSTALL_DIR"
log_info "已删除安装目录和数据"
else
# 只删除代码,保留数据
rm -rf "$INSTALL_DIR/frontend"
rm -rf "$INSTALL_DIR/backend/venv"
rm -rf "$INSTALL_DIR/.git"
log_info "已删除程序文件,保留数据目录"
fi
log_info "卸载完成!"
}
# 更新函数
update() {
log_step "更新 M-Team Helper..."
# 停止服务
systemctl stop ${SERVICE_NAME} 2>/dev/null || true
# 更新代码
cd "$INSTALL_DIR"
git fetch origin
git reset --hard origin/main
log_info "代码已更新到: $(git log --oneline -1)"
# 重新构建前端
setup_frontend
# 更新后端依赖
cd "$INSTALL_DIR/backend"
source venv/bin/activate
pip install -r requirements.txt
deactivate
# 重启服务
systemctl start ${SERVICE_NAME}
log_info "更新完成!"
systemctl status ${SERVICE_NAME} --no-pager || true
}
# 主函数
main() {
echo ""
echo "============================================================"
echo " M-Team Helper 一键部署脚本"
echo "============================================================"
echo ""
# 检查参数
case "$1" in
uninstall)
check_root
uninstall
exit 0
;;
update)
check_root
update
exit 0
;;
esac
check_root
detect_os
log_info "开始部署..."
echo ""
install_base_deps
install_python
install_nodejs
clone_repo
setup_frontend
setup_backend
create_systemd_service
configure_firewall
start_services
show_info
}
# 运行主函数
main "$@"