Skip to content

Commit

Permalink
update iis library to netcore (#1342)
Browse files Browse the repository at this point in the history
  • Loading branch information
zentron authored Sep 22, 2024
1 parent aa13332 commit 590fc6f
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ public void ShouldExecuteFeatureScripts(DeploymentType deploymentType)
result.AssertOutput("Hello World!");
}

#if IIS_SUPPORT
[Test]
[Category(TestCategory.CompatibleOS.OnlyWindows)]
public void ShouldModifyIisWebsiteRoot()
Expand All @@ -202,7 +201,7 @@ public void ShouldModifyIisWebsiteRoot()
// Create the website
var originalWebRootPath = Path.Combine(Path.GetTempPath(), "CalamariTestIisSite");
FileSystem.EnsureDirectoryExists(originalWebRootPath);
var webServer = WebServerSupport.AutoDetect();
var webServer = new WebServerSevenSupport();
var siteName = "CalamariTest-" + Guid.NewGuid();
webServer.CreateWebSiteOrVirtualDirectory(siteName, "/", originalWebRootPath, 1081);

Expand All @@ -219,7 +218,6 @@ public void ShouldModifyIisWebsiteRoot()
webServer.DeleteWebSite(siteName);
FileSystem.DeleteDirectory(originalWebRootPath);
}
#endif

[Test]
public void ShouldRunConfiguredScripts()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#if IIS_SUPPORT
using System;
using System;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using Calamari.Common.Plumbing.FileSystem;
using Calamari.Common.Plumbing.Variables;
using Calamari.Deployment;
using Calamari.Integration.Iis;
using Calamari.Testing.Helpers;
using Calamari.Tests.Fixtures.Deployment.Packages;
#if WINDOWS_CERTIFICATE_STORE_SUPPORT
using Calamari.Tests.Helpers.Certificates;
#endif
using Microsoft.Web.Administration;
using NUnit.Framework;
using Polly;
Expand Down Expand Up @@ -629,5 +629,4 @@ private Application FindWebApplication(string websiteName, string virtualPath)
return GetWebSite(websiteName).Applications.Single(ap => ap.Path == virtualPath);
}
}
}
#endif
}
8 changes: 3 additions & 5 deletions source/Calamari.Tests/Fixtures/Iis/IisFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if IIS_SUPPORT
using System;
using System;
using Calamari.Integration.Iis;
using Calamari.Testing.Helpers;
using NUnit.Framework;
Expand All @@ -10,7 +9,7 @@ namespace Calamari.Tests.Fixtures.Iis
[Category(TestCategory.CompatibleOS.OnlyWindows)]
public class IisFixture
{
readonly WebServerSupport webServer = WebServerSupport.AutoDetect();
readonly WebServerSupport webServer = new WebServerSevenSupport();
string siteName;

[SetUp]
Expand Down Expand Up @@ -61,5 +60,4 @@ public void TearDown()
webServer.DeleteWebSite(siteName);
}
}
}
#endif
}
1 change: 1 addition & 0 deletions source/Calamari/Calamari.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Web.Administration" Version="11.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Octodiff" Version="1.1.2" />
<PackageReference Include="FSharp.Compiler.Tools" Version="4.0.0.1" />
Expand Down
6 changes: 2 additions & 4 deletions source/Calamari/Commands/DeployPackageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public override int Execute(string[] commandLineArguments)
var configurationTransformer = ConfigurationTransformer.FromVariables(variables, log);
var transformFileLocator = new TransformFileLocator(fileSystem, log);
var embeddedResources = new AssemblyEmbeddedResources();
#if IIS_SUPPORT

var iis = new InternetInformationServer();
featureClasses.AddRange(new IFeature[] { new IisWebSiteBeforeDeployFeature(windowsX509CertificateStore), new IisWebSiteAfterPostDeployFeature(windowsX509CertificateStore) });
#endif

if (!CalamariEnvironment.IsRunningOnWindows)
{
featureClasses.Add(new NginxFeature(NginxServer.AutoDetect(), fileSystem));
Expand All @@ -118,9 +118,7 @@ public override int Execute(string[] commandLineArguments)
new PackagedScriptConvention(new DeployPackagedScriptBehaviour(log, fileSystem, scriptEngine, commandLineRunner)),
new ConfiguredScriptConvention(new DeployConfiguredScriptBehaviour(log, fileSystem, scriptEngine, commandLineRunner)),
new FeatureConvention(DeploymentStages.AfterDeploy, featureClasses, fileSystem, scriptEngine, commandLineRunner, embeddedResources),
#if IIS_SUPPORT
new LegacyIisWebSiteConvention(fileSystem, iis),
#endif
new FeatureConvention(DeploymentStages.BeforePostDeploy, featureClasses, fileSystem, scriptEngine, commandLineRunner, embeddedResources),
new PackagedScriptConvention(new PostDeployPackagedScriptBehaviour(log, fileSystem, scriptEngine, commandLineRunner)),
new ConfiguredScriptConvention(new PostDeployConfiguredScriptBehaviour( log, fileSystem, scriptEngine, commandLineRunner)),
Expand Down
17 changes: 9 additions & 8 deletions source/Calamari/Integration/Iis/InternetInformationServer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#if IIS_SUPPORT
using System.Linq;
using System.Linq;
using Calamari.Common.Plumbing.Logging;

namespace Calamari.Integration.Iis
{
Expand All @@ -24,13 +24,14 @@ public bool OverwriteHomeDirectory(string iisWebSiteNameAndVirtualDirectory, str
var iisSiteName = parts.First();
var remainder = parts.Skip(1).Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
var virtualDirectory = remainder.Length > 0 ? string.Join("/", remainder) : null;
WebServerSupport server = new WebServerSevenSupport();

var server = legacySupport
? WebServerSupport.Legacy()
: WebServerSupport.AutoDetect();

#if IIS_SUPPORT
if(legacySupport) {
server = new WebServerSixSupport();
}
#endif
return server.ChangeHomeDirectory(iisSiteName, virtualDirectory, path);
}
}
}
#endif
}
20 changes: 8 additions & 12 deletions source/Calamari/Integration/Iis/WebServerSevenSupport.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if IIS_SUPPORT
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Web.Administration;
Expand All @@ -8,8 +7,6 @@ namespace Calamari.Integration.Iis
{
public class WebServerSevenSupport : WebServerSupport
{
const string Localhost = "localhost";

public override void CreateWebSiteOrVirtualDirectory(string webSiteName, string virtualDirectoryPath, string webRootPath, int port)
{
var virtualParts = (virtualDirectoryPath ?? String.Empty).Split('/', '\\').Select(x => x.Trim()).Where(x => x.Length > 0).ToArray();
Expand Down Expand Up @@ -155,7 +152,7 @@ public VirtualDirectory FindVirtualDirectory(string webSiteName, string virtualD
return virtualDirectory;
}

public class VirtualDirectoryNode
class VirtualDirectoryNode
{
public string FullVirtualPath { get; set; }
public VirtualDirectory VirtualDirectory { get; set; }
Expand All @@ -172,7 +169,7 @@ public Site GetWebSite(string webSiteName)
return site;
}

public Site FindWebSite(string webSiteName)
Site FindWebSite(string webSiteName)
{
return Execute(serverManager => serverManager.Sites.FirstOrDefault(x => String.Equals(x.Name, webSiteName, StringComparison.OrdinalIgnoreCase)));
}
Expand All @@ -193,7 +190,7 @@ public ApplicationPool GetApplicationPool(string applicationPoolName)
return applicationPool;
}

public ApplicationPool FindApplicationPool(string applicationPoolName)
ApplicationPool FindApplicationPool(string applicationPoolName)
{
return Execute(serverManager => serverManager.ApplicationPools.FirstOrDefault(x => String.Equals(x.Name, applicationPoolName, StringComparison.OrdinalIgnoreCase)));
}
Expand All @@ -203,21 +200,20 @@ public bool ApplicationPoolExists(string applicationPool)
return FindApplicationPool(applicationPool) != null;
}

private void Execute(Action<ServerManager> action)
void Execute(Action<ServerManager> action)
{
using (var serverManager = ServerManager.OpenRemote(Localhost))
using (var serverManager = new ServerManager())
{
action(serverManager);
}
}

private TResult Execute<TResult>(Func<ServerManager, TResult> func)
TResult Execute<TResult>(Func<ServerManager, TResult> func)
{
var result = default(TResult);
Action<ServerManager> action = serverManager => result = func(serverManager);
Execute(action);
return result;
}
}
}
#endif
}
27 changes: 2 additions & 25 deletions source/Calamari/Integration/Iis/WebServerSupport.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if IIS_SUPPORT
using System;
using System;

namespace Calamari.Integration.Iis
{
Expand All @@ -9,27 +8,5 @@ public abstract class WebServerSupport
public abstract string GetHomeDirectory(string webSiteName, string virtualDirectoryPath);
public abstract void DeleteWebSite(string webSiteName);
public abstract bool ChangeHomeDirectory(string webSiteName, string virtualDirectoryPath, string newWebRootPath);

public static WebServerSupport Legacy()
{
return new WebServerSixSupport();
}

public static WebServerSupport AutoDetect()
{
// Sources:
// http://support.microsoft.com/kb/224609
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx

#pragma warning disable DE0009 // API is deprecated
if (Environment.OSVersion.Version.Major < 6)
#pragma warning restore DE0009 // API is deprecated
{
return new WebServerSixSupport();
}

return new WebServerSevenSupport();
}
}
}
#endif
}

0 comments on commit 590fc6f

Please sign in to comment.