From a86a3156fd4f85076ecfd4e080f29ceed33d657a Mon Sep 17 00:00:00 2001
From: karnem <mikhail.karnevskiy@desy.de>
Date: Tue, 28 Nov 2023 11:45:39 +0100
Subject: [PATCH] Enable standalone service from the other nodes in the
 notwork. Discovery substitutes service endpoints to the actual hostname if
 endpoint is equal to 0.0.0.0.

---
 deploy/asapo_services_light/discovery.json    | 10 +--
 .../src/asapo_discovery/common/structs.go     | 84 ++++++++++++++-----
 2 files changed, 66 insertions(+), 28 deletions(-)

diff --git a/deploy/asapo_services_light/discovery.json b/deploy/asapo_services_light/discovery.json
index d249f418f..858f06d68 100644
--- a/deploy/asapo_services_light/discovery.json
+++ b/deploy/asapo_services_light/discovery.json
@@ -2,19 +2,19 @@
   "Mode": "static",
   "Receiver": {
     "MaxConnections": 32,
-    "StaticEndpoints": [ "localhost:8411" ]
+    "StaticEndpoints": [ "0.0.0.0:8411" ]
   },
   "Broker": {
-    "StaticEndpoint": "localhost:8413"
+    "StaticEndpoint": "0.0.0.0:8413"
   },
   "Mongo": {
-    "StaticEndpoint": "localhost:27017"
+    "StaticEndpoint": "0.0.0.0:27017"
   },
   "FileTransferService": {
-    "StaticEndpoint": "localhost:8414"
+    "StaticEndpoint": "0.0.0.0:8414"
   },
   "Monitoring": {
-    "StaticEndpoint": "localhost:8420"
+    "StaticEndpoint": "0.0.0.0:8420"
   },
   "Port": 8410,
   "LogLevel": "info"
diff --git a/discovery/src/asapo_discovery/common/structs.go b/discovery/src/asapo_discovery/common/structs.go
index e9826cb8c..ecedb4f95 100644
--- a/discovery/src/asapo_discovery/common/structs.go
+++ b/discovery/src/asapo_discovery/common/structs.go
@@ -1,51 +1,89 @@
 package common
 
-import "errors"
+import (
+	log "asapo_common/logger"
+	"errors"
+	"os"
+	"strings"
+)
 
 type ReceiverInfo struct {
-	StaticEndpoints	 []string
-	MaxConnections   int
-	UseIBAddress bool
+	StaticEndpoints []string
+	MaxConnections  int
+	UseIBAddress    bool
 }
 
 type MonitoringInfo struct {
-	StaticEndpoint		 string
+	StaticEndpoint string
 }
 
 type BrokerInfo struct {
-	StaticEndpoint		 string
+	StaticEndpoint string
 }
 
 type MongoInfo struct {
-	StaticEndpoint		 string
+	StaticEndpoint string
 }
 
 type FtsInfo struct {
-	StaticEndpoint		 string
+	StaticEndpoint string
 }
 
 type Settings struct {
-	Receiver 		ReceiverInfo
-	Broker 		    BrokerInfo
-	Monitoring		MonitoringInfo
-	Mongo 			MongoInfo
-	FileTransferService  FtsInfo
-	ConsulEndpoints []string
-	Mode			string
-	Kubernetes struct {
-		Mode string
+	Receiver            ReceiverInfo
+	Broker              BrokerInfo
+	Monitoring          MonitoringInfo
+	Mongo               MongoInfo
+	FileTransferService FtsInfo
+	ConsulEndpoints     []string
+	Mode                string
+	Kubernetes          struct {
+		Mode       string
 		ConfigFile string
-		Namespace string
+		Namespace  string
 	}
-	Port            int
-	LogLevel        string
+	Port     int
+	LogLevel string
+}
+
+func (settings *Settings) setCurrentServer() error {
+	hostname, err := os.Hostname()
+	if err != nil {
+		return err
+	}
+
+	if settings.Receiver.StaticEndpoints[0] > "0.0.0.0" {
+		settings.Receiver.StaticEndpoints[0] = strings.Replace(settings.Receiver.StaticEndpoints[0], "0.0.0.0", hostname, 1)
+		log.Info("Replace Receiver server to " + hostname)
+	}
+	if settings.Broker.StaticEndpoint > "0.0.0.0" {
+		settings.Broker.StaticEndpoint = strings.Replace(settings.Broker.StaticEndpoint, "0.0.0.0", hostname, 1)
+		log.Info("Replace Broker server to " + hostname)
+	}
+	if settings.Monitoring.StaticEndpoint > "0.0.0.0" {
+		settings.Monitoring.StaticEndpoint = strings.Replace(settings.Monitoring.StaticEndpoint, "0.0.0.0", hostname, 1)
+		log.Info("Replace Monitoring server to " + hostname)
+	}
+	if settings.Mongo.StaticEndpoint > "0.0.0.0" {
+		settings.Mongo.StaticEndpoint = strings.Replace(settings.Mongo.StaticEndpoint, "0.0.0.0", hostname, 1)
+		log.Info("Replace Mongo server to " + hostname)
+	}
+	if settings.FileTransferService.StaticEndpoint > "0.0.0.0" {
+		settings.FileTransferService.StaticEndpoint = strings.Replace(settings.FileTransferService.StaticEndpoint, "0.0.0.0", hostname, 1)
+		log.Info("Replace FileTransferService server to " + hostname)
+	}
+	return nil
 }
 
 func (settings *Settings) Validate() error {
-	if settings.Mode == "static"{
-		if len(settings.Receiver.StaticEndpoints) == 0 || len(settings.Broker.StaticEndpoint) == 0 || len(settings.Mongo.StaticEndpoint) == 0{
+	if settings.Mode == "static" {
+		if len(settings.Receiver.StaticEndpoints) == 0 || len(settings.Broker.StaticEndpoint) == 0 || len(settings.Mongo.StaticEndpoint) == 0 {
 			return errors.New("static endpoints not set")
 		}
+		err := settings.setCurrentServer()
+		if err != nil {
+			return errors.New("unable to get server name")
+		}
 	}
 
 	if settings.Receiver.MaxConnections == 0 {
@@ -61,7 +99,7 @@ func (settings *Settings) Validate() error {
 	}
 
 	if settings.Mode != "static" && settings.Mode != "consul" && settings.Mode != "kubernetes" {
-		return errors.New("wrong mode: "  + settings.Mode+ ", (allowed static|consul|kubernetes)")
+		return errors.New("wrong mode: " + settings.Mode + ", (allowed static|consul|kubernetes)")
 	}
 
 	return nil
-- 
GitLab