소스 검색

Add ServerName

Matthew R. Kasun 3 년 전
부모
커밋
9010ba19ff
8개의 변경된 파일65개의 추가작업 그리고 76개의 파일을 삭제
  1. 1 1
      config/config.go
  2. 1 1
      logic/accesskeys.go
  3. 1 1
      models/accessToken.go
  4. 0 1
      models/node.go
  5. 2 2
      netclient/config/config.go
  6. 38 59
      netclient/functions/daemon.go
  7. 2 2
      netclient/functions/mqpublish.go
  8. 20 9
      servercfg/serverconf.go

+ 1 - 1
config/config.go

@@ -72,7 +72,7 @@ type ServerConfig struct {
 	PortForwardServices   string `yaml:"portforwardservices"`
 	HostNetwork           string `yaml:"hostnetwork"`
 	MQPort                string `yaml:"mqport"`
-	MQEndPoint            string `yaml:"mqendpoint"`
+	ServerName            string `yaml:"servername"`
 }
 
 // SQLConfig - Generic SQL Config

+ 1 - 1
logic/accesskeys.go

@@ -56,7 +56,7 @@ func CreateAccessKey(accesskey models.AccessKey, network models.Network) (models
 	servervals := models.ServerConfig{
 		GRPCConnString: s.GRPCConnString,
 		GRPCSSL:        s.GRPCSSL,
-		MQEndpoint:     s.MQEndPoint,
+		ServerName:     s.ServerName,
 	}
 	accessToken.ServerConfig = servervals
 	accessToken.ClientConfig.Network = netID

+ 1 - 1
models/accessToken.go

@@ -14,5 +14,5 @@ type ClientConfig struct {
 type ServerConfig struct {
 	GRPCConnString string `json:"grpcconn"`
 	GRPCSSL        string `json:"grpcssl"`
-	MQEndpoint     string `json:"mqendpoint"`
+	ServerName     string `json:"servername"`
 }

+ 0 - 1
models/node.go

@@ -81,7 +81,6 @@ type Node struct {
 	Version      string      `json:"version" bson:"version" yaml:"version"`
 	CommID       string      `json:"commid" bson:"commid" yaml:"comid"`
 	TrafficKeys  TrafficKeys `json:"traffickeys" bson:"traffickeys" yaml:"traffickeys"`
-	MQEndPoint   string      `json:"mqendpont" yaml:"mqendpoint"`
 }
 
 // NodesArray - used for node sorting

+ 2 - 2
netclient/config/config.go

@@ -33,7 +33,7 @@ type ServerConfig struct {
 	GRPCAddress string `yaml:"grpcaddress"`
 	AccessKey   string `yaml:"accesskey"`
 	GRPCSSL     string `yaml:"grpcssl"`
-	MQEndPoint  string `yaml:"MQEndpoint"`
+	ServerName  string `yaml:"servername"`
 }
 
 // Write - writes the config of a client to disk
@@ -188,7 +188,7 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) {
 		cfg.Server.AccessKey = accesstoken.ClientConfig.Key
 		cfg.Node.LocalRange = accesstoken.ClientConfig.LocalRange
 		cfg.Server.GRPCSSL = accesstoken.ServerConfig.GRPCSSL
-		cfg.Server.MQEndPoint = accesstoken.ServerConfig.MQEndpoint
+		cfg.Server.ServerName = accesstoken.ServerConfig.ServerName
 		if c.String("grpcserver") != "" {
 			cfg.Server.GRPCAddress = c.String("grpcserver")
 		}

+ 38 - 59
netclient/functions/daemon.go

@@ -24,6 +24,7 @@ import (
 	"github.com/gravitl/netmaker/netclient/daemon"
 	"github.com/gravitl/netmaker/netclient/ncutils"
 	"github.com/gravitl/netmaker/netclient/wireguard"
+	"github.com/gravitl/netmaker/servercfg"
 	"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
 )
 
@@ -43,30 +44,24 @@ func Daemon() error {
 	logger.Log(0, "starting daemon")
 	// == start mq for each server ==
 	// == add waitgroup and cancel for checkin routine ==
-	//ctx, cancel := context.WithCancel(context.Background())
+	ctx, cancel := context.WithCancel(context.Background())
 	wg := sync.WaitGroup{}
-	//networks, _ := ncutils.GetSystemNetworks()
-	//	for _, network := range networks {
-	//		//temporary code --- remove in version v0.13.0
-	//		removeHostDNS(network, ncutils.IsWindows())
-	//		// end of code to be removed in version v0.13.0
-	//		var cfg config.ClientConfig
-	//		cfg.Network = network
-	//		cfg.ReadConfig()
-	//		// == initial pull of all networks ==
-	//		initialPull(cfg.Network)
-	//		logger.Log(1, "started mq for server  ", cfg.NetworkSettings.NetID)
-	//		wg.Add(1)
-	//		go messageQueue(ctx, wg, &cfg)
-	//	}
-	//wg.Add(1)
-	//go Checkin(ctx, &wg)
-	var cfg config.ClientConfig
-	cfg.Network = "netmaker"
-	cfg.ReadConfig()
-	//Hello(&cfg)
-	client := SetupMQTT(&cfg, false)
-	defer client.Disconnect(500)
+	networks, _ := ncutils.GetSystemNetworks()
+	for _, network := range networks {
+		//temporary code --- remove in version v0.13.0
+		removeHostDNS(network, ncutils.IsWindows())
+		// end of code to be removed in version v0.13.0
+		var cfg config.ClientConfig
+		cfg.Network = network
+		cfg.ReadConfig()
+		// == initial pull of all networks ==
+		initialPull(cfg.Network)
+		logger.Log(1, "started mq for server  ", cfg.NetworkSettings.NetID)
+		wg.Add(1)
+		go messageQueue(ctx, wg, &cfg)
+	}
+	wg.Add(1)
+	go Checkin(ctx, &wg)
 	quit := make(chan os.Signal, 1)
 	signal.Notify(quit, syscall.SIGTERM, os.Interrupt, os.Kill)
 	<-quit
@@ -75,7 +70,7 @@ func Daemon() error {
 	//		cancel.(context.CancelFunc)()
 	//	}
 	//}
-	//cancel()
+	cancel()
 	logger.Log(0, "shutting down netclient daemon")
 	wg.Wait()
 	logger.Log(0, "shutdown complete")
@@ -122,13 +117,6 @@ func PingServer(nodeCfg *config.ClientConfig) error {
 }
 
 // == Private ==
-func setTempSubs(client mqtt.Client) {
-	if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
-		logger.Log(0, token.Error().Error())
-		return
-	}
-	logger.Log(0, "subscribed to all topics for debugging purposes")
-}
 
 // sets MQ client subscriptions for a specific node config
 // should be called for each node belonging to a given comms network
@@ -177,22 +165,20 @@ func messageQueue(ctx context.Context, wg sync.WaitGroup, cfg *config.ClientConf
 	//var commsCfg config.ClientConfig
 	//commsCfg.Network = commsNet
 	//commsCfg.ReadConfig()
-	logger.Log(0, "netclient daemon started for server: ", cfg.Server.MQEndPoint)
-	client := SetupMQTT(cfg, false)
+	logger.Log(0, "netclient daemon started for server: ", cfg.Server.ServerName)
+	client := setupMQTT(cfg, false)
 	defer client.Disconnect(250)
 	<-ctx.Done()
-	logger.Log(0, "shutting down daemon for server ", cfg.Server.MQEndPoint)
+	logger.Log(0, "shutting down daemon for server ", cfg.Server.ServerName)
 	wg.Done()
 }
 
-// SetupMQTT creates a connection to broker and return client
+// setupMQTT creates a connection to broker and return client
 // utilizes comms client configs to setup connections
-func SetupMQTT(cfg *config.ClientConfig, publish bool) mqtt.Client {
+func setupMQTT(cfg *config.ClientConfig, publish bool) mqtt.Client {
 	opts := mqtt.NewClientOptions()
-	//server := getServerAddress(commsCfg)
-	opts.AddBroker(cfg.Server.MQEndPoint) // TODO get the appropriate port of the comms mq server
-	//opts.ClientID = ncutils.MakeRandomString(23) // helps avoid id duplication on broker
-	logger.Log(0, "added broker ", cfg.Server.MQEndPoint)
+	opts.AddBroker(servercfg.GetMessageQueueEndpoint(true))
+	logger.Log(2, "added broker ", cfg.Server.ServerName)
 	opts.SetDefaultPublishHandler(All)
 	opts.SetAutoReconnect(true)
 	opts.SetConnectRetry(true)
@@ -202,17 +188,16 @@ func SetupMQTT(cfg *config.ClientConfig, publish bool) mqtt.Client {
 	opts.SetOnConnectHandler(func(client mqtt.Client) {
 		logger.Log(0, "Connection Handler")
 		if !publish {
-			//networks, err := ncutils.GetSystemNetworks()
-			//if err != nil {
-			//	logger.Log(0, "error retriving networks ", err.Error())
-			//}
-			//for _, network := range networks {
-			//	var currNodeCfg config.ClientConfig
-			//	currNodeCfg.Network = network
-			//	currNodeCfg.ReadConfig()
-			//	setSubscriptions(client, &currNodeCfg)
-			//}
-			setTempSubs(client)
+			networks, err := ncutils.GetSystemNetworks()
+			if err != nil {
+				logger.Log(0, "error retriving networks ", err.Error())
+			}
+			for _, network := range networks {
+				var currNodeCfg config.ClientConfig
+				currNodeCfg.Network = network
+				currNodeCfg.ReadConfig()
+				setSubscriptions(client, &currNodeCfg)
+			}
 		}
 	})
 	opts.SetOrderMatters(true)
@@ -241,13 +226,7 @@ func SetupMQTT(cfg *config.ClientConfig, publish bool) mqtt.Client {
 			}
 			time.Sleep(time.Second)
 		}
-		if token := client.Connect(); !token.WaitTimeout(time.Second*2) && token.Error() != nil {
-			//token := client.Connect()
-			//if done := token.WaitTimeout(time.Second * 2); !done {
-			//	logger.Log(0, "mq client connect timeout")
-			//}
-			//err := token.Error()
-			//if err != nil {
+		if token := client.Connect(); token.Wait() && token.Error() != nil {
 			logger.Log(0, token.Error().Error())
 			logger.Log(0, "unable to connect to broker, retrying ...")
 			if time.Now().After(tperiod) {
@@ -366,7 +345,7 @@ func getServers(networks []string) (map[string]bool, error) {
 	for _, network := range networks {
 		cfg.Network = network
 		cfg.ReadConfig()
-		response[cfg.Node.MQEndPoint] = true
+		response[cfg.Server.ServerName] = true
 	}
 	return response, nil
 }

+ 2 - 2
netclient/functions/mqpublish.go

@@ -75,7 +75,7 @@ func Checkin(ctx context.Context, wg *sync.WaitGroup) {
 					}
 				}
 				if err := PingServer(&nodeCfg); err != nil {
-					logger.Log(0, "could not ping server  ", nodeCfg.Server.MQEndPoint, "\n", err.Error())
+					logger.Log(0, "could not ping server  ", nodeCfg.Server.ServerName, "\n", err.Error())
 				} else {
 					Hello(&nodeCfg)
 				}
@@ -130,7 +130,7 @@ func publish(nodeCfg *config.ClientConfig, dest string, msg []byte, qos byte) er
 		return err
 	}
 	logger.Log(0, "calling setup MQ ", nodeCfg.NetworkSettings.NetID)
-	client := SetupMQTT(nodeCfg, true)
+	client := setupMQTT(nodeCfg, true)
 	defer client.Disconnect(250)
 	//encrypted, err := ncutils.Chunk(msg, serverPubKey, trafficPrivKey)
 	//if err != nil {

+ 20 - 9
servercfg/serverconf.go

@@ -96,7 +96,7 @@ func GetServerConfig() config.ServerConfig {
 	cfg.ManageIPTables = ManageIPTables()
 	services := strings.Join(GetPortForwardServiceList(), ",")
 	cfg.PortForwardServices = services
-	cfg.MQEndPoint = GetMessageQueueEndpoint(true)
+	cfg.ServerName = GetServerName()
 
 	return cfg
 }
@@ -259,19 +259,30 @@ func GetMQPort() string {
 	return mqport
 }
 
+// GetServerName - gets FQDN of server
+func GetServerName() string {
+	name := ""
+	if os.Getenv("DOMAIN_NAME") != "" {
+		name = os.Getenv("DOMAIN_NAME")
+	} else if config.Config.Server.ServerName != "" {
+		name = config.Config.Server.ServerName
+	}
+	return name
+}
+
 // GetMessageQueueEndpoint - gets the message queue endpoint
 func GetMessageQueueEndpoint(ssl bool) string {
 	if ssl {
-		host, _ := GetPublicIP()
-		if os.Getenv("MQ_HOST") != "" {
-			host = os.Getenv("MQ_HOST")
-		} else if config.Config.Server.MQHOST != "" {
-			host = config.Config.Server.MQHOST
+		endpoint := ""
+		if os.Getenv("SERVER_NAME") != "" {
+			endpoint = "ssl://" + os.Getenv("SERVER_NAME") + ":8883"
+		} else if config.Config.Server.ServerName != "" {
+			endpoint = "ssl://" + config.Config.Server.ServerName + ":8883"
 		}
-		return host + ":8883"
+		return endpoint
 	}
-	//Do we want MQ port configurable???
-	return "127.0.0.1:1883"
+	// only the netmaker node should use unsecure endpoint
+	return "tcl://127.0.0.1:1883"
 }
 
 // GetMasterKey - gets the configured master key of server