bootstrap.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #!/bin/bash
  2. set -e
  3. command_exists() {
  4. command -v "$1" >/dev/null 2>&1
  5. }
  6. log() {
  7. echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
  8. }
  9. if [ "$EUID" -eq 0 ]; then
  10. log "Please do not run as root. Run as regular user with sudo access."
  11. exit 1
  12. fi
  13. # Check for required arguments
  14. if [ -z "$1" ]; then
  15. log "Error: Runner token is required"
  16. log "Usage: $0 <runner-token> [tailscale-auth-key]"
  17. exit 1
  18. fi
  19. RUNNER_TOKEN=$1
  20. TAILSCALE_AUTH_KEY=$2
  21. REPO="exo-explore/exo"
  22. # Add sudoers configuration
  23. log "Configuring sudo access..."
  24. SUDOERS_CONTENT="$(whoami) ALL=(ALL) NOPASSWD: ALL"
  25. echo "$SUDOERS_CONTENT" | sudo tee /etc/sudoers.d/github-runner > /dev/null
  26. sudo chmod 440 /etc/sudoers.d/github-runner
  27. log "Configuring privacy permissions..."
  28. sudo tccutil reset All
  29. sudo tccutil reset SystemPolicyAllFiles
  30. sudo tccutil reset SystemPolicyNetworkVolumes
  31. # Configure power management for maximum performance
  32. log "Configuring power management..."
  33. sudo pmset -a powermode 2 # Force highest performance mode
  34. sudo pmset -a gpuswitch 2 # Force discrete/high-performance GPU
  35. sudo pmset -a lowpowermode 0
  36. sudo pmset -a lessbright 0
  37. sudo pmset -a disablesleep 1
  38. sudo pmset -a sleep 0
  39. sudo pmset -a hibernatemode 0
  40. sudo pmset -a autopoweroff 0
  41. sudo pmset -a standby 0
  42. sudo pmset -a powernap 0
  43. # For Python specifically
  44. PYTHON_PATH="/opt/homebrew/bin/python3.12"
  45. sudo chmod 755 "$PYTHON_PATH"
  46. # Add to firewall
  47. log "Configuring firewall access..."
  48. sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add "$PYTHON_PATH"
  49. sudo /usr/libexec/ApplicationFirewall/socketfilterfw --unblock "$PYTHON_PATH"
  50. # Set Homebrew paths based on architecture
  51. if [ "$(uname -p)" = "arm" ]; then
  52. BREW_PREFIX="/opt/homebrew"
  53. else
  54. BREW_PREFIX="/usr/local"
  55. fi
  56. # Install Homebrew if not present
  57. if ! command_exists brew; then
  58. log "Installing Homebrew..."
  59. /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  60. echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
  61. eval "$(/opt/homebrew/bin/brew shellenv)"
  62. fi
  63. # Install required packages
  64. log "Installing required packages..."
  65. export HOMEBREW_NO_AUTO_UPDATE=1
  66. brew install python@3.12 coreutils
  67. # Optional Tailscale setup if auth key is provided
  68. if [ -n "$TAILSCALE_AUTH_KEY" ]; then
  69. log "Installing and configuring Tailscale..."
  70. brew install --quiet tailscale
  71. sudo brew services stop tailscale 2>/dev/null || true
  72. sudo rm -f /var/db/tailscale/tailscaled.state 2>/dev/null || true
  73. sudo brew services start tailscale
  74. sleep 2
  75. sudo tailscale up --authkey=$TAILSCALE_AUTH_KEY
  76. # Enable SSH and Screen Sharing
  77. log "Enabling remote access services..."
  78. sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
  79. sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart \
  80. -activate \
  81. -configure -access -on \
  82. -configure -allowAccessFor -allUsers \
  83. -configure -restart -agent -privs -all
  84. # Create launch daemon for remote access
  85. sudo bash -c 'cat > /Library/LaunchDaemons/com.remote.access.setup.plist' << 'EOL'
  86. <?xml version="1.0" encoding="UTF-8"?>
  87. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  88. <plist version="1.0">
  89. <dict>
  90. <key>Label</key>
  91. <string>com.remote.access.setup</string>
  92. <key>ProgramArguments</key>
  93. <array>
  94. <string>/bin/bash</string>
  95. <string>-c</string>
  96. <string>
  97. launchctl load -w /System/Library/LaunchDaemons/ssh.plist;
  98. /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -access -on
  99. </string>
  100. </array>
  101. <key>RunAtLoad</key>
  102. <true/>
  103. </dict>
  104. </plist>
  105. EOL
  106. sudo chmod 644 /Library/LaunchDaemons/com.remote.access.setup.plist
  107. sudo launchctl load -w /Library/LaunchDaemons/com.remote.access.setup.plist
  108. fi
  109. # Configure GitHub Actions Runner
  110. log "Gathering system metadata..."
  111. MACHINE_NAME=$(scutil --get ComputerName)
  112. MACHINE_NAME="runner-$(echo -n "$MACHINE_NAME" | tr '[:upper:]' '[:lower:]' | tr -cd '[:alnum:]-')"
  113. # Enhanced Apple Silicon detection
  114. MACHINE_INFO=$(system_profiler SPHardwareDataType)
  115. CHIP_FULL=$(echo "$MACHINE_INFO" | grep "Chip" | cut -d: -f2 | xargs)
  116. if [[ $CHIP_FULL =~ "Apple" ]]; then
  117. CHIP_MODEL=$(echo "$CHIP_FULL" | sed 's/^Apple //' | tr -d ' ' | tr '[:lower:]' '[:upper:]')
  118. GPU_CORES=$(ioreg -l | grep "gpu-core-count" | awk -F'= ' '{print $2}')
  119. if [ -z "$GPU_CORES" ]; then
  120. GPU_CORES="N/A"
  121. fi
  122. else
  123. CHIP_MODEL="Intel"
  124. GPU_CORES="N/A"
  125. fi
  126. MEMORY=$(($(sysctl -n hw.memsize) / 1024 / 1024 / 1024))
  127. # Set up GitHub Runner
  128. RUNNER_DIR="$HOME/actions-runner"
  129. # Check if runner is already configured
  130. if [ -f "$RUNNER_DIR/.runner" ]; then
  131. log "Runner already configured. Stopping existing service..."
  132. sudo launchctl unload /Library/LaunchDaemons/com.github.runner.plist 2>/dev/null || true
  133. fi
  134. # Create runner directory if it doesn't exist
  135. mkdir -p "$RUNNER_DIR"
  136. cd "$RUNNER_DIR"
  137. CUSTOM_LABELS="self-hosted,macos,arm64,${CHIP_MODEL}_GPU${GPU_CORES}_${MEMORY}GB"
  138. # Only download and extract if not already present or if forced
  139. if [ ! -f "$RUNNER_DIR/run.sh" ] || [ "${FORCE_SETUP:-false}" = "true" ]; then
  140. log "Downloading GitHub Actions runner..."
  141. RUNNER_VERSION=$(curl -s https://api.github.com/repos/actions/runner/releases/latest | grep '"tag_name":' | cut -d'"' -f4)
  142. curl -o actions-runner.tar.gz -L "https://github.com/actions/runner/releases/download/${RUNNER_VERSION}/actions-runner-osx-arm64-${RUNNER_VERSION#v}.tar.gz"
  143. tar xzf actions-runner.tar.gz
  144. rm actions-runner.tar.gz
  145. else
  146. log "Runner already downloaded, skipping download step"
  147. fi
  148. log "Configuring runner with labels: $CUSTOM_LABELS"
  149. ./config.sh --unattended \
  150. --url "https://github.com/${REPO}" \
  151. --token "${RUNNER_TOKEN}" \
  152. --name "${MACHINE_NAME}" \
  153. --labels "${CUSTOM_LABELS}" \
  154. --work "_work"
  155. # Set optimal performance settings
  156. log "Configuring system for optimal performance..."
  157. # Configure CPU performance
  158. log "Setting CPU performance controls..."
  159. # Disable timer coalescing
  160. sudo sysctl -w kern.timer.coalescing_enabled=0
  161. sudo sysctl -w kern.timer_coalesce_bg_scale=-5
  162. sudo sysctl -w kern.timer_resort_threshold_ns=0
  163. # Set minimum timer intervals
  164. sudo sysctl -w kern.wq_max_timer_interval_usecs=1000
  165. sudo sysctl -w kern.timer_coalesce_bg_ns_max=1000
  166. # Set minimum timer coalescing for all tiers
  167. sudo sysctl -w kern.timer_coalesce_tier0_scale=-5
  168. sudo sysctl -w kern.timer_coalesce_tier0_ns_max=1000
  169. sudo sysctl -w kern.timer_coalesce_tier1_scale=-5
  170. sudo sysctl -w kern.timer_coalesce_tier1_ns_max=1000
  171. sudo sysctl -w kern.timer_coalesce_tier2_scale=-5
  172. sudo sysctl -w kern.timer_coalesce_tier2_ns_max=1000
  173. sudo sysctl -w kern.timer_coalesce_tier3_scale=-5
  174. sudo sysctl -w kern.timer_coalesce_tier3_ns_max=1000
  175. sudo sysctl -w kern.timer_coalesce_tier4_scale=-5
  176. sudo sysctl -w kern.timer_coalesce_tier4_ns_max=1000
  177. # Disable QoS restrictions
  178. sudo sysctl -w net.qos.policy.restricted=0
  179. sudo sysctl -w net.qos.policy.restrict_avapps=0
  180. sudo sysctl -w net.qos.policy.wifi_enabled=0
  181. sudo sysctl -w net.qos.policy.capable_enabled=0
  182. # Set scheduler parameters
  183. sudo sysctl -w kern.sched_rt_avoid_cpu0=0
  184. sudo sysctl -w debug.sched=2
  185. sudo sysctl -w net.pktsched.netem.sched_output_ival_ms=1
  186. # Clean up any existing runner services
  187. log "Cleaning up existing runner services..."
  188. for service in com.github.runner com.github.runner.monitor com.github.runner.cpuaffinity com.github.runner.affinity; do
  189. sudo launchctl bootout system/$service 2>/dev/null || true
  190. sudo rm -f /Library/LaunchDaemons/$service.plist
  191. done
  192. # Create a simple runner service configuration
  193. sudo tee /Library/LaunchDaemons/com.github.runner.plist > /dev/null << EOF
  194. <?xml version="1.0" encoding="UTF-8"?>
  195. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  196. <plist version="1.0">
  197. <dict>
  198. <key>Label</key>
  199. <string>com.github.runner</string>
  200. <key>UserName</key>
  201. <string>$(whoami)</string>
  202. <key>GroupName</key>
  203. <string>staff</string>
  204. <key>WorkingDirectory</key>
  205. <string>$RUNNER_DIR</string>
  206. <key>ProgramArguments</key>
  207. <array>
  208. <string>$RUNNER_DIR/run.sh</string>
  209. </array>
  210. <key>RunAtLoad</key>
  211. <true/>
  212. <key>KeepAlive</key>
  213. <dict>
  214. <key>SuccessfulExit</key>
  215. <false/>
  216. <key>Crashed</key>
  217. <true/>
  218. </dict>
  219. <key>ProcessType</key>
  220. <string>Interactive</string>
  221. <key>LowPriorityIO</key>
  222. <false/>
  223. <key>AbandonProcessGroup</key>
  224. <false/>
  225. <key>EnableTransactions</key>
  226. <true/>
  227. <key>ThrottleInterval</key>
  228. <integer>0</integer>
  229. <key>HardResourceLimits</key>
  230. <dict>
  231. <key>NumberOfFiles</key>
  232. <integer>524288</integer>
  233. <key>MemoryLock</key>
  234. <integer>-1</integer>
  235. </dict>
  236. <key>SoftResourceLimits</key>
  237. <dict>
  238. <key>NumberOfFiles</key>
  239. <integer>524288</integer>
  240. <key>MemoryLock</key>
  241. <integer>-1</integer>
  242. </dict>
  243. <key>QOSClass</key>
  244. <string>User-Interactive</string>
  245. <key>StandardOutPath</key>
  246. <string>$RUNNER_DIR/_diag/runner.log</string>
  247. <key>StandardErrorPath</key>
  248. <string>$RUNNER_DIR/_diag/runner.err</string>
  249. <key>EnvironmentVariables</key>
  250. <dict>
  251. <key>PATH</key>
  252. <string>/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
  253. </dict>
  254. <key>Nice</key>
  255. <integer>-20</integer>
  256. </dict>
  257. </plist>
  258. EOF
  259. # Set proper permissions for the LaunchDaemon
  260. sudo chown root:wheel /Library/LaunchDaemons/com.github.runner.plist
  261. sudo chmod 644 /Library/LaunchDaemons/com.github.runner.plist
  262. # Remove any existing service
  263. sudo launchctl bootout system/com.github.runner 2>/dev/null || true
  264. # Load the new service using bootstrap
  265. sudo launchctl bootstrap system /Library/LaunchDaemons/com.github.runner.plist
  266. # Add Runner.Listener permissions (after runner installation)
  267. RUNNER_PATH="$RUNNER_DIR/bin/Runner.Listener"
  268. sudo chmod 755 "$RUNNER_PATH"
  269. sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add "$RUNNER_PATH"
  270. sudo /usr/libexec/ApplicationFirewall/socketfilterfw --unblock "$RUNNER_PATH"
  271. # Create connection info file if Tailscale is configured
  272. if [ -n "$TAILSCALE_AUTH_KEY" ]; then
  273. TAILSCALE_IP=$(tailscale ip)
  274. cat > "$HOME/remote_access_info.txt" << EOL
  275. Mac Remote Access Information
  276. ============================
  277. Computer Name: $MACHINE_NAME
  278. Username: $USER
  279. Tailscale IP: $TAILSCALE_IP
  280. SSH Command: ssh $USER@$TAILSCALE_IP
  281. Screen Sharing: vnc://$TAILSCALE_IP
  282. EOL
  283. chmod 600 "$HOME/remote_access_info.txt"
  284. fi
  285. log "Verifying runner service status..."
  286. if sudo launchctl list | grep com.github.runner > /dev/null; then
  287. log "GitHub Actions runner service is running successfully!"
  288. log "Runner labels: $CUSTOM_LABELS"
  289. [ -n "$TAILSCALE_AUTH_KEY" ] && log "Remote access details saved to: $HOME/remote_access_info.txt"
  290. else
  291. log "Error: Failed to start GitHub Actions runner service"
  292. exit 1
  293. fi