Skip to content

Commit 7312db1

Browse files
committed
Add environment variables for Launcher path and args in ANCM
1 parent e00a7c4 commit 7312db1

File tree

4 files changed

+95
-13
lines changed

4 files changed

+95
-13
lines changed

src/Servers/IIS/AspNetCoreModuleV2/CommonLib/ConfigurationSection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#define CS_ASPNETCORE_DETAILEDERRORS L"ASPNETCORE_DETAILEDERRORS"
3434
#define CS_ASPNETCORE_ENVIRONMENT L"ASPNETCORE_ENVIRONMENT"
3535
#define CS_DOTNET_ENVIRONMENT L"DOTNET_ENVIRONMENT"
36+
#define CS_ANCM_LAUNCHER_PATH L"ANCM_LAUNCHER_PATH"
37+
#define CS_ANCM_LAUNCHER_ARGS L"ANCM_LAUNCHER_ARGS"
3638

3739
class ConfigurationSection: NonCopyable
3840
{

src/Servers/IIS/AspNetCoreModuleV2/InProcessRequestHandler/InProcessOptions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "InProcessOptions.h"
55
#include "InvalidOperationException.h"
66
#include "EventLog.h"
7+
#include "Environment.h"
78

89
HRESULT InProcessOptions::Create(
910
IHttpServer& pServer,
@@ -51,6 +52,11 @@ InProcessOptions::InProcessOptions(const ConfigurationSource &configurationSourc
5152
auto const aspNetCoreSection = configurationSource.GetRequiredSection(CS_ASPNETCORE_SECTION);
5253
m_strArguments = aspNetCoreSection->GetString(CS_ASPNETCORE_PROCESS_ARGUMENTS).value_or(CS_ASPNETCORE_PROCESS_ARGUMENTS_DEFAULT);
5354
m_strProcessPath = aspNetCoreSection->GetRequiredString(CS_ASPNETCORE_PROCESS_EXE_PATH);
55+
// We prefer the environment variables for LAUNCHER_PATH and LAUNCHER_ARGS
56+
m_strProcessPath = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_PATH)
57+
.value_or(m_strProcessPath);
58+
m_strArguments = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_ARGS)
59+
.value_or(m_strArguments);
5460
m_fStdoutLogEnabled = aspNetCoreSection->GetRequiredBool(CS_ASPNETCORE_STDOUT_LOG_ENABLED);
5561
m_struStdoutLogFile = aspNetCoreSection->GetRequiredString(CS_ASPNETCORE_STDOUT_LOG_FILE);
5662
m_fDisableStartUpErrorPage = aspNetCoreSection->GetRequiredBool(CS_ASPNETCORE_DISABLE_START_UP_ERROR_PAGE);

src/Servers/IIS/AspNetCoreModuleV2/RequestHandlerLib/requesthandler_config.cpp

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "environmentvariablehash.h"
88
#include "exceptions.h"
99
#include "config_utility.h"
10+
#include "Environment.h"
1011

1112
REQUESTHANDLER_CONFIG::~REQUESTHANDLER_CONFIG()
1213
{
@@ -101,6 +102,8 @@ REQUESTHANDLER_CONFIG::Populate(
101102
BSTR bstrBasicAuthSection = NULL;
102103
BSTR bstrAnonymousAuthSection = NULL;
103104
BSTR bstrAspNetCoreSection = NULL;
105+
auto launcherPathEnv = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_PATH);
106+
auto launcherArgsEnv = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_ARGS);
104107

105108
pAdminManager = pHttpServer->GetAdminManager();
106109
try
@@ -248,12 +251,43 @@ REQUESTHANDLER_CONFIG::Populate(
248251
goto Finished;
249252
}
250253

251-
hr = GetElementStringProperty(pAspNetCoreElement,
252-
CS_ASPNETCORE_PROCESS_EXE_PATH,
253-
&m_struProcessPath);
254-
if (FAILED(hr))
254+
// We prefer the environment variables for LAUNCHER_PATH and LAUNCHER_ARGS
255+
if (launcherPathEnv.has_value())
255256
{
256-
goto Finished;
257+
hr = m_struProcessPath.Copy(launcherPathEnv.value().c_str());
258+
if (FAILED(hr))
259+
{
260+
goto Finished;
261+
}
262+
}
263+
else
264+
{
265+
hr = GetElementStringProperty(pAspNetCoreElement,
266+
CS_ASPNETCORE_PROCESS_EXE_PATH,
267+
&m_struProcessPath);
268+
if (FAILED(hr))
269+
{
270+
goto Finished;
271+
}
272+
}
273+
274+
if (launcherArgsEnv.has_value())
275+
{
276+
hr = m_struArguments.Copy(launcherArgsEnv.value().c_str());
277+
if (FAILED(hr))
278+
{
279+
goto Finished;
280+
}
281+
}
282+
else
283+
{
284+
hr = GetElementStringProperty(pAspNetCoreElement,
285+
CS_ASPNETCORE_PROCESS_ARGUMENTS,
286+
&m_struArguments);
287+
if (FAILED(hr))
288+
{
289+
goto Finished;
290+
}
257291
}
258292

259293
hr = GetElementStringProperty(pAspNetCoreElement,
@@ -281,14 +315,6 @@ REQUESTHANDLER_CONFIG::Populate(
281315
goto Finished;
282316
}
283317

284-
hr = GetElementStringProperty(pAspNetCoreElement,
285-
CS_ASPNETCORE_PROCESS_ARGUMENTS,
286-
&m_struArguments);
287-
if (FAILED(hr))
288-
{
289-
goto Finished;
290-
}
291-
292318
hr = GetElementDWORDProperty(pAspNetCoreElement,
293319
CS_ASPNETCORE_RAPID_FAILS_PER_MINUTE,
294320
&m_dwRapidFailsPerMinute);

src/Servers/IIS/IIS/test/Common.FunctionalTests/Inprocess/StartupTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,54 @@ public async Task StackOverflowCanBeSetBySettingLargerStackViaHandlerSetting()
880880
Assert.True(result.IsSuccessStatusCode);
881881
}
882882

883+
[ConditionalTheory]
884+
[RequiresIIS(IISCapability.PoolEnvironmentVariables)]
885+
[RequiresNewShim]
886+
[RequiresNewHandler]
887+
[InlineData(HostingModel.InProcess)]
888+
[InlineData(HostingModel.OutOfProcess)]
889+
public async Task EnvironmentVariableForLauncherPathIsPreferred(HostingModel hostingModel)
890+
{
891+
var deploymentParameters = Fixture.GetBaseDeploymentParameters(hostingModel);
892+
893+
deploymentParameters.EnvironmentVariables["ANCM_LAUNCHER_PATH"] = "nope";
894+
var result = await DeployAsync(deploymentParameters);
895+
var response = await result.HttpClient.GetAsync("/");
896+
897+
if (hostingModel == HostingModel.InProcess)
898+
{
899+
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
900+
}
901+
else
902+
{
903+
Assert.Equal(HttpStatusCode.BadGateway, response.StatusCode);
904+
}
905+
}
906+
907+
[ConditionalTheory]
908+
[RequiresIIS(IISCapability.PoolEnvironmentVariables)]
909+
[RequiresNewShim]
910+
[RequiresNewHandler]
911+
[InlineData(HostingModel.InProcess)]
912+
[InlineData(HostingModel.OutOfProcess)]
913+
public async Task EnvironmentVariableForLauncherArgsIsPreferred(HostingModel hostingModel)
914+
{
915+
var deploymentParameters = Fixture.GetBaseDeploymentParameters(hostingModel);
916+
917+
deploymentParameters.EnvironmentVariables["ANCM_LAUNCHER_ARGS"] = "fail";
918+
var result = await DeployAsync(deploymentParameters);
919+
var response = await result.HttpClient.GetAsync("/");
920+
921+
if (hostingModel == HostingModel.InProcess)
922+
{
923+
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
924+
}
925+
else
926+
{
927+
Assert.Equal(HttpStatusCode.BadGateway, response.StatusCode);
928+
}
929+
}
930+
883931
private static void VerifyDotnetRuntimeEventLog(IISDeploymentResult deploymentResult)
884932
{
885933
var entries = GetEventLogsFromDotnetRuntime(deploymentResult);

0 commit comments

Comments
 (0)