commands.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. if ncutils.IsFreeBSD() {
  34. daemon.RemoveFreebsdDaemon()
  35. }
  36. }
  37. } else {
  38. ncutils.PrintLog("success", 0)
  39. }
  40. if err != nil && strings.Contains(err.Error(), "ALREADY_INSTALLED") {
  41. ncutils.PrintLog(err.Error(), 0)
  42. err = nil
  43. }
  44. return err
  45. }
  46. ncutils.PrintLog("joined "+cfg.Network, 1)
  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. if strings.Contains(err.Error(), "could not find iface") {
  106. err = Pull(cfg)
  107. if err != nil {
  108. ncutils.PrintLog(err.Error(), 1)
  109. }
  110. } else {
  111. ncutils.PrintLog("error checking in for "+network+" network: "+err.Error(), 1)
  112. }
  113. } else {
  114. ncutils.PrintLog("checked in successfully for "+network, 1)
  115. }
  116. }
  117. if len(networks) == 0 {
  118. if ncutils.IsWindows() { // Windows specific - there are no netclients, so stop daemon process
  119. daemon.StopWindowsDaemon()
  120. }
  121. }
  122. errN = err
  123. err = nil
  124. } else {
  125. err = functions.CheckConfig(cfg)
  126. }
  127. if err == nil && errN != nil {
  128. err = errN
  129. }
  130. return err
  131. }
  132. // Leave - runs the leave command from cli
  133. func Leave(cfg config.ClientConfig) error {
  134. err := functions.LeaveNetwork(cfg.Network)
  135. if err != nil {
  136. ncutils.PrintLog("error attempting to leave network "+cfg.Network, 1)
  137. } else {
  138. ncutils.PrintLog("success", 0)
  139. }
  140. return err
  141. }
  142. // Push - runs push command
  143. func Push(cfg config.ClientConfig) error {
  144. var err error
  145. if cfg.Network == "all" || ncutils.IsWindows() {
  146. ncutils.PrintLog("pushing config to server for all networks.", 0)
  147. networks, err := ncutils.GetSystemNetworks()
  148. if err != nil {
  149. ncutils.PrintLog("error retrieving networks, exiting.", 0)
  150. return err
  151. }
  152. for _, network := range networks {
  153. err = functions.Push(network)
  154. if err != nil {
  155. ncutils.PrintLog("error pushing network configs for network: "+network+"\n"+err.Error(), 1)
  156. } else {
  157. ncutils.PrintLog("pushed network config for "+network, 1)
  158. }
  159. }
  160. err = nil
  161. } else {
  162. err = functions.Push(cfg.Network)
  163. }
  164. if err == nil {
  165. ncutils.PrintLog("completed pushing network configs to remote server", 1)
  166. ncutils.PrintLog("success", 1)
  167. } else {
  168. ncutils.PrintLog("error occurred pushing configs", 1)
  169. }
  170. return err
  171. }
  172. // Pull - runs pull command from cli
  173. func Pull(cfg config.ClientConfig) error {
  174. var err error
  175. if cfg.Network == "all" {
  176. ncutils.PrintLog("No network selected. Running Pull for all networks.", 0)
  177. networks, err := ncutils.GetSystemNetworks()
  178. if err != nil {
  179. ncutils.PrintLog("Error retrieving networks. Exiting.", 1)
  180. return err
  181. }
  182. for _, network := range networks {
  183. _, err = functions.Pull(network, true)
  184. if err != nil {
  185. ncutils.PrintLog("Error pulling network config for network: "+network+"\n"+err.Error(), 1)
  186. } else {
  187. ncutils.PrintLog("pulled network config for "+network, 1)
  188. }
  189. }
  190. err = nil
  191. } else {
  192. _, err = functions.Pull(cfg.Network, true)
  193. }
  194. ncutils.PrintLog("reset network and peer configs", 1)
  195. if err == nil {
  196. ncutils.PrintLog("reset network and peer configs", 1)
  197. ncutils.PrintLog("success", 1)
  198. } else {
  199. ncutils.PrintLog("error occurred pulling configs from server", 1)
  200. }
  201. return err
  202. }
  203. // List - runs list command from cli
  204. func List(cfg config.ClientConfig) error {
  205. err := functions.List(cfg.Network)
  206. return err
  207. }
  208. // Uninstall - runs uninstall command from cli
  209. func Uninstall() error {
  210. ncutils.PrintLog("uninstalling netclient...", 0)
  211. err := functions.Uninstall()
  212. ncutils.PrintLog("uninstalled netclient", 0)
  213. return err
  214. }
  215. func Daemon() error {
  216. err := functions.Daemon()
  217. return err
  218. }