Skip to content

Commit 369dcba

Browse files
authored
feat: Set application name via command line with the .msi installer (#2648)
* Initial code * InnerText, not Value * Working solution for setting app pool naming * Cleanup
1 parent 0ae54fa commit 369dcba

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/Agent/MsiInstaller/Installer/Product.wxs

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ SPDX-License-Identifier: Apache-2.0
172172
<CustomAction Id="SaveDeferredCustomActionData" DllEntry="SaveDeferredCustomActionData" Execute="immediate" Return="check" BinaryRef="CustomActionsDll" />
173173
<CustomAction Id="MigrateConfiguration" DllEntry="MigrateConfiguration" Execute="deferred" Impersonate="no" Return="check" BinaryRef="CustomActionsDll" />
174174
<CustomAction Id="SetLicenseKey" DllEntry="SetLicenseKey" Execute="deferred" Impersonate="no" Return="check" BinaryRef="CustomActionsDll" />
175+
<CustomAction Id="SetAppName" DllEntry="SetAppName" Execute="deferred" Impersonate="no" Return="check" BinaryRef="CustomActionsDll" />
175176
<CustomAction Id="CleanupPreviousInstall" DllEntry="CleanupPreviousInstall" Execute="deferred" Impersonate="no" Return="check" BinaryRef="CustomActionsDll" />
176177
<CustomAction Id="RestartIis" DllEntry="RestartIis" Impersonate="yes" BinaryRef="CustomActionsDll" />
177178
<CustomAction Id="CloseStatusMonitor" DllEntry="CloseStatusMonitor" Impersonate="yes" BinaryRef="CustomActionsDll" />

src/Agent/MsiInstaller/InstallerActions/CustomActions.cs

+81
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static ActionResult SaveDeferredCustomActionData(Session session)
3535
var customActionData = new CustomActionData
3636
{
3737
{ "NR_LICENSE_KEY", session["NR_LICENSE_KEY"] },
38+
{ "NR_APP_NAME", session["NR_APP_NAME"] },
39+
{ "NR_USE_APP_POOL_NAMING", session["NR_USE_APP_POOL_NAMING"] },
3840
{ "NETAGENTCOMMONFOLDER", session["NETAGENTCOMMONFOLDER"] },
3941
{ "LOGSFOLDER", session["LOGSFOLDER"] },
4042
{ "NETAGENTFOLDER", session["NETAGENTFOLDER"] },
@@ -43,6 +45,7 @@ public static ActionResult SaveDeferredCustomActionData(Session session)
4345

4446
session.DoAction("MigrateConfiguration", customActionData);
4547
session.DoAction("SetLicenseKey", customActionData);
48+
session.DoAction("SetAppName", customActionData);
4649
session.DoAction("CleanupPreviousInstall", customActionData);
4750

4851
return ActionResult.Success;
@@ -111,6 +114,84 @@ public static ActionResult SetLicenseKey(Session session)
111114
return ActionResult.Success;
112115
}
113116

117+
[CustomAction]
118+
public static ActionResult SetAppName(Session session)
119+
{
120+
try
121+
{
122+
var path = session.CustomActionData["NETAGENTCOMMONFOLDER"] + @"\newrelic.config";
123+
124+
// appName will be an empty string if the property wasn't set via the command line,
125+
// OR if the property was set with an empty string (NR_APP_NAME="")
126+
var appName = session.CustomActionData["NR_APP_NAME"];
127+
var useAppPoolNaming = session.CustomActionData["NR_USE_APP_POOL_NAMING"].Equals(bool.TrueString, StringComparison.CurrentCultureIgnoreCase)
128+
|| session.CustomActionData["NR_USE_APP_POOL_NAMING"].Equals("1");
129+
130+
session.Log($"SetAppName: appName={appName}, useAppPoolNaming={useAppPoolNaming}");
131+
if (string.IsNullOrEmpty(appName) && !useAppPoolNaming)
132+
{
133+
session.Log("NR_APP_NAME was not set or was set to an empty string, and NR_USE_APP_POOL_NAMING was not set or was not set to true. Application name not modified.");
134+
return ActionResult.Success;
135+
}
136+
137+
var document = new XmlDocument();
138+
document.Load(path);
139+
var namespaceManager = new XmlNamespaceManager(document.NameTable);
140+
namespaceManager.AddNamespace("newrelic-config", "urn:newrelic-config");
141+
142+
var applicationNode = document.SelectSingleNode("/newrelic-config:configuration/newrelic-config:application", namespaceManager);
143+
if (applicationNode == null)
144+
{
145+
session.Log("Unable to locate /configuration/application node in newrelic.config. Application name not set.");
146+
return ActionResult.Success;
147+
}
148+
149+
// NR_USE_APP_POOL_NAMING takes precedence over anything set in NR_APP_NAME
150+
if (useAppPoolNaming)
151+
{
152+
session.Log("NR_APP_NAME = UseAppPoolNaming, deleting name nodes from application element");
153+
var childNodes = applicationNode.ChildNodes;
154+
foreach (XmlNode childNode in childNodes)
155+
{
156+
session.Log($"found child node with name {childNode.Name}");
157+
if (childNode.Name.Equals("name"))
158+
{
159+
session.Log("Deleting 'name' node");
160+
applicationNode.RemoveChild(childNode);
161+
}
162+
}
163+
document.Save(session.CustomActionData["NETAGENTCOMMONFOLDER"] + @"\newrelic.config");
164+
session.Log("newrelic.config saved with application name elements removed.");
165+
return ActionResult.Success;
166+
}
167+
168+
// If we get here, NR_USE_APP_POOL_NAMING was not set or not set to true,
169+
// and NR_APP_NAME has a non-empty value. Set the app name to NR_APP_NAME.
170+
// If no <name>appName</name> node exists, create it
171+
XmlNode applicationNameNode;
172+
applicationNameNode = document.SelectSingleNode("/newrelic-config:configuration/newrelic-config:application/newrelic-config:name", namespaceManager);
173+
if (applicationNameNode == null)
174+
{
175+
session.Log("Unable to locate existing /configuration/application/name node in newrelic.config, creating a new one.");
176+
var newNameNode = document.CreateElement("name", "urn:newrelic-config");
177+
applicationNameNode = applicationNode.AppendChild(newNameNode);
178+
}
179+
session.Log("/configuration/application/name node found or created in newrelic.config");
180+
181+
applicationNameNode.InnerText = appName;
182+
session.Log("Application name set to " + appName);
183+
184+
document.Save(session.CustomActionData["NETAGENTCOMMONFOLDER"] + @"\newrelic.config");
185+
session.Log("newrelic.config saved with updated application name.");
186+
}
187+
catch (Exception exception)
188+
{
189+
session.Log("Exception while attempting to set the application name in newrelic.config.\n{0}", exception);
190+
}
191+
192+
return ActionResult.Success;
193+
}
194+
114195
[CustomAction]
115196
public static ActionResult CleanupPreviousInstall(Session session)
116197
{

0 commit comments

Comments
 (0)