commands.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package command
  2. import (
  3. "os"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/gravitl/netmaker/netclient/config"
  8. "github.com/gravitl/netmaker/netclient/daemon"
  9. "github.com/gravitl/netmaker/netclient/functions"
  10. "github.com/gravitl/netmaker/netclient/ncutils"
  11. )
  12. // Join - join command to run from cli
  13. func Join(cfg config.ClientConfig, privateKey string) error {
  14. var err error
  15. err = functions.JoinNetwork(cfg, privateKey)
  16. if err != nil && !cfg.DebugOn {
  17. if !strings.Contains(err.Error(), "ALREADY_INSTALLED") {
  18. ncutils.PrintLog("error installing: "+err.Error(), 1)
  19. err = functions.LeaveNetwork(cfg.Network)
  20. if err != nil {
  21. err = functions.WipeLocal(cfg.Network)
  22. if err != nil {
  23. ncutils.PrintLog("error removing artifacts: "+err.Error(), 1)
  24. }
  25. }
  26. if cfg.Daemon != "off" {
  27. if ncutils.IsLinux() {
  28. err = daemon.RemoveSystemDServices()
  29. }
  30. if err != nil {
  31. ncutils.PrintLog("error removing services: "+err.Error(), 1)
  32. }
  33. }
  34. } else {
  35. ncutils.PrintLog("success", 0)
  36. }
  37. if err != nil && strings.Contains(err.Error(), "ALREADY_INSTALLED") {
  38. ncutils.PrintLog(err.Error(), 0)
  39. err = nil
  40. }
  41. return err
  42. }
  43. ncutils.PrintLog("joined "+cfg.Network, 1)
  44. if cfg.Daemon != "off" {
  45. err = daemon.InstallDaemon(cfg)
  46. }
  47. if ncutils.IsWindows() {
  48. ncutils.PrintLog("setting up WireGuard app", 0)
  49. time.Sleep(time.Second >> 1)
  50. functions.Pull(cfg.Network, true)
  51. }
  52. return err
  53. }
  54. func getWindowsInterval() int {
  55. interval := 15
  56. networks, err := ncutils.GetSystemNetworks()
  57. if err != nil {
  58. return interval
  59. }
  60. cfg, err := config.ReadConfig(networks[0])
  61. if err != nil {
  62. return interval
  63. }
  64. netint, err := strconv.Atoi(cfg.Server.CheckinInterval)
  65. if err == nil && netint != 0 {
  66. interval = netint
  67. }
  68. return interval
  69. }
  70. // RunUserspaceDaemon - runs continual checkins
  71. func RunUserspaceDaemon() {
  72. cfg := config.ClientConfig{
  73. Network: "all",
  74. }
  75. interval := getWindowsInterval()
  76. dur := time.Duration(interval) * time.Second
  77. for {
  78. CheckIn(cfg)
  79. time.Sleep(dur)
  80. }
  81. }
  82. // CheckIn - runs checkin command from cli
  83. func CheckIn(cfg config.ClientConfig) error {
  84. //log.Println("checkin --- diabled for now")
  85. //return nil
  86. var err error
  87. var errN error
  88. if cfg.Network == "" {
  89. ncutils.PrintLog("required, '-n', exiting", 0)
  90. os.Exit(1)
  91. } else if cfg.Network == "all" {
  92. ncutils.PrintLog("running checkin for all networks", 1)
  93. networks, err := ncutils.GetSystemNetworks()
  94. if err != nil {
  95. ncutils.PrintLog("error retrieving networks, exiting", 1)
  96. return err
  97. }
  98. for _, network := range networks {
  99. currConf, err := config.ReadConfig(network)
  100. if err != nil {
  101. continue
  102. }
  103. err = functions.CheckConfig(*currConf)
  104. if err != nil {
  105. ncutils.PrintLog("error checking in for "+network+" network: "+err.Error(), 1)
  106. } else {
  107. ncutils.PrintLog("checked in successfully for "+network, 1)
  108. }
  109. }
  110. if len(networks) == 0 {
  111. if ncutils.IsWindows() { // Windows specific - there are no netclients, so stop daemon process
  112. daemon.StopWindowsDaemon()
  113. }
  114. }
  115. errN = err
  116. err = nil
  117. } else {
  118. err = functions.CheckConfig(cfg)
  119. }
  120. if err == nil && errN != nil {
  121. err = errN
  122. }
  123. return err
  124. }
  125. // Leave - runs the leave command from cli
  126. func Leave(cfg config.ClientConfig) error {
  127. err := functions.LeaveNetwork(cfg.Network)
  128. if err != nil {
  129. ncutils.PrintLog("error attempting to leave network "+cfg.Network, 1)
  130. } else {
  131. ncutils.PrintLog("success", 0)
  132. }
  133. return err
  134. }
  135. // Push - runs push command
  136. func Push(cfg config.ClientConfig) error {
  137. var err error
  138. if cfg.Network == "all" || ncutils.IsWindows() {
  139. ncutils.PrintLog("pushing config to server for all networks.", 0)
  140. networks, err := ncutils.GetSystemNetworks()
  141. if err != nil {
  142. ncutils.PrintLog("error retrieving networks, exiting.", 0)
  143. return err
  144. }
  145. for _, network := range networks {
  146. err = functions.Push(network)
  147. if err != nil {
  148. ncutils.PrintLog("error pushing network configs for network: "+network+"\n"+err.Error(), 1)
  149. } else {
  150. ncutils.PrintLog("pushed network config for "+network, 1)
  151. }
  152. }
  153. err = nil
  154. } else {
  155. err = functions.Push(cfg.Network)
  156. }
  157. if err == nil {
  158. ncutils.PrintLog("completed pushing network configs to remote server", 1)
  159. ncutils.PrintLog("success", 1)
  160. } else {
  161. ncutils.PrintLog("error occurred pushing configs", 1)
  162. }
  163. return err
  164. }
  165. // Pull - runs pull command from cli
  166. func Pull(cfg config.ClientConfig) error {
  167. var err error
  168. if cfg.Network == "all" {
  169. ncutils.PrintLog("No network selected. Running Pull for all networks.", 0)
  170. networks, err := ncutils.GetSystemNetworks()
  171. if err != nil {
  172. ncutils.PrintLog("Error retrieving networks. Exiting.", 1)
  173. return err
  174. }
  175. for _, network := range networks {
  176. _, err = functions.Pull(network, true)
  177. if err != nil {
  178. ncutils.PrintLog("Error pulling network config for network: "+network+"\n"+err.Error(), 1)
  179. } else {
  180. ncutils.PrintLog("pulled network config for "+network, 1)
  181. }
  182. }
  183. err = nil
  184. } else {
  185. _, err = functions.Pull(cfg.Network, true)
  186. }
  187. ncutils.PrintLog("reset network and peer configs", 1)
  188. if err == nil {
  189. ncutils.PrintLog("reset network and peer configs", 1)
  190. ncutils.PrintLog("success", 1)
  191. } else {
  192. ncutils.PrintLog("error occurred pulling configs from server", 1)
  193. }
  194. return err
  195. }
  196. // List - runs list command from cli
  197. func List(cfg config.ClientConfig) error {
  198. err := functions.List(cfg.Network)
  199. return err
  200. }
  201. // Uninstall - runs uninstall command from cli
  202. func Uninstall() error {
  203. ncutils.PrintLog("uninstalling netclient...", 0)
  204. err := functions.Uninstall()
  205. ncutils.PrintLog("uninstalled netclient", 0)
  206. return err
  207. }
  208. func Daemon() error {
  209. err := functions.Daemon()
  210. return err
  211. }