bootstrap.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <github-pat> [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: /usr/sbin/tccutil, /bin/launchctl, /usr/bin/tee /Library/LaunchDaemons/*, /usr/bin/sqlite3, /usr/libexec/ApplicationFirewall/socketfilterfw, /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart, /opt/homebrew/bin/brew, /usr/local/bin/brew, /usr/bin/xcode-select, /usr/bin/chown, /bin/mkdir, /usr/bin/touch, /usr/sbin/softwareupdate"
  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. mkdir -p "$RUNNER_DIR"
  118. cd "$RUNNER_DIR"
  119. CUSTOM_LABELS="self-hosted,macos,arm64,${CHIP_MODEL}_GPU${GPU_CORES}_${MEMORY}GB"
  120. log "Downloading GitHub Actions runner..."
  121. RUNNER_VERSION=$(curl -s https://api.github.com/repos/actions/runner/releases/latest | grep '"tag_name":' | cut -d'"' -f4)
  122. 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"
  123. tar xzf actions-runner.tar.gz
  124. rm actions-runner.tar.gz
  125. log "Configuring runner with labels: $CUSTOM_LABELS"
  126. ./config.sh --unattended \
  127. --url "https://github.com/${REPO}" \
  128. --token "${RUNNER_TOKEN}" \
  129. --name "${MACHINE_NAME}" \
  130. --labels "${CUSTOM_LABELS}" \
  131. --work "_work"
  132. # Create and load launch daemon
  133. log "Creating LaunchDaemon service..."
  134. sudo tee /Library/LaunchDaemons/com.github.runner.plist > /dev/null << EOF
  135. <?xml version="1.0" encoding="UTF-8"?>
  136. <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  137. <plist version="1.0">
  138. <dict>
  139. <key>Label</key>
  140. <string>com.github.runner</string>
  141. <key>UserName</key>
  142. <string>$(whoami)</string>
  143. <key>WorkingDirectory</key>
  144. <string>${RUNNER_DIR}</string>
  145. <key>ProgramArguments</key>
  146. <array>
  147. <string>${RUNNER_DIR}/run.sh</string>
  148. </array>
  149. <key>RunAtLoad</key>
  150. <true/>
  151. <key>KeepAlive</key>
  152. <true/>
  153. <key>StandardOutPath</key>
  154. <string>${RUNNER_DIR}/stdout.log</string>
  155. <key>StandardErrorPath</key>
  156. <string>${RUNNER_DIR}/stderr.log</string>
  157. </dict>
  158. </plist>
  159. EOF
  160. log "Starting GitHub Actions runner service..."
  161. sudo launchctl load /Library/LaunchDaemons/com.github.runner.plist
  162. # Add Runner.Listener permissions (after runner installation)
  163. RUNNER_PATH="$RUNNER_DIR/bin/Runner.Listener"
  164. sudo chmod 755" "$RUNNER_PATH"
  165. sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add "$RUNNER_PATH"
  166. sudo /usr/libexec/ApplicationFirewall/socketfilterfw --unblock "$RUNNER_PATH"
  167. # Create connection info file if Tailscale is configured
  168. if [ -n "$TAILSCALE_AUTH_KEY" ]; then
  169. TAILSCALE_IP=$(tailscale ip)
  170. cat > "$HOME/remote_access_info.txt" << EOL
  171. Mac Remote Access Information
  172. ============================
  173. Computer Name: $MACHINE_NAME
  174. Username: $USER
  175. Tailscale IP: $TAILSCALE_IP
  176. SSH Command: ssh $USER@$TAILSCALE_IP
  177. Screen Sharing: vnc://$TAILSCALE_IP
  178. EOL
  179. chmod 600 "$HOME/remote_access_info.txt"
  180. fi
  181. log "Verifying runner service status..."
  182. if sudo launchctl list | grep com.github.runner > /dev/null; then
  183. log "GitHub Actions runner service is running successfully!"
  184. log "Runner labels: $CUSTOM_LABELS"
  185. [ -n "$TAILSCALE_AUTH_KEY" ] && log "Remote access details saved to: $HOME/remote_access_info.txt"
  186. else
  187. log "Error: Failed to start GitHub Actions runner service"
  188. exit 1
  189. fi