-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-remote
More file actions
executable file
·384 lines (349 loc) · 13.3 KB
/
start-remote
File metadata and controls
executable file
·384 lines (349 loc) · 13.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
380
381
382
383
384
#!/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
WBEAM_GUI_HELPER="$ROOT_DIR/host/scripts/gui_session.sh"
if [[ -f "$WBEAM_GUI_HELPER" ]]; then
# shellcheck source=host/scripts/gui_session.sh
source "$WBEAM_GUI_HELPER"
fi
usage() {
cat <<'USAGE'
Usage:
./start-remote --list-sessions [desktop_user]
./start-remote --local [desktop_user] [--quick] [--redeploy] [--rebuild-host] [--no-host-restart] [--fresh-service] [--session-id SID] [--display :N]
./start-remote --rdp [desktop_user] [--quick] [--redeploy] [--rebuild-host] [--no-host-restart] [--fresh-service] [--session-id SID] [--display :N]
WBEAM_DEV_REMOTE_USER=<user> ./start-remote
Defaults:
desktop_user = active graphical user (or WBEAM_DEV_REMOTE_USER)
session scope = --local
Flow:
1) optionally remove desktop user service (forced in --redeploy)
2) restart host (selected GUI session)
3) snapshot adb devices + start USB udev probe logging
4) optional full Android deploy-all only with --redeploy
5) optional desktop rebuild (forced in --redeploy)
6) launch desktop GUI (selected GUI session)
7) print host-probe and daemon status
Modes:
--quick explicit fast startup (no Android deploy, no host rebuild)
default same as --quick
--rebuild-host rebuild host binary before startup
--no-host-restart keep currently running host daemon (skip host down/start)
--redeploy full cycle: rebuild host + force uninstall/install APK on all devices
--fresh-service stop+disable+remove user service unit before startup
--local force local machine seat (loginctl Remote=no)
--rdp force remote session only (loginctl Remote=yes)
--session-id SID force exact loginctl session id for runas-remote
--display :N force exact DISPLAY for selected session
Compatibility aliases:
--local-session == --local
--remote-session == --rdp
--actual-session == --local
--real-machine == --local
USAGE
}
REDEPLOY=0
REBUILD_HOST=0
HOST_RESTART=1
FRESH_SERVICE=0
REBUILD_DESKTOP=0
DESKTOP_USER="${WBEAM_DEV_REMOTE_USER:-}"
SESSION_REMOTE_FILTER="no"
SESSION_ID_OVERRIDE=""
DISPLAY_OVERRIDE=""
if [[ "${1:-}" == "--list-sessions" ]]; then
if [[ -n "${2:-}" && "${2:0:1}" != "-" ]]; then
./runas-remote --list-sessions "$2"
else
./runas-remote --list-sessions
fi
exit 0
fi
session_remote_matches() {
local remote="${1:-}"
case "$SESSION_REMOTE_FILTER" in
any) true ;;
no)
remote="${remote,,}"
[[ -z "$remote" || "$remote" == "no" || "$remote" == "-" || "$remote" == "unknown" ]]
;;
yes) [[ "${remote,,}" == "yes" ]] ;;
*) true ;;
esac
}
resolve_active_user() {
# Delegate to the canonical shared resolver from gui_session.sh.
if command -v wbeam_resolve_active_gui_user &>/dev/null; then
wbeam_resolve_active_gui_user "$SESSION_REMOTE_FILTER"
else
# Fallback: local implementation when gui_session.sh is not available.
local sid name type state active remote
while read -r sid _; do
[[ -n "$sid" ]] || continue
name="$(loginctl show-session "$sid" -p Name --value 2>/dev/null || true)"
type="$(loginctl show-session "$sid" -p Type --value 2>/dev/null || true)"
state="$(loginctl show-session "$sid" -p State --value 2>/dev/null || true)"
active="$(loginctl show-session "$sid" -p Active --value 2>/dev/null || true)"
remote="$(loginctl show-session "$sid" -p Remote --value 2>/dev/null || true)"
if [[ -n "$name" && "$name" != "root" && "$active" == "yes" && "$state" == "active" ]] && session_remote_matches "$remote"; then
if [[ "$type" == "x11" || "$type" == "wayland" ]]; then
echo "$name"
return 0
fi
fi
done < <(loginctl list-sessions --no-legend 2>/dev/null | awk '{print $1" "$2}')
return 1
fi
}
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--redeploy|--deploy)
REDEPLOY=1
REBUILD_HOST=1
REBUILD_DESKTOP=1
shift
;;
--quick)
REDEPLOY=0
REBUILD_HOST=0
REBUILD_DESKTOP=0
shift
;;
--rebuild-host)
REBUILD_HOST=1
shift
;;
--rebuild-desktop)
REBUILD_DESKTOP=1
shift
;;
--no-host-restart)
HOST_RESTART=0
shift
;;
--fresh-service)
FRESH_SERVICE=1
shift
;;
--local|--local-session|--actual-session|--real-machine)
SESSION_REMOTE_FILTER="no"
shift
;;
--rdp|--remote-session)
SESSION_REMOTE_FILTER="yes"
shift
;;
--session-id)
SESSION_ID_OVERRIDE="${2:-}"
shift 2
;;
--display)
DISPLAY_OVERRIDE="${2:-}"
shift 2
;;
--no-deploy)
REDEPLOY=0
shift
;;
--*)
echo "[start-remote] unknown flag: $1" >&2
usage
exit 1
;;
*)
DESKTOP_USER="$1"
shift
;;
esac
done
if [[ "$REDEPLOY" -eq 1 ]]; then
FRESH_SERVICE=1
fi
if [[ -z "$DESKTOP_USER" ]]; then
DESKTOP_USER="$(resolve_active_user || true)"
fi
if [[ -z "$DESKTOP_USER" ]]; then
echo "[start-remote] desktop_user is required (arg or WBEAM_DEV_REMOTE_USER)." >&2
echo "[start-remote] Hint: ./start-remote <user>" >&2
exit 1
fi
# Drop legacy env names and stale deploy overrides from the parent shell.
unset WBEAM_REMOTE_HOST_IP WBEAM_REMOTE_USER || true
unset WBEAM_ANDROID_HOST WBEAM_ANDROID_API_HOST WBEAM_ANDROID_STREAM_HOST WBEAM_DEV_REMOTE_HOST_IP || true
run_step() {
local title="$1"
shift
echo
echo "[start-remote] $title"
"$@"
}
runas_remote_exec() {
local -a env_args
env_args=("RUNAS_REMOTE_SESSION_REMOTE=$SESSION_REMOTE_FILTER")
# Always set these to avoid stale exported values from parent shell.
env_args+=("RUNAS_REMOTE_SESSION_ID=$SESSION_ID_OVERRIDE")
env_args+=("RUNAS_REMOTE_DISPLAY=$DISPLAY_OVERRIDE")
env "${env_args[@]}" ./runas-remote "$@"
}
next_log_file() {
local domain="$1"
local stamp last idx
stamp="$(date +%Y%m%d-%H%M%S)"
mkdir -p "$ROOT_DIR/logs"
last="$(
ls -1 "$ROOT_DIR/logs" 2>/dev/null \
| sed -n "s/.*\\.${domain}\\.\\([0-9]\\+\\)\\.log$/\\1/p" \
| sort -n \
| tail -n1
)"
if [[ -n "$last" ]]; then
idx=$((10#$last + 1))
else
idx=1
fi
printf '%s/logs/%s.%s.%04d.log\n' "$ROOT_DIR" "$stamp" "$domain" "$idx"
}
adb_snapshot_log() {
local logfile="$1"
local api model batt serial
local -a serials
{
echo "[adb-snapshot] ts=$(date -Iseconds)"
echo "[adb-snapshot] adb devices -l:"
adb devices -l || true
echo
mapfile -t serials < <(adb devices | awk 'NR>1 && $2=="device" {print $1}')
for serial in "${serials[@]}"; do
api="$(adb -s "$serial" shell getprop ro.build.version.sdk 2>/dev/null | tr -d '\r' || true)"
model="$(adb -s "$serial" shell getprop ro.product.model 2>/dev/null | tr -d '\r' || true)"
batt="$(adb -s "$serial" shell dumpsys battery 2>/dev/null | awk -F': ' '/level:/{print $2; exit}' | tr -d '\r' || true)"
echo "[adb-snapshot][$serial] model=${model:-unknown} api=${api:-unknown} battery=${batt:-unknown}"
done
} >>"$logfile" 2>&1
}
start_usb_probe_logger() {
local logfile="$1"
local pidfile="$ROOT_DIR/logs/udev-monitor.pid"
local old_pid
if [[ -f "$pidfile" ]]; then
old_pid="$(cat "$pidfile" 2>/dev/null || true)"
if [[ -n "${old_pid:-}" ]] && kill -0 "$old_pid" >/dev/null 2>&1; then
kill "$old_pid" >/dev/null 2>&1 || true
fi
rm -f "$pidfile"
fi
if ! command -v udevadm >/dev/null 2>&1; then
echo "[start-remote] WARN: udevadm not found, USB probe monitor disabled." >&2
return 0
fi
nohup bash -lc "stdbuf -oL udevadm monitor --udev --subsystem-match=usb >>'$logfile' 2>&1" >/dev/null 2>&1 &
echo "$!" > "$pidfile"
echo "[start-remote] USB probe logger started pid=$(cat "$pidfile") file=$logfile"
}
cd "$ROOT_DIR"
if [[ ! -x "$ROOT_DIR/wbeam" ]]; then
echo "[start-remote] missing executable: $ROOT_DIR/wbeam" >&2
exit 1
fi
if [[ ! -x "$ROOT_DIR/runas-remote" ]]; then
echo "[start-remote] missing executable: $ROOT_DIR/runas-remote" >&2
exit 1
fi
if [[ "$HOST_RESTART" -eq 1 ]]; then
run_step "host stop (cleanup stale processes)" ./wbeam host down
else
run_step "host stop (cleanup stale processes)" bash -lc "echo '[start-remote] skipped (--no-host-restart)'"
fi
if [[ "$FRESH_SERVICE" -eq 1 ]]; then
run_step "desktop service remove" runas_remote_exec "$DESKTOP_USER" bash -- -lc "systemctl --user stop wbeam-daemon >/dev/null 2>&1 || true; systemctl --user disable wbeam-daemon >/dev/null 2>&1 || true; rm -f ~/.config/systemd/user/wbeam-daemon.service; systemctl --user daemon-reload >/dev/null 2>&1 || true"
else
run_step "desktop service keep" bash -lc "echo '[start-remote] keeping existing user service (use --fresh-service to reset)'"
fi
run_step "desktop GUI cleanup (stale tauri/vite)" bash -lc "fuser -k 1420/tcp >/dev/null 2>&1 || true; pkill -u '$DESKTOP_USER' -x wbeam-desktop-t >/dev/null 2>&1 || true"
if [[ "$REDEPLOY" -eq 1 || "$REBUILD_HOST" -eq 1 ]]; then
BUILD_REV="$(./wbeam version new)"
else
BUILD_REV="$(./wbeam version current)"
fi
run_step "shared build version" bash -lc "echo [start-remote] build_rev=${BUILD_REV}"
if [[ "$REBUILD_HOST" -eq 1 ]]; then
run_step "host build (release)" env WBEAM_BUILD_REV="$BUILD_REV" ./wbeam host build
else
run_step "host build (release)" bash -lc "echo '[start-remote] skipped (fast mode, use --rebuild-host or --redeploy)'"
fi
if [[ "$HOST_RESTART" -eq 1 ]]; then
run_step "host start (debug, background in selected GUI session)" runas_remote_exec "$DESKTOP_USER" bash -- -lc "cd '$ROOT_DIR' && WBEAM_BUILD_REV='$BUILD_REV' setsid -f nohup ./host/scripts/run_wbeamd_debug.sh 5001 5000 >/dev/null 2>&1"
else
run_step "host start (debug, background in remote GUI session user=${DESKTOP_USER})" bash -lc "echo '[start-remote] skipped (--no-host-restart)'"
fi
run_step "wait for control API" bash -lc 'for i in $(seq 1 180); do curl -fsS --max-time 1 http://127.0.0.1:5001/v1/status >/dev/null 2>&1 && exit 0; sleep 0.5; done; exit 1'
run_step "wait for supported host-probe" bash -lc 'for i in $(seq 1 60); do probe="$(curl -fsS --max-time 1 http://127.0.0.1:5001/v1/host-probe 2>/dev/null || true)"; if [[ "$probe" == *"\"supported\":true"* ]]; then exit 0; fi; sleep 0.5; done; echo "[start-remote] WARN: host-probe not yet supported=true (continuing)" >&2; exit 0'
ADB_LOG="$(next_log_file adb)"
bash -lc "adb start-server >/dev/null 2>&1 || true"
: > "$ADB_LOG"
run_step "adb snapshot (startup)" adb_snapshot_log "$ADB_LOG"
UDEV_LOG="$(next_log_file udev)"
run_step "USB probe monitor start (udev)" start_usb_probe_logger "$UDEV_LOG"
deploy_failed=0
if [[ "$REDEPLOY" -eq 1 ]]; then
run_step "android uninstall APK on all adb devices" bash -lc 'adb start-server >/dev/null 2>&1 || true; for s in $(adb devices | awk '"'"'NR>1 && $2=="device"{print $1}'"'"'); do echo "[start-remote][$s] uninstall com.wbeam"; adb -s "$s" uninstall com.wbeam >/dev/null 2>&1 || true; done'
if ! run_step "android deploy-all" env WBEAM_BUILD_REV="$BUILD_REV" WBEAM_ANDROID_FORCE_INSTALL=1 WBEAM_ANDROID_SKIP_LAUNCH=1 ./wbeam android deploy-all; then
deploy_failed=1
echo "[start-remote] WARN: android deploy-all failed; desktop launch skipped." >&2
fi
else
run_step "android deploy-all" bash -lc "echo '[start-remote] skipped (fast mode, use --redeploy)'"
fi
run_step "adb snapshot (post-deploy)" adb_snapshot_log "$ADB_LOG"
run_step "version doctor" ./wbeam version doctor
if [[ "$REBUILD_DESKTOP" -eq 1 ]]; then
run_step "desktop app rebuild" bash -lc "npm --prefix '$ROOT_DIR/desktop/apps/desktop-tauri' run build"
else
run_step "desktop app rebuild" bash -lc "echo '[start-remote] skipped (use --rebuild-desktop or --redeploy)'"
fi
DESKTOP_LOG=""
if [[ "$deploy_failed" -eq 0 ]]; then
DESKTOP_LOG="$(next_log_file desktop)"
run_step "launch desktop GUI (background)" runas_remote_exec "$DESKTOP_USER" bash -- -lc "cd '$ROOT_DIR' && nohup ./desktop.sh >'$DESKTOP_LOG' 2>&1 & disown"
run_step "wait for desktop process" bash -lc "for i in \$(seq 1 120); do pgrep -u '$DESKTOP_USER' -x wbeam-desktop-t >/dev/null 2>&1 && exit 0; sleep 0.5; done; echo '[start-remote] WARN: desktop process did not become ready (continuing).' >&2; exit 0"
else
echo "[start-remote] desktop launch skipped because deploy failed."
fi
echo
echo "[start-remote] host-probe:"
curl -sS http://127.0.0.1:5001/v1/host-probe || true
echo
echo "[start-remote] status:"
curl -sS http://127.0.0.1:5001/v1/status || true
echo
echo
echo "[start-remote] mode: redeploy=${REDEPLOY} rebuild_host=${REBUILD_HOST} rebuild_desktop=${REBUILD_DESKTOP} host_restart=${HOST_RESTART} fresh_service=${FRESH_SERVICE}"
echo "[start-remote] session_scope: remote_filter=${SESSION_REMOTE_FILTER}"
if [[ -n "$SESSION_ID_OVERRIDE" ]]; then
echo "[start-remote] session_scope: forced_sid=${SESSION_ID_OVERRIDE}"
fi
if [[ -n "$DISPLAY_OVERRIDE" ]]; then
echo "[start-remote] session_scope: forced_display=${DISPLAY_OVERRIDE}"
fi
echo "[start-remote] logs:"
if [[ -n "$DESKTOP_LOG" ]]; then
echo " desktop: $DESKTOP_LOG"
else
echo " desktop: (not launched)"
fi
echo " adb: $ADB_LOG"
echo " udev: $UDEV_LOG"
if [[ "$deploy_failed" -eq 1 ]]; then
exit 1
fi