Skip to content

Commit c6803b7

Browse files
committed
impv: macOS launchctl as the user and no longer require root persission
1 parent a98f3d0 commit c6803b7

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

models/weaveinit/run_l1_node.go

+1
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ func initializeApp(state *RunL1NodeState) tea.Cmd {
750750
}
751751

752752
nodeVersion = applicationVersion["version"].(string)
753+
state.initiadVersion = nodeVersion
753754
goos := runtime.GOOS
754755
goarch := runtime.GOARCH
755756
url = getBinaryURL(nodeVersion, goos, goarch)

utils/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ func InitializeConfig() error {
2424
return fmt.Errorf("failed to create data directory: %v", err)
2525
}
2626

27+
logPath := filepath.Join(homeDir, WeaveLogDirectory)
28+
if err := os.MkdirAll(logPath, os.ModePerm); err != nil {
29+
return fmt.Errorf("failed to create log directory: %v", err)
30+
}
31+
2732
viper.SetConfigFile(configPath)
2833
viper.SetConfigType("json")
2934

utils/constants.go

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
const (
44
WeaveDirectory = ".weave"
55
WeaveDataDirectory = WeaveDirectory + "/data"
6+
WeaveLogDirectory = WeaveDirectory + "/log"
67

78
SnapshotFilename = "snapshot.weave"
89

utils/service_manager.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ const DarwinRunL1NodeTemplate = `<?xml version="1.0" encoding="UTF-8"?>
2424
</array>
2525
2626
<key>RunAtLoad</key>
27-
<true/>
27+
<false/>
2828
2929
<key>KeepAlive</key>
30-
<true/>
30+
<false/>
3131
3232
<!-- Adding the environment variable -->
3333
<key>EnvironmentVariables</key>
@@ -39,10 +39,10 @@ const DarwinRunL1NodeTemplate = `<?xml version="1.0" encoding="UTF-8"?>
3939
</dict>
4040
4141
<key>StandardOutPath</key>
42-
<string>/tmp/initia.stdout.log</string>
42+
<string>%[4]s/initia.stdout.log</string>
4343
4444
<key>StandardErrorPath</key>
45-
<string>/tmp/initia.stderr.log</string>
45+
<string>%[4]s/initia.stderr.log</string>
4646
4747
<key>HardResourceLimits</key>
4848
<dict>
@@ -66,7 +66,11 @@ func CreateService(serviceName, serviceContent string) error {
6666
}
6767
return EnableService(serviceName)
6868
case "darwin":
69-
cmd := exec.Command("sudo", "tee", fmt.Sprintf("/Library/LaunchDaemons/%s.plist", serviceName))
69+
userHome, err := os.UserHomeDir()
70+
if err != nil {
71+
return fmt.Errorf("failed to get user home directory: %v", err)
72+
}
73+
cmd := exec.Command("tee", filepath.Join(userHome, fmt.Sprintf("Library/LaunchAgents/%s.plist", serviceName)))
7074
cmd.Stdin = strings.NewReader(serviceContent)
7175
if err := cmd.Run(); err != nil {
7276
return fmt.Errorf("failed to create service: %v", err)
@@ -94,7 +98,11 @@ func EnableService(serviceName string) error {
9498
}
9599

96100
func LoadService(serviceName string) error {
97-
loadCmd := exec.Command("sudo", "launchctl", "load", fmt.Sprintf("/Library/LaunchDaemons/%s.plist", serviceName))
101+
userHome, err := os.UserHomeDir()
102+
if err != nil {
103+
return fmt.Errorf("failed to get user home directory: %v", err)
104+
}
105+
loadCmd := exec.Command("launchctl", "load", filepath.Join(userHome, fmt.Sprintf("Library/LaunchAgents/%s.plist", serviceName)))
98106
if err := loadCmd.Run(); err != nil {
99107
return fmt.Errorf("failed to load service: %v", err)
100108
}
@@ -110,12 +118,7 @@ func StartService(serviceName string) error {
110118
}
111119
return nil
112120
case "darwin":
113-
loadCmd := exec.Command("sudo", "launchctl", "load", fmt.Sprintf("/Library/LaunchDaemons/%s.plist", serviceName))
114-
if err := loadCmd.Run(); err != nil {
115-
return fmt.Errorf("failed to load service: %v", err)
116-
}
117-
118-
startCmd := exec.Command("sudo", "launchctl", "start", serviceName)
121+
startCmd := exec.Command("launchctl", "start", serviceName)
119122
if err := startCmd.Run(); err != nil {
120123
return fmt.Errorf("failed to start service: %v", err)
121124
}
@@ -135,7 +138,11 @@ func StopService(serviceName string) error {
135138
}
136139
return nil
137140
case "darwin":
138-
cmd := exec.Command("sudo", "launchctl", "unload", fmt.Sprintf("/Library/LaunchDaemons/%s.plist", serviceName))
141+
userHome, err := os.UserHomeDir()
142+
if err != nil {
143+
return fmt.Errorf("failed to get user home directory: %v", err)
144+
}
145+
cmd := exec.Command("launchctl", "unload", filepath.Join(userHome, fmt.Sprintf("Library/LaunchAgents/%s.plist", serviceName)))
139146
if err := cmd.Run(); err != nil {
140147
return fmt.Errorf("failed to stop service: %v", err)
141148
}
@@ -162,6 +169,7 @@ func GetDarwinRunL1NodePlist(version string) string {
162169
panic(fmt.Errorf("failed to get user home directory: %v", err))
163170
}
164171
weaveDataPath := filepath.Join(userHome, WeaveDataDirectory)
172+
weaveLogPath := filepath.Join(userHome, WeaveLogDirectory)
165173
binaryPath := filepath.Join(weaveDataPath, "initia@"+version)
166174
initiaHome := filepath.Join(userHome, InitiaDirectory)
167175
if err = os.Setenv("DYLD_LIBRARY_PATH", binaryPath); err != nil {
@@ -171,5 +179,5 @@ func GetDarwinRunL1NodePlist(version string) string {
171179
panic(fmt.Errorf("failed to set HOME: %v", err))
172180
}
173181

174-
return fmt.Sprintf(DarwinRunL1NodeTemplate, binaryPath, userHome, initiaHome)
182+
return fmt.Sprintf(DarwinRunL1NodeTemplate, binaryPath, userHome, initiaHome, weaveLogPath)
175183
}

0 commit comments

Comments
 (0)