windows.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package daemon
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/gravitl/netmaker/logger"
  9. "github.com/gravitl/netmaker/netclient/ncutils"
  10. )
  11. // SetupWindowsDaemon - sets up the Windows daemon service
  12. func SetupWindowsDaemon() error {
  13. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") {
  14. if err := writeServiceConfig(); err != nil {
  15. return err
  16. }
  17. }
  18. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.exe") {
  19. logger.Log(0, "performing first time daemon setup")
  20. err := ncutils.GetEmbedded()
  21. if err != nil {
  22. return err
  23. }
  24. logger.Log(0, "finished daemon setup")
  25. }
  26. //get exact formatted commands
  27. RunWinSWCMD("install")
  28. time.Sleep(1)
  29. RunWinSWCMD("start")
  30. return nil
  31. }
  32. // RestartWindowsDaemon - restarts windows service
  33. func RestartWindowsDaemon() {
  34. RunWinSWCMD("stop")
  35. time.Sleep(1)
  36. RunWinSWCMD("start")
  37. }
  38. // CleanupWindows - cleans up windows files
  39. func CleanupWindows() {
  40. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") {
  41. writeServiceConfig()
  42. }
  43. RunWinSWCMD("stop")
  44. RunWinSWCMD("uninstall")
  45. os.RemoveAll(ncutils.GetNetclientPath())
  46. log.Println("Netclient on Windows, uninstalled")
  47. }
  48. func writeServiceConfig() error {
  49. serviceConfigPath := ncutils.GetNetclientPathSpecific() + "winsw.xml"
  50. scriptString := fmt.Sprintf(`<service>
  51. <id>netclient</id>
  52. <name>Netclient</name>
  53. <description>Connects Windows nodes to one or more Netmaker networks.</description>
  54. <executable>%v</executable>
  55. <arguments>daemon</arguments>
  56. <log mode="roll"></log>
  57. </service>
  58. `, strings.Replace(ncutils.GetNetclientPathSpecific()+"netclient.exe", `\\`, `\`, -1))
  59. if !ncutils.FileExists(serviceConfigPath) {
  60. err := os.WriteFile(serviceConfigPath, []byte(scriptString), 0600)
  61. if err != nil {
  62. return err
  63. }
  64. logger.Log(0, "wrote the daemon config file to the Netclient directory")
  65. }
  66. return nil
  67. }
  68. // RunWinSWCMD - Run a command with the winsw.exe tool (start, stop, install, uninstall)
  69. func RunWinSWCMD(command string) {
  70. // check if command allowed
  71. allowedCommands := map[string]bool{
  72. "start": true,
  73. "stop": true,
  74. "install": true,
  75. "uninstall": true,
  76. }
  77. if !allowedCommands[command] {
  78. logger.Log(0, "command "+command+" unsupported by winsw")
  79. return
  80. }
  81. // format command
  82. dirPath := strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)
  83. winCmd := fmt.Sprintf(`"%swinsw.exe" "%s"`, dirPath, command)
  84. logger.Log(0, "running "+command+" of Windows Netclient daemon")
  85. // run command and log for success/failure
  86. out, err := ncutils.RunCmdFormatted(winCmd, true)
  87. if err != nil {
  88. logger.Log(0, "error with "+command+" of Windows Netclient daemon: "+err.Error()+" : "+out)
  89. } else {
  90. logger.Log(0, "successfully ran "+command+" of Windows Netclient daemon")
  91. }
  92. }