#!/bin/bash set -euo pipefail cd "$(dirname "$0")/.." source Scripts/remote-sync.sh if [[ $# -lt 1 || $# -gt 2 ]]; then printf 'Usage: %s [--dry-run|--check|--apply]\n' "$0" >&2 exit 2 fi host=$1 mode=${2:---dry-run} remote_validate_host "$host" case "$mode" in --dry-run|--check|--apply) ;; *) printf 'Unknown setup mode: %s\n' "$mode" >&2; exit 2 ;; esac printf 'Bench host: %s; mode: %s\n' "$host" "$mode" printf '%s\n' \ 'Use a dedicated logged-in console user, AC power, and a deliberately selected Xcode/Swift toolchain.' \ 'Proposed AC settings: sudo pmset -c sleep 0 displaysleep 0 powernap 0 lowpowermode 0' \ 'Proposed backup exclusion: sudo tmutil addexclusion ~/parcel-ci' \ 'Manual: put ~/parcel-ci in Spotlight Privacy (do not disable indexing on the whole boot volume).' \ 'Manual: grant Accessibility to the remote build/Parcel.app, then run PARCEL_E2E_PREFLIGHT_ONLY=1 Scripts/test-e2e.sh.' \ 'Optional manual sudoers policy: permit only /usr/bin/powermetrics and /usr/bin/leaks for this user; review it with visudo.' \ 'No permission grants, toolchain selection, or sudoers changes are automated.' [[ $mode != --dry-run ]] || exit 0 # shellcheck disable=SC2029 ssh "$host" "bash -s -- $mode" <<'REMOTE' set -euo pipefail mode=$1 console_user=$(stat -f %Su /dev/console) [[ $console_user == "$(id -un)" ]] || { printf 'Bench user is not the logged-in console user.\n' >&2; exit 2; } sw_vers xcode-select -p swift --version battery=$(pmset -g batt) printf '%s\n' "$battery" [[ $battery == *"Now drawing from 'AC Power'"* ]] || { printf 'AC power is required.\n' >&2; exit 2; } pmset -g therm if [[ $mode == --apply ]]; then mkdir -p "$HOME/parcel-ci" sudo pmset -c sleep 0 displaysleep 0 powernap 0 lowpowermode 0 sudo tmutil addexclusion "$HOME/parcel-ci" fi settings=$(pmset -g custom) printf '%s\n' "$settings" in_ac=0 seen_ac=0 sleep_value= displaysleep_value= powernap_value= lowpowermode_value= powermode_value= while IFS= read -r line; do if [[ $line =~ ^AC\ Power:[[:space:]]*$ ]]; then in_ac=1; seen_ac=1; continue; fi if [[ $line =~ ^[^[:space:]] ]]; then in_ac=0; fi [[ $in_ac == 1 ]] || continue if [[ $line =~ ^[[:space:]]*([[:alnum:]_]+)[[:space:]]+([0-9]+)[[:space:]]*$ ]]; then key=${BASH_REMATCH[1]} value=${BASH_REMATCH[2]} case "$key" in sleep) sleep_value=$value ;; displaysleep) displaysleep_value=$value ;; powernap) powernap_value=$value ;; lowpowermode) lowpowermode_value=$value ;; powermode) powermode_value=$value ;; esac fi done <<< "$settings" [[ $seen_ac == 1 ]] || { printf 'Missing AC power settings\n' >&2; exit 1; } for key in sleep displaysleep powernap; do variable="${key}_value" [[ ${!variable} == 0 ]] || { printf 'Unexpected AC setting: %s\n' "$key" >&2; exit 1; } done [[ -n $lowpowermode_value || -n $powermode_value ]] || { printf 'Low Power Mode must be off\n' >&2; exit 1; } for value in "$lowpowermode_value" "$powermode_value"; do [[ -z $value || $value == 0 || $value == 2 ]] || { printf 'Low Power Mode must be off\n' >&2; exit 1; } done exclusion=$(tmutil isexcluded "$HOME/parcel-ci") printf '%s\n' "$exclusion" [[ $exclusion == *'[Excluded]'* ]] || { printf 'Backup exclusion is not active.\n' >&2; exit 2; } printf 'AC settings and backup exclusion verified; Accessibility and Spotlight remain manual preflight requirements.\n' REMOTE