failover.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "github.com/google/uuid"
  8. "github.com/gorilla/mux"
  9. controller "github.com/gravitl/netmaker/controllers"
  10. "github.com/gravitl/netmaker/logger"
  11. "github.com/gravitl/netmaker/logic"
  12. "github.com/gravitl/netmaker/models"
  13. "github.com/gravitl/netmaker/mq"
  14. proLogic "github.com/gravitl/netmaker/pro/logic"
  15. "golang.org/x/exp/slog"
  16. )
  17. // FailOverHandlers - handlers for FailOver
  18. func FailOverHandlers(r *mux.Router) {
  19. r.HandleFunc("/api/v1/node/{nodeid}/failover", logic.SecurityCheck(true, http.HandlerFunc(createfailOver))).Methods(http.MethodPost)
  20. r.HandleFunc("/api/v1/node/{nodeid}/failover", logic.SecurityCheck(true, http.HandlerFunc(deletefailOver))).Methods(http.MethodDelete)
  21. r.HandleFunc("/api/v1/node/{network}/failover/reset", logic.SecurityCheck(true, http.HandlerFunc(resetFailOver))).Methods(http.MethodPost)
  22. r.HandleFunc("/api/v1/node/{nodeid}/failover_me", controller.Authorize(true, false, "host", http.HandlerFunc(failOverME))).Methods(http.MethodPost)
  23. }
  24. // swagger:route POST /api/v1/node/failover node createfailOver
  25. //
  26. // Create a relay.
  27. //
  28. // Schemes: https
  29. //
  30. // Security:
  31. // oauth
  32. //
  33. // Responses:
  34. // 200: nodeResponse
  35. func createfailOver(w http.ResponseWriter, r *http.Request) {
  36. var params = mux.Vars(r)
  37. nodeid := params["nodeid"]
  38. // confirm host exists
  39. node, err := logic.GetNodeByID(nodeid)
  40. if err != nil {
  41. slog.Error("failed to get node:", "error", err.Error())
  42. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
  43. return
  44. }
  45. if _, exists := proLogic.FailOverExists(node.Network); exists {
  46. logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("failover exists already in the network"), "badrequest"))
  47. return
  48. }
  49. host, err := logic.GetHost(node.HostID.String())
  50. if err != nil {
  51. logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("error getting host"+err.Error()), "badrequest"))
  52. return
  53. }
  54. if host.OS != models.OS_Types.Linux {
  55. logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("only linux nodes can act as failovers"), "badrequest"))
  56. return
  57. }
  58. if node.IsRelayed {
  59. logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("cannot set relayed node as failover"), "badrequest"))
  60. return
  61. }
  62. node.IsFailOver = true
  63. err = logic.UpsertNode(&node)
  64. if err != nil {
  65. slog.Error("failed to upsert node", "node", node.ID.String(), "error", err)
  66. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
  67. return
  68. }
  69. go mq.PublishPeerUpdate()
  70. w.Header().Set("Content-Type", "application/json")
  71. logic.ReturnSuccessResponseWithJson(w, r, node, "created failover successfully")
  72. }
  73. func resetFailOver(w http.ResponseWriter, r *http.Request) {
  74. var params = mux.Vars(r)
  75. net := params["network"]
  76. nodes, err := logic.GetNetworkNodes(net)
  77. if err != nil {
  78. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
  79. return
  80. }
  81. for _, node := range nodes {
  82. if node.FailedOverBy != uuid.Nil {
  83. node.FailedOverBy = uuid.Nil
  84. node.FailOverPeers = make(map[string]struct{})
  85. logic.UpsertNode(&node)
  86. }
  87. }
  88. go mq.PublishPeerUpdate()
  89. w.Header().Set("Content-Type", "application/json")
  90. logic.ReturnSuccessResponse(w, r, "failover has been reset successfully")
  91. }
  92. // swagger:route DELETE /api/v1/node/failover node deletefailOver
  93. //
  94. // Create a relay.
  95. //
  96. // Schemes: https
  97. //
  98. // Security:
  99. // oauth
  100. //
  101. // Responses:
  102. // 200: nodeResponse
  103. func deletefailOver(w http.ResponseWriter, r *http.Request) {
  104. var params = mux.Vars(r)
  105. nodeid := params["nodeid"]
  106. // confirm host exists
  107. node, err := logic.GetNodeByID(nodeid)
  108. if err != nil {
  109. slog.Error("failed to get node:", "error", err.Error())
  110. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
  111. return
  112. }
  113. node.IsFailOver = false
  114. // Reset FailOvered Peers
  115. err = logic.UpsertNode(&node)
  116. if err != nil {
  117. slog.Error("failed to upsert node", "node", node.ID.String(), "error", err)
  118. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
  119. return
  120. }
  121. go func() {
  122. proLogic.ResetFailOver(&node)
  123. mq.PublishPeerUpdate()
  124. }()
  125. w.Header().Set("Content-Type", "application/json")
  126. logic.ReturnSuccessResponseWithJson(w, r, node, "deleted failover successfully")
  127. }
  128. // swagger:route POST /api/node/{nodeid}/failOverME node failOver_me
  129. //
  130. // Create a relay.
  131. //
  132. // Schemes: https
  133. //
  134. // Security:
  135. // oauth
  136. //
  137. // Responses:
  138. // 200: nodeResponse
  139. func failOverME(w http.ResponseWriter, r *http.Request) {
  140. var params = mux.Vars(r)
  141. nodeid := params["nodeid"]
  142. // confirm host exists
  143. node, err := logic.GetNodeByID(nodeid)
  144. if err != nil {
  145. logger.Log(0, r.Header.Get("user"), "failed to get node:", err.Error())
  146. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
  147. return
  148. }
  149. failOverNode, exists := proLogic.FailOverExists(node.Network)
  150. if !exists {
  151. logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("failover node doesn't exist in the network"), "badrequest"))
  152. return
  153. }
  154. var failOverReq models.FailOverMeReq
  155. err = json.NewDecoder(r.Body).Decode(&failOverReq)
  156. if err != nil {
  157. logger.Log(0, r.Header.Get("user"), "error decoding request body: ", err.Error())
  158. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
  159. return
  160. }
  161. var sendPeerUpdate bool
  162. peerNode, err := logic.GetNodeByID(failOverReq.NodeID)
  163. if err != nil {
  164. slog.Error("peer not found: ", "nodeid", failOverReq.NodeID, "error", err)
  165. logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("peer not found"), "badrequest"))
  166. return
  167. }
  168. if node.IsRelayed || node.IsFailOver {
  169. logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("node is relayed or acting as failover"), "badrequest"))
  170. return
  171. }
  172. if peerNode.IsRelayed || peerNode.IsFailOver {
  173. logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("peer node is relayed or acting as failover"), "badrequest"))
  174. return
  175. }
  176. err = proLogic.SetFailOverCtx(failOverNode, node, peerNode)
  177. if err != nil {
  178. slog.Error("failed to create failover", "id", node.ID.String(),
  179. "network", node.Network, "error", err)
  180. logic.ReturnErrorResponse(w, r, logic.FormatError(fmt.Errorf("failed to create failover: %v", err), "internal"))
  181. return
  182. }
  183. slog.Info("[auto-relay] created relay on node", "node", node.ID.String(), "network", node.Network)
  184. sendPeerUpdate = true
  185. if sendPeerUpdate {
  186. go mq.PublishPeerUpdate()
  187. }
  188. w.Header().Set("Content-Type", "application/json")
  189. logic.ReturnSuccessResponse(w, r, "relayed successfully")
  190. }