Browse Source

Merge pull request #1946 from gravitl/GRA-985/host_updates_logic

Gra 985/host updates logic
dcarns 2 years ago
parent
commit
bb942cb936
2 changed files with 47 additions and 2 deletions
  1. 7 1
      controllers/hosts.go
  2. 40 1
      mq/handlers.go

+ 7 - 1
controllers/hosts.go

@@ -108,7 +108,13 @@ func updateHost(w http.ResponseWriter, r *http.Request) {
 			logger.Log(0, r.Header.Get("user"), "failed to update host networks roles in DynSec:", err.Error())
 		}
 	}
-	// TODO: publish host update through MQ
+	// publish host update through MQ
+	if err := mq.HostUpdate(&models.HostUpdate{
+		Action: models.UpdateHost,
+		Host:   *newHost,
+	}); err != nil {
+		logger.Log(0, r.Header.Get("user"), "failed to send host update: ", currHost.ID.String(), err.Error())
+	}
 	go func() {
 		if err := mq.PublishPeerUpdate(); err != nil {
 			logger.Log(0, "fail to publish peer update: ", err.Error())

+ 40 - 1
mq/handlers.go

@@ -142,13 +142,25 @@ func UpdateHost(client mqtt.Client, msg mqtt.Message) {
 			return
 		}
 		logger.Log(0, "recieved host update for host: ", id)
+		var sendPeerUpdate bool
 		switch hostUpdate.Action {
 		case models.UpdateHost:
-			// TODO: logic to update host recieved from client
+			sendPeerUpdate = updateHostFromClient(&hostUpdate.Host, currentHost)
+			err := logic.UpsertHost(currentHost)
+			if err != nil {
+				logger.Log(0, "failed to update host: ", currentHost.ID.String(), err.Error())
+				return
+			}
 		case models.DeleteHost:
 			// TODO: logic to delete host on the server
 
 		}
+		if sendPeerUpdate {
+			err := PublishPeerUpdate()
+			if err != nil {
+				logger.Log(0, "failed to pulish peer update: ", err.Error())
+			}
+		}
 		// if servercfg.Is_EE && ifaceDelta {
 		// 	if err = logic.EnterpriseResetAllPeersFailovers(currentHost.ID.String(), currentHost.Network); err != nil {
 		// 		logger.Log(1, "failed to reset failover list during node update", currentHost.ID.String(), currentHost.Network)
@@ -158,6 +170,33 @@ func UpdateHost(client mqtt.Client, msg mqtt.Message) {
 	}(msg)
 }
 
+// used for updating host on server with update recieved from client
+func updateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool) {
+
+	if newHost.ListenPort != 0 && currHost.ListenPort != newHost.ListenPort {
+		currHost.ListenPort = newHost.ListenPort
+		sendPeerUpdate = true
+	}
+	if newHost.ProxyListenPort != 0 && currHost.ProxyListenPort != newHost.ProxyListenPort {
+		currHost.ProxyListenPort = newHost.ProxyListenPort
+		sendPeerUpdate = true
+	}
+	if currHost.ProxyEnabled != newHost.ProxyEnabled {
+		currHost.ProxyEnabled = newHost.ProxyEnabled
+		sendPeerUpdate = true
+	}
+	if currHost.EndpointIP.String() != newHost.EndpointIP.String() {
+		currHost.EndpointIP = newHost.EndpointIP
+		sendPeerUpdate = true
+	}
+	currHost.DaemonInstalled = newHost.DaemonInstalled
+	currHost.Debug = newHost.Debug
+	currHost.Verbosity = newHost.Verbosity
+	currHost.Version = newHost.Version
+	currHost.Name = newHost.Name
+	return
+}
+
 // UpdateMetrics  message Handler -- handles updates from client nodes for metrics
 func UpdateMetrics(client mqtt.Client, msg mqtt.Message) {
 	if servercfg.Is_EE {