-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
189 lines (158 loc) · 4.17 KB
/
deploy.sh
File metadata and controls
189 lines (158 loc) · 4.17 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
#!/bin/bash
# SilkFlow 部署脚本
# 用法: ./deploy.sh [选项]
# 选项:
# --build 重新构建镜像
# --stop 停止服务
# --restart 重启服务
# --logs 查看日志
set -e
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 项目配置
PROJECT_NAME="silkflow"
DOCKER_IMAGE="${PROJECT_NAME}:latest"
# 打印带颜色的消息
print_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查 Docker 是否安装
check_docker() {
if ! command -v docker &> /dev/null; then
print_error "Docker 未安装,请先安装 Docker"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
print_error "Docker Compose 未安装,请先安装 Docker Compose"
exit 1
fi
print_info "Docker 版本: $(docker --version)"
print_info "Docker Compose 版本: $(docker-compose --version)"
}
# 检查环境变量
check_env() {
if [ ! -f .env.production ]; then
print_warn ".env.production 文件不存在,正在创建示例文件..."
cat > .env.production << EOF
PORT=3005
API_ACCESS_TOKEN=your_token_here
MINIO_ACCESS_KEY=your_access_key
MINIO_SECRET_KEY=your_secret_key
MINIO_BUCKET_NAME=wechat
MINIO_URL=https://minio-api.wyts.tech
MINIO_ENDPOINT=minio-api.wyts.tech
MINIO_PORT=443
MINIO_USE_SSL=true
MINIO_UPLOAD_PREFIX=silk-mp3
EOF
print_warn "请编辑 .env.production 文件,设置正确的配置"
exit 1
fi
# 检查必需的环境变量
source .env.production
if [ "$API_ACCESS_TOKEN" = "your_token_here" ]; then
print_error "请在 .env.production 中设置 API_ACCESS_TOKEN"
exit 1
fi
print_info "环境变量检查通过"
}
# 构建 Docker 镜像
build_image() {
print_info "开始构建 Docker 镜像..."
docker build -t ${DOCKER_IMAGE} .
print_info "镜像构建成功: ${DOCKER_IMAGE}"
}
# 启动服务
start_service() {
print_info "启动 SilkFlow 服务..."
docker-compose --env-file .env.production up -d
print_info "服务启动成功!"
print_info "访问地址: http://localhost:3005"
print_info "健康检查: http://localhost:3005/api/health"
}
# 停止服务
stop_service() {
print_info "停止 SilkFlow 服务..."
docker-compose down
print_info "服务已停止"
}
# 重启服务
restart_service() {
print_info "重启 SilkFlow 服务..."
docker-compose restart
print_info "服务已重启"
}
# 查看日志
view_logs() {
print_info "查看服务日志 (Ctrl+C 退出)..."
docker-compose logs -f
}
# 查看状态
show_status() {
print_info "服务状态:"
docker-compose ps
}
# 清理
cleanup() {
print_info "清理未使用的镜像和容器..."
docker system prune -f
print_info "清理完成"
}
# 主函数
main() {
echo "=================================="
echo " SilkFlow 部署脚本"
echo "=================================="
echo ""
check_docker
case "${1:-}" in
--build)
check_env
build_image
start_service
;;
--stop)
stop_service
;;
--restart)
restart_service
;;
--logs)
view_logs
;;
--status)
show_status
;;
--cleanup)
cleanup
;;
*)
check_env
# 检查镜像是否存在
if ! docker images | grep -q ${PROJECT_NAME}; then
print_info "镜像不存在,开始构建..."
build_image
fi
start_service
show_status
echo ""
print_info "常用命令:"
echo " 查看日志: ./deploy.sh --logs"
echo " 重启服务: ./deploy.sh --restart"
echo " 停止服务: ./deploy.sh --stop"
echo " 重新构建: ./deploy.sh --build"
echo " 查看状态: ./deploy.sh --status"
;;
esac
}
# 运行主函数
main "$@"