-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·329 lines (270 loc) · 9.29 KB
/
Copy pathupdate.sh
File metadata and controls
executable file
·329 lines (270 loc) · 9.29 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
#!/usr/bin/env bash
# ═══════════════════════════════════════════════════════════════
# MIZAN (ميزان) — Self-Updater
#
# Usage:
# ./update.sh # Update to latest version
# ./update.sh --check # Check for updates without installing
# ./update.sh --version # Show current version
#
# Or via other entry points:
# make update
# ./start.sh update
# ═══════════════════════════════════════════════════════════════
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# ───── Colors ─────
if [ -t 1 ]; then
GOLD='\033[0;33m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
DIM='\033[2m'
BOLD='\033[1m'
NC='\033[0m'
else
GOLD='' GREEN='' BLUE='' RED='' DIM='' BOLD='' NC=''
fi
info() { echo -e " ${BLUE}➜${NC} $1"; }
success() { echo -e " ${GREEN}✓${NC} $1"; }
warn() { echo -e " ${GOLD}⚠${NC} $1"; }
fail() { echo -e " ${RED}✗${NC} $1"; }
step() { echo -e "\n ${GOLD}━━━${NC} ${BOLD}$1${NC}"; }
# ───── Version ─────
current_version() {
if [ -f VERSION ]; then
cat VERSION
else
echo "unknown"
fi
}
# ───── Check for Updates ─────
check_updates() {
step "Checking for updates"
local current
current="$(current_version)"
info "Current version: ${BOLD}${current}${NC}"
# Fetch latest from remote without merging
if ! git fetch origin 2>/dev/null; then
fail "Could not reach remote repository"
echo -e " ${DIM}Check your internet connection${NC}"
return 1
fi
local branch
branch="$(git branch --show-current 2>/dev/null || echo "main")"
# Check if there are new commits
local behind
behind="$(git rev-list --count HEAD..origin/${branch} 2>/dev/null || echo "0")"
if [ "$behind" = "0" ]; then
success "Already up to date (v${current})"
return 1
else
local latest_msg
latest_msg="$(git log --oneline origin/${branch} -1 2>/dev/null | cut -c1-60)"
info "${BOLD}${behind} update(s) available${NC}"
info "Latest: ${DIM}${latest_msg}${NC}"
return 0
fi
}
# ───── Stop Running Services ─────
stop_services() {
step "Stopping running services"
local stopped=0
# Stop via PID files
if [ -f /tmp/mizan-backend.pid ]; then
kill "$(cat /tmp/mizan-backend.pid)" 2>/dev/null && stopped=1 || true
rm -f /tmp/mizan-backend.pid
fi
if [ -f /tmp/mizan-frontend.pid ]; then
kill "$(cat /tmp/mizan-frontend.pid)" 2>/dev/null && stopped=1 || true
rm -f /tmp/mizan-frontend.pid
fi
# Stop any stray processes
pkill -f "uvicorn.*api.main" 2>/dev/null && stopped=1 || true
pkill -f "vite.*mizan" 2>/dev/null && stopped=1 || true
if [ "$stopped" = "1" ]; then
success "Services stopped"
else
info "No running services found"
fi
}
# ───── Pull Latest Code ─────
pull_code() {
step "Pulling latest code"
local branch
branch="$(git branch --show-current 2>/dev/null || echo "main")"
# Stash any local changes
local stashed=0
if ! git diff --quiet 2>/dev/null; then
info "Stashing local changes..."
git stash push -m "mizan-update-$(date +%s)" 2>/dev/null && stashed=1 || true
fi
# Pull
if git pull origin "$branch" 2>/dev/null; then
success "Code updated from ${branch}"
else
fail "Git pull failed"
if [ "$stashed" = "1" ]; then
info "Restoring local changes..."
git stash pop 2>/dev/null || true
fi
return 1
fi
# Restore stashed changes if any
if [ "$stashed" = "1" ]; then
info "Restoring local changes..."
git stash pop 2>/dev/null || warn "Could not auto-restore changes (saved in git stash)"
fi
}
# ───── Update Backend ─────
update_backend() {
step "Updating backend"
if [ -f "venv/bin/activate" ]; then
# shellcheck disable=SC1091
source venv/bin/activate
pip install -e "." --quiet 2>/dev/null
success "Backend dependencies updated"
elif [ -f "backend/venv/bin/activate" ]; then
# shellcheck disable=SC1091
source backend/venv/bin/activate
pip install -r backend/requirements.txt --quiet 2>/dev/null
success "Backend dependencies updated"
else
info "No virtual environment found — run: make setup"
fi
}
# ───── Update Frontend ─────
update_frontend() {
step "Updating frontend"
if [ ! -d "frontend" ]; then
warn "No frontend directory found"
return
fi
cd "$SCRIPT_DIR/frontend"
if command -v node &>/dev/null; then
npm install --silent 2>/dev/null
success "Frontend dependencies installed"
info "Building frontend..."
if npm run build 2>/dev/null; then
success "Frontend built successfully"
else
fail "Frontend build failed — check for errors"
fi
else
warn "Node.js not found — skipping frontend build"
fi
cd "$SCRIPT_DIR"
}
# ───── Restart Services ─────
restart_services() {
step "Restarting MIZAN"
if [ -f "start.sh" ]; then
# Use start.sh which handles everything
bash start.sh start &
local pid=$!
# Wait a moment for startup
sleep 3
if kill -0 "$pid" 2>/dev/null; then
success "MIZAN is running!"
echo ""
echo -e " ${BLUE}Frontend:${NC} http://localhost:3000"
echo -e " ${BLUE}Backend:${NC} http://localhost:8000"
echo -e " ${BLUE}API Docs:${NC} http://localhost:8000/docs"
else
warn "Start may still be in progress — check: ./start.sh status"
fi
else
info "Start manually with: make dev"
fi
}
# ───── Docker Update ─────
update_docker() {
step "Updating Docker deployment"
local compose_file="docker-compose.yml"
if [ -f "docker-compose.prod.yml" ]; then
compose_file="docker-compose.prod.yml"
fi
info "Rebuilding containers..."
docker compose -f "$compose_file" build --quiet 2>/dev/null || \
docker-compose -f "$compose_file" build --quiet 2>/dev/null
info "Restarting services..."
docker compose -f "$compose_file" up -d 2>/dev/null || \
docker-compose -f "$compose_file" up -d 2>/dev/null
success "Docker services updated and restarted"
}
# ───── Detect Install Method ─────
detect_method() {
if docker compose ps 2>/dev/null | grep -q "mizan" || \
docker-compose ps 2>/dev/null | grep -q "mizan"; then
echo "docker"
else
echo "local"
fi
}
# ───── Full Update ─────
full_update() {
echo ""
echo -e "${GOLD} ╔═══════════════════════════════════════════════╗${NC}"
echo -e "${GOLD} ║ ${BOLD}ميزان${NC}${GOLD} · MIZAN UPDATER ║${NC}"
echo -e "${GOLD} ╚═══════════════════════════════════════════════╝${NC}"
echo ""
local old_version
old_version="$(current_version)"
local method
method="$(detect_method)"
# 1. Check for updates
if ! check_updates; then
return 0
fi
# 2. Stop services
if [ "$method" != "docker" ]; then
stop_services
fi
# 3. Pull code
pull_code || return 1
# 4. Update deps + rebuild
if [ "$method" = "docker" ]; then
update_docker
else
update_backend
update_frontend
restart_services
fi
# 5. Done
local new_version
new_version="$(current_version)"
echo ""
echo -e " ${GOLD}═══════════════════════════════════════════════════${NC}"
echo -e " ${GREEN}${BOLD} Update complete!${NC}"
if [ "$old_version" != "$new_version" ]; then
echo -e " ${DIM} ${old_version} → ${new_version}${NC}"
fi
echo -e " ${GOLD}═══════════════════════════════════════════════════${NC}"
echo ""
}
# ───── Main ─────
case "${1:-}" in
--check|-c)
check_updates
;;
--version|-v)
echo "MIZAN v$(current_version)"
;;
--help|-h|help)
echo "Usage: ./update.sh [OPTIONS]"
echo ""
echo "Options:"
echo " (no args) Update to latest version"
echo " --check Check for updates without installing"
echo " --version Show current version"
echo " --help Show this help"
;;
"")
full_update
;;
*)
echo "Unknown option: $1 (use --help)"
exit 1
;;
esac