mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 16:57:51 +08:00
feat(NET-449): add sync feature to request a host pull from server (#2491)
This commit is contained in:
@@ -13,12 +13,14 @@ import (
|
|||||||
"github.com/gravitl/netmaker/mq"
|
"github.com/gravitl/netmaker/mq"
|
||||||
"github.com/gravitl/netmaker/servercfg"
|
"github.com/gravitl/netmaker/servercfg"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"golang.org/x/exp/slog"
|
||||||
)
|
)
|
||||||
|
|
||||||
func hostHandlers(r *mux.Router) {
|
func hostHandlers(r *mux.Router) {
|
||||||
r.HandleFunc("/api/hosts", logic.SecurityCheck(false, http.HandlerFunc(getHosts))).Methods(http.MethodGet)
|
r.HandleFunc("/api/hosts", logic.SecurityCheck(false, http.HandlerFunc(getHosts))).Methods(http.MethodGet)
|
||||||
r.HandleFunc("/api/hosts/keys", logic.SecurityCheck(true, http.HandlerFunc(updateAllKeys))).Methods(http.MethodPut)
|
r.HandleFunc("/api/hosts/keys", logic.SecurityCheck(true, http.HandlerFunc(updateAllKeys))).Methods(http.MethodPut)
|
||||||
r.HandleFunc("/api/hosts/{hostid}/keys", logic.SecurityCheck(true, http.HandlerFunc(updateKeys))).Methods(http.MethodPut)
|
r.HandleFunc("/api/hosts/{hostid}/keys", logic.SecurityCheck(true, http.HandlerFunc(updateKeys))).Methods(http.MethodPut)
|
||||||
|
r.HandleFunc("/api/hosts/{hostid}/sync", logic.SecurityCheck(true, http.HandlerFunc(syncHost))).Methods(http.MethodPost)
|
||||||
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(updateHost))).Methods(http.MethodPut)
|
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(updateHost))).Methods(http.MethodPut)
|
||||||
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(deleteHost))).Methods(http.MethodDelete)
|
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(deleteHost))).Methods(http.MethodDelete)
|
||||||
r.HandleFunc("/api/hosts/{hostid}/networks/{network}", logic.SecurityCheck(true, http.HandlerFunc(addHostToNetwork))).Methods(http.MethodPost)
|
r.HandleFunc("/api/hosts/{hostid}/networks/{network}", logic.SecurityCheck(true, http.HandlerFunc(addHostToNetwork))).Methods(http.MethodPost)
|
||||||
@@ -583,3 +585,43 @@ func updateKeys(w http.ResponseWriter, r *http.Request) {
|
|||||||
logger.Log(2, r.Header.Get("user"), "updated key on host", host.Name)
|
logger.Log(2, r.Header.Get("user"), "updated key on host", host.Name)
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// swagger:route POST /api/hosts/{hostId}/sync host syncHost
|
||||||
|
//
|
||||||
|
// Requests a host to pull.
|
||||||
|
//
|
||||||
|
// Schemes: https
|
||||||
|
//
|
||||||
|
// Security:
|
||||||
|
// oauth
|
||||||
|
//
|
||||||
|
// Responses:
|
||||||
|
// 200: networkBodyResponse
|
||||||
|
func syncHost(w http.ResponseWriter, r *http.Request) {
|
||||||
|
hostId := mux.Vars(r)["hostid"]
|
||||||
|
|
||||||
|
var errorResponse = models.ErrorResponse{}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
host, err := logic.GetHost(hostId)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to retrieve host", "user", r.Header.Get("user"), "error", err)
|
||||||
|
errorResponse.Code = http.StatusBadRequest
|
||||||
|
errorResponse.Message = err.Error()
|
||||||
|
logic.ReturnErrorResponse(w, r, errorResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
hostUpdate := models.HostUpdate{
|
||||||
|
Action: models.RequestPull,
|
||||||
|
Host: *host,
|
||||||
|
}
|
||||||
|
if err = mq.HostUpdate(&hostUpdate); err != nil {
|
||||||
|
slog.Error("failed to send host pull request", "host", host.ID.String(), "error", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
slog.Info("requested host pull", "user", r.Header.Get("user"), "host", host.ID)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
@@ -107,6 +107,8 @@ const (
|
|||||||
RegisterWithTurn = "REGISTER_WITH_TURN"
|
RegisterWithTurn = "REGISTER_WITH_TURN"
|
||||||
// UpdateKeys - update wireguard private/public keys
|
// UpdateKeys - update wireguard private/public keys
|
||||||
UpdateKeys = "UPDATE_KEYS"
|
UpdateKeys = "UPDATE_KEYS"
|
||||||
|
// RequestPull - request a pull from a host
|
||||||
|
RequestPull = "REQ_PULL"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SignalAction - turn peer signal action
|
// SignalAction - turn peer signal action
|
||||||
|
Reference in New Issue
Block a user