commands.go 5.9 KB

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