Skip to content

Commit 34cb75e

Browse files
committed
add deployment.sh
1 parent 54e3a36 commit 34cb75e

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed

deployment.sh

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
#!/usr/bin/env bash
2+
3+
# Exit script if any command fails
4+
set -e
5+
6+
# ==============================================================================
7+
# gshark 自动化部署与启动脚本 (v12 - Rename Config)
8+
#
9+
# 更新:
10+
# - 自动将 config-temp.yaml 重命名为 config.yaml,确保后端服务能正确启动。
11+
#
12+
# 依赖: curl, jq, nginx, lsof
13+
# ==============================================================================
14+
15+
# --- Function to download gshark by showing an interactive menu ---
16+
download_gshark() {
17+
echo "[INFO] 从 GitHub 获取最新的 release 列表..."
18+
local REPO="madneal/gshark"
19+
local API_URL="https://api.github.com/repos/$REPO/releases/latest"
20+
local api_response
21+
api_response=$(curl -s "$API_URL")
22+
if [[ -z "$api_response" ]]; then
23+
echo "[ERROR] 从 GitHub 获取 release 数据失败。"
24+
exit 1
25+
fi
26+
local asset_names=()
27+
while IFS= read -r line; do
28+
asset_names+=("$line")
29+
done < <(echo "$api_response" | jq -r '.assets[].name')
30+
local asset_urls=()
31+
while IFS= read -r line; do
32+
asset_urls+=("$line")
33+
done < <(echo "$api_response" | jq -r '.assets[].browser_download_url')
34+
if [[ ${#asset_names[@]} -eq 0 ]]; then
35+
echo "[ERROR] 未找到任何 release 文件。"
36+
exit 1
37+
fi
38+
echo "[INFO] 请选择要下载的 release 包:"
39+
local i=0
40+
for name in "${asset_names[@]}"; do
41+
echo " $((i+1))) $name"
42+
i=$((i+1))
43+
done
44+
echo
45+
local choice
46+
while true; do
47+
read -p "请输入你的选择 (1-${#asset_names[@]}): " choice
48+
if [[ "$choice" =~ ^[0-9]+$ ]] && [[ "$choice" -ge 1 ]] && [[ "$choice" -le ${#asset_names[@]} ]]; then
49+
break
50+
else
51+
echo "[WARN] 输入无效, 请输入 1 到 ${#asset_names[@]} 之间的数字。"
52+
fi
53+
done
54+
local selected_index=$((choice - 1))
55+
download_url="${asset_urls[$selected_index]}"
56+
GSHARK_ZIP="${asset_names[$selected_index]}"
57+
echo "[INFO] 开始下载: '${GSHARK_ZIP}'..."
58+
curl -L -o "$GSHARK_ZIP" "$download_url"
59+
if [[ $? -ne 0 ]]; then
60+
echo "[ERROR] 下载失败。"
61+
exit 1
62+
fi
63+
echo "[INFO] 下载成功。"
64+
}
65+
66+
# --- Main deployment logic ---
67+
main() {
68+
echo "--- gshark 自动化部署与启动开始 ---"
69+
70+
# 1. Pre-flight Checks & Cleanup
71+
echo "[INFO] 执行启动前检查与清理..."
72+
echo "[INFO] 清理旧的 gshark 压缩包和解压目录..."
73+
rm -f gshark_*.tar.gz gshark_*.zip
74+
rm -rf gshark_*-*-*
75+
76+
if ! command -v lsof &> /dev/null; then
77+
echo "[WARN] 'lsof' 命令未找到,无法检查端口占用情况。将跳过此步骤。"
78+
else
79+
PID_ON_PORT=$(lsof -t -i:8888 || true)
80+
if [[ -n "$PID_ON_PORT" ]]; then
81+
echo "[WARN] 发现旧的 gshark 进程 (PID: $PID_ON_PORT) 正在使用端口 8888。"
82+
echo "[INFO] 正在停止该进程以避免冲突..."
83+
kill "$PID_ON_PORT"
84+
sleep 1
85+
echo "[INFO] 旧进程已停止。"
86+
else
87+
echo "[INFO] 端口 8888 未被占用,检查通过。"
88+
fi
89+
fi
90+
91+
# 2. 检查核心依赖
92+
if ! command -v jq &> /dev/null || ! command -v curl &> /dev/null || ! command -v nginx &> /dev/null; then
93+
echo "[ERROR] 'curl', 'jq', 'nginx' 是必需的。请先安装它们。"
94+
exit 1
95+
fi
96+
echo "[INFO] 所有核心依赖项检查通过。"
97+
98+
# 3. 自动下载
99+
download_gshark
100+
101+
# 4. 获取 Nginx 路径并确定系统类型
102+
echo "[INFO] 检测 Nginx 和系统环境..."
103+
NGINX_CONF=$(nginx -t 2>&1 | grep "test is successful" | awk '{print $4}')
104+
local NGINX_CONF_USER=""
105+
local CHOWN_OWNER=""
106+
107+
if [[ "$OSTYPE" == "darwin"* ]]; then
108+
OS="macos"
109+
HTML_ROOT="/usr/local/var/www"
110+
NGINX_CONF_USER="$(whoami) staff"
111+
CHOWN_OWNER="$(whoami):staff"
112+
echo "[INFO] 系统: macOS, Web 根目录: $HTML_ROOT, 配置文件: $NGINX_CONF"
113+
else
114+
OS="linux"
115+
HTML_ROOT="/var/www/html"
116+
NGINX_CONF_USER="www-data www-data"
117+
CHOWN_OWNER="www-data:www-data"
118+
echo "[INFO] 系统: Linux, Web 根目录: $HTML_ROOT, 配置文件: $NGINX_CONF"
119+
fi
120+
121+
# 5. 备份并创建 Nginx 配置
122+
echo "[INFO] 备份当前 Nginx 配置..."
123+
BACKUP_FILE="${NGINX_CONF}.backup.$(date +%Y%m%d_%H%M%S)"
124+
sudo cp "$NGINX_CONF" "$BACKUP_FILE"
125+
echo "[INFO] 备份完成: $BACKUP_FILE"
126+
echo "[INFO] 创建新的 Nginx 配置..."
127+
sudo tee "$NGINX_CONF" > /dev/null << EOF
128+
user $NGINX_CONF_USER;
129+
worker_processes 1;
130+
events { worker_connections 1024; }
131+
http {
132+
include mime.types;
133+
default_type application/octet-stream;
134+
sendfile on;
135+
keepalive_timeout 65;
136+
server {
137+
listen 8080;
138+
server_name localhost;
139+
location / { root $HTML_ROOT; index index.html index.htm; autoindex on; }
140+
location /api/ {
141+
rewrite ^/api/(.*)\$ /\$1 break;
142+
proxy_pass http://127.0.0.1:8888;
143+
proxy_set_header Host \$http_host;
144+
proxy_set_header X-Real-IP \$remote_addr;
145+
}
146+
}
147+
}
148+
EOF
149+
150+
# 6. 测试配置
151+
echo "[INFO] 测试新的 Nginx 配置..."
152+
if ! sudo nginx -t; then
153+
echo "[ERROR] Nginx 配置测试失败。请检查 $NGINX_CONF"
154+
echo "[INFO] 你的旧配置已恢复。"
155+
sudo mv "$BACKUP_FILE" "$NGINX_CONF"
156+
exit 1
157+
fi
158+
159+
# 7. 停止并启动 Nginx 服务
160+
echo "[INFO] 正在应用新的 Nginx 配置..."
161+
if [[ "$OS" == "macos" ]]; then
162+
echo "[INFO] 停止 Homebrew Nginx 服务 (如果正在运行)..."
163+
brew services stop nginx || true
164+
echo "[INFO] 启动 Homebrew Nginx 服务..."
165+
brew services start nginx
166+
else
167+
echo "[INFO] 停止 systemd Nginx 服务 (如果正在运行)..."
168+
sudo systemctl stop nginx || true
169+
echo "[INFO] 启动 systemd Nginx 服务..."
170+
sudo systemctl start nginx
171+
fi
172+
173+
# 8. 部署前端文件
174+
echo "[INFO] 部署前端文件到 $HTML_ROOT ..."
175+
unzip -o -q "$GSHARK_ZIP"
176+
GSHARK_DIR=$(find . -maxdepth 1 -type d -name "gshark*")
177+
178+
sudo mkdir -p "$HTML_ROOT"
179+
sudo cp -r "$GSHARK_DIR/dist/"* "$HTML_ROOT/"
180+
sudo chown -R "$CHOWN_OWNER" "$HTML_ROOT"
181+
echo "[INFO] 前端文件部署完成。"
182+
183+
# 9. 配置并启动后端服务
184+
echo "[INFO] 准备启动后端服务..."
185+
cd "$GSHARK_DIR"
186+
187+
# <-- NEW: Rename config file for the service to use -->
188+
if [[ -f "config-temp.yaml" ]]; then
189+
echo "[INFO] 正在将 'config-temp.yaml' 重命名为 'config.yaml'..."
190+
mv config-temp.yaml config.yaml
191+
else
192+
echo "[WARN] 未找到 'config-temp.yaml'。将继续启动,服务可能使用默认配置。"
193+
fi
194+
195+
echo "[INFO] 赋予 gshark 执行权限..."
196+
chmod +x gshark
197+
198+
echo "[INFO] 正在后台启动 gshark 服务 (日志写入 gshark.log)..."
199+
./gshark serve > gshark.log 2>&1 &
200+
GSHARK_PID=$!
201+
202+
cd ..
203+
204+
sleep 2
205+
206+
if ! kill -0 $GSHARK_PID > /dev/null 2>&1; then
207+
echo "[ERROR] gshark 后端服务启动失败!请检查 '$(pwd)/$GSHARK_DIR/gshark.log' 获取详细信息。"
208+
exit 1
209+
fi
210+
echo "[INFO] gshark 后端服务已成功启动 (PID: $GSHARK_PID)。"
211+
212+
echo
213+
echo "--- ✅ 全自动部署与启动成功! ---"
214+
echo "前端和后端服务均已在后台运行。"
215+
echo
216+
echo " - 访问应用: http://localhost:8080"
217+
echo " - 后端 PID: $GSHARK_PID"
218+
echo " - 日志文件: $(pwd)/$GSHARK_DIR/gshark.log"
219+
echo " - 停止后端: kill $GSHARK_PID"
220+
echo
221+
}
222+
223+
# 执行主函数
224+
main

0 commit comments

Comments
 (0)