-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeploy.sh
More file actions
340 lines (281 loc) · 11 KB
/
Copy pathdeploy.sh
File metadata and controls
340 lines (281 loc) · 11 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
#!/bin/bash
set -e
export PATH="$HOME/.local/bin:$PATH"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
error_exit() {
echo -e "${RED}错误: $1${NC}" >&2
exit 1
}
log() {
echo -e "${GREEN}▸${NC} $1"
}
[ ! -f config.yaml ] && error_exit "找不到配置文件"
if ! command -v yq &> /dev/null; then
if command -v wget &> /dev/null; then
wget -qO /tmp/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 2>&1 | grep -v "^$" || true
else
curl -sL https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -o /tmp/yq 2>&1 | grep -v "^$" || true
fi
chmod +x /tmp/yq
sudo mv /tmp/yq /usr/local/bin/yq || mv /tmp/yq ~/.local/bin/yq
fi
SERVER_IP=$(curl -s ip.sb)
[ -z "$SERVER_IP" ] && SERVER_IP=$(hostname -I | awk '{print $1}')
[ -z "$SERVER_IP" ] && SERVER_IP="localhost"
DB_HOST="localhost"
DB_PORT="5432"
DB_NAME="antihub_db"
DB_USER="antihub_db"
DB_PASSWORD=$(openssl rand -hex 16)
PLUGIN_DB_HOST="localhost"
PLUGIN_DB_PORT="5432"
PLUGIN_DB_NAME="antigv_plugin_db"
PLUGIN_DB_USER="antigv_plugin_db"
PLUGIN_DB_PASSWORD=$(openssl rand -hex 16)
BACKEND_PORT=$(yq eval '.backend.port' config.yaml)
JWT_SECRET=$(yq eval '.backend.jwt_secret' config.yaml)
OAUTH_CLIENT_ID=$(yq eval '.backend.oauth_client_id' config.yaml)
OAUTH_CLIENT_SECRET=$(yq eval '.backend.oauth_client_secret' config.yaml)
OAUTH_REDIRECT_URI=$(yq eval '.backend.oauth_redirect_uri' config.yaml | sed "s/localhost/$SERVER_IP/g")
GITHUB_CLIENT_ID=$(yq eval '.backend.github_client_id' config.yaml)
GITHUB_CLIENT_SECRET=$(yq eval '.backend.github_client_secret' config.yaml)
GITHUB_REDIRECT_URI=$(yq eval '.backend.github_redirect_uri' config.yaml | sed "s/localhost/$SERVER_IP/g")
PLUGIN_PORT=$(yq eval '.plugin.port' config.yaml)
PLUGIN_HOST=$(yq eval '.plugin.host' config.yaml)
PLUGIN_ADMIN_KEY=$(yq eval '.plugin.admin_api_key' config.yaml)
PLUGIN_ENCRYPTION_KEY=$(yq eval '.plugin.encryption_key' config.yaml)
FRONTEND_PORT=$(yq eval '.frontend.port' config.yaml)
FRONTEND_API_URL=$(yq eval '.frontend.api_url' config.yaml | sed "s/localhost/$SERVER_IP/g")
FRONTEND_URL=$(yq eval '.frontend.frontend_url' config.yaml | sed "s/localhost/$SERVER_IP/g")
REDIS_URL=$(yq eval '.redis.url' config.yaml)
[[ "$OSTYPE" != "linux-gnu"* ]] && error_exit "不支持的系统类型: $OSTYPE"
if command -v apt-get &> /dev/null; then
PKG_MGR="apt"
elif command -v yum &> /dev/null; then
PKG_MGR="yum"
elif command -v dnf &> /dev/null; then
PKG_MGR="dnf"
elif command -v pacman &> /dev/null; then
PKG_MGR="pacman"
else
error_exit "不支持的包管理器"
fi
PACKAGES=()
! command -v curl &> /dev/null && ! command -v wget &> /dev/null && PACKAGES+=("curl")
! command -v git &> /dev/null && PACKAGES+=("git")
! command -v psql &> /dev/null && PACKAGES+=("postgresql")
! command -v redis-server &> /dev/null && PACKAGES+=("redis")
if [ ${#PACKAGES[@]} -gt 0 ]; then
case $PKG_MGR in
apt)
sudo apt-get update -qq
sudo apt-get install -y ${PACKAGES[@]} postgresql-contrib
;;
yum)
sudo yum install -y ${PACKAGES[@]} postgresql-server postgresql-contrib
[ ! -f /var/lib/pgsql/data/PG_VERSION ] && sudo postgresql-setup --initdb
;;
dnf)
sudo dnf install -y ${PACKAGES[@]} postgresql-server postgresql-contrib
[ ! -f /var/lib/pgsql/data/PG_VERSION ] && sudo postgresql-setup --initdb
;;
pacman)
sudo pacman -S --noconfirm ${PACKAGES[@]}
[ ! -d /var/lib/postgres/data/base ] && sudo -u postgres initdb -D /var/lib/postgres/data
;;
esac
fi
if command -v psql &> /dev/null; then
sudo systemctl is-active --quiet postgresql || sudo systemctl start postgresql
sudo systemctl is-enabled --quiet postgresql || sudo systemctl enable postgresql 2>/dev/null || true
fi
if command -v redis-server &> /dev/null; then
sudo systemctl is-active --quiet redis-server || sudo systemctl start redis-server
sudo systemctl is-enabled --quiet redis-server || sudo systemctl enable redis-server 2>/dev/null || true
fi
if ! command -v node &> /dev/null; then
case $PKG_MGR in
apt)
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
;;
yum|dnf)
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -
sudo $PKG_MGR install -y nodejs
;;
pacman)
sudo pacman -S --noconfirm nodejs npm
;;
esac
fi
NODE_VERSION=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
[ "$NODE_VERSION" -lt 18 ] && error_exit "需要 Node.js >= 18.0.0"
if ! command -v uv &> /dev/null; then
(command -v curl &> /dev/null && curl -LsSf https://astral.sh/uv/install.sh || wget -qO- https://astral.sh/uv/install.sh) | sh
export PATH="$HOME/.local/bin:$PATH"
fi
npm config set registry https://registry.npmjs.org/
NPM_PACKAGES=()
! command -v pnpm &> /dev/null && NPM_PACKAGES+=("pnpm")
! command -v pm2 &> /dev/null && NPM_PACKAGES+=("pm2")
[ ${#NPM_PACKAGES[@]} -gt 0 ] && log "安装Package: ${NPM_PACKAGES[*]}" && npm install -g ${NPM_PACKAGES[@]}
[ ! -d "AntiHub" ] && git clone -q https://github.com/AntiHub-Project/AntiHub.git
[ ! -d "Backend" ] && git clone -q https://github.com/AntiHub-Project/Backend.git
[ ! -d "Antigv-plugin" ] && git clone -q https://github.com/AntiHub-Project/Antigv-plugin.git
cd /tmp
sudo -u postgres psql -tc "SELECT 1 FROM pg_database WHERE datname = '$DB_NAME'" | grep -q 1 || \
sudo -u postgres psql -c "CREATE DATABASE $DB_NAME;"
sudo -u postgres psql -tc "SELECT 1 FROM pg_roles WHERE rolname = '$DB_USER'" | grep -q 1 || \
sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD' SUPERUSER;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;"
sudo -u postgres psql -tc "SELECT 1 FROM pg_database WHERE datname = '$PLUGIN_DB_NAME'" | grep -q 1 || \
sudo -u postgres psql -c "CREATE DATABASE $PLUGIN_DB_NAME;"
sudo -u postgres psql -tc "SELECT 1 FROM pg_roles WHERE rolname = '$PLUGIN_DB_USER'" | grep -q 1 || \
sudo -u postgres psql -c "CREATE USER $PLUGIN_DB_USER WITH PASSWORD '$PLUGIN_DB_PASSWORD' SUPERUSER;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $PLUGIN_DB_NAME TO $PLUGIN_DB_USER;"
PG_HBA=$(sudo -u postgres psql -t -c "SHOW hba_file;" | xargs)
cd - > /dev/null
if [ -f "$PG_HBA" ]; then
grep -q "host.*$DB_NAME.*$DB_USER.*md5" "$PG_HBA" || \
echo "host $DB_NAME $DB_USER 127.0.0.1/32 md5" | sudo tee -a "$PG_HBA"
grep -q "host.*$PLUGIN_DB_NAME.*$PLUGIN_DB_USER.*md5" "$PG_HBA" || \
echo "host $PLUGIN_DB_NAME $PLUGIN_DB_USER 127.0.0.1/32 md5" | sudo tee -a "$PG_HBA"
sudo systemctl reload postgresql
fi
cd "$SCRIPT_DIR/Antigv-plugin"
[ "$PLUGIN_ADMIN_KEY" == "auto_generate" ] || [ -z "$PLUGIN_ADMIN_KEY" ] && PLUGIN_ADMIN_KEY="sk-admin-$(openssl rand -hex 32)"
cat > config.json <<EOF
{
"server": {
"port": $PLUGIN_PORT,
"host": "$PLUGIN_HOST"
},
"oauth": {
"callbackUrl": "http://localhost:42532/oauth-callback"
},
"database": {
"host": "$PLUGIN_DB_HOST",
"port": $PLUGIN_DB_PORT,
"database": "$PLUGIN_DB_NAME",
"user": "$PLUGIN_DB_USER",
"password": "$PLUGIN_DB_PASSWORD",
"max": 20,
"idleTimeoutMillis": 30000,
"connectionTimeoutMillis": 2000
},
"api": {
"url": "https://daily-cloudcode-pa.sandbox.googleapis.com/v1internal:streamGenerateContent?alt=sse",
"modelsUrl": "https://daily-cloudcode-pa.sandbox.googleapis.com/v1internal:fetchAvailableModels",
"host": "daily-cloudcode-pa.sandbox.googleapis.com",
"userAgent": "antigravity/1.11.3 windows/amd64"
},
"defaults": {
"temperature": 1,
"top_p": 0.85,
"top_k": 50,
"max_tokens": 8096
},
"security": {
"maxRequestSize": "50mb",
"adminApiKey": "$PLUGIN_ADMIN_KEY"
},
"systemInstruction": ""
}
EOF
npm install
[ -f schema.sql ] && PGPASSWORD=$PLUGIN_DB_PASSWORD psql -h $PLUGIN_DB_HOST -p $PLUGIN_DB_PORT -U $PLUGIN_DB_USER -d $PLUGIN_DB_NAME -f schema.sql || true
pm2 delete antigv-plugin || true
pm2 start src/server/index.js --name antigv-plugin
pm2 save
cd "$SCRIPT_DIR/Backend"
[ "$JWT_SECRET" == "auto_generate" ] || [ -z "$JWT_SECRET" ] && JWT_SECRET=$(openssl rand -hex 32)
if [ "$PLUGIN_ENCRYPTION_KEY" == "auto_generate" ] || [ -z "$PLUGIN_ENCRYPTION_KEY" ]; then
uv sync
PLUGIN_ENCRYPTION_KEY=$(uv run python generate_encryption_key.py | grep -v "^$" | grep -v "请将以下" | tail -n 1)
fi
cat > .env <<EOF
# Application Configuration
APP_ENV=production
LOG_LEVEL=INFO
# Database Configuration
DATABASE_URL=postgresql+asyncpg://$DB_USER:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME
# Redis Configuration
REDIS_URL=$REDIS_URL
# JWT Configuration
JWT_SECRET_KEY=$JWT_SECRET
JWT_ALGORITHM=HS256
JWT_EXPIRE_HOURS=24
# Linux.do OAuth Configuration
OAUTH_CLIENT_ID=$OAUTH_CLIENT_ID
OAUTH_CLIENT_SECRET=$OAUTH_CLIENT_SECRET
OAUTH_REDIRECT_URI=$OAUTH_REDIRECT_URI
OAUTH_AUTHORIZATION_ENDPOINT=https://connect.linux.do/oauth2/authorize
OAUTH_TOKEN_ENDPOINT=https://connect.linux.do/oauth2/token
OAUTH_USER_INFO_ENDPOINT=https://connect.linux.do/api/user
# GitHub OAuth Configuration
GITHUB_CLIENT_ID=$GITHUB_CLIENT_ID
GITHUB_CLIENT_SECRET=$GITHUB_CLIENT_SECRET
GITHUB_REDIRECT_URI=$GITHUB_REDIRECT_URI
# Plug-in API Configuration
PLUGIN_API_BASE_URL=http://$PLUGIN_HOST:$PLUGIN_PORT
PLUGIN_API_ADMIN_KEY=$PLUGIN_ADMIN_KEY
PLUGIN_API_ENCRYPTION_KEY=$PLUGIN_ENCRYPTION_KEY
EOF
uv sync
uv run alembic upgrade head || error_exit "数据库迁移失败"
pm2 delete antihub-backend || true
pm2 start "uv run uvicorn app.main:app --host 0.0.0.0 --port $BACKEND_PORT" --name antihub-backend
pm2 save
cd "$SCRIPT_DIR/AntiHub"
cat > .env <<EOF
NEXT_PUBLIC_API_URL=$FRONTEND_API_URL
NEXT_PUBLIC_FRONTEND_URL=$FRONTEND_URL
EOF
if [ "${SKIP_FRONTEND_BUILD}" != "true" ]; then
pnpm install
pnpm run build || error_exit "前端构建失败"
else
log "跳过前端构建,使用预编译文件"
[ ! -d ".next" ] && error_exit "未找到预编译的 .next 目录,请先在本地构建或设置 SKIP_FRONTEND_BUILD=false"
fi
cat > ecosystem.config.js <<EOF
module.exports = {
apps: [{
name: 'antihub-frontend',
script: 'pnpm',
args: 'start',
cwd: '$(pwd)',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: $FRONTEND_PORT
},
error_file: 'logs/pm2-error.log',
out_file: 'logs/pm2-out.log',
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
merge_logs: true
}]
};
EOF
mkdir -p logs
pm2 delete antihub-frontend || true
pm2 start ecosystem.config.js
pm2 save
cd "$SCRIPT_DIR"
command -v systemctl &>/dev/null && pm2 startup systemd -u $(whoami) --hp $(eval echo ~$(whoami)) || true
echo ""
log "部署完成"
echo ""
echo "后端: http://$SERVER_IP:$BACKEND_PORT"
echo "前端: http://$SERVER_IP:$FRONTEND_PORT"
echo ""
pm2 status
echo ""