commands.go 5.8 KB

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