windows.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }
  37. // CleanupWindows - cleans up windows files
  38. func CleanupWindows() {
  39. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") {
  40. writeServiceConfig()
  41. }
  42. StopWindowsDaemon()
  43. RemoveWindowsDaemon()
  44. os.RemoveAll(ncutils.GetNetclientPath())
  45. log.Println("Netclient on Windows, uninstalled")
  46. }
  47. func writeServiceConfig() error {
  48. serviceConfigPath := ncutils.GetNetclientPathSpecific() + "winsw.xml"
  49. scriptString := fmt.Sprintf(`<service>
  50. <id>netclient</id>
  51. <name>Netclient</name>
  52. <description>Connects Windows nodes to one or more Netmaker networks.</description>
  53. <executable>%v</executable>
  54. <arguments>daemon</arguments>
  55. <log mode="roll"></log>
  56. </service>
  57. `, strings.Replace(ncutils.GetNetclientPathSpecific()+"netclient.exe", `\\`, `\`, -1))
  58. if !ncutils.FileExists(serviceConfigPath) {
  59. err := os.WriteFile(serviceConfigPath, []byte(scriptString), 0600)
  60. if err != nil {
  61. return err
  62. }
  63. ncutils.Log("wrote the daemon config file to the Netclient directory")
  64. }
  65. return nil
  66. }
  67. // == Daemon ==
  68. // StopWindowsDaemon - stops the Windows daemon
  69. func StopWindowsDaemon() {
  70. ncutils.Log("stopping Windows, Netclient daemon")
  71. // stop daemon, will not overwrite
  72. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe stop`, true)
  73. }
  74. // RemoveWindowsDaemon - removes the Windows daemon
  75. func RemoveWindowsDaemon() {
  76. // uninstall daemon, will not restart or start another
  77. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe uninstall`, true)
  78. ncutils.Log("uninstalled Windows, Netclient daemon")
  79. }
  80. // func copyWinswOver() error {
  81. // input, err := ioutil.ReadFile(".\\winsw.exe")
  82. // if err != nil {
  83. // ncutils.Log("failed to find winsw.exe")
  84. // return err
  85. // }
  86. // if err = ioutil.WriteFile(ncutils.GetNetclientPathSpecific()+"winsw.exe", input, 0644); err != nil {
  87. // ncutils.Log("failed to copy winsw.exe to " + ncutils.GetNetclientPath())
  88. // return err
  89. // }
  90. // if err = os.Remove(".\\winsw.exe"); err != nil {
  91. // ncutils.Log("failed to cleanup local winsw.exe, feel free to delete it")
  92. // return err
  93. // }
  94. // ncutils.Log("finished copying winsw.exe")
  95. // return nil
  96. // }
  97. // func downloadWinsw() error {
  98. // fullURLFile := "https://github.com/winsw/winsw/releases/download/v2.11.0/WinSW-x64.exe"
  99. // fileName := "winsw.exe"
  100. // // Create the file
  101. // file, err := os.Create(fileName)
  102. // if err != nil {
  103. // ncutils.Log("could not create file on OS for Winsw")
  104. // return err
  105. // }
  106. // defer file.Close()
  107. // client := http.Client{
  108. // CheckRedirect: func(r *http.Request, via []*http.Request) error {
  109. // r.URL.Opaque = r.URL.Path
  110. // return nil
  111. // },
  112. // }
  113. // // Put content on file
  114. // ncutils.Log("downloading service tool...")
  115. // resp, err := client.Get(fullURLFile)
  116. // if err != nil {
  117. // ncutils.Log("could not GET Winsw")
  118. // return err
  119. // }
  120. // defer resp.Body.Close()
  121. // _, err = io.Copy(file, resp.Body)
  122. // if err != nil {
  123. // ncutils.Log("could not mount winsw.exe")
  124. // return err
  125. // }
  126. // ncutils.Log("finished downloading Winsw")
  127. // return nil
  128. // }