windows.go 4.0 KB

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