netmaker/netclient/daemon/macos.go

122 lines
3.4 KiB
Go
Raw Normal View History

2021-09-20 02:03:47 +08:00
package daemon
import (
"log"
"os"
"path/filepath"
2022-02-02 13:02:36 +08:00
"time"
2021-11-12 21:53:50 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
2021-09-20 02:03:47 +08:00
)
const MAC_SERVICE_NAME = "com.gravitl.netclient"
2022-02-08 00:30:15 +08:00
const MAC_EXEC_DIR = "/usr/local/bin/"
2021-09-20 02:03:47 +08:00
2022-01-25 20:31:50 +08:00
// SetupMacDaemon - Creates a daemon service from the netclient under LaunchAgents for MacOS
2022-02-19 04:18:50 +08:00
func SetupMacDaemon() error {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return err
}
binarypath := dir + "/netclient"
2022-02-08 00:30:15 +08:00
if !ncutils.FileExists(MAC_EXEC_DIR + "netclient") {
err = ncutils.Copy(binarypath, MAC_EXEC_DIR+"netclient")
if err != nil {
log.Println(err)
return err
}
}
2021-09-22 02:01:52 +08:00
_, errN := os.Stat("~/Library/LaunchAgents")
if os.IsNotExist(errN) {
os.Mkdir("~/Library/LaunchAgents", 0755)
2021-09-20 02:03:47 +08:00
}
2022-02-19 04:18:50 +08:00
err = CreateMacService(MAC_SERVICE_NAME)
2021-09-20 02:03:47 +08:00
if err != nil {
return err
}
2021-09-22 02:01:52 +08:00
_, err = ncutils.RunCmd("launchctl load /Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true)
2021-09-20 02:03:47 +08:00
return err
}
2022-01-25 20:31:50 +08:00
// CleanupMac - Removes the netclient checkin daemon from LaunchDaemons
2021-09-20 02:03:47 +08:00
func CleanupMac() {
2021-09-22 02:01:52 +08:00
_, err := ncutils.RunCmd("launchctl unload /Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true)
if ncutils.FileExists("/Library/LaunchDaemons/" + MAC_SERVICE_NAME + ".plist") {
err = os.Remove("/Library/LaunchDaemons/" + MAC_SERVICE_NAME + ".plist")
}
if err != nil {
ncutils.PrintLog(err.Error(), 1)
}
os.RemoveAll(ncutils.GetNetclientPath())
2022-02-08 00:30:15 +08:00
os.Remove(MAC_EXEC_DIR + "netclient")
2021-09-20 02:03:47 +08:00
}
// RestartLaunchD - restart launch daemon
2022-02-02 13:02:36 +08:00
func RestartLaunchD() {
ncutils.RunCmd("launchctl unload /Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true)
time.Sleep(time.Second >> 2)
ncutils.RunCmd("launchctl load /Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true)
}
// StopLaunchD - stop launch daemon
func StopLaunchD() {
ncutils.RunCmd("launchctl unload /System/Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true)
}
2022-01-25 20:31:50 +08:00
// CreateMacService - Creates the mac service file for LaunchDaemons
2022-02-19 04:18:50 +08:00
func CreateMacService(servicename string) error {
2021-09-22 02:01:52 +08:00
_, err := os.Stat("/Library/LaunchDaemons")
if os.IsNotExist(err) {
os.Mkdir("/Library/LaunchDaemons", 0755)
} else if err != nil {
log.Println("couldnt find or create /Library/LaunchDaemons")
return err
2021-09-20 02:03:47 +08:00
}
2022-02-19 04:18:50 +08:00
daemonstring := MacDaemonString()
daemonbytes := []byte(daemonstring)
if !ncutils.FileExists("/Library/LaunchDaemons/com.gravitl.netclient.plist") {
2022-01-07 04:05:38 +08:00
err = os.WriteFile("/Library/LaunchDaemons/com.gravitl.netclient.plist", daemonbytes, 0644)
2021-09-20 02:03:47 +08:00
}
return err
2021-09-20 02:03:47 +08:00
}
2022-01-25 20:31:50 +08:00
// MacDaemonString - the file contents for the mac netclient daemon service (launchdaemon)
2022-02-19 04:18:50 +08:00
func MacDaemonString() string {
return `<?xml version='1.0' encoding='UTF-8'?>
2022-02-22 10:49:08 +08:00
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\" >
<plist version='1.0'>
<dict>
<key>Label</key><string>com.gravitl.netclient</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/netclient</string>
<string>daemon</string>
</array>
<key>StandardOutPath</key><string>/etc/netclient/com.gravitl.netclient.log</string>
<key>StandardErrorPath</key><string>/etc/netclient/com.gravitl.netclient.log</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>AbandonProcessGroup</key><true/>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
</dict>
</plist>
2022-02-19 04:18:50 +08:00
`
2021-09-20 02:03:47 +08:00
}
2022-01-25 20:31:50 +08:00
// MacTemplateData - struct to represent the mac service
2021-09-20 02:03:47 +08:00
type MacTemplateData struct {
2021-09-22 02:01:52 +08:00
Label string
Interval string
2021-09-20 02:03:47 +08:00
}