-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredeploy-local
More file actions
executable file
·379 lines (337 loc) · 11.3 KB
/
redeploy-local
File metadata and controls
executable file
·379 lines (337 loc) · 11.3 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_PATH="$(readlink -f "${BASH_SOURCE[0]}")"
ROOT_DIR="$(cd "$(dirname "$SCRIPT_PATH")" && pwd)"
WBEAM_CONFIG_HELPER="$ROOT_DIR/host/scripts/wbeam_config.sh"
if [[ -f "$WBEAM_CONFIG_HELPER" ]]; then
# shellcheck source=host/scripts/wbeam_config.sh
source "$WBEAM_CONFIG_HELPER"
wbeam_load_config "$ROOT_DIR"
fi
USER_UID="$(id -u)"
USER_RUNTIME_DIR="/run/user/$USER_UID"
DESKTOP_DIR="$ROOT_DIR/desktop/apps/desktop-tauri"
HOST_BIN="$ROOT_DIR/host/rust/target/release/wbeamd-server"
ANDROID_DEBUG_APK="$ROOT_DIR/android/app/build/outputs/apk/debug/app-debug.apk"
REDEPLOY_COMPAT_STATE_FILE="$ROOT_DIR/.wbeam_redeploy_compat.state"
usage() {
cat <<'USAGE'
Usage:
./redeploy-local [--host-restart] [--no-host-build] [--no-android] [--no-desktop-build] [--no-desktop-start]
Flow (default):
1) stop host daemon processes
2) remove desktop user service unit
3) choose shared build revision (reuse current when rebuild is not required)
4) build host (release)
5) (optional) start host debug daemon in background
6) rebuild desktop app bundle
7) deploy Android APK to all connected ADB devices (force install)
8) run version doctor
9) launch desktop UI (after APK deploy)
Notes:
- Service is intentionally removed here; desktop UI should own service lifecycle.
- By default this script does NOT start host daemon; use --host-restart only for diagnostics.
USAGE
}
HOST_RESTART=0
HOST_BUILD=1
ANDROID_DEPLOY=1
DESKTOP_BUILD=1
DESKTOP_START=1
BUILD_REV=""
BUILD_REV_BUMPED=0
FINGERPRINT_HASH=""
adb_connected_serials_retry() {
local attempts=0
local prev=""
local cur=""
local stable=0
local -a serials=()
if ! command -v adb >/dev/null 2>&1; then
return 0
fi
adb start-server >/dev/null 2>&1 || true
while (( attempts < 6 )); do
mapfile -t serials < <(adb devices | awk 'NR>1 && $2=="device" {print $1}')
cur="${serials[*]:-}"
if [[ "$cur" == "$prev" ]]; then
stable=$((stable + 1))
else
stable=0
prev="$cur"
fi
if (( stable >= 1 )); then
break
fi
attempts=$((attempts + 1))
sleep 1
done
printf '%s\n' "${serials[@]}"
}
adb_installed_wbeam_version() {
local serial="$1"
adb -s "$serial" shell dumpsys package com.wbeam 2>/dev/null \
| sed -n 's/^[[:space:]]*versionName=//p' \
| head -n 1 \
| tr -d '\r'
}
ensure_desktop_dev_deps() {
if [[ -f "$DESKTOP_DIR/node_modules/vite/client.d.ts" ]]; then
return 0
fi
echo "[redeploy-local] desktop deps missing (vite/client); installing dev dependencies"
if [[ -f "$DESKTOP_DIR/package-lock.json" ]]; then
NODE_ENV=development npm --prefix "$DESKTOP_DIR" ci --include=dev
else
NODE_ENV=development npm --prefix "$DESKTOP_DIR" install --include=dev
fi
}
compat_state_read() {
local key="$1"
if [[ ! -f "$REDEPLOY_COMPAT_STATE_FILE" ]]; then
return 0
fi
awk -F= -v target="$key" '$1 == target { print $2 }' "$REDEPLOY_COMPAT_STATE_FILE" | tail -n 1
}
compat_state_write() {
local build_rev="$1"
local fingerprint_hash="$2"
cat > "$REDEPLOY_COMPAT_STATE_FILE" <<EOF
build_rev=$build_rev
fingerprint_hash=$fingerprint_hash
EOF
}
compat_fingerprint_hash() {
if ! command -v git >/dev/null 2>&1; then
return 1
fi
if ! git -C "$ROOT_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
return 1
fi
local tree_android tree_host tree_wbeam dirty
tree_android="$(git -C "$ROOT_DIR" rev-parse "HEAD:android" 2>/dev/null || echo "missing-android-tree")"
tree_host="$(git -C "$ROOT_DIR" rev-parse "HEAD:host/rust" 2>/dev/null || echo "missing-host-tree")"
tree_wbeam="$(git -C "$ROOT_DIR" rev-parse "HEAD:wbeam" 2>/dev/null || echo "missing-wbeam-tree")"
dirty="$(git -C "$ROOT_DIR" status --porcelain=v1 --untracked-files=all -- android host/rust wbeam 2>/dev/null || true)"
if command -v sha256sum >/dev/null 2>&1; then
printf 'android=%s\nhost=%s\nwbeam=%s\n%s\n' "$tree_android" "$tree_host" "$tree_wbeam" "$dirty" \
| sha256sum \
| awk '{print $1}'
return 0
fi
if command -v shasum >/dev/null 2>&1; then
printf 'android=%s\nhost=%s\nwbeam=%s\n%s\n' "$tree_android" "$tree_host" "$tree_wbeam" "$dirty" \
| shasum -a 256 \
| awk '{print $1}'
return 0
fi
return 1
}
resolve_build_rev() {
local current_rev current_hash prev_hash
local needs_bump=0
local -a reasons=()
if [[ -n "${WBEAM_BUILD_REV:-}" ]]; then
BUILD_REV="$WBEAM_BUILD_REV"
BUILD_REV_BUMPED=0
FINGERPRINT_HASH=""
echo "[redeploy-local] build_rev source=env"
return 0
fi
current_rev="$(./wbeam version current || true)"
current_hash="$(compat_fingerprint_hash || true)"
prev_hash="$(compat_state_read fingerprint_hash)"
if [[ "$HOST_BUILD" -eq 1 ]] && [[ ! -x "$HOST_BIN" ]]; then
needs_bump=1
reasons+=("host-binary-missing")
fi
if [[ "$ANDROID_DEPLOY" -eq 1 ]] && [[ ! -f "$ANDROID_DEBUG_APK" ]]; then
needs_bump=1
reasons+=("android-apk-missing")
fi
if [[ "$HOST_BUILD" -eq 1 || "$ANDROID_DEPLOY" -eq 1 ]]; then
if [[ -z "$current_hash" ]]; then
needs_bump=1
reasons+=("fingerprint-unavailable")
elif [[ -z "$prev_hash" ]]; then
needs_bump=1
reasons+=("no-fingerprint-baseline")
elif [[ "$current_hash" != "$prev_hash" ]]; then
needs_bump=1
reasons+=("compat-inputs-changed")
fi
fi
if [[ "$needs_bump" -eq 1 ]]; then
BUILD_REV="$(./wbeam version new)"
BUILD_REV_BUMPED=1
FINGERPRINT_HASH="$current_hash"
echo "[redeploy-local] build_rev source=new reasons=${reasons[*]}"
return 0
fi
if [[ -z "$current_rev" ]]; then
BUILD_REV="$(./wbeam version new)"
BUILD_REV_BUMPED=1
FINGERPRINT_HASH="$current_hash"
echo "[redeploy-local] build_rev source=new reasons=missing-current-revision"
return 0
fi
BUILD_REV="$current_rev"
BUILD_REV_BUMPED=0
FINGERPRINT_HASH="$current_hash"
echo "[redeploy-local] build_rev source=reuse"
}
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--host-restart)
HOST_RESTART=1
shift
;;
--no-host-restart)
HOST_RESTART=0
shift
;;
--no-host-build)
HOST_BUILD=0
shift
;;
--no-android)
ANDROID_DEPLOY=0
shift
;;
--no-desktop-build)
DESKTOP_BUILD=0
shift
;;
--no-desktop-start)
DESKTOP_START=0
shift
;;
*)
echo "[redeploy-local] unknown arg: $1" >&2
usage
exit 1
;;
esac
done
cd "$ROOT_DIR"
if [[ ! -x "$ROOT_DIR/wbeam" ]]; then
echo "[redeploy-local] missing executable: $ROOT_DIR/wbeam" >&2
exit 1
fi
echo "[redeploy-local] host stop"
./wbeam host down || true
echo "[redeploy-local] remove desktop user service"
if command -v systemctl >/dev/null 2>&1; then
XDG_RUNTIME_DIR="$USER_RUNTIME_DIR" DBUS_SESSION_BUS_ADDRESS="unix:path=$USER_RUNTIME_DIR/bus" \
systemctl --user stop wbeam-daemon.service >/dev/null 2>&1 || true
XDG_RUNTIME_DIR="$USER_RUNTIME_DIR" DBUS_SESSION_BUS_ADDRESS="unix:path=$USER_RUNTIME_DIR/bus" \
systemctl --user disable wbeam-daemon.service >/dev/null 2>&1 || true
if ! rm -f "$HOME/.config/systemd/user/wbeam-daemon.service" >/dev/null 2>&1; then
echo "[redeploy-local] WARN: failed to remove $HOME/.config/systemd/user/wbeam-daemon.service"
fi
XDG_RUNTIME_DIR="$USER_RUNTIME_DIR" DBUS_SESSION_BUS_ADDRESS="unix:path=$USER_RUNTIME_DIR/bus" \
systemctl --user daemon-reload >/dev/null 2>&1 || true
else
echo "[redeploy-local] WARN: systemctl not found; service cleanup skipped"
fi
resolve_build_rev
echo "[redeploy-local] build_rev=$BUILD_REV"
if [[ "$HOST_BUILD" -eq 1 ]]; then
echo "[redeploy-local] host build (release)"
WBEAM_BUILD_REV="$BUILD_REV" ./wbeam host build
else
echo "[redeploy-local] host build skipped (--no-host-build)"
fi
if [[ "$HOST_RESTART" -eq 1 ]]; then
echo "[redeploy-local] host start (debug background)"
WBEAM_BUILD_REV="$BUILD_REV" setsid -f nohup ./host/scripts/run_wbeamd_debug.sh 5001 5000 >/dev/null 2>&1
echo "[redeploy-local] wait for control API"
for _ in $(seq 1 180); do
if curl -fsS --max-time 1 http://127.0.0.1:5001/v1/status >/dev/null 2>&1; then
break
fi
sleep 0.5
done
else
echo "[redeploy-local] host start skipped (default; use --host-restart for diagnostics)"
fi
if [[ "$DESKTOP_BUILD" -eq 1 ]]; then
echo "[redeploy-local] desktop build"
ensure_desktop_dev_deps
NODE_ENV=development npm --prefix "$DESKTOP_DIR" run build
else
echo "[redeploy-local] desktop build skipped (--no-desktop-build)"
fi
if [[ "$ANDROID_DEPLOY" -eq 1 ]]; then
if command -v adb >/dev/null 2>&1; then
mapfile -t DEPLOY_SERIALS < <(adb_connected_serials_retry)
DEVICE_COUNT="${#DEPLOY_SERIALS[@]}"
else
DEVICE_COUNT=0
fi
if [[ "$DEVICE_COUNT" -gt 0 ]]; then
SERIALS_CSV="$(IFS=,; echo "${DEPLOY_SERIALS[*]}")"
echo "[redeploy-local] android deploy-all (devices=$DEVICE_COUNT serials=${DEPLOY_SERIALS[*]})"
WBEAM_BUILD_REV="$BUILD_REV" \
WBEAM_ANDROID_FORCE_INSTALL=1 \
WBEAM_ANDROID_SERIAL="" \
WBEAM_ANDROID_SERIALS="$SERIALS_CSV" \
./wbeam android deploy-all
VERIFY_FAIL=0
for serial in "${DEPLOY_SERIALS[@]}"; do
installed_ver="$(adb_installed_wbeam_version "$serial" || true)"
if [[ "$installed_ver" == "$BUILD_REV" ]]; then
echo "[redeploy-local][$serial] verify ok (version=$installed_ver)"
continue
fi
echo "[redeploy-local][$serial] WARN: expected version=$BUILD_REV, got=${installed_ver:-<none>} — retrying targeted deploy"
if WBEAM_BUILD_REV="$BUILD_REV" \
WBEAM_ANDROID_FORCE_INSTALL=1 \
WBEAM_ANDROID_SKIP_LAUNCH=1 \
WBEAM_ANDROID_SERIAL="$serial" \
./wbeam android deploy; then
installed_ver="$(adb_installed_wbeam_version "$serial" || true)"
if [[ "$installed_ver" == "$BUILD_REV" ]]; then
echo "[redeploy-local][$serial] recovery ok (version=$installed_ver)"
continue
fi
fi
echo "[redeploy-local][$serial] ERROR: deploy verification failed (expected=$BUILD_REV got=${installed_ver:-<none>})" >&2
VERIFY_FAIL=1
done
if [[ "$VERIFY_FAIL" -ne 0 ]]; then
echo "[redeploy-local] android deploy verification failed for one or more devices" >&2
exit 1
fi
else
echo "[redeploy-local] android deploy skipped (no adb devices in 'device' state)"
fi
else
echo "[redeploy-local] android deploy skipped (--no-android)"
fi
if [[ "$BUILD_REV_BUMPED" -eq 1 ]]; then
if [[ -n "$FINGERPRINT_HASH" ]]; then
compat_state_write "$BUILD_REV" "$FINGERPRINT_HASH"
echo "[redeploy-local] compat fingerprint updated"
else
echo "[redeploy-local] WARN: build_rev bumped but fingerprint unavailable; baseline not updated"
fi
fi
echo "[redeploy-local] version doctor"
./wbeam version doctor || true
if [[ "$DESKTOP_START" -eq 1 ]]; then
DESKTOP_USER="${WBEAM_DEV_REMOTE_USER:-$(id -un)}"
DESKTOP_LOG="$ROOT_DIR/logs/$(date +%Y%m%d-%H%M%S).desktop.redeploy-local.log"
echo "[redeploy-local] launch desktop UI via runas-remote user=$DESKTOP_USER (background)"
nohup env RUNAS_REMOTE_SESSION_REMOTE=no RUNAS_REMOTE_QUIET=1 \
"$ROOT_DIR/runas-remote" "$DESKTOP_USER" "$ROOT_DIR/desktop.sh" >"$DESKTOP_LOG" 2>&1 &
disown || true
echo "[redeploy-local] desktop log: $DESKTOP_LOG"
else
echo "[redeploy-local] desktop start skipped (--no-desktop-start)"
fi
echo "[redeploy-local] DONE"