commands.go 5.8 KB

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