From aa985cba21f3a4b51ba459ee70139d671f9027a9 Mon Sep 17 00:00:00 2001 From: Saetch Date: Thu, 27 Jun 2024 14:27:12 +0200 Subject: [PATCH] fixed getting hostname --- Cluster/Node_cs/ApiConfig.cs | 79 ++++++++++++++++-------------------- deploy/deploy-nodes.yaml | 4 +- 2 files changed, 38 insertions(+), 45 deletions(-) diff --git a/Cluster/Node_cs/ApiConfig.cs b/Cluster/Node_cs/ApiConfig.cs index 7722d2e..47dd86f 100644 --- a/Cluster/Node_cs/ApiConfig.cs +++ b/Cluster/Node_cs/ApiConfig.cs @@ -1,5 +1,6 @@ using System.Collections.Concurrent; +using System.Diagnostics; using System.Net; using System.Net.Sockets; using System.Text.Json.Serialization; @@ -15,54 +16,14 @@ public class ApiConfig public String hostname = Environment.GetEnvironmentVariable("HOSTNAME"); public String COORDINATOR_SERVICE_URL = "coordinator"; public int PORT = 8080; - public string PODENV = Environment.GetEnvironmentVariable("PODENV"); - public ushort ownerHash = 0; - Console.WriteLine("PODENV is: "+ this.PODENV); - - if (this.PODENV == "enabled"){ - try - { - Console.WriteLine("Attempting to get the pod IP address ..."); - // Start a new process to run the "hostname -i" command - ProcessStartInfo processStartInfo = new ProcessStartInfo() - { - FileName = "/bin/sh", - Arguments = "-c \"hostname -i\"", - RedirectStandardOutput = true, - UseShellExecute = false, - CreateNoWindow = true - }; - - using (Process process = new Process()) - { - process.StartInfo = processStartInfo; - process.Start(); - - // Read the output (which should be the pod IP address) - string podIP = process.StandardOutput.ReadToEnd().Trim(); - process.WaitForExit(); - - // Print or use the pod IP as needed - hostname = podIP; - Console.WriteLine("Pod IP: " + podIP); - - } - } - catch (Exception ex) - { - Console.WriteLine("Error: " + ex.Message); - } - } - - - - - + public String PODENV = Environment.GetEnvironmentVariable("PODENV"); + public ushort ownerHash = 0; public int initializeConfigValues() { Console.WriteLine("hostname is: "+ this.hostname); String env = Environment.GetEnvironmentVariable("CLUSTER_ENVIRONMENT"); + Console.WriteLine("CLUSTER_ENVIRONMENT is: "+ env); if (env == "local"){ Console.WriteLine("CLUSTER_ENVIRONMENT is: "+ env); this.COORDINATOR_SERVICE_URL = "localhost"; @@ -70,6 +31,38 @@ public int initializeConfigValues() this.BICUBIC_INTERPOLATION_SERVICE_URL = "http://localhost:5555/calculate"; this.hostname = "node1"; } + if (env == "pod"){ + try + { + // Start a new process to run the "hostname -i" command + ProcessStartInfo processStartInfo = new ProcessStartInfo() + { + FileName = "/bin/sh", + Arguments = "-c \"hostname -i\"", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using (Process process = new Process()) + { + process.StartInfo = processStartInfo; + process.Start(); + + // Read the output (which should be the pod IP address) + string podIP = process.StandardOutput.ReadToEnd().Trim(); + process.WaitForExit(); + + // Print or use the pod IP as needed + Console.WriteLine("Pod IP: " + podIP); + this.hostname = podIP; + } + } + catch (Exception ex) + { + Console.WriteLine("Error trying to execute hostname command: " + ex.Message); + } + } TcpClient tcpClient = new TcpClient(); var result = tcpClient.BeginConnect(this.COORDINATOR_SERVICE_URL, this.PORT, null, null); diff --git a/deploy/deploy-nodes.yaml b/deploy/deploy-nodes.yaml index e93027a..c10232d 100644 --- a/deploy/deploy-nodes.yaml +++ b/deploy/deploy-nodes.yaml @@ -29,6 +29,6 @@ spec: memory: "100Mi" cpu: "100m" env: - - name: PODENV - value: "enabled" + - name: CLUSTER_ENVIRONMENT + value: "pod"