commands.go 5.8 KB

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