Adding creating Azure specific configuration

This commit is contained in:
moozzyk 2016-01-20 12:53:44 -08:00
parent 674efca4de
commit bed9432275
3 changed files with 51 additions and 18 deletions

View File

@ -1,11 +1,12 @@
// 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.Xml;
using System.Xml.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.DotNet.ProjectModel;
using Microsoft.Extensions.Configuration;
namespace Microsoft.AspNetCore.Tools.PublishIIS
{
@ -39,7 +40,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS
}
var applicationName = Path.ChangeExtension(GetApplicationName(applicationBasePath), "exe");
var transformedConfig = WebConfigTransform.Transform(webConfigXml, applicationName);
var transformedConfig = WebConfigTransform.Transform(webConfigXml, applicationName, ConfigureForAzure());
using (var f = new FileStream(webConfigPath, FileMode.Create))
{
@ -92,5 +93,13 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS
return string.Empty;
}
private static bool ConfigureForAzure()
{
var configureForAzureValue = Environment.GetEnvironmentVariable("DOTNET_CONFIGURE_AZURE");
return string.Equals(configureForAzureValue, "true", StringComparison.Ordinal) ||
string.Equals(configureForAzureValue, "1", StringComparison.Ordinal) ||
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME"));
}
}
}

View File

@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS
{
public static class WebConfigTransform
{
public static XDocument Transform(XDocument webConfig, string appName)
public static XDocument Transform(XDocument webConfig, string appName, bool configureForAzure)
{
const string HandlersElementName = "handlers";
const string httpPlatformElementName = "httpPlatform";
@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS
var webServerSection = GetOrCreateChild(webConfig.Root, "system.webServer");
TransformHandlers(GetOrCreateChild(webServerSection, HandlersElementName));
TransformHttpPlatform(GetOrCreateChild(webServerSection, httpPlatformElementName), appName);
TransformHttpPlatform(GetOrCreateChild(webServerSection, httpPlatformElementName), appName, configureForAzure);
// make sure that the httpPlatform element is after handlers element
var httpPlatformElement = webServerSection.Element(HandlersElementName)
@ -55,11 +55,14 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS
SetAttributeValueIfEmpty(platformHandlerElement, "resourceType", "Unspecified");
}
private static void TransformHttpPlatform(XElement httpPlatformElement, string appName)
private static void TransformHttpPlatform(XElement httpPlatformElement, string appName, bool configureForAzure)
{
httpPlatformElement.SetAttributeValue("processPath", Path.Combine("..", appName));
var appPath = Path.Combine(configureForAzure ? @"%home%\site" : "..", appName);
var logPath = Path.Combine(configureForAzure ? @"\\?\%home%\LogFiles" : @"..\logs", "stdout.log");
httpPlatformElement.SetAttributeValue("processPath", appPath);
SetAttributeValueIfEmpty(httpPlatformElement, "stdoutLogEnabled", "false");
SetAttributeValueIfEmpty(httpPlatformElement, "stdoutLogFile", @"..\logs\stdout.log");
SetAttributeValueIfEmpty(httpPlatformElement, "stdoutLogFile", logPath);
SetAttributeValueIfEmpty(httpPlatformElement, "startupTimeLimit", "3600");
}

View File

@ -19,13 +19,15 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
[Fact]
public void WebConfigTransform_creates_new_config_if_one_does_not_exist()
{
Assert.True(XNode.DeepEquals(WebConfigTemplate, WebConfigTransform.Transform(null, "test.exe")));
Assert.True(XNode.DeepEquals(WebConfigTemplate,
WebConfigTransform.Transform(null, "test.exe", configureForAzure: false)));
}
[Fact]
public void WebConfigTransform_creates_new_config_if_one_has_unexpected_format()
{
Assert.True(XNode.DeepEquals(WebConfigTemplate, WebConfigTransform.Transform(XDocument.Parse("<unexpected />"), "test.exe")));
Assert.True(XNode.DeepEquals(WebConfigTemplate,
WebConfigTransform.Transform(XDocument.Parse("<unexpected />"), "test.exe", configureForAzure: false)));
}
[Theory]
@ -42,7 +44,8 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
input.Descendants(elementName).Remove();
}
Assert.True(XNode.DeepEquals(WebConfigTemplate, WebConfigTransform.Transform(input, "test.exe")));
Assert.True(XNode.DeepEquals(WebConfigTemplate,
WebConfigTransform.Transform(input, "test.exe", configureForAzure: false)));
}
[Theory]
@ -59,7 +62,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
var input = new XDocument(WebConfigTemplate);
input.Descendants(elementName).Single().SetAttributeValue(attributeName, attributeValue);
var output = WebConfigTransform.Transform(input, "test.exe");
var output = WebConfigTransform.Transform(input, "test.exe", configureForAzure: false);
Assert.Equal(attributeValue, (string)output.Descendants(elementName).Single().Attribute(attributeName));
}
@ -67,7 +70,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
public void WebConfigTransform_overwrites_processPath()
{
var newProcessPath =
(string)WebConfigTransform.Transform(WebConfigTemplate, "app.exe")
(string)WebConfigTransform.Transform(WebConfigTemplate, "app.exe", configureForAzure: false)
.Descendants("httpPlatform").Single().Attribute("processPath");
Assert.Equal(@"..\app.exe", newProcessPath);
@ -79,7 +82,8 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
var input = new XDocument(WebConfigTemplate);
input.Descendants("add").Single().SetAttributeValue("name", "httpplatformhandler");
Assert.True(XNode.DeepEquals(WebConfigTemplate, WebConfigTransform.Transform(input, "test.exe")));
Assert.True(XNode.DeepEquals(WebConfigTemplate,
WebConfigTransform.Transform(input, "test.exe", configureForAzure: false)));
}
[Fact]
@ -93,7 +97,8 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
input.Descendants("httpPlatform").Single().Add(envVarsElement);
Assert.True(XNode.DeepEquals(envVarsElement,
WebConfigTransform.Transform(input, "app.exe").Descendants("httpPlatform").Elements().Single()));
WebConfigTransform.Transform(input, "app.exe", configureForAzure: false)
.Descendants("httpPlatform").Elements().Single()));
}
[Fact]
@ -104,7 +109,8 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
Assert.Equal(
"false",
(string)WebConfigTransform.Transform(input, "test.exe").Descendants().Attributes("stdoutLogEnabled").Single());
(string)WebConfigTransform.Transform(input, "test.exe", configureForAzure: false)
.Descendants().Attributes("stdoutLogEnabled").Single());
}
[Theory]
@ -124,7 +130,8 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
Assert.Equal(
@"..\logs\stdout.log",
(string)WebConfigTransform.Transform(input, "test.exe").Descendants().Attributes("stdoutLogFile").Single());
(string)WebConfigTransform.Transform(input, "test.exe", configureForAzure: false)
.Descendants().Attributes("stdoutLogFile").Single());
}
[Theory]
@ -145,7 +152,20 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
Assert.Equal(
"mylog.txt",
(string)WebConfigTransform.Transform(input, "test.exe").Descendants().Attributes("stdoutLogFile").Single());
(string)WebConfigTransform.Transform(input, "test.exe", configureForAzure: false)
.Descendants().Attributes("stdoutLogFile").Single());
}
[Fact]
public void WebConfigTransform_correctly_configures_for_Azure()
{
var input = new XDocument(WebConfigTemplate);
input.Descendants("httpPlatform").Attributes().Remove();
Assert.True(XNode.DeepEquals(
XDocument.Parse(@"<httpPlatform processPath=""%home%\site\test.exe"" stdoutLogEnabled=""false""
stdoutLogFile=""\\?\%home%\LogFiles\stdout.log"" startupTimeLimit=""3600""/>").Root,
WebConfigTransform.Transform(input, "test.exe", configureForAzure: true).Descendants("httpPlatform").Single()));
}
private bool VerifyMissingElementCreated(params string[] elementNames)
@ -156,7 +176,8 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
input.Descendants(elementName).Remove();
}
return XNode.DeepEquals(WebConfigTemplate, WebConfigTransform.Transform(input, "test.exe"));
return XNode.DeepEquals(WebConfigTemplate,
WebConfigTransform.Transform(input, "test.exe", configureForAzure: false));
}
}
}