2022-02-17 04:40:51 +08:00
package mq
import (
"encoding/json"
2022-09-14 03:25:56 +08:00
"errors"
2022-02-17 04:40:51 +08:00
"fmt"
2022-09-14 03:25:56 +08:00
"time"
2022-02-17 04:40:51 +08:00
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/servercfg"
2023-11-08 21:43:41 +08:00
"golang.org/x/exp/slog"
2022-02-17 04:40:51 +08:00
)
2023-01-06 23:57:40 +08:00
// PublishPeerUpdate --- determines and publishes a peer update to all the hosts
2024-01-11 18:29:19 +08:00
func PublishPeerUpdate ( replacePeers bool ) error {
2022-02-17 04:40:51 +08:00
if ! servercfg . IsMessageQueueBackend ( ) {
return nil
}
2023-01-04 12:35:05 +08:00
2023-01-06 19:28:41 +08:00
hosts , err := logic . GetAllHosts ( )
2022-02-17 04:40:51 +08:00
if err != nil {
2023-01-04 12:35:05 +08:00
logger . Log ( 1 , "err getting all hosts" , err . Error ( ) )
2022-02-17 04:40:51 +08:00
return err
}
2023-06-28 23:35:21 +08:00
allNodes , err := logic . GetAllNodes ( )
if err != nil {
return err
}
2023-01-04 12:35:05 +08:00
for _ , host := range hosts {
2023-01-26 02:21:18 +08:00
host := host
2024-01-16 01:47:36 +08:00
go func ( host models . Host ) {
if err = PublishSingleHostPeerUpdate ( & host , allNodes , nil , nil , replacePeers ) ; err != nil {
logger . Log ( 1 , "failed to publish peer update to host" , host . ID . String ( ) , ": " , err . Error ( ) )
}
} ( host )
2022-02-17 04:40:51 +08:00
}
2022-09-30 02:49:44 +08:00
return err
}
2023-02-24 23:37:11 +08:00
// PublishDeletedNodePeerUpdate --- determines and publishes a peer update
// to all the hosts with a deleted node to account for
func PublishDeletedNodePeerUpdate ( delNode * models . Node ) error {
if ! servercfg . IsMessageQueueBackend ( ) {
return nil
}
2022-11-20 12:05:29 +08:00
2023-02-24 23:37:11 +08:00
hosts , err := logic . GetAllHosts ( )
if err != nil {
logger . Log ( 1 , "err getting all hosts" , err . Error ( ) )
return err
}
2023-06-28 23:35:21 +08:00
allNodes , err := logic . GetAllNodes ( )
if err != nil {
return err
}
2023-02-24 23:37:11 +08:00
for _ , host := range hosts {
host := host
2024-01-11 18:29:19 +08:00
if err = PublishSingleHostPeerUpdate ( & host , allNodes , delNode , nil , false ) ; err != nil {
2023-03-14 03:06:07 +08:00
logger . Log ( 1 , "failed to publish peer update to host" , host . ID . String ( ) , ": " , err . Error ( ) )
}
}
return err
}
// PublishDeletedClientPeerUpdate --- determines and publishes a peer update
// to all the hosts with a deleted ext client to account for
func PublishDeletedClientPeerUpdate ( delClient * models . ExtClient ) error {
if ! servercfg . IsMessageQueueBackend ( ) {
return nil
}
hosts , err := logic . GetAllHosts ( )
if err != nil {
logger . Log ( 1 , "err getting all hosts" , err . Error ( ) )
return err
}
2023-06-28 23:35:21 +08:00
nodes , err := logic . GetAllNodes ( )
if err != nil {
return err
}
2023-03-14 03:06:07 +08:00
for _ , host := range hosts {
host := host
2023-08-25 16:53:01 +08:00
if host . OS != models . OS_Types . IoT {
2024-01-11 18:29:19 +08:00
if err = PublishSingleHostPeerUpdate ( & host , nodes , nil , [ ] models . ExtClient { * delClient } , false ) ; err != nil {
2023-08-25 16:53:01 +08:00
logger . Log ( 1 , "failed to publish peer update to host" , host . ID . String ( ) , ": " , err . Error ( ) )
}
2023-02-24 23:37:11 +08:00
}
}
return err
}
2023-02-23 23:55:42 +08:00
// PublishSingleHostPeerUpdate --- determines and publishes a peer update to one host
2024-01-11 18:29:19 +08:00
func PublishSingleHostPeerUpdate ( host * models . Host , allNodes [ ] models . Node , deletedNode * models . Node , deletedClients [ ] models . ExtClient , replacePeers bool ) error {
2022-11-20 12:05:29 +08:00
2023-06-29 02:47:42 +08:00
peerUpdate , err := logic . GetPeerUpdateForHost ( "" , host , allNodes , deletedNode , deletedClients )
2022-09-30 02:49:44 +08:00
if err != nil {
return err
}
2024-01-11 18:29:19 +08:00
peerUpdate . ReplacePeers = replacePeers
2022-09-30 02:49:44 +08:00
data , err := json . Marshal ( & peerUpdate )
if err != nil {
return err
}
2023-01-04 13:33:13 +08:00
return publish ( host , fmt . Sprintf ( "peers/host/%s/%s" , host . ID . String ( ) , servercfg . GetServer ( ) ) , data )
2022-02-17 04:40:51 +08:00
}
// NodeUpdate -- publishes a node update
2022-12-21 04:29:09 +08:00
func NodeUpdate ( node * models . Node ) error {
2022-12-24 02:41:17 +08:00
host , err := logic . GetHost ( node . HostID . String ( ) )
2022-12-22 19:42:33 +08:00
if err != nil {
return nil
}
2022-12-21 04:29:09 +08:00
if ! servercfg . IsMessageQueueBackend ( ) {
2022-02-17 04:40:51 +08:00
return nil
}
2022-12-21 04:29:09 +08:00
logger . Log ( 3 , "publishing node update to " + node . ID . String ( ) )
2022-09-02 16:52:28 +08:00
2022-12-21 04:29:09 +08:00
//if len(node.NetworkSettings.AccessKeys) > 0 {
//node.NetworkSettings.AccessKeys = []models.AccessKey{} // not to be sent (don't need to spread access keys around the network; we need to know how to reach other nodes, not become them)
//}
2022-09-02 16:52:28 +08:00
2022-12-02 20:05:41 +08:00
data , err := json . Marshal ( node )
if err != nil {
logger . Log ( 2 , "error marshalling node update " , err . Error ( ) )
return err
}
2023-02-15 02:00:08 +08:00
if err = publish ( host , fmt . Sprintf ( "node/update/%s/%s" , node . Network , node . ID ) , data ) ; err != nil {
2022-12-21 04:29:09 +08:00
logger . Log ( 2 , "error publishing node update to peer " , node . ID . String ( ) , err . Error ( ) )
2022-12-02 20:05:41 +08:00
return err
}
2022-11-20 12:05:29 +08:00
2022-02-17 04:40:51 +08:00
return nil
}
2023-01-17 13:47:13 +08:00
// HostUpdate -- publishes a host update to clients
func HostUpdate ( hostUpdate * models . HostUpdate ) error {
2023-01-11 23:33:43 +08:00
if ! servercfg . IsMessageQueueBackend ( ) {
return nil
}
2023-01-17 13:47:13 +08:00
logger . Log ( 3 , "publishing host update to " + hostUpdate . Host . ID . String ( ) )
2023-01-11 23:33:43 +08:00
2023-01-17 13:47:13 +08:00
data , err := json . Marshal ( hostUpdate )
2023-01-11 23:33:43 +08:00
if err != nil {
logger . Log ( 2 , "error marshalling node update " , err . Error ( ) )
return err
}
2023-01-17 13:47:13 +08:00
if err = publish ( & hostUpdate . Host , fmt . Sprintf ( "host/update/%s/%s" , hostUpdate . Host . ID . String ( ) , servercfg . GetServer ( ) ) , data ) ; err != nil {
logger . Log ( 2 , "error publishing host update to" , hostUpdate . Host . ID . String ( ) , err . Error ( ) )
2023-01-11 23:33:43 +08:00
return err
}
return nil
}
2022-04-25 23:12:49 +08:00
// ServerStartNotify - notifies all non server nodes to pull changes after a restart
func ServerStartNotify ( ) error {
nodes , err := logic . GetAllNodes ( )
2022-09-14 03:25:56 +08:00
if err != nil {
2022-04-25 23:12:49 +08:00
return err
}
for i := range nodes {
nodes [ i ] . Action = models . NODE_FORCE_UPDATE
if err = NodeUpdate ( & nodes [ i ] ) ; err != nil {
2022-12-21 04:29:09 +08:00
logger . Log ( 1 , "error when notifying node" , nodes [ i ] . ID . String ( ) , "of a server startup" )
2022-04-25 23:12:49 +08:00
}
2022-09-14 03:25:56 +08:00
}
2022-04-25 23:12:49 +08:00
return nil
}
2022-09-14 03:25:56 +08:00
2023-11-08 21:43:41 +08:00
// PublishMqUpdatesForDeletedNode - published all the required updates for deleted node
func PublishMqUpdatesForDeletedNode ( node models . Node , sendNodeUpdate bool , gwClients [ ] models . ExtClient ) {
// notify of peer change
node . PendingDelete = true
node . Action = models . NODE_DELETE
if sendNodeUpdate {
if err := NodeUpdate ( & node ) ; err != nil {
slog . Error ( "error publishing node update to node" , "node" , node . ID , "error" , err )
}
}
if err := PublishDeletedNodePeerUpdate ( & node ) ; err != nil {
logger . Log ( 1 , "error publishing peer update " , err . Error ( ) )
}
2023-12-20 14:24:53 +08:00
if servercfg . IsDNSMode ( ) {
logic . SetDNS ( )
2023-11-08 21:43:41 +08:00
}
2023-02-07 03:50:29 +08:00
}
2022-09-14 03:25:56 +08:00
2023-09-01 10:12:05 +08:00
func PushMetricsToExporter ( metrics models . Metrics ) error {
2022-09-14 03:25:56 +08:00
logger . Log ( 2 , "----> Pushing metrics to exporter" )
data , err := json . Marshal ( metrics )
if err != nil {
return errors . New ( "failed to marshal metrics: " + err . Error ( ) )
}
2022-09-30 21:01:57 +08:00
if token := mqclient . Publish ( "metrics_exporter" , 2 , true , data ) ; ! token . WaitTimeout ( MQ_TIMEOUT * time . Second ) || token . Error ( ) != nil {
2022-09-14 03:25:56 +08:00
var err error
if token . Error ( ) == nil {
err = errors . New ( "connection timeout" )
} else {
err = token . Error ( )
}
return err
}
return nil
}
2023-02-15 02:29:20 +08:00
// sendPeers - retrieve networks, send peer ports to all peers
func sendPeers ( ) {
hosts , err := logic . GetAllHosts ( )
2023-02-28 03:52:50 +08:00
if err != nil && len ( hosts ) > 0 {
2023-02-15 02:29:20 +08:00
logger . Log ( 1 , "error retrieving networks for keepalive" , err . Error ( ) )
}
2023-06-28 23:35:21 +08:00
nodes , err := logic . GetAllNodes ( )
if err != nil {
return
}
2023-02-15 02:29:20 +08:00
var force bool
peer_force_send ++
if peer_force_send == 5 {
servercfg . SetHost ( )
force = true
peer_force_send = 0
err := logic . TimerCheckpoint ( ) // run telemetry & log dumps if 24 hours has passed..
if err != nil {
logger . Log ( 3 , "error occurred on timer," , err . Error ( ) )
}
//collectServerMetrics(networks[:])
}
2023-03-07 01:21:51 +08:00
if force {
for _ , host := range hosts {
2023-02-15 02:29:20 +08:00
host := host
logger . Log ( 2 , "sending scheduled peer update (5 min)" )
2024-01-11 18:29:19 +08:00
if err = PublishSingleHostPeerUpdate ( & host , nodes , nil , nil , false ) ; err != nil {
2023-02-15 02:29:20 +08:00
logger . Log ( 1 , "error publishing peer updates for host: " , host . ID . String ( ) , " Err: " , err . Error ( ) )
}
}
}
}