Skip to content

Commit ae7385e

Browse files
committed
Read env vars for server name and env name
Signed-off-by: Richard Ogin <rogin@users.noreply.github.com>
1 parent 371f041 commit ae7385e

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

server/src/com/mirth/connect/server/Mirth.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.sql.DriverManager;
2020
import java.sql.SQLException;
2121
import java.util.ArrayList;
22-
import java.util.Calendar;
2322
import java.util.List;
2423
import java.util.Timer;
2524
import java.util.TimerTask;
@@ -29,10 +28,10 @@
2928
import org.apache.commons.io.IOUtils;
3029
import org.apache.commons.lang3.StringUtils;
3130
import org.apache.commons.lang3.math.NumberUtils;
32-
import org.apache.logging.log4j.ThreadContext;
3331
import org.apache.logging.log4j.Level;
3432
import org.apache.logging.log4j.LogManager;
3533
import org.apache.logging.log4j.Logger;
34+
import org.apache.logging.log4j.ThreadContext;
3635
import org.apache.logging.log4j.core.Appender;
3736
import org.apache.logging.log4j.core.filter.Filterable;
3837
import org.apache.velocity.runtime.RuntimeConstants;
@@ -345,9 +344,11 @@ public void startup() {
345344
((org.apache.logging.log4j.core.Logger) velocityLogger).setLevel(Level.OFF);
346345
}
347346

347+
configurationController.updateServerSettingsFromEnvironment();
348+
348349
eventController.dispatchEvent(new ServerEvent(configurationController.getServerId(), "Server startup"));
349350

350-
// Start web server before starting the engine in case there is a
351+
// Start web server before starting the engine in case there is a
351352
// problem starting the engine that causes it to hang
352353
startWebServer();
353354

server/src/com/mirth/connect/server/controllers/ConfigurationController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,9 @@ public Properties getPropertiesForGroup(String group) {
360360
public abstract void setChannelTags(Set<ChannelTag> tags);
361361

362362
public abstract Set<ChannelTag> getChannelTags();
363+
364+
/**
365+
* Update server settings based on environment variables.
366+
*/
367+
public abstract void updateServerSettingsFromEnvironment();
363368
}

server/src/com/mirth/connect/server/controllers/DefaultConfigurationController.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Locale;
4242
import java.util.Map;
4343
import java.util.Map.Entry;
44+
import java.util.Optional;
4445
import java.util.Properties;
4546
import java.util.Set;
4647
import java.util.SortedMap;
@@ -75,7 +76,6 @@
7576
import org.apache.ibatis.session.SqlSessionManager;
7677
import org.apache.logging.log4j.LogManager;
7778
import org.apache.logging.log4j.Logger;
78-
import org.bouncycastle.asn1.ASN1Sequence;
7979
import org.bouncycastle.asn1.x500.X500Name;
8080
import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier;
8181
import org.bouncycastle.asn1.x509.BasicConstraints;
@@ -1704,4 +1704,38 @@ public ConnectionTestResponse sendTestEmail(Properties properties) throws Except
17041704
return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, e.getMessage());
17051705
}
17061706
}
1707+
1708+
@Override
1709+
public void updateServerSettingsFromEnvironment() {
1710+
Optional<String> newServerName = getEnvironmentVariable("MC_SERVER_NAME");
1711+
Optional<String> newEnvName = getEnvironmentVariable("MC_ENV_NAME");
1712+
1713+
if (newServerName.isPresent() || newEnvName.isPresent()) {
1714+
try {
1715+
ServerSettings serverSettings = getServerSettings();
1716+
1717+
if (newServerName.isPresent()) {
1718+
serverSettings.setServerName(newServerName.get());
1719+
}
1720+
if (newEnvName.isPresent()) {
1721+
serverSettings.setEnvironmentName(newEnvName.get());
1722+
}
1723+
1724+
setServerSettings(serverSettings);
1725+
} catch (ControllerException e) {
1726+
logger.error("Failed to update server settings via environment variables", e);
1727+
}
1728+
}
1729+
}
1730+
1731+
/**
1732+
* Pull an environment variable. Values are trimmed, and only non-empty values are returned.
1733+
*
1734+
* @param envName the environment variable name
1735+
* @return the property's value
1736+
*/
1737+
private Optional<String> getEnvironmentVariable(String envName) {
1738+
String propValue = StringUtils.trimToEmpty(System.getenv(envName));
1739+
return StringUtils.isNotBlank(propValue) ? Optional.of(propValue) : Optional.empty();
1740+
}
17071741
}

0 commit comments

Comments
 (0)