From bed94322751ffbf3c24177d1714eb96361b2e756 Mon Sep 17 00:00:00 2001 From: moozzyk Date: Wed, 20 Jan 2016 12:53:44 -0800 Subject: [PATCH] Adding creating Azure specific configuration --- src/dotnet-publish-iis/PublishIISCommand.cs | 13 +++++- src/dotnet-publish-iis/WebConfigTransform.cs | 13 +++--- .../WebConfigTransformFacts.cs | 43 ++++++++++++++----- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/dotnet-publish-iis/PublishIISCommand.cs b/src/dotnet-publish-iis/PublishIISCommand.cs index 05e05f52b4..06c9c34c2b 100644 --- a/src/dotnet-publish-iis/PublishIISCommand.cs +++ b/src/dotnet-publish-iis/PublishIISCommand.cs @@ -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")); + } } } diff --git a/src/dotnet-publish-iis/WebConfigTransform.cs b/src/dotnet-publish-iis/WebConfigTransform.cs index de3df0cebc..75e3cc8714 100644 --- a/src/dotnet-publish-iis/WebConfigTransform.cs +++ b/src/dotnet-publish-iis/WebConfigTransform.cs @@ -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"); } diff --git a/test/dotnet-publish-iis.Tests/WebConfigTransformFacts.cs b/test/dotnet-publish-iis.Tests/WebConfigTransformFacts.cs index d61a5f8623..753c05ad4e 100644 --- a/test/dotnet-publish-iis.Tests/WebConfigTransformFacts.cs +++ b/test/dotnet-publish-iis.Tests/WebConfigTransformFacts.cs @@ -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(""), "test.exe"))); + Assert.True(XNode.DeepEquals(WebConfigTemplate, + WebConfigTransform.Transform(XDocument.Parse(""), "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(@"").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)); } } } \ No newline at end of file