windows.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package daemon
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "os"
  9. "strings"
  10. "github.com/gravitl/netmaker/netclient/ncutils"
  11. )
  12. // SetupWindowsDaemon - sets up the Windows daemon service
  13. func SetupWindowsDaemon() error {
  14. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") {
  15. if err := writeServiceConfig(); err != nil {
  16. return err
  17. }
  18. }
  19. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.exe") {
  20. ncutils.Log("performing first time daemon setup")
  21. if !ncutils.FileExists(".\\winsw.exe") {
  22. err := downloadWinsw()
  23. if err != nil {
  24. return err
  25. }
  26. }
  27. err := copyWinswOver()
  28. if err != nil {
  29. return err
  30. }
  31. ncutils.Log("finished daemon setup")
  32. }
  33. // install daemon, will not overwrite
  34. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe install`, false)
  35. // start daemon, will not restart or start another
  36. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`, false)
  37. ncutils.Log(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1) + `winsw.exe start`)
  38. return nil
  39. }
  40. // CleanupWindows - cleans up windows files
  41. func CleanupWindows() {
  42. if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") {
  43. writeServiceConfig()
  44. }
  45. StopWindowsDaemon()
  46. RemoveWindowsDaemon()
  47. os.RemoveAll(ncutils.GetNetclientPath())
  48. log.Println("Netclient on Windows, uninstalled")
  49. }
  50. func writeServiceConfig() error {
  51. serviceConfigPath := ncutils.GetNetclientPathSpecific() + "winsw.xml"
  52. scriptString := fmt.Sprintf(`<service>
  53. <id>netclient</id>
  54. <name>Netclient</name>
  55. <description>Connects Windows nodes to one or more Netmaker networks.</description>
  56. <executable>%v</executable>
  57. <log mode="roll"></log>
  58. </service>
  59. `, strings.Replace(ncutils.GetNetclientPathSpecific()+"netclient.exe", `\\`, `\`, -1))
  60. if !ncutils.FileExists(serviceConfigPath) {
  61. err := ioutil.WriteFile(serviceConfigPath, []byte(scriptString), 0644)
  62. if err != nil {
  63. return err
  64. }
  65. ncutils.Log("wrote the daemon config file to the Netclient directory")
  66. }
  67. return nil
  68. }
  69. // == Daemon ==
  70. // StopWindowsDaemon - stops the Windows daemon
  71. func StopWindowsDaemon() {
  72. ncutils.Log("no networks detected, stopping Windows, Netclient daemon")
  73. // stop daemon, will not overwrite
  74. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe stop`, true)
  75. }
  76. // RemoveWindowsDaemon - removes the Windows daemon
  77. func RemoveWindowsDaemon() {
  78. // uninstall daemon, will not restart or start another
  79. ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe uninstall`, true)
  80. ncutils.Log("uninstalled Windows, Netclient daemon")
  81. }
  82. func copyWinswOver() error {
  83. input, err := ioutil.ReadFile(".\\winsw.exe")
  84. if err != nil {
  85. ncutils.Log("failed to find winsw.exe")
  86. return err
  87. }
  88. if err = ioutil.WriteFile(ncutils.GetNetclientPathSpecific()+"winsw.exe", input, 0644); err != nil {
  89. ncutils.Log("failed to copy winsw.exe to " + ncutils.GetNetclientPath())
  90. return err
  91. }
  92. if err = os.Remove(".\\winsw.exe"); err != nil {
  93. ncutils.Log("failed to cleanup local winsw.exe, feel free to delete it")
  94. return err
  95. }
  96. ncutils.Log("finished copying winsw.exe")
  97. return nil
  98. }
  99. func downloadWinsw() error {
  100. fullURLFile := "https://github.com/winsw/winsw/releases/download/v2.11.0/WinSW-x64.exe"
  101. fileName := "winsw.exe"
  102. // Create the file
  103. file, err := os.Create(fileName)
  104. if err != nil {
  105. ncutils.Log("could not create file on OS for Winsw")
  106. return err
  107. }
  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. defer file.Close()
  128. ncutils.Log("finished downloading Winsw")
  129. return nil
  130. }