windows.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package daemon
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strings"
  7. "github.com/gravitl/netmaker/logger"
  8. "github.com/gravitl/netmaker/netclient/ncutils"
  9. )
  10. // SetupWindowsDaemon - sets up the Windows daemon service
  11. func SetupWindowsDaemon() error {
  12. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") {
  13. if err := writeServiceConfig(); err != nil {
  14. return err
  15. }
  16. }
  17. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.exe") {
  18. logger.Log(0, "performing first time daemon setup")
  19. err := ncutils.GetEmbedded()
  20. if err != nil {
  21. return err
  22. }
  23. logger.Log(0, "finished daemon setup")
  24. }
  25. // install daemon, will not overwrite
  26. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe install`, false)
  27. // start daemon, will not restart or start another
  28. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`, false)
  29. logger.Log(0, strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`)
  30. return nil
  31. }
  32. // RestartWindowsDaemon - restarts windows service
  33. func RestartWindowsDaemon() {
  34. StopWindowsDaemon()
  35. // start daemon, will not restart or start another
  36. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`, false)
  37. }
  38. // CleanupWindows - cleans up windows files
  39. func CleanupWindows() {
  40. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") {
  41. writeServiceConfig()
  42. }
  43. StopWindowsDaemon()
  44. RemoveWindowsDaemon()
  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. // == Daemon ==
  69. // StopWindowsDaemon - stops the Windows daemon
  70. func StopWindowsDaemon() {
  71. logger.Log(0, "stopping Windows, Netclient daemon")
  72. // stop daemon, will not overwrite
  73. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe stop`, true)
  74. }
  75. // RemoveWindowsDaemon - removes the Windows daemon
  76. func RemoveWindowsDaemon() {
  77. // uninstall daemon, will not restart or start another
  78. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe uninstall`, true)
  79. logger.Log(0, "uninstalled Windows, Netclient daemon")
  80. }
  81. // func copyWinswOver() error {
  82. // input, err := ioutil.ReadFile(".\\winsw.exe")
  83. // if err != nil {
  84. // logger.Log(0, "failed to find winsw.exe")
  85. // return err
  86. // }
  87. // if err = ioutil.WriteFile(ncutils.GetNetclientPathSpecific()+"winsw.exe", input, 0644); err != nil {
  88. // logger.Log(0, "failed to copy winsw.exe to " + ncutils.GetNetclientPath())
  89. // return err
  90. // }
  91. // if err = os.Remove(".\\winsw.exe"); err != nil {
  92. // logger.Log(0, "failed to cleanup local winsw.exe, feel free to delete it")
  93. // return err
  94. // }
  95. // logger.Log(0, "finished copying winsw.exe")
  96. // return nil
  97. // }
  98. // func downloadWinsw() error {
  99. // fullURLFile := "https://github.com/winsw/winsw/releases/download/v2.11.0/WinSW-x64.exe"
  100. // fileName := "winsw.exe"
  101. // // Create the file
  102. // file, err := os.Create(fileName)
  103. // if err != nil {
  104. // logger.Log(0, "could not create file on OS for Winsw")
  105. // return err
  106. // }
  107. // defer file.Close()
  108. // client := http.Client{
  109. // CheckRedirect: func(r *http.Request, via []*http.Request) error {
  110. // r.URL.Opaque = r.URL.Path
  111. // return nil
  112. // },
  113. // }
  114. // // Put content on file
  115. // logger.Log(0, "downloading service tool...")
  116. // resp, err := client.Get(fullURLFile)
  117. // if err != nil {
  118. // logger.Log(0, "could not GET Winsw")
  119. // return err
  120. // }
  121. // defer resp.Body.Close()
  122. // _, err = io.Copy(file, resp.Body)
  123. // if err != nil {
  124. // logger.Log(0, "could not mount winsw.exe")
  125. // return err
  126. // }
  127. // logger.Log(0, "finished downloading Winsw")
  128. // return nil
  129. // }