Skip to content

Commit

Permalink
Move config operations from helpers to functions on the deployment pa…
Browse files Browse the repository at this point in the history
…rameters (#1063)
  • Loading branch information
jkotalik committed Jul 19, 2018
1 parent 97ebe36 commit 89fda83
Show file tree
Hide file tree
Showing 21 changed files with 375 additions and 294 deletions.
4 changes: 2 additions & 2 deletions build/dependencies.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project>
<Project>
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>
Expand All @@ -14,7 +14,7 @@
<MicrosoftAspNetCoreHttpOverridesPackageVersion>2.2.0-preview1-34694</MicrosoftAspNetCoreHttpOverridesPackageVersion>
<MicrosoftAspNetCoreHttpPackageVersion>2.2.0-preview1-34694</MicrosoftAspNetCoreHttpPackageVersion>
<MicrosoftAspNetCoreHttpSysSourcesPackageVersion>2.2.0-preview1-34694</MicrosoftAspNetCoreHttpSysSourcesPackageVersion>
<MicrosoftAspNetCoreServerIntegrationTestingPackageVersion>0.6.0-a-preview1-pk-rem-iis-17083</MicrosoftAspNetCoreServerIntegrationTestingPackageVersion>
<MicrosoftAspNetCoreServerIntegrationTestingPackageVersion>0.6.0-a-preview1-DeploymentParameters-17085</MicrosoftAspNetCoreServerIntegrationTestingPackageVersion>
<MicrosoftAspNetCoreServerKestrelPackageVersion>2.2.0-preview1-34694</MicrosoftAspNetCoreServerKestrelPackageVersion>
<MicrosoftAspNetCoreTestHostPackageVersion>2.2.0-preview1-34694</MicrosoftAspNetCoreTestHostPackageVersion>
<MicrosoftAspNetCoreTestingPackageVersion>2.2.0-preview1-34694</MicrosoftAspNetCoreTestingPackageVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.AspNetCore.Server.IntegrationTesting.Common;
using Microsoft.Extensions.Logging;

Expand All @@ -14,12 +12,17 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS
/// <summary>
/// Deployer for IIS.
/// </summary>
public partial class IISDeployer : ApplicationDeployer
public partial class IISDeployer : IISDeployerBase
{
private IISApplication _application;
private CancellationTokenSource _hostShutdownToken = new CancellationTokenSource();

public IISDeployer(DeploymentParameters deploymentParameters, ILoggerFactory loggerFactory)
: base(new IISDeploymentParameters(deploymentParameters), loggerFactory)
{
}

public IISDeployer(IISDeploymentParameters deploymentParameters, ILoggerFactory loggerFactory)
: base(deploymentParameters, loggerFactory)
{
}
Expand Down Expand Up @@ -53,18 +56,19 @@ public override async Task<DeploymentResult> DeployAsync()
DeploymentParameters.ServerConfigTemplateContent = File.ReadAllText("IIS.config");
}

_application = new IISApplication(DeploymentParameters, Logger);
_application = new IISApplication(IISDeploymentParameters, Logger);

// For now, only support using published output
DeploymentParameters.PublishApplicationBeforeDeployment = true;

if (DeploymentParameters.PublishApplicationBeforeDeployment)
{
DotnetPublish();
contentRoot = DeploymentParameters.PublishedApplicationRootPath;
IISDeploymentParameters.AddDebugLogToWebConfig(Path.Combine(contentRoot, $"{_application.WebSiteName}.txt"));
RunWebConfigActions();
}

WebConfigHelpers.AddDebugLogToWebConfig(contentRoot, Path.Combine(contentRoot, $"{_application.WebSiteName}.txt"));

var uri = TestUriHelper.BuildTestUri(ServerType.IIS, DeploymentParameters.ApplicationBaseUriHint);
// To prevent modifying the IIS setup concurrently.
await _application.StartIIS(uri, contentRoot);
Expand All @@ -74,7 +78,7 @@ public override async Task<DeploymentResult> DeployAsync()

return new DeploymentResult(
LoggerFactory,
DeploymentParameters,
IISDeploymentParameters,
applicationBaseUri: uri.ToString(),
contentRoot: contentRoot,
hostShutdownToken: _hostShutdownToken.Token
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS
{
public abstract class IISDeployerBase : ApplicationDeployer
{
public IISDeploymentParameters IISDeploymentParameters { get; }

public IISDeployerBase(IISDeploymentParameters deploymentParameters, ILoggerFactory loggerFactory)
: base(deploymentParameters, loggerFactory)
{
IISDeploymentParameters = deploymentParameters;
}

public void RunWebConfigActions()
{
if (IISDeploymentParameters == null)
{
return;
}

if (!DeploymentParameters.PublishApplicationBeforeDeployment)
{
throw new InvalidOperationException("Cannot modify web.config file if no published output.");
}

var path = Path.Combine(DeploymentParameters.PublishedApplicationRootPath, "web.config");
var webconfig = XDocument.Load(path);
var xElement = webconfig.Descendants("system.webServer").Single();
foreach (var action in IISDeploymentParameters.WebConfigActionList)
{
action.Invoke(xElement);
}

webconfig.Save(path);
}

public string RunServerConfigActions(string serverConfigString)
{
if (IISDeploymentParameters == null)
{
return serverConfigString;
}

var serverConfig = XDocument.Parse(serverConfigString);
var xElement = serverConfig.Descendants("configuration").FirstOrDefault();

foreach (var action in IISDeploymentParameters.ServerConfigActionList)
{
action.Invoke(xElement);
}
return xElement.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS
{
public static class IISDeploymentParameterExtensions
{
public static void AddDebugLogToWebConfig(this IISDeploymentParameters parameters, string filename)
{
parameters.HandlerSettings["debugLevel"] = "4";
parameters.HandlerSettings["debugFile"] = filename;
}

public static void AddServerConfigAction(this IISDeploymentParameters parameters, Action<XElement> action)
{
parameters.ServerConfigActionList.Add(action);
}

public static void ModifyWebConfig(this IISDeploymentParameters parameters, Action<XElement> transform)
{
parameters.WebConfigActionList.Add(transform);
}

public static void ModifyAspNetCoreSectionInWebConfig(this IISDeploymentParameters parameters, string key, string value)
=> ModifyAttributeInWebConfig(parameters, key, value, section: "aspNetCore");


public static void ModifyAttributeInWebConfig(this IISDeploymentParameters parameters, string key, string value, string section)
{
parameters.WebConfigActionList.Add((element) =>
{
element.Descendants(section).SingleOrDefault().SetAttributeValue(key, value);
});
}

public static void AddHttpsToServerConfig(this IISDeploymentParameters parameters)
{
parameters.ServerConfigActionList.Add(
element =>
{
element.Descendants("binding")
.Single()
.SetAttributeValue("protocol", "https");
element.Descendants("access")
.Single()
.SetAttributeValue("sslFlags", "Ssl, SslNegotiateCert");
});
}

public static void AddWindowsAuthToServerConfig(this IISDeploymentParameters parameters)
{
parameters.ServerConfigActionList.Add(
element =>
{
element.Descendants("windowsAuthentication")
.Single()
.SetAttributeValue("enabled", "true");
});
}

public static void ModifyHandlerSectionInWebConfig(this IISDeploymentParameters parameters, string key, string value)
{
parameters.WebConfigActionList.Add(element =>
{
element.Descendants("handlers")
.FirstOrDefault()
.Descendants("add")
.FirstOrDefault()
.SetAttributeValue(key, value);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS
{
public class IISDeploymentParameters : DeploymentParameters
{
public IISDeploymentParameters() : base()
{
WebConfigActionList = CreateDefaultWebConfigActionList();
}

public IISDeploymentParameters(TestVariant variant)
: base(variant)
{
WebConfigActionList = CreateDefaultWebConfigActionList();
}

public IISDeploymentParameters(
string applicationPath,
ServerType serverType,
RuntimeFlavor runtimeFlavor,
RuntimeArchitecture runtimeArchitecture)
: base(applicationPath, serverType, runtimeFlavor, runtimeArchitecture)
{
WebConfigActionList = CreateDefaultWebConfigActionList();
}

public IISDeploymentParameters(DeploymentParameters parameters)
: base(parameters)
{
WebConfigActionList = CreateDefaultWebConfigActionList();

if (parameters is IISDeploymentParameters)
{
var tempParameters = (IISDeploymentParameters)parameters;
WebConfigActionList = tempParameters.WebConfigActionList;
ServerConfigActionList = tempParameters.ServerConfigActionList;
WebConfigBasedEnvironmentVariables = tempParameters.WebConfigBasedEnvironmentVariables;
HandlerSettings = tempParameters.HandlerSettings;
}
}

private IList<Action<XElement>> CreateDefaultWebConfigActionList()
{
return new List<Action<XElement>>() { AddWebConfigEnvironmentVariables(), AddHandlerSettings() };
}

public IList<Action<XElement>> WebConfigActionList { get; }

public IList<Action<XElement>> ServerConfigActionList { get; } = new List<Action<XElement>>();

public IDictionary<string, string> WebConfigBasedEnvironmentVariables { get; set; } = new Dictionary<string, string>();

public IDictionary<string, string> HandlerSettings { get; set; } = new Dictionary<string, string>();

private Action<XElement> AddWebConfigEnvironmentVariables()
{
return xElement =>
{
if (WebConfigBasedEnvironmentVariables.Count == 0)
{
return;
}
var element = xElement.Descendants("environmentVariables").SingleOrDefault();
if (element == null)
{
element = new XElement("environmentVariables");
xElement.Descendants("aspNetCore").SingleOrDefault().Add(element);
}
foreach (var envVar in WebConfigBasedEnvironmentVariables)
{
CreateOrSetElement(element, envVar.Key, envVar.Value, "environmentVariable");
}
};
}

private Action<XElement> AddHandlerSettings()
{
return xElement =>
{
if (HandlerSettings.Count == 0)
{
return;
}
var element = xElement.Descendants("handlerSettings").SingleOrDefault();
if (element == null)
{
element = new XElement("handlerSettings");
xElement.Descendants("aspNetCore").SingleOrDefault().Add(element);
}
foreach (var handlerSetting in HandlerSettings)
{
CreateOrSetElement(element, handlerSetting.Key, handlerSetting.Value, "handlerSetting");
}
};
}

private static void CreateOrSetElement(XElement rootElement, string name, string value, string elementName)
{
if (rootElement.Descendants()
.Attributes()
.Where(attribute => attribute.Value == name)
.Any())
{
return;
}
var element = new XElement(elementName);
element.SetAttributeValue("name", name);
element.SetAttributeValue("value", value);
rootElement.Add(element);
}
}
}
Loading

0 comments on commit 89fda83

Please sign in to comment.