forked from dataelement/Clawith
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestart.sh
More file actions
executable file
·271 lines (235 loc) · 12.1 KB
/
restart.sh
File metadata and controls
executable file
·271 lines (235 loc) · 12.1 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
#!/bin/bash
# Clawith — Restart Script
# Usage: ./restart.sh
set -e
# ═══════════════════════════════════════════════════════
# 配置
# ═══════════════════════════════════════════════════════
ROOT="$(cd "$(dirname "$0")" && pwd)"
DATA_DIR="$ROOT/.data"
PID_DIR="$DATA_DIR/pid"
LOG_DIR="$DATA_DIR/log"
BACKEND_DIR="$ROOT/backend"
FRONTEND_DIR="$ROOT/frontend"
BACKEND_PORT=8008
FRONTEND_PORT=3008
FRONTEND_LOG="$LOG_DIR/frontend.log"
BACKEND_LOG="$LOG_DIR/backend.log"
BACKEND_PID="$PID_DIR/backend.pid"
FRONTEND_PID="$PID_DIR/frontend.pid"
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[0;33m'; CYAN='\033[0;36m'; NC='\033[0m'
# ═══════════════════════════════════════════════════════
# 初始化目录
# ═══════════════════════════════════════════════════════
init_dirs() {
mkdir -p "$PID_DIR" "$LOG_DIR"
}
# ═══════════════════════════════════════════════════════
# 加载环境变量
# ═══════════════════════════════════════════════════════
load_env() {
if [ -f "$ROOT/.env" ]; then
set -a
source "$ROOT/.env"
set +a
fi
: "${DATABASE_URL:=postgresql+asyncpg://clawith:clawith@localhost:5432/clawith?ssl=disable}"
export DATABASE_URL
PG_PORT=$(echo "$DATABASE_URL" | grep -oP 'localhost:\K[0-9]+' 2>/dev/null || echo "$DATABASE_URL" | sed -n 's/.*localhost:\([0-9]*\).*/\1/p')
PG_PORT=${PG_PORT:-5432}
export PG_PORT
}
# ═══════════════════════════════════════════════════════
# 清理旧进程
# ═══════════════════════════════════════════════════════
cleanup() {
echo -e "${YELLOW}🔄 Stopping existing services...${NC}"
for pidfile in "$BACKEND_PID" "$FRONTEND_PID"; do
if [ -f "$pidfile" ]; then
kill -9 "$(cat "$pidfile")" 2>/dev/null || true
rm -f "$pidfile"
fi
done
for port in $BACKEND_PORT $FRONTEND_PORT; do
if command -v lsof &>/dev/null; then
lsof -ti:$port | xargs kill -9 2>/dev/null || true
elif command -v fuser &>/dev/null; then
fuser -k $port/tcp 2>/dev/null || true
fi
done
sleep 1
}
# ═══════════════════════════════════════════════════════
# 等待端口就绪
# ═══════════════════════════════════════════════════════
wait_for_port() {
local port=$1 name=$2 max=${3:-10}
for i in $(seq 1 "$max"); do
if curl -s -o /dev/null -m 1 "http://localhost:$port" 2>/dev/null; then
echo -e " ${GREEN}✅ $name ready (${i}s)${NC}"
return 0
fi
sleep 1
done
echo -e " ${RED}❌ $name failed to start in ${max}s${NC}"
return 1
}
# ═══════════════════════════════════════════════════════
# 添加 PostgreSQL 到 PATH
# ═══════════════════════════════════════════════════════
add_pg_path() {
if [ -d "$ROOT/.pg/bin" ]; then
export PATH="$ROOT/.pg/bin:$PATH"
fi
for dir in /www/server/pgsql/bin /usr/local/pgsql/bin; do
if [ -x "$dir/pg_isready" ] && ! command -v pg_isready &>/dev/null; then
export PATH="$dir:$PATH"
fi
done
}
# ═══════════════════════════════════════════════════════
# 启动 PostgreSQL
# ═══════════════════════════════════════════════════════
start_postgres() {
add_pg_path
if command -v pg_isready &>/dev/null; then
if ! pg_isready -h localhost -p "$PG_PORT" -q 2>/dev/null; then
echo -e "${YELLOW}🐘 Starting PostgreSQL (port $PG_PORT)...${NC}"
STARTED=false
[ -f "$ROOT/.pgdata/PG_VERSION" ] && command -v pg_ctl &>/dev/null && \
pg_ctl -D "$ROOT/.pgdata" -l "$ROOT/.pgdata/pg.log" start >/dev/null 2>&1 && STARTED=true
if [ "$STARTED" = false ] && command -v brew &>/dev/null; then
brew services start postgresql@15 2>/dev/null || brew services start postgresql 2>/dev/null || true
STARTED=true
fi
if [ "$STARTED" = false ] && command -v systemctl &>/dev/null; then
sudo systemctl start postgresql 2>/dev/null || true
STARTED=true
fi
for i in $(seq 1 10); do
if pg_isready -h localhost -p "$PG_PORT" -q 2>/dev/null; then
echo -e " ${GREEN}✅ PostgreSQL ready (${i}s)${NC}"
return 0
fi
sleep 1
done
echo -e " ${RED}❌ PostgreSQL failed to start on port $PG_PORT${NC}"
exit 1
else
echo -e "${GREEN}🐘 PostgreSQL already running (port $PG_PORT)${NC}"
fi
else
echo -e "${YELLOW}🐘 pg_isready not found — assuming PostgreSQL is running${NC}"
fi
}
# ═══════════════════════════════════════════════════════
# 启动后端
# ═══════════════════════════════════════════════════════
start_backend() {
echo -e "${YELLOW}🚀 Starting backend...${NC}"
cd "$BACKEND_DIR"
# Auto-run data migrations (idempotent)
echo -e "${YELLOW}🔄 Running data migrations...${NC}"
.venv/bin/python -m app.scripts.migrate_schedules_to_triggers || true
nohup env PYTHONUNBUFFERED=1 \
PUBLIC_BASE_URL="${PUBLIC_BASE_URL:-}" \
DATABASE_URL="$DATABASE_URL" \
.venv/bin/uvicorn app.main:app --host 0.0.0.0 --port $BACKEND_PORT \
> "$BACKEND_LOG" 2>&1 &
echo $! > "$BACKEND_PID"
wait_for_port $BACKEND_PORT "Backend" 10
}
# ═══════════════════════════════════════════════════════
# 启动前端
# ═══════════════════════════════════════════════════════
start_frontend() {
echo -e "${YELLOW}🚀 Starting frontend...${NC}"
cd "$FRONTEND_DIR"
nohup node_modules/.bin/vite --host 0.0.0.0 --port $FRONTEND_PORT \
> "$FRONTEND_LOG" 2>&1 &
echo $! > "$FRONTEND_PID"
wait_for_port $FRONTEND_PORT "Frontend" 8
}
# ═══════════════════════════════════════════════════════
# 验证代理
# ═══════════════════════════════════════════════════════
verify_proxy() {
echo -e "${YELLOW}🔍 Verifying API proxy...${NC}"
HEALTH=$(curl -s -m 3 http://localhost:$FRONTEND_PORT/api/health 2>/dev/null || echo "FAIL")
if echo "$HEALTH" | grep -q "ok"; then
echo -e " ${GREEN}✅ Proxy working${NC}"
else
echo -e " ${YELLOW}⚠️ Proxy may need a moment, backend direct check:${NC}"
curl -s http://localhost:$BACKEND_PORT/api/health && echo ""
fi
}
# ═══════════════════════════════════════════════════════
# 打印访问信息
# ═══════════════════════════════════════════════════════
print_info() {
SERVER_IP=$(hostname -I 2>/dev/null | awk '{print $1}')
[ -z "$SERVER_IP" ] && SERVER_IP=$(ifconfig 2>/dev/null | grep 'inet ' | grep -v '127.0.0.1' | head -1 | awk '{print $2}')
[ -z "$SERVER_IP" ] && SERVER_IP="<your-server-ip>"
echo ""
echo -e "${GREEN}═══════════════════════════════════════${NC}"
echo -e "${GREEN} Clawith running!${NC}"
echo -e "${GREEN}═══════════════════════════════════════${NC}"
echo ""
echo -e " ${CYAN}Local:${NC} http://localhost:$FRONTEND_PORT"
echo -e " ${CYAN}Network:${NC} http://${SERVER_IP}:$FRONTEND_PORT"
echo -e " ${CYAN}API:${NC} http://${SERVER_IP}:$BACKEND_PORT"
echo ""
echo -e " Backend log: tail -f $BACKEND_LOG"
echo -e " Frontend log: tail -f $FRONTEND_LOG"
}
# ═══════════════════════════════════════════════════════
# Docker 模式
# ═══════════════════════════════════════════════════════
run_docker_mode() {
if command -v docker &>/dev/null && docker ps | grep -E 'clawith' >/dev/null; then
echo -e "${YELLOW}🐳 Detected existing Docker containers! Starting in Docker mode...${NC}"
DIR_NAME=$(basename "$(dirname "$ROOT")")
[ -z "$DIR_NAME" ] && DIR_NAME="custom"
PROJECT_NAME="clawith-${DIR_NAME}"
echo -e " Using project name: ${GREEN}$PROJECT_NAME${NC}"
export COMPOSE_PROJECT_NAME="$PROJECT_NAME"
cd "$ROOT"
# 查找空闲端口
FRONTEND_PORT=3008
while true; do
if docker ps 2>/dev/null | grep -q ":$FRONTEND_PORT->"; then
FRONTEND_PORT=$((FRONTEND_PORT+1)); continue
fi
if command -v ss &>/dev/null && ss -tln 2>/dev/null | grep -qE ":$FRONTEND_PORT\b"; then
FRONTEND_PORT=$((FRONTEND_PORT+1)); continue
fi
if command -v lsof &>/dev/null && lsof -nP -iTCP:$FRONTEND_PORT -sTCP:LISTEN >/dev/null 2>&1; then
FRONTEND_PORT=$((FRONTEND_PORT+1)); continue
fi
break
done
echo -e " Allocated Frontend Port: ${GREEN}$FRONTEND_PORT${NC}"
export FRONTEND_PORT
docker compose down 2>/dev/null || docker-compose down 2>/dev/null || true
docker compose up -d --build 2>/dev/null || docker-compose up -d --build
echo -e "${GREEN}✅ Deployed via Docker. Port: $FRONTEND_PORT${NC}"
echo -e " ${CYAN}Local:${NC} http://localhost:$FRONTEND_PORT"
exit 0
fi
}
# ═══════════════════════════════════════════════════════
# Main
# ═══════════════════════════════════════════════════════
main() {
init_dirs
load_env
run_docker_mode || true
# 启动本地模式,如果docker 不存在
cleanup
start_postgres
start_backend
start_frontend
verify_proxy
print_info
}
main "$@"