windows.go 4.0 KB

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