42 lines
1.5 KiB
Bash
Executable File
42 lines
1.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -u
|
|
DEVICE="${DEVICE_ID:-xgfe65eimrrofyws}"
|
|
APK="${APK_PATH:-$(cd "$(dirname "$0")/.." && pwd)/android-app/app/build/outputs/apk/debug/app-debug.apk}"
|
|
LOG="${UPDATE_LOG:-/tmp/workphone-wss-autoupdate-${DEVICE}.log}"
|
|
INTERVAL="${POLL_INTERVAL:-10}"
|
|
MAX_WAIT="${MAX_WAIT_SECONDS:-86400}"
|
|
BASES=("http://192.168.110.47:8899" "http://192.168.110.101:8899")
|
|
start=$(date +%s)
|
|
exec >>"$LOG" 2>&1
|
|
printf '[%s] watcher_start device=%s apk=%s\n' "$(date '+%F %T')" "$DEVICE" "$APK"
|
|
if [[ ! -s "$APK" ]]; then echo "apk_missing"; exit 2; fi
|
|
while (( $(date +%s) - start < MAX_WAIT )); do
|
|
for base in "${BASES[@]}"; do
|
|
health=$(curl -sS --max-time 4 "$base/health" 2>/dev/null || true)
|
|
if python3 - "$DEVICE" "$health" <<'PY'
|
|
import json,sys
|
|
try:d=json.loads(sys.argv[2])
|
|
except:raise SystemExit(1)
|
|
raise SystemExit(0 if sys.argv[1] in (d.get('device_ids') or []) else 1)
|
|
PY
|
|
then
|
|
printf '[%s] online base=%s uploading\n' "$(date '+%F %T')" "$base"
|
|
receipt=$(curl -sS --max-time 600 -X POST "$base/api/v3/adb/devices/$DEVICE/app/install-ws" -F "file=@$APK;type=application/vnd.android.package-archive" 2>&1)
|
|
printf '[%s] receipt=%s\n' "$(date '+%F %T')" "$receipt"
|
|
if python3 - "$receipt" <<'PY'
|
|
import json,sys
|
|
try:d=json.loads(sys.argv[1])
|
|
except:raise SystemExit(1)
|
|
raise SystemExit(0 if int(d.get('code',500))==200 else 1)
|
|
PY
|
|
then
|
|
echo "UPDATE_SUCCESS"
|
|
exit 0
|
|
fi
|
|
fi
|
|
done
|
|
sleep "$INTERVAL"
|
|
done
|
|
echo "UPDATE_WAIT_TIMEOUT"
|
|
exit 3
|