From 2cefd9b8a040bea71454b958b76920b20c8501b2 Mon Sep 17 00:00:00 2001
From: Rudi Schlatte <rudi@ifi.uio.no>
Date: Wed, 15 May 2024 12:05:13 +0200
Subject: [PATCH] Set more environment variables in defineCluster

Set ONM_IP, ONM_URL, BROKER_ADDRESS, BROKER_PORT, ACTIVEMQ_HOST,
ACTIVEMQ_PORT, based on the values of APP_ACTIVEMQ_HOST (or
--app-activemq-host parameter), AP_ACTIVEMQ_PORT (or --app-activemq-port
parameter), ONM_IP (or --onm-ip parameter), ONM_URL (or --onm-url parameter).

See https://bugs.launchpad.net/nebulous/+bug/2062521

Change-Id: Iad3614ba0aeeeab48f809e4f7b185474039905f3
---
 .../templates/deployment.yaml                 |  8 ++++++
 .../nebulous-optimiser-controller/values.yaml |  6 ++++
 .../optimiser/controller/Main.java            | 28 +++++++++++++++++++
 .../controller/NebulousAppDeployer.java       | 28 +++++++++++++++++--
 4 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/charts/nebulous-optimiser-controller/templates/deployment.yaml b/charts/nebulous-optimiser-controller/templates/deployment.yaml
index 5b0b80e..e808ecb 100644
--- a/charts/nebulous-optimiser-controller/templates/deployment.yaml
+++ b/charts/nebulous-optimiser-controller/templates/deployment.yaml
@@ -51,6 +51,14 @@ spec:
                 secretKeyRef:
                   name: {{ include "nebulous-optimiser-controller.fullname" . }}-secrets
                   key: ACTIVEMQ_PASSWORD
+            - name: APP_ACTIVEMQ_HOST
+              value: "{{ .Values.app.ACTIVEMQ_HOST }}"
+            - name: APP_ACTIVEMQ_PORT
+              value: "{{ .Values.app.ACTIVEMQ_PORT }}"
+            - name: ONM_IP
+              value: "{{ .Values.app.ONM_IP }}"
+            - name: ONM_URL
+              value: "{{ .Values.app.ONM_URL }}"
           # livenessProbe:
           #   httpGet:
           #     path: /
diff --git a/charts/nebulous-optimiser-controller/values.yaml b/charts/nebulous-optimiser-controller/values.yaml
index e759449..46f2ed0 100644
--- a/charts/nebulous-optimiser-controller/values.yaml
+++ b/charts/nebulous-optimiser-controller/values.yaml
@@ -89,5 +89,11 @@ activemq:
   ACTIVEMQ_PORT: 5672
   ACTIVEMQ_USER: admin
 
+app:
+  ONM_IP: '123'
+  ONM_URL: onm-url''
+  ACTIVEMQ_HOST: 'nebulous-activemq'
+  ACTIVEMQ_PORT: '131'
+
 secrets:
   ACTIVEMQ_PASSWORD: nebulous
diff --git a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java
index ad340b6..bca4957 100644
--- a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java
+++ b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java
@@ -69,6 +69,34 @@ public class Main implements Callable<Integer> {
     @Getter
     private static Path logDirectory;
 
+    @Option(names = {"--app-activemq-host"},
+        description = "The hostname of the ActiveMQ server in a deployed app.  Can also be set via the @|bold APP_ACTIVEMQ_HOST|@ environment variable.",
+        paramLabel = "APP_ACTIVEMQ_HOST",
+        defaultValue = "${APP_ACTIVEMQ_HOST}")
+    @Getter
+    private static String appBrokerAddress;
+
+    @Option(names = {"--app-activemq-port"},
+        description = "The port of the ActiveMQ server in a deployed app.  Can also be set via the @|bold APP_ACTIVEMQ_PORT|@ environment variable.",
+        paramLabel = "APP_ACTIVEMQ_PORT",
+        defaultValue = "${APP_ACTIVEMQ_PORT:-5672}")
+    @Getter
+    private static int appBrokerPort;
+
+    @Option(names = {"--onm-ip"},
+        description = "The IP address of the ONM server in a deployed app.  Can also be set via the @|bold ONM_IP|@ environment variable.  NOTE: will be deprecated soon.",
+        paramLabel = "ONM_IP",
+        defaultValue = "${ONM_IP}")
+    @Getter
+    private static String onmIp;
+
+    @Option(names = {"--onm-url"},
+        description = "The URL of the ONM server in a deployed app.  Can also be set via the @|bold ONM_URL|@ environment variable.",
+        paramLabel = "ONM_URL",
+        defaultValue = "${ONM_URL}")
+    @Getter
+    private static String onmUrl;
+
     @Option(names = {"--verbose", "-v"},
             description = "Turn on more verbose logging output. Can be given multiple times. When not given, print only warnings and error messages. With @|underline -v|@, print status messages. With @|underline -vvv|@, print everything.",
         scope = ScopeType.INHERIT)
diff --git a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/NebulousAppDeployer.java b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/NebulousAppDeployer.java
index 01b0420..d2e2b0e 100644
--- a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/NebulousAppDeployer.java
+++ b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/NebulousAppDeployer.java
@@ -405,10 +405,32 @@ public class NebulousAppDeployer {
                     .put("cloudId", candidate.getCloud().getId());
             });
         ObjectNode environment = cluster.withObject("/env-var");
+        // See https://openproject.nebulouscloud.eu/projects/nebulous-collaboration-hub/wiki/env-variables-necessary-for-nebulous-application-deployment-scripts
         environment.put("APPLICATION_ID", appUUID);
-        // TODO: consider pre-parsing environment variables and storing them
-        // in the app object instead of reading them from the raw dsl message
-        // here
+        if (Main.getAppBrokerAddress() == null || Main.getAppBrokerAddress().equals("")) {
+            log.warn("ActiveMQ broker address for app (APP_ACTIVEMQ_HOST) is not set, optimistically continuing with 'localhost'");
+            environment.put("BROKER_ADDRESS", "localhost");
+            environment.put("ACTIVEMQ_HOST", "localhost");
+        } else {
+            environment.put("BROKER_ADDRESS", Main.getAppBrokerAddress());
+            environment.put("ACTIVEMQ_HOST", Main.getAppBrokerAddress());
+        }
+        // Don't warn when those are unset, 5672 is usually the right call
+        environment.put("BROKER_PORT", Main.getAppBrokerPort());
+        environment.put("ACTIVEMQ_PORT", Main.getAppBrokerPort());
+        if (Main.getOnmIp() == null || Main.getOnmIp().equals("")) {
+            log.warn("Overlay Network Manager address (ONM_IP) is not set, continuing without setting ONM_IP for the app");
+        } else {
+            environment.put("ONM_IP", Main.getOnmIp());
+        }
+        if (Main.getOnmUrl() == null || Main.getOnmUrl().equals("")) {
+            log.warn("Overlay Network Manager address (ONM_URL) is not set, continuing without setting ONM_URL for the app");
+        } else {
+            environment.put("ONM_URL", Main.getOnmUrl());
+        }
+        // TODO: consider pre-parsing environment variables from the app
+        // message and storing them in the app object instead of reading them
+        // from the raw JSON here -- but it's not that important
         for (final JsonNode v : app.getOriginalAppMessage().withArray("/environmentVariables")) {
             if (v.has("name") && v.has("value") && v.get("name").isTextual()) {
                 // TODO: figure out what to do with the `"secret":true` field