macos.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package daemon
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "github.com/gravitl/netmaker/netclient/ncutils"
  7. )
  8. const MAC_SERVICE_NAME = "com.gravitl.netclient"
  9. func SetupMacDaemon(interval string) error {
  10. if !ncutils.FileExists("/etc/netclient/netclient") {
  11. binarypath, err := os.Executable()
  12. if err != nil {
  13. return err
  14. }
  15. ncutils.PrintLog("installing binary from "+binarypath, 0)
  16. err = ncutils.Copy(binarypath, "/etc/netclient/netclient")
  17. if err != nil {
  18. log.Println(err)
  19. return err
  20. }
  21. }
  22. _, err := os.Stat("~/Library/LaunchAgents")
  23. if os.IsNotExist(err) {
  24. os.Mkdir("~/Library/LaunchAgents", 0755)
  25. }
  26. err = CreateMacService(MAC_SERVICE_NAME, interval)
  27. if err != nil {
  28. return err
  29. }
  30. _, err = ncutils.RunCmd("launchctl load /Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true)
  31. return err
  32. }
  33. func CleanupMac() {
  34. _, err := ncutils.RunCmd("launchctl unload /Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true)
  35. if ncutils.FileExists("/Library/LaunchDaemons/" + MAC_SERVICE_NAME + ".plist") {
  36. err = os.Remove("/Library/LaunchDaemons/" + MAC_SERVICE_NAME + ".plist")
  37. }
  38. if err != nil {
  39. ncutils.PrintLog(err.Error(), 1)
  40. }
  41. os.RemoveAll(ncutils.GetNetclientPath())
  42. }
  43. func CreateMacService(servicename string, interval string) error {
  44. _, err := os.Stat("/Library/LaunchDaemons")
  45. if os.IsNotExist(err) {
  46. os.Mkdir("/Library/LaunchDaemons", 0755)
  47. } else if err != nil {
  48. log.Println("couldnt find or create /Library/LaunchDaemons")
  49. return err
  50. }
  51. daemonstring := MacDaemonString(interval)
  52. daemonbytes := []byte(daemonstring)
  53. if !ncutils.FileExists("/Library/LaunchDaemons/com.gravitl.netclient.plist") {
  54. err = os.WriteFile("/Library/LaunchDaemons/com.gravitl.netclient.plist", daemonbytes, 0644)
  55. }
  56. return err
  57. }
  58. func MacDaemonString(interval string) string {
  59. return fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8'?>
  60. <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\" >
  61. <plist version='1.0'>
  62. <dict>
  63. <key>Label</key><string>com.gravitl.netclient</string>
  64. <key>ProgramArguments</key>
  65. <array>
  66. <string>/etc/netclient/netclient</string>
  67. <string>checkin</string>
  68. <string>-n</string>
  69. <string>all</string>
  70. </array>
  71. <key>StandardOutPath</key><string>/etc/netclient/com.gravitl.netclient.log</string>
  72. <key>StandardErrorPath</key><string>/etc/netclient/com.gravitl.netclient.log</string>
  73. <key>AbandonProcessGroup</key><true/>
  74. <key>StartInterval</key>
  75. <integer>%s</integer>
  76. <key>EnvironmentVariables</key>
  77. <dict>
  78. <key>PATH</key>
  79. <string>/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
  80. </dict>
  81. </dict>
  82. </plist>
  83. `, interval)
  84. }
  85. type MacTemplateData struct {
  86. Label string
  87. Interval string
  88. }