| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/xml" |
| 6 | ) |
| 7 | |
| 8 | // AgentLabel is the launchd user agent's label. |
| 9 | const AgentLabel = "io.amadan.equipo-runner" |
| 10 | |
| 11 | // Exit statuses under launchd. KeepAlive restarts the runner only after |
| 12 | // a non-zero exit (SuccessfulExit false): an update exits ExitUpdated so |
| 13 | // the new binary starts, a runner that lost its lease to a newer process |
| 14 | // exits 0 so the two never take turns, and a crash restarts. |
| 15 | const ExitUpdated = 75 |
| 16 | |
| 17 | // LaunchAgent is ~/Library/LaunchAgents/io.amadan.equipo-runner.plist |
| 18 | // (spec §3.2): runs at load, kept alive, a Standard process so builds |
| 19 | // are not throttled, its environment from envFile (launchd has no |
| 20 | // EnvironmentFile), logs under logDir, private files. |
| 21 | func LaunchAgent(binary, envFile, logDir string) []byte { |
| 22 | var b bytes.Buffer |
| 23 | esc := func(s string) string { |
| 24 | var e bytes.Buffer |
| 25 | _ = xml.EscapeText(&e, []byte(s)) |
| 26 | return e.String() |
| 27 | } |
| 28 | b.WriteString(`<?xml version="1.0" encoding="UTF-8"?> |
| 29 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 30 | <plist version="1.0"> |
| 31 | <dict> |
| 32 | <key>Label</key> |
| 33 | <string>` + AgentLabel + `</string> |
| 34 | <key>ProgramArguments</key> |
| 35 | <array> |
| 36 | <string>` + esc(binary) + `</string> |
| 37 | <string>run</string> |
| 38 | <string>--env-file</string> |
| 39 | <string>` + esc(envFile) + `</string> |
| 40 | </array> |
| 41 | <key>RunAtLoad</key> |
| 42 | <true/> |
| 43 | <key>KeepAlive</key> |
| 44 | <dict> |
| 45 | <key>SuccessfulExit</key> |
| 46 | <false/> |
| 47 | </dict> |
| 48 | <key>ProcessType</key> |
| 49 | <string>Standard</string> |
| 50 | <key>ThrottleInterval</key> |
| 51 | <integer>10</integer> |
| 52 | <key>ExitTimeOut</key> |
| 53 | <integer>45</integer> |
| 54 | <key>Umask</key> |
| 55 | <integer>63</integer> |
| 56 | <key>StandardOutPath</key> |
| 57 | <string>` + esc(logDir+"/runner.log") + `</string> |
| 58 | <key>StandardErrorPath</key> |
| 59 | <string>` + esc(logDir+"/runner.log") + `</string> |
| 60 | </dict> |
| 61 | </plist> |
| 62 | `) |
| 63 | return b.Bytes() |
| 64 | } |
| 65 | |