This commit is contained in:
Matthew R. Kasun 2022-02-16 19:50:10 -05:00 committed by 0xdcarns
parent 407e46c117
commit f1b5518bf3
2 changed files with 53 additions and 47 deletions

View file

@ -2,6 +2,7 @@ package controller
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
@ -585,7 +586,18 @@ func updateNode(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(newNode)
runUpdates(&newNode, true, ifaceDelta)
if isServer(&node) {
if ifaceDelta {
if err := mq.PublishPeerUpdate(&node); err != nil {
logger.Log(1, "failed to publish peer update "+err.Error())
}
}
if err := logic.UpdateNode(&node, &newNode); err != nil {
logger.Log(1, "error updating server node "+err.Error())
}
} else {
runUpdates(&newNode)
}
}
func deleteNode(w http.ResponseWriter, r *http.Request) {
@ -620,23 +632,18 @@ func deleteNode(w http.ResponseWriter, r *http.Request) {
returnSuccessResponse(w, r, nodeid+" deleted.")
logger.Log(1, r.Header.Get("user"), "Deleted node", nodeid, "from network", params["network"])
//:w
runUpdates(&node, false)
}
func runUpdates(node *models.Node, nodeUpdate bool) error {
func runUpdates(node *models.Node) error {
//don't publish to server node
if nodeUpdate && !isServer(node) {
if err := mq.NodeUpdate(node); err != nil {
logger.Log(1, "error publishing node update", err.Error())
return err
}
if isServer(node) {
return errors.New("update to server node not permited")
}
if err := runServerUpdate(node, isServer(node)); err != nil {
logger.Log(1, "internal error when running peer node:", err.Error())
if err := mq.NodeUpdate(node); err != nil {
logger.Log(1, "error publishing node update", err.Error())
return err
}
return nil
}

View file

@ -13,7 +13,6 @@ import (
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/mq"
"github.com/gravitl/netmaker/servercfg"
"github.com/gravitl/netmaker/serverctl"
)
@ -107,7 +106,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object)
Type: nodepb.NODE_TYPE,
}
runUpdates(&node, false, false)
//runUpdates(&node, false, false)
go func(node *models.Node) {
if node.UDPHolePunch == "yes" {
@ -134,47 +133,47 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object)
}
// NodeServiceServer.UpdateNode updates a node and responds over gRPC
func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
// func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) {
var newnode models.Node
if err := json.Unmarshal([]byte(req.GetData()), &newnode); err != nil {
return nil, err
}
// var newnode models.Node
// if err := json.Unmarshal([]byte(req.GetData()), &newnode); err != nil {
// return nil, err
// }
node, err := logic.GetNodeByID(newnode.ID)
if err != nil {
return nil, err
}
// node, err := logic.GetNodeByID(newnode.ID)
// if err != nil {
// return nil, err
// }
ifaceDelta := logic.IfaceDelta(&node, &newnode)
// ifaceDelta := logic.IfaceDelta(&node, &newnode)
if !servercfg.GetRce() {
newnode.PostDown = node.PostDown
newnode.PostUp = node.PostUp
}
// if !servercfg.GetRce() {
// newnode.PostDown = node.PostDown
// newnode.PostUp = node.PostUp
// }
err = logic.UpdateNode(&node, &newnode)
if err != nil {
return nil, err
}
newnode.NetworkSettings, err = logic.GetNetworkSettings(node.Network)
if err != nil {
return nil, err
}
getServerAddrs(&newnode)
// err = logic.UpdateNode(&node, &newnode)
// if err != nil {
// return nil, err
// }
// newnode.NetworkSettings, err = logic.GetNetworkSettings(node.Network)
// if err != nil {
// return nil, err
// }
// getServerAddrs(&newnode)
nodeData, errN := json.Marshal(&newnode)
if errN != nil {
return nil, err
}
// nodeData, errN := json.Marshal(&newnode)
// if errN != nil {
// return nil, err
// }
runUpdates(&newnode, false, ifaceDelta)
// runUpdates(&newnode, false, ifaceDelta)
return &nodepb.Object{
Data: string(nodeData),
Type: nodepb.NODE_TYPE,
}, nil
}
// return &nodepb.Object{
// Data: string(nodeData),
// Type: nodepb.NODE_TYPE,
// }, nil
// }
func getServerAddrs(node *models.Node) {
serverNodes := logic.GetServerNodes(node.Network)