|
1 | 1 | package daemon
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "fmt"
|
| 6 | + "path/filepath" |
| 7 | + "text/template" |
| 8 | + |
| 9 | + "github.com/github/git-bundle-server/internal/common" |
5 | 10 | )
|
6 | 11 |
|
7 |
| -type launchd struct{} |
| 12 | +const launchTemplate string = `<?xml version="1.0" encoding="UTF-8"?> |
| 13 | +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 14 | +<plist version="1.0"> |
| 15 | + <dict> |
| 16 | + <key>Label</key><string>{{.Label}}</string> |
| 17 | + <key>Program</key><string>{{.Program}}</string> |
| 18 | + <key>StandardOutPath</key><string>{{.StdOut}}</string> |
| 19 | + <key>StandardErrorPath</key><string>{{.StdErr}}</string> |
| 20 | + </dict> |
| 21 | +</plist> |
| 22 | +` |
| 23 | + |
| 24 | +const domainFormat string = "gui/%s" |
| 25 | + |
| 26 | +const LaunchdServiceNotFoundErrorCode int = 113 |
| 27 | + |
| 28 | +type launchdConfig struct { |
| 29 | + DaemonConfig |
| 30 | + StdOut string |
| 31 | + StdErr string |
| 32 | +} |
| 33 | + |
| 34 | +type launchd struct { |
| 35 | + user common.UserProvider |
| 36 | + cmdExec common.CommandExecutor |
| 37 | + fileSystem common.FileSystem |
| 38 | +} |
| 39 | + |
| 40 | +func NewLaunchdProvider( |
| 41 | + u common.UserProvider, |
| 42 | + c common.CommandExecutor, |
| 43 | + fs common.FileSystem, |
| 44 | +) DaemonProvider { |
| 45 | + return &launchd{ |
| 46 | + user: u, |
| 47 | + cmdExec: c, |
| 48 | + fileSystem: fs, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (l *launchd) isBootstrapped(serviceTarget string) (bool, error) { |
| 53 | + // run 'launchctl print' on given service target to see if it exists |
| 54 | + exitCode, err := l.cmdExec.Run("launchctl", "print", serviceTarget) |
| 55 | + if err != nil { |
| 56 | + return false, err |
| 57 | + } |
| 58 | + |
| 59 | + if exitCode == 0 { |
| 60 | + return true, nil |
| 61 | + } else if exitCode == LaunchdServiceNotFoundErrorCode { |
| 62 | + return false, nil |
| 63 | + } else { |
| 64 | + return false, fmt.Errorf("could not determine if service '%s' is bootstrapped: "+ |
| 65 | + "'launchctl print' exited with status '%d'", serviceTarget, exitCode) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (l *launchd) bootstrapFile(domain string, filename string) error { |
| 70 | + // run 'launchctl bootstrap' on given domain & file |
| 71 | + exitCode, err := l.cmdExec.Run("launchctl", "bootstrap", domain, filename) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + if exitCode != 0 { |
| 77 | + return fmt.Errorf("'launchctl bootstrap' exited with status %d", exitCode) |
| 78 | + } |
| 79 | + |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func (l *launchd) bootoutFile(domain string, filename string) error { |
| 84 | + // run 'launchctl bootout' on given domain & file |
| 85 | + exitCode, err := l.cmdExec.Run("launchctl", "bootout", domain, filename) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
8 | 89 |
|
9 |
| -func NewLaunchdProvider() DaemonProvider { |
10 |
| - return &launchd{} |
| 90 | + if exitCode != 0 { |
| 91 | + return fmt.Errorf("'launchctl bootout' exited with status %d", exitCode) |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
11 | 95 | }
|
12 | 96 |
|
13 | 97 | func (l *launchd) Create(config *DaemonConfig, force bool) error {
|
14 |
| - return fmt.Errorf("not implemented") |
| 98 | + // Add launchd-specific config |
| 99 | + lConfig := &launchdConfig{ |
| 100 | + DaemonConfig: *config, |
| 101 | + StdOut: "/dev/null", |
| 102 | + StdErr: "/dev/null", |
| 103 | + } |
| 104 | + |
| 105 | + // Generate the configuration |
| 106 | + var newPlist bytes.Buffer |
| 107 | + t, err := template.New(config.Label).Parse(launchTemplate) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("unable to generate launchd configuration: %w", err) |
| 110 | + } |
| 111 | + t.Execute(&newPlist, lConfig) |
| 112 | + |
| 113 | + // Check the existing file - if it's the same as the new content, do not overwrite |
| 114 | + user, err := l.user.CurrentUser() |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("could not get current user for launchd service: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + filename := filepath.Join(user.HomeDir, "Library", "LaunchAgents", fmt.Sprintf("%s.plist", config.Label)) |
| 120 | + domainTarget := fmt.Sprintf(domainFormat, user.Uid) |
| 121 | + serviceTarget := fmt.Sprintf("%s/%s", domainTarget, config.Label) |
| 122 | + |
| 123 | + alreadyLoaded, err := l.isBootstrapped(serviceTarget) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + // First, verify whether the file exists |
| 129 | + // TODO: only overwrite file if file contents have changed |
| 130 | + fileExists, err := l.fileSystem.FileExists(filename) |
| 131 | + if err != nil { |
| 132 | + return fmt.Errorf("could not determine whether plist '%s' exists: %w", filename, err) |
| 133 | + } |
| 134 | + |
| 135 | + if alreadyLoaded && !fileExists { |
| 136 | + // Abort on corrupted configuration |
| 137 | + return fmt.Errorf("service target '%s' is bootstrapped, but its plist doesn't exist", serviceTarget) |
| 138 | + } |
| 139 | + |
| 140 | + if !force && alreadyLoaded { |
| 141 | + // Not forcing a refresh of the file, so we do nothing |
| 142 | + return nil |
| 143 | + } |
| 144 | + |
| 145 | + // Otherwise, write & bootstrap the file |
| 146 | + if alreadyLoaded { |
| 147 | + // Unload the old file, if necessary |
| 148 | + l.bootoutFile(domainTarget, filename) |
| 149 | + } |
| 150 | + |
| 151 | + if !fileExists || force { |
| 152 | + err = l.fileSystem.WriteFile(filename, newPlist.Bytes()) |
| 153 | + if err != nil { |
| 154 | + return fmt.Errorf("unable to overwrite plist file: %w", err) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + err = l.bootstrapFile(domainTarget, filename) |
| 159 | + if err != nil { |
| 160 | + return fmt.Errorf("could not bootstrap daemon process '%s': %w", config.Label, err) |
| 161 | + } |
| 162 | + |
| 163 | + return nil |
15 | 164 | }
|
16 | 165 |
|
17 | 166 | func (l *launchd) Start(label string) error {
|
|
0 commit comments