Skip to content

Commit 1411583

Browse files
committed
Read env vars for server name and env name
Signed-off-by: Richard Ogin <[email protected]>
1 parent 8175b47 commit 1411583

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;
@@ -28,10 +27,10 @@
2827
import org.apache.commons.io.IOUtils;
2928
import org.apache.commons.lang3.StringUtils;
3029
import org.apache.commons.lang3.math.NumberUtils;
31-
import org.apache.logging.log4j.ThreadContext;
3230
import org.apache.logging.log4j.Level;
3331
import org.apache.logging.log4j.LogManager;
3432
import org.apache.logging.log4j.Logger;
33+
import org.apache.logging.log4j.ThreadContext;
3534
import org.apache.logging.log4j.core.Appender;
3635
import org.apache.logging.log4j.core.filter.Filterable;
3736
import org.apache.velocity.runtime.RuntimeConstants;
@@ -344,9 +343,11 @@ public void startup() {
344343
((org.apache.logging.log4j.core.Logger) velocityLogger).setLevel(Level.OFF);
345344
}
346345

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

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

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;
@@ -74,7 +75,6 @@
7475
import org.apache.ibatis.session.SqlSessionManager;
7576
import org.apache.logging.log4j.LogManager;
7677
import org.apache.logging.log4j.Logger;
77-
import org.bouncycastle.asn1.ASN1Sequence;
7878
import org.bouncycastle.asn1.x500.X500Name;
7979
import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier;
8080
import org.bouncycastle.asn1.x509.BasicConstraints;
@@ -1696,4 +1696,38 @@ public ConnectionTestResponse sendTestEmail(Properties properties) throws Except
16961696
return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, e.getMessage());
16971697
}
16981698
}
1699+
1700+
@Override
1701+
public void updateServerSettingsFromEnvironment() {
1702+
Optional<String> newServerName = getEnvironmentVariable("MC_SERVER_NAME");
1703+
Optional<String> newEnvName = getEnvironmentVariable("MC_ENV_NAME");
1704+
1705+
if (newServerName.isPresent() || newEnvName.isPresent()) {
1706+
try {
1707+
ServerSettings serverSettings = getServerSettings();
1708+
1709+
if (newServerName.isPresent()) {
1710+
serverSettings.setServerName(newServerName.get());
1711+
}
1712+
if (newEnvName.isPresent()) {
1713+
serverSettings.setEnvironmentName(newEnvName.get());
1714+
}
1715+
1716+
setServerSettings(serverSettings);
1717+
} catch (ControllerException e) {
1718+
logger.error("Failed to update server settings via environment variables", e);
1719+
}
1720+
}
1721+
}
1722+
1723+
/**
1724+
* Pull an environment variable. Values are trimmed, and only non-empty values are returned.
1725+
*
1726+
* @param envName the environment variable name
1727+
* @return the property's value
1728+
*/
1729+
private Optional<String> getEnvironmentVariable(String envName) {
1730+
String propValue = StringUtils.trimToEmpty(System.getenv(envName));
1731+
return StringUtils.isNotBlank(propValue) ? Optional.of(propValue) : Optional.empty();
1732+
}
16991733
}

0 commit comments

Comments
 (0)