bootstrap.sh 10 KB

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