windows.go 2.8 KB

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