windows.go 4.3 KB

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