-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
161 lines (138 loc) · 6.14 KB
/
Copy pathdeploy.sh
File metadata and controls
161 lines (138 loc) · 6.14 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
#!/usr/bin/env bash
# deploy.sh — update mayflower_sandbox + AIToolkit app, restart service
# Usage: ./deploy.sh [--no-pip] [--no-follow]
set -euo pipefail
# ── Config ────────────────────────────────────────────────────────────────────
MAYFLOWER_DIR="/root/mayflower_sandbox"
APP_DIR="${AITOOLKIT_APP_DIR:-/var/www/aitoolkit}"
# CrispLib lives next to the app dir by default; honour CRISPLIB_PATH if set.
CRISPLIB_DIR="${CRISPLIB_PATH:-$(dirname "$APP_DIR")/CrispLib}"
CRISPLIB_REPO="https://github.com/CrispStrobe/CrispLib.git"
# CrispASR: honour CRISPASR_PATH env, else probe sibling dirs.
CRISPASR_REPO="https://github.com/CrispStrobe/CrispASR.git"
VENV_PIP="$APP_DIR/venv/bin/pip"
SERVICE="${AITOOLKIT_SERVICE:-aitoolkit.service}"
FOLLOW_LOG=true
REINSTALL_PIP=true
# ── Arg parsing ───────────────────────────────────────────────────────────────
for arg in "$@"; do
case $arg in
--no-pip) REINSTALL_PIP=false ;;
--no-follow) FOLLOW_LOG=false ;;
*) echo "Unknown argument: $arg"; exit 1 ;;
esac
done
# ── Helpers ───────────────────────────────────────────────────────────────────
log() { echo -e "\033[1;34m==>\033[0m $*"; }
ok() { echo -e "\033[1;32m ✔\033[0m $*"; }
err() { echo -e "\033[1;31m ✘\033[0m $*" >&2; }
die() { err "$*"; exit 1; }
require_dir() {
[[ -d "$1" ]] || die "Directory not found: $1"
}
git_pull() {
local dir="$1"
require_dir "$dir"
log "git pull → $dir"
cd "$dir"
# Abort any in-progress rebase/merge to avoid getting stuck
if [[ -d .git/rebase-merge || -d .git/rebase-apply ]]; then
err "Rebase in progress in $dir — aborting"
git rebase --abort 2>/dev/null || true
fi
if [[ -f .git/MERGE_HEAD ]]; then
err "Merge in progress in $dir — aborting"
git merge --abort 2>/dev/null || true
fi
# Stash any local modifications so pull doesn't fail
local stashed=false
if ! git diff --quiet || ! git diff --cached --quiet; then
log " Local changes detected — stashing"
git stash push -m "deploy.sh auto-stash $(date +%Y%m%dT%H%M%S)"
stashed=true
fi
local before
before=$(git rev-parse HEAD)
git pull --ff-only 2>&1 | sed 's/^/ /'
local after
after=$(git rev-parse HEAD)
if [[ "$before" != "$after" ]]; then
ok "Updated $(git rev-parse --short "$before")..$(git rev-parse --short "$after")"
else
ok "Already up to date"
fi
if $stashed; then
log " Restoring stash"
git stash pop || err " stash pop failed — resolve manually with: cd $dir && git stash show -p"
fi
}
# ── Step 1: pull mayflower_sandbox ───────────────────────────────────────────
git_pull "$MAYFLOWER_DIR"
# ── Step 2: reinstall package into venv ──────────────────────────────────────
if $REINSTALL_PIP; then
log "pip install -e . → $MAYFLOWER_DIR"
[[ -x "$VENV_PIP" ]] || die "pip not found: $VENV_PIP"
"$VENV_PIP" install -e "$MAYFLOWER_DIR" --quiet 2>&1 | sed 's/^/ /'
ok "mayflower_sandbox installed"
fi
# ── Step 3: pull AIToolkit app ────────────────────────────────────────────────
git_pull "$APP_DIR"
# ── Step 3a: pull or clone CrispLib (sibling repo, sys.path injected) ────────
if [[ -d "$CRISPLIB_DIR/.git" ]]; then
git_pull "$CRISPLIB_DIR"
elif [[ ! -d "$CRISPLIB_DIR" ]]; then
log "Cloning CrispLib → $CRISPLIB_DIR"
git clone --depth 1 "$CRISPLIB_REPO" "$CRISPLIB_DIR" 2>&1 | sed 's/^/ /'
ok "CrispLib cloned"
else
err "CrispLib dir exists but is not a git checkout: $CRISPLIB_DIR"
fi
# ── Step 3b: refresh app Python deps (picks up defusedxml etc.) ──────────────
if $REINSTALL_PIP; then
log "pip install -r requirements.txt → $APP_DIR"
"$VENV_PIP" install -r "$APP_DIR/requirements.txt" --quiet 2>&1 | sed 's/^/ /' || \
err " pip install -r requirements.txt failed — check log"
ok "app dependencies refreshed"
fi
# ── Step 3c: ensure CRISPASR_PATH is set in .env ─────────────────────────────
# If already configured, skip. Otherwise probe sibling dirs for a built binary.
if grep -q '^CRISPASR_PATH=' "$APP_DIR/.env" 2>/dev/null; then
ok "CRISPASR_PATH already set in .env"
else
_parent="$(dirname "$APP_DIR")"
_found=""
for _name in CrispASR whisper.cpp; do
_dir="$_parent/$_name"
for _build in build-ninja-compile build build-ninja-clean; do
if [[ -x "$_dir/$_build/bin/crispasr" ]]; then
_found="$_dir"
break 2
fi
done
done
if [[ -n "$_found" ]]; then
log "Setting CRISPASR_PATH=$_found in .env"
echo "CRISPASR_PATH=$_found" >> "$APP_DIR/.env"
ok "CrispASR binary found at $_found"
else
err "CrispASR binary not found in sibling dirs. Set CRISPASR_PATH manually in $APP_DIR/.env"
fi
fi
# ── Step 4: restart service ───────────────────────────────────────────────────
log "Restarting $SERVICE"
systemctl restart "$SERVICE"
# Give it a moment to either start or crash
sleep 2
STATUS=$(systemctl is-active "$SERVICE" 2>/dev/null || true)
if [[ "$STATUS" == "active" ]]; then
ok "$SERVICE is running"
else
err "$SERVICE status: $STATUS"
systemctl status "$SERVICE" --no-pager -n 20
exit 1
fi
# ── Step 5: follow logs ───────────────────────────────────────────────────────
if $FOLLOW_LOG; then
log "Following logs (Ctrl-C to stop)…"
journalctl -u "$SERVICE" -f --since "now"
fi