Skip to content

Commit e173551

Browse files
author
Steve Salas
committed
Add Code Pulse instance name and trace port config
- Uses the CODE_PULSE_INSTANCE_NAME environment variable to support running multiple copies of Code Pulse. An instance name can consist of alphanumeric characters, dashes, and underscores. Names can be no longer than 50 characters. - Enables a tracing port override by using the CODE_PULSE_TRACE_PORT environment variable. - Fixes an issue that can block the configuration to override the symbol service port using the SYMBOL_SERVICE_PORT environment variable.
1 parent e1940e1 commit e173551

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

codepulse/src/main/resources/application.conf

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cp {
22
userSettings {
33
# The port used to listen for agent connections
44
tracePort = 8765,
5+
tracePort=${?CODE_PULSE_TRACE_PORT}
56
symbolService {
67
port = "49582"
78
port = ${?SYMBOL_SERVICE_PORT}

codepulse/src/main/scala/com/secdec/codepulse/package.scala

+17-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ package object codepulse {
6262
lazy val dateRaw = versionInfo.releaseDate
6363
}
6464

65+
val maxInstanceNameEnvLength = 50
66+
val instanceNameEnvVar = "CODE_PULSE_INSTANCE_NAME"
67+
val instanceName: String = sys.env.get(instanceNameEnvVar).getOrElse("").replaceAll("[^A-Za-z0-9_-]", "_").take(maxInstanceNameEnvLength)
68+
6569
object paths {
66-
val appData = ApplicationData.getApplicationDataFolder("Code Dx", "Code Pulse", "codepulse")
67-
val localAppData = ApplicationData.getLocalApplicationDataFolder("Code Dx", "Code Pulse", "codepulse")
70+
71+
val appData = ApplicationData.getApplicationDataFolder("Code Dx", "Code Pulse", "codepulse", codepulse.instanceName)
72+
val localAppData = ApplicationData.getLocalApplicationDataFolder("Code Dx", "Code Pulse", "codepulse", codepulse.instanceName)
6873
val logFiles = appData / "log-files"
6974

7075
logFiles.mkdirs
@@ -75,6 +80,7 @@ package object codepulse {
7580
private var config =
7681
ConfigFactory
7782
.parseFile(configFile)
83+
.resolve()
7884
.withFallback(ConfigFactory.load())
7985
.withOnlyPath("cp.userSettings") // do not return systemSettings, which could get persisted to disk later on
8086

@@ -135,8 +141,16 @@ package object codepulse {
135141
val renderOptions = ConfigRenderOptions.defaults.setJson(false).setOriginComments(false)
136142
val output = config.root.render(renderOptions)
137143

144+
// preserve option to override tracePort via an environment variable
145+
val tracePortPattern = """(tracePort="?\d+"?)""".r
146+
val outputWithTracePort = tracePortPattern.replaceFirstIn(output, "$1\n" + " " * 12 + "tracePort=\\${?CODE_PULSE_TRACE_PORT}")
147+
148+
// preserve option to override port for symbol service via an environment variable
149+
val symbolServicePortPattern = """(port="?\d+"?)""".r
150+
val outputWithServicePortPattern = symbolServicePortPattern.replaceFirstIn(outputWithTracePort, "$1\n" + " " * 16 + "port=\\${?SYMBOL_SERVICE_PORT}")
151+
138152
val writer = new FileWriter(configFile)
139-
writer.write(output)
153+
writer.write(outputWithServicePortPattern)
140154
writer.close()
141155
}
142156
}

codepulse/src/main/scala/com/secdec/codepulse/util/ApplicationData.scala

+29-20
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,43 @@ import java.io.File
2727
object ApplicationData {
2828
import Implicits._
2929

30-
private def getAppData(companyName: String, appName: String, appShortName: String, local: Boolean) = OperatingSystem.current match {
31-
case OperatingSystem.Windows =>
32-
val appData = if (local) WindowsHelper.localAppDataFolder else WindowsHelper.appDataFolder
33-
if (appData == null)
34-
throw new IllegalStateException("Could not find application data folder")
30+
private def getAppData(companyName: String, appName: String, appShortName: String, local: Boolean, instanceName: String) = {
3531

36-
new File(appData) / companyName / appName
32+
val appDataFolder = OperatingSystem.current match {
33+
case OperatingSystem.Windows =>
34+
val appData = if (local) WindowsHelper.localAppDataFolder else WindowsHelper.appDataFolder
35+
if (appData == null)
36+
throw new IllegalStateException("Could not find application data folder")
3737

38-
case OperatingSystem.OSX =>
39-
val home = System.getProperty("user.home")
40-
if (home == null)
41-
throw new IllegalStateException("Could not find user home folder")
38+
new File(appData) / companyName / appName
4239

43-
new File(home) / "Library" / "Application Support" / appName
40+
case OperatingSystem.OSX =>
41+
val home = System.getProperty("user.home")
42+
if (home == null)
43+
throw new IllegalStateException("Could not find user home folder")
4444

45-
case OperatingSystem.Linux | OperatingSystem.Unix =>
46-
val home = System.getProperty("user.home")
47-
if (home == null)
48-
throw new IllegalStateException("Could not find user home folder")
45+
new File(home) / "Library" / "Application Support" / appName
4946

50-
new File(home) / s".$appShortName"
47+
case OperatingSystem.Linux | OperatingSystem.Unix =>
48+
val home = System.getProperty("user.home")
49+
if (home == null)
50+
throw new IllegalStateException("Could not find user home folder")
5151

52-
case _ =>
53-
throw new NotImplementedError
52+
new File(home) / s".$appShortName"
53+
54+
case _ =>
55+
throw new NotImplementedError
56+
}
57+
58+
if (instanceName.isEmpty()) {
59+
appDataFolder
60+
} else {
61+
new File(appDataFolder.getPath, instanceName)
62+
}
5463
}
5564

56-
def getApplicationDataFolder(companyName: String, appName: String, appShortName: String) = getAppData(companyName, appName, appShortName, false)
57-
def getLocalApplicationDataFolder(companyName: String, appName: String, appShortName: String) = getAppData(companyName, appName, appShortName, true)
65+
def getApplicationDataFolder(companyName: String, appName: String, appShortName: String, instanceName: String) = getAppData(companyName, appName, appShortName, false, instanceName)
66+
def getLocalApplicationDataFolder(companyName: String, appName: String, appShortName: String, instanceName: String) = getAppData(companyName, appName, appShortName, true, instanceName)
5867

5968
/** Internal helper for Windows to query for appdata folder the proper way */
6069
private object WindowsHelper {

0 commit comments

Comments
 (0)