commands.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 cfg.Daemon != "off" {
  48. err = daemon.InstallDaemon(cfg)
  49. }
  50. if ncutils.IsWindows() {
  51. ncutils.PrintLog("setting up WireGuard app", 0)
  52. time.Sleep(time.Second >> 1)
  53. functions.Pull(cfg.Network, true)
  54. }
  55. return err
  56. }
  57. func getWindowsInterval() int {
  58. interval := 15
  59. networks, err := ncutils.GetSystemNetworks()
  60. if err != nil {
  61. return interval
  62. }
  63. cfg, err := config.ReadConfig(networks[0])
  64. if err != nil {
  65. return interval
  66. }
  67. netint, err := strconv.Atoi(cfg.Server.CheckinInterval)
  68. if err == nil && netint != 0 {
  69. interval = netint
  70. }
  71. return interval
  72. }
  73. // RunUserspaceDaemon - runs continual checkins
  74. func RunUserspaceDaemon() {
  75. cfg := config.ClientConfig{
  76. Network: "all",
  77. }
  78. interval := getWindowsInterval()
  79. dur := time.Duration(interval) * time.Second
  80. for {
  81. CheckIn(cfg)
  82. time.Sleep(dur)
  83. }
  84. }
  85. // CheckIn - runs checkin command from cli
  86. func CheckIn(cfg config.ClientConfig) error {
  87. //log.Println("checkin --- diabled for now")
  88. //return nil
  89. var err error
  90. var errN error
  91. if cfg.Network == "" {
  92. ncutils.PrintLog("required, '-n', exiting", 0)
  93. os.Exit(1)
  94. } else if cfg.Network == "all" {
  95. ncutils.PrintLog("running checkin for all networks", 1)
  96. networks, err := ncutils.GetSystemNetworks()
  97. if err != nil {
  98. ncutils.PrintLog("error retrieving networks, exiting", 1)
  99. return err
  100. }
  101. for _, network := range networks {
  102. currConf, err := config.ReadConfig(network)
  103. if err != nil {
  104. continue
  105. }
  106. err = functions.CheckConfig(*currConf)
  107. if err != nil {
  108. if strings.Contains(err.Error(), "could not find iface") {
  109. err = Pull(cfg)
  110. if err != nil {
  111. ncutils.PrintLog(err.Error(), 1)
  112. }
  113. } else {
  114. ncutils.PrintLog("error checking in for "+network+" network: "+err.Error(), 1)
  115. }
  116. } else {
  117. ncutils.PrintLog("checked in successfully for "+network, 1)
  118. }
  119. }
  120. if len(networks) == 0 {
  121. if ncutils.IsWindows() { // Windows specific - there are no netclients, so stop daemon process
  122. daemon.StopWindowsDaemon()
  123. }
  124. }
  125. errN = err
  126. err = nil
  127. } else {
  128. err = functions.CheckConfig(cfg)
  129. }
  130. if err == nil && errN != nil {
  131. err = errN
  132. }
  133. return err
  134. }
  135. // Leave - runs the leave command from cli
  136. func Leave(cfg config.ClientConfig) error {
  137. err := functions.LeaveNetwork(cfg.Network)
  138. if err != nil {
  139. ncutils.PrintLog("error attempting to leave network "+cfg.Network, 1)
  140. } else {
  141. ncutils.PrintLog("success", 0)
  142. }
  143. return err
  144. }
  145. // Push - runs push command
  146. func Push(cfg config.ClientConfig) error {
  147. var err error
  148. if cfg.Network == "all" || ncutils.IsWindows() {
  149. ncutils.PrintLog("pushing config to server for all networks.", 0)
  150. networks, err := ncutils.GetSystemNetworks()
  151. if err != nil {
  152. ncutils.PrintLog("error retrieving networks, exiting.", 0)
  153. return err
  154. }
  155. for _, network := range networks {
  156. err = functions.Push(network)
  157. if err != nil {
  158. ncutils.PrintLog("error pushing network configs for network: "+network+"\n"+err.Error(), 1)
  159. } else {
  160. ncutils.PrintLog("pushed network config for "+network, 1)
  161. }
  162. }
  163. err = nil
  164. } else {
  165. err = functions.Push(cfg.Network)
  166. }
  167. if err == nil {
  168. ncutils.PrintLog("completed pushing network configs to remote server", 1)
  169. ncutils.PrintLog("success", 1)
  170. } else {
  171. ncutils.PrintLog("error occurred pushing configs", 1)
  172. }
  173. return err
  174. }
  175. // Pull - runs pull command from cli
  176. func Pull(cfg config.ClientConfig) error {
  177. var err error
  178. if cfg.Network == "all" {
  179. ncutils.PrintLog("No network selected. Running Pull for all networks.", 0)
  180. networks, err := ncutils.GetSystemNetworks()
  181. if err != nil {
  182. ncutils.PrintLog("Error retrieving networks. Exiting.", 1)
  183. return err
  184. }
  185. for _, network := range networks {
  186. _, err = functions.Pull(network, true)
  187. if err != nil {
  188. ncutils.PrintLog("Error pulling network config for network: "+network+"\n"+err.Error(), 1)
  189. } else {
  190. ncutils.PrintLog("pulled network config for "+network, 1)
  191. }
  192. }
  193. err = nil
  194. } else {
  195. _, err = functions.Pull(cfg.Network, true)
  196. }
  197. ncutils.PrintLog("reset network and peer configs", 1)
  198. if err == nil {
  199. ncutils.PrintLog("reset network and peer configs", 1)
  200. ncutils.PrintLog("success", 1)
  201. } else {
  202. ncutils.PrintLog("error occurred pulling configs from server", 1)
  203. }
  204. return err
  205. }
  206. // List - runs list command from cli
  207. func List(cfg config.ClientConfig) error {
  208. err := functions.List(cfg.Network)
  209. return err
  210. }
  211. // Uninstall - runs uninstall command from cli
  212. func Uninstall() error {
  213. ncutils.PrintLog("uninstalling netclient...", 0)
  214. err := functions.Uninstall()
  215. ncutils.PrintLog("uninstalled netclient", 0)
  216. return err
  217. }
  218. func Daemon() error {
  219. err := functions.Daemon()
  220. return err
  221. }