From ae7385e6c7e671269811b39b6dec8e69f3a41d5f Mon Sep 17 00:00:00 2001 From: Richard Ogin Date: Fri, 18 Apr 2025 20:40:07 -0500 Subject: [PATCH] Read env vars for server name and env name Signed-off-by: Richard Ogin --- .../src/com/mirth/connect/server/Mirth.java | 7 ++-- .../controllers/ConfigurationController.java | 5 +++ .../DefaultConfigurationController.java | 36 ++++++++++++++++++- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/server/src/com/mirth/connect/server/Mirth.java b/server/src/com/mirth/connect/server/Mirth.java index 5e8cacfcf0..e8ad2a41a8 100644 --- a/server/src/com/mirth/connect/server/Mirth.java +++ b/server/src/com/mirth/connect/server/Mirth.java @@ -19,7 +19,6 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Calendar; import java.util.List; import java.util.Timer; import java.util.TimerTask; @@ -29,10 +28,10 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; -import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.filter.Filterable; import org.apache.velocity.runtime.RuntimeConstants; @@ -345,9 +344,11 @@ public void startup() { ((org.apache.logging.log4j.core.Logger) velocityLogger).setLevel(Level.OFF); } + configurationController.updateServerSettingsFromEnvironment(); + eventController.dispatchEvent(new ServerEvent(configurationController.getServerId(), "Server startup")); - // Start web server before starting the engine in case there is a + // Start web server before starting the engine in case there is a // problem starting the engine that causes it to hang startWebServer(); diff --git a/server/src/com/mirth/connect/server/controllers/ConfigurationController.java b/server/src/com/mirth/connect/server/controllers/ConfigurationController.java index e4a9ce88c3..dc72cd6a3e 100644 --- a/server/src/com/mirth/connect/server/controllers/ConfigurationController.java +++ b/server/src/com/mirth/connect/server/controllers/ConfigurationController.java @@ -360,4 +360,9 @@ public Properties getPropertiesForGroup(String group) { public abstract void setChannelTags(Set tags); public abstract Set getChannelTags(); + + /** + * Update server settings based on environment variables. + */ + public abstract void updateServerSettingsFromEnvironment(); } diff --git a/server/src/com/mirth/connect/server/controllers/DefaultConfigurationController.java b/server/src/com/mirth/connect/server/controllers/DefaultConfigurationController.java index 49e113d23e..950b2190e7 100644 --- a/server/src/com/mirth/connect/server/controllers/DefaultConfigurationController.java +++ b/server/src/com/mirth/connect/server/controllers/DefaultConfigurationController.java @@ -41,6 +41,7 @@ import java.util.Locale; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.Properties; import java.util.Set; import java.util.SortedMap; @@ -75,7 +76,6 @@ import org.apache.ibatis.session.SqlSessionManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.bouncycastle.asn1.ASN1Sequence; import org.bouncycastle.asn1.x500.X500Name; import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier; import org.bouncycastle.asn1.x509.BasicConstraints; @@ -1704,4 +1704,38 @@ public ConnectionTestResponse sendTestEmail(Properties properties) throws Except return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, e.getMessage()); } } + + @Override + public void updateServerSettingsFromEnvironment() { + Optional newServerName = getEnvironmentVariable("MC_SERVER_NAME"); + Optional newEnvName = getEnvironmentVariable("MC_ENV_NAME"); + + if (newServerName.isPresent() || newEnvName.isPresent()) { + try { + ServerSettings serverSettings = getServerSettings(); + + if (newServerName.isPresent()) { + serverSettings.setServerName(newServerName.get()); + } + if (newEnvName.isPresent()) { + serverSettings.setEnvironmentName(newEnvName.get()); + } + + setServerSettings(serverSettings); + } catch (ControllerException e) { + logger.error("Failed to update server settings via environment variables", e); + } + } + } + + /** + * Pull an environment variable. Values are trimmed, and only non-empty values are returned. + * + * @param envName the environment variable name + * @return the property's value + */ + private Optional getEnvironmentVariable(String envName) { + String propValue = StringUtils.trimToEmpty(System.getenv(envName)); + return StringUtils.isNotBlank(propValue) ? Optional.of(propValue) : Optional.empty(); + } }