From 520a931faad5bc0ef41cb289a255c75ee79e2d95 Mon Sep 17 00:00:00 2001 From: zoroz Date: Fri, 21 Jul 2023 11:20:51 +0200 Subject: [PATCH] added support for IIS remote server management --- .../Tasks/Iis/AddWebSiteBindingTask.cs | 22 +++++++---- src/FlubuCore/Tasks/Iis/CreateAppPoolTask.cs | 14 ++++++- .../Tasks/Iis/CreateWebApplicationTask.cs | 38 +++++++++++++------ src/FlubuCore/Tasks/Iis/CreateWebSiteTask.cs | 21 ++++++++-- src/FlubuCore/Tasks/Iis/DeleteAppPoolTask.cs | 13 ++++++- .../Iis/Interfaces/ICreateAppPoolTask.cs | 2 + .../Interfaces/ICreateWebApplicationTask.cs | 38 ++++++++++++++++--- .../Iis/Interfaces/ICreateWebSiteTask.cs | 8 ++-- .../Iis/Interfaces/IDeleteAppPoolTask.cs | 2 + 9 files changed, 125 insertions(+), 33 deletions(-) diff --git a/src/FlubuCore/Tasks/Iis/AddWebSiteBindingTask.cs b/src/FlubuCore/Tasks/Iis/AddWebSiteBindingTask.cs index a5b82e6b..4fb69425 100644 --- a/src/FlubuCore/Tasks/Iis/AddWebSiteBindingTask.cs +++ b/src/FlubuCore/Tasks/Iis/AddWebSiteBindingTask.cs @@ -12,12 +12,10 @@ namespace FlubuCore.Tasks.Iis public class AddWebsiteBindingTask : TaskBase, IAddWebsiteBindingTask { private string _siteName; - private string _bindProtocol; - private string _certificateStore; - private string _certificateHash; + private string _serverName; protected override string Description { get; set; } @@ -45,6 +43,12 @@ public IAddWebsiteBindingTask CertificateHash(string hash) return this; } + public IAddWebsiteBindingTask ForServer(string serverName) + { + _serverName = serverName; + return this; + } + protected override int DoExecute(ITaskContextInternal context) { if (string.IsNullOrEmpty(_siteName)) @@ -55,12 +59,16 @@ protected override int DoExecute(ITaskContextInternal context) (string.IsNullOrEmpty(_certificateStore) || string.IsNullOrEmpty(_certificateHash))) throw new TaskExecutionException("Certificate store or hash not set for SSL protocol", 1); - using (ServerManager manager = new ServerManager()) + ServerManager serverManager = string.IsNullOrEmpty(_serverName) + ? new ServerManager() + : ServerManager.OpenRemote(_serverName); + + using (serverManager) { - Site site = manager.Sites[_siteName]; + Site site = serverManager.Sites[_siteName]; //// See if this binding is already on some site - if (manager.Sites.Where(st => st.Bindings.Where(b => b.Protocol == _bindProtocol).Any()).Any()) + if (serverManager.Sites.Where(st => st.Bindings.Where(b => b.Protocol == _bindProtocol).Any()).Any()) { DoLogInfo($"Binding for protocol '{_bindProtocol}' already exists! Doing nothing."); return 0; @@ -72,7 +80,7 @@ protected override int DoExecute(ITaskContextInternal context) binding.CertificateHash = Encoding.UTF8.GetBytes(_certificateHash); site.Bindings.Add(binding); - manager.CommitChanges(); + serverManager.CommitChanges(); } return 0; diff --git a/src/FlubuCore/Tasks/Iis/CreateAppPoolTask.cs b/src/FlubuCore/Tasks/Iis/CreateAppPoolTask.cs index f4937566..8dd1da36 100644 --- a/src/FlubuCore/Tasks/Iis/CreateAppPoolTask.cs +++ b/src/FlubuCore/Tasks/Iis/CreateAppPoolTask.cs @@ -10,7 +10,7 @@ public class CreateAppPoolTask : TaskBase, ICreateAppPoo private bool _classicManagedPipelineMode; private string _description; - + private string _serverName; private string _managedRuntimeVersion; private CreateApplicationPoolMode _mode; @@ -55,9 +55,19 @@ public ICreateAppPoolTask ManagedRuntimeVersion(string managedRuntimeVersion) return this; } + public ICreateAppPoolTask ForServer(string serverName) + { + _serverName = serverName; + return this; + } + protected override int DoExecute(ITaskContextInternal context) { - using (var serverManager = new ServerManager()) + ServerManager serverManager = string.IsNullOrEmpty(_serverName) + ? new ServerManager() + : ServerManager.OpenRemote(_serverName); + + using (serverManager) { var applicationPoolCollection = serverManager.ApplicationPools; diff --git a/src/FlubuCore/Tasks/Iis/CreateWebApplicationTask.cs b/src/FlubuCore/Tasks/Iis/CreateWebApplicationTask.cs index d911e617..32da328a 100644 --- a/src/FlubuCore/Tasks/Iis/CreateWebApplicationTask.cs +++ b/src/FlubuCore/Tasks/Iis/CreateWebApplicationTask.cs @@ -18,6 +18,7 @@ public class CreateWebApplicationTask : IisTaskBase, private string _applicationPoolName = "DefaultAppPool"; private CreateWebApplicationMode _mode = CreateWebApplicationMode.FailIfAlreadyExists; private string _description; + private string _serverName; public CreateWebApplicationTask(string applicationName) { @@ -84,9 +85,21 @@ public ICreateWebApplicationTask AddMimeType(params MimeType[] mimeTypes) return this; } + public ICreateWebApplicationTask ForServer(string serverName) + { + _serverName = serverName; + return this; + } + protected override int DoExecute(ITaskContextInternal context) { - using (ServerManager serverManager = new ServerManager()) + ServerManager serverManager = string.IsNullOrEmpty(_serverName) + ? new ServerManager() + : ServerManager.OpenRemote(_serverName); + + string vdirPath = "/" + _applicationName; + + using (serverManager) { if (!WebsiteExists(serverManager, _websiteName)) { @@ -96,7 +109,6 @@ protected override int DoExecute(ITaskContextInternal context) Site site = serverManager.Sites[_websiteName]; - string vdirPath = "/" + _applicationName; foreach (Application application in site.Applications) { if (application.Path == vdirPath) @@ -122,16 +134,20 @@ protected override int DoExecute(ITaskContextInternal context) return 0; } } + } - using (ServerManager manager = new ServerManager()) - { - Site defaultSite = manager.Sites[_websiteName]; - Application ourApplication = defaultSite.Applications.Add(vdirPath, _localPath); - ourApplication.ApplicationPoolName = _applicationPoolName; - var config = ourApplication.GetWebConfiguration(); - AddMimeTypes(config, _mimeTypes); - manager.CommitChanges(); - } + serverManager = string.IsNullOrEmpty(_serverName) + ? new ServerManager() + : ServerManager.OpenRemote(_serverName); + + using (serverManager) + { + Site defaultSite = serverManager.Sites[_websiteName]; + Application ourApplication = defaultSite.Applications.Add(vdirPath, _localPath); + ourApplication.ApplicationPoolName = _applicationPoolName; + var config = ourApplication.GetWebConfiguration(); + AddMimeTypes(config, _mimeTypes); + serverManager.CommitChanges(); } return 0; diff --git a/src/FlubuCore/Tasks/Iis/CreateWebSiteTask.cs b/src/FlubuCore/Tasks/Iis/CreateWebSiteTask.cs index 7686b030..987a01ba 100644 --- a/src/FlubuCore/Tasks/Iis/CreateWebSiteTask.cs +++ b/src/FlubuCore/Tasks/Iis/CreateWebSiteTask.cs @@ -30,6 +30,8 @@ public class CreateWebsiteTask : IisTaskBase, ICreateWebsite /// private string _physicalPath; + private string _serverName; + private string _applicationPoolName = "DefaultAppPool"; private IList _mimeTypes; @@ -63,24 +65,30 @@ public CreateWebsiteBindingProtocol WebsiteName(string siteName) return new CreateWebsiteBindingProtocol(this); } - public CreateWebsiteTask WebsiteMode(CreateWebApplicationMode value) + public ICreateWebsiteTask WebsiteMode(CreateWebApplicationMode value) { _siteMode = value; return this; } - public CreateWebsiteTask ApplicationPoolName(string applicationPool) + public ICreateWebsiteTask ApplicationPoolName(string applicationPool) { _applicationPoolName = applicationPool; return this; } - public CreateWebsiteTask AddMimeType(MimeType mimeType) + public ICreateWebsiteTask AddMimeType(MimeType mimeType) { _mimeTypes.Add(mimeType); return this; } + public ICreateWebsiteTask ForServer(string serverName) + { + _serverName = serverName; + return this; + } + /// /// Creates or updated the web site. /// @@ -88,7 +96,12 @@ public CreateWebsiteTask AddMimeType(MimeType mimeType) protected override int DoExecute(ITaskContextInternal context) { Validate(); - using (ServerManager serverManager = new ServerManager()) + + ServerManager serverManager = string.IsNullOrEmpty(_serverName) + ? new ServerManager() + : ServerManager.OpenRemote(_serverName); + + using (serverManager) { var webSiteExists = WebsiteExists(serverManager, _webSiteName); diff --git a/src/FlubuCore/Tasks/Iis/DeleteAppPoolTask.cs b/src/FlubuCore/Tasks/Iis/DeleteAppPoolTask.cs index 8f0b1bee..a454431a 100644 --- a/src/FlubuCore/Tasks/Iis/DeleteAppPoolTask.cs +++ b/src/FlubuCore/Tasks/Iis/DeleteAppPoolTask.cs @@ -10,6 +10,7 @@ public class DeleteAppPoolTask : TaskBase, IDeleteAppPo private bool _failIfNotExist; private string _description; + private string _serverName; public DeleteAppPoolTask(string appPoolName) { @@ -43,9 +44,19 @@ public IDeleteAppPoolTask FailIfNotExist() return this; } + public IDeleteAppPoolTask ForServer(string serverName) + { + _serverName = serverName; + return this; + } + protected override int DoExecute(ITaskContextInternal context) { - using (ServerManager serverManager = new ServerManager()) + ServerManager serverManager = string.IsNullOrEmpty(_serverName) + ? new ServerManager() + : ServerManager.OpenRemote(_serverName); + + using (serverManager) { ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools; diff --git a/src/FlubuCore/Tasks/Iis/Interfaces/ICreateAppPoolTask.cs b/src/FlubuCore/Tasks/Iis/Interfaces/ICreateAppPoolTask.cs index 89c98293..587560a7 100644 --- a/src/FlubuCore/Tasks/Iis/Interfaces/ICreateAppPoolTask.cs +++ b/src/FlubuCore/Tasks/Iis/Interfaces/ICreateAppPoolTask.cs @@ -21,5 +21,7 @@ public interface ICreateAppPoolTask : ITaskOfT ICreateAppPoolTask ManagedRuntimeVersion(string managedRuntimeVersion); ICreateAppPoolTask Mode(CreateApplicationPoolMode mode); + + ICreateAppPoolTask ForServer(string serverName); } } diff --git a/src/FlubuCore/Tasks/Iis/Interfaces/ICreateWebApplicationTask.cs b/src/FlubuCore/Tasks/Iis/Interfaces/ICreateWebApplicationTask.cs index 89c863de..e18969c1 100644 --- a/src/FlubuCore/Tasks/Iis/Interfaces/ICreateWebApplicationTask.cs +++ b/src/FlubuCore/Tasks/Iis/Interfaces/ICreateWebApplicationTask.cs @@ -1,5 +1,33 @@ -namespace FlubuCore.Tasks.Iis.Interfaces { public interface ICreateWebApplicationTask : ITaskOfT { ICreateWebApplicationTask LocalPath(string localPath); /// - /// Application Name. - /// - /// - /// ICreateWebApplicationTask ApplicationName(string applicationName); /// /// Name of the application pool application will be controoler by. /// ICreateWebApplicationTask ApplicationPoolName(string applicationPoolName); ICreateWebApplicationTask ParentVirtualDirectoryName(string parentVirualDirectoryName); /// /// Web site name web application will be added to. /// ICreateWebApplicationTask WebsiteName(string websiteName); /// /// Mime types to be added. /// ICreateWebApplicationTask AddMimeType(params MimeType[] mimeTypes); } } \ No newline at end of file +namespace FlubuCore.Tasks.Iis.Interfaces +{ + public interface ICreateWebApplicationTask : ITaskOfT + { + ICreateWebApplicationTask LocalPath(string localPath); + + /// + /// Application Name. + /// + /// + /// + ICreateWebApplicationTask ApplicationName(string applicationName); + + /// + /// Name of the application pool application will be controoler by. + /// + ICreateWebApplicationTask ApplicationPoolName(string applicationPoolName); + + ICreateWebApplicationTask ParentVirtualDirectoryName(string parentVirualDirectoryName); + + /// + /// Web site name web application will be added to. + /// + ICreateWebApplicationTask WebsiteName(string websiteName); + + /// + /// Mime types to be added. + /// + ICreateWebApplicationTask AddMimeType(params MimeType[] mimeTypes); + + ICreateWebApplicationTask ForServer(string serverName); + } +} \ No newline at end of file diff --git a/src/FlubuCore/Tasks/Iis/Interfaces/ICreateWebSiteTask.cs b/src/FlubuCore/Tasks/Iis/Interfaces/ICreateWebSiteTask.cs index d4334b35..de9f3f6e 100644 --- a/src/FlubuCore/Tasks/Iis/Interfaces/ICreateWebSiteTask.cs +++ b/src/FlubuCore/Tasks/Iis/Interfaces/ICreateWebSiteTask.cs @@ -22,20 +22,22 @@ public interface ICreateWebsiteTask : ITaskOfT /// /// The website Mode /// The Iis7CreateWebSiteTask. - CreateWebsiteTask WebsiteMode(CreateWebApplicationMode value); + ICreateWebsiteTask WebsiteMode(CreateWebApplicationMode value); /// /// Set web site application pool name. /// /// The application pool name /// The Iis7CreateWebSiteTask. - CreateWebsiteTask ApplicationPoolName(string applicationPool); + ICreateWebsiteTask ApplicationPoolName(string applicationPool); /// /// Add MimeType. Can be used multiple times. /// /// The mime type /// The Iis7CreateWebSiteTask. - CreateWebsiteTask AddMimeType(MimeType mimeType); + ICreateWebsiteTask AddMimeType(MimeType mimeType); + + ICreateWebsiteTask ForServer(string serverName); } } diff --git a/src/FlubuCore/Tasks/Iis/Interfaces/IDeleteAppPoolTask.cs b/src/FlubuCore/Tasks/Iis/Interfaces/IDeleteAppPoolTask.cs index 9bdc93ff..1d1e956a 100644 --- a/src/FlubuCore/Tasks/Iis/Interfaces/IDeleteAppPoolTask.cs +++ b/src/FlubuCore/Tasks/Iis/Interfaces/IDeleteAppPoolTask.cs @@ -13,5 +13,7 @@ public interface IDeleteAppPoolTask : ITaskOfT /// task fails with exception if application pool doesn't exists. Otherwise not. /// IDeleteAppPoolTask FailIfNotExist(); + + IDeleteAppPoolTask ForServer(string serverName); } }