windows.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package daemon
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strings"
  7. "github.com/gravitl/netmaker/netclient/ncutils"
  8. )
  9. // SetupWindowsDaemon - sets up the Windows daemon service
  10. func SetupWindowsDaemon() error {
  11. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") {
  12. if err := writeServiceConfig(); err != nil {
  13. return err
  14. }
  15. }
  16. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.exe") {
  17. ncutils.Log("performing first time daemon setup")
  18. err := ncutils.GetEmbedded()
  19. if err != nil {
  20. return err
  21. }
  22. ncutils.Log("finished daemon setup")
  23. }
  24. // install daemon, will not overwrite
  25. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe install`, false)
  26. // start daemon, will not restart or start another
  27. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`, false)
  28. ncutils.Log(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1) + `winsw.exe start`)
  29. return nil
  30. }
  31. // RestartWindowsDaemon - restarts windows service
  32. func RestartWindowsDaemon() {
  33. StopWindowsDaemon()
  34. // start daemon, will not restart or start another
  35. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`, false)
  36. ncutils.Log(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1) + `winsw.exe start`)
  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. ncutils.Log("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. ncutils.Log("no networks detected, 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. ncutils.Log("uninstalled Windows, Netclient daemon")
  80. }
  81. // func copyWinswOver() error {
  82. // input, err := ioutil.ReadFile(".\\winsw.exe")
  83. // if err != nil {
  84. // ncutils.Log("failed to find winsw.exe")
  85. // return err
  86. // }
  87. // if err = ioutil.WriteFile(ncutils.GetNetclientPathSpecific()+"winsw.exe", input, 0644); err != nil {
  88. // ncutils.Log("failed to copy winsw.exe to " + ncutils.GetNetclientPath())
  89. // return err
  90. // }
  91. // if err = os.Remove(".\\winsw.exe"); err != nil {
  92. // ncutils.Log("failed to cleanup local winsw.exe, feel free to delete it")
  93. // return err
  94. // }
  95. // ncutils.Log("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. // ncutils.Log("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. // ncutils.Log("downloading service tool...")
  116. // resp, err := client.Get(fullURLFile)
  117. // if err != nil {
  118. // ncutils.Log("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. // ncutils.Log("could not mount winsw.exe")
  125. // return err
  126. // }
  127. // ncutils.Log("finished downloading Winsw")
  128. // return nil
  129. // }