2021-09-20 02:03:47 +08:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2021-09-22 02:43:14 +08:00
|
|
|
"io/ioutil"
|
2021-09-20 02:03:47 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
2021-10-03 00:28:17 +08:00
|
|
|
"fmt"
|
2021-09-29 06:24:10 +08:00
|
|
|
"path/filepath"
|
2021-09-20 02:03:47 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const MAC_SERVICE_NAME = "com.gravitl.netclient"
|
|
|
|
|
2021-10-03 00:28:17 +08:00
|
|
|
func SetupMacDaemon(interval string) error {
|
2021-09-29 06:24:10 +08:00
|
|
|
|
|
|
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
binarypath := dir + "/netclient"
|
|
|
|
|
|
|
|
if !ncutils.FileExists("/etc/netclient/netclient") {
|
|
|
|
_, err = ncutils.Copy(binarypath, "/etc/netclient/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
|
|
|
}
|
2021-10-03 00:28:17 +08:00
|
|
|
err = CreateMacService(MAC_SERVICE_NAME, interval)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-10-03 00:28:17 +08:00
|
|
|
func CreateMacService(servicename string, interval 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
|
|
|
}
|
2021-10-03 00:28:17 +08:00
|
|
|
daemonstring := MacDaemonString(interval)
|
2021-09-22 02:43:14 +08:00
|
|
|
daemonbytes := []byte(daemonstring)
|
|
|
|
|
|
|
|
if !ncutils.FileExists("/Library/LaunchDaemons/com.gravitl.netclient.plist") {
|
|
|
|
err = ioutil.WriteFile("/Library/LaunchDaemons/com.gravitl.netclient.plist", daemonbytes, 0644)
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
2021-09-22 02:43:14 +08:00
|
|
|
return err
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-10-03 00:28:17 +08:00
|
|
|
func MacDaemonString(interval string) string {
|
|
|
|
return fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8'?>
|
2021-09-22 02:43:14 +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>/etc/netclient/netclient</string>
|
|
|
|
<string>checkin</string>
|
|
|
|
<string>-n</string>
|
|
|
|
<string>all</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>AbandonProcessGroup</key><true/>
|
|
|
|
<key>StartInterval</key>
|
2021-10-03 00:28:17 +08:00
|
|
|
<integer>%s</integer>
|
2021-09-22 02:43:14 +08:00
|
|
|
<key>EnvironmentVariables</key>
|
|
|
|
<dict>
|
|
|
|
<key>PATH</key>
|
|
|
|
<string>/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
|
|
|
|
</dict>
|
|
|
|
</dict>
|
2021-09-20 02:03:47 +08:00
|
|
|
</plist>
|
2021-10-03 00:28:17 +08:00
|
|
|
`,interval)
|
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
|
|
|
}
|