Skip to content

Commit

Permalink
added support for IIS remote server management
Browse files Browse the repository at this point in the history
  • Loading branch information
zoroz committed Jul 21, 2023
1 parent e1a1726 commit 520a931
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 33 deletions.
22 changes: 15 additions & 7 deletions src/FlubuCore/Tasks/Iis/AddWebSiteBindingTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ namespace FlubuCore.Tasks.Iis
public class AddWebsiteBindingTask : TaskBase<int, IAddWebsiteBindingTask>, IAddWebsiteBindingTask
{
private string _siteName;

private string _bindProtocol;

private string _certificateStore;

private string _certificateHash;
private string _serverName;

protected override string Description { get; set; }

Expand Down Expand Up @@ -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))
Expand All @@ -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;
Expand All @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions src/FlubuCore/Tasks/Iis/CreateAppPoolTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CreateAppPoolTask : TaskBase<int, CreateAppPoolTask>, ICreateAppPoo

private bool _classicManagedPipelineMode;
private string _description;

private string _serverName;
private string _managedRuntimeVersion;

private CreateApplicationPoolMode _mode;
Expand Down Expand Up @@ -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;

Expand Down
38 changes: 27 additions & 11 deletions src/FlubuCore/Tasks/Iis/CreateWebApplicationTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CreateWebApplicationTask : IisTaskBase<ICreateWebApplicationTask>,
private string _applicationPoolName = "DefaultAppPool";
private CreateWebApplicationMode _mode = CreateWebApplicationMode.FailIfAlreadyExists;
private string _description;
private string _serverName;

public CreateWebApplicationTask(string applicationName)
{
Expand Down Expand Up @@ -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))
{
Expand All @@ -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)
Expand All @@ -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;
Expand Down
21 changes: 17 additions & 4 deletions src/FlubuCore/Tasks/Iis/CreateWebSiteTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class CreateWebsiteTask : IisTaskBase<ICreateWebsiteTask>, ICreateWebsite
/// </summary>
private string _physicalPath;

private string _serverName;

private string _applicationPoolName = "DefaultAppPool";

private IList<MimeType> _mimeTypes;
Expand Down Expand Up @@ -63,32 +65,43 @@ 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;
}

/// <summary>
/// Creates or updated the web site.
/// </summary>
/// <param name="context">The task context</param>
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);

Expand Down
13 changes: 12 additions & 1 deletion src/FlubuCore/Tasks/Iis/DeleteAppPoolTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class DeleteAppPoolTask : TaskBase<int, IDeleteAppPoolTask>, IDeleteAppPo

private bool _failIfNotExist;
private string _description;
private string _serverName;

public DeleteAppPoolTask(string appPoolName)
{
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions src/FlubuCore/Tasks/Iis/Interfaces/ICreateAppPoolTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ public interface ICreateAppPoolTask : ITaskOfT<int, CreateAppPoolTask>
ICreateAppPoolTask ManagedRuntimeVersion(string managedRuntimeVersion);

ICreateAppPoolTask Mode(CreateApplicationPoolMode mode);

ICreateAppPoolTask ForServer(string serverName);
}
}
38 changes: 33 additions & 5 deletions src/FlubuCore/Tasks/Iis/Interfaces/ICreateWebApplicationTask.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
namespace FlubuCore.Tasks.Iis.Interfaces{ public interface ICreateWebApplicationTask : ITaskOfT<int, ICreateWebApplicationTask> { ICreateWebApplicationTask LocalPath(string localPath); /// <summary>
/// Application Name.
/// </summary>
/// <param name="applicationName"></param>
/// <returns></returns> ICreateWebApplicationTask ApplicationName(string applicationName); /// <summary> /// Name of the application pool application will be controoler by. /// </summary> ICreateWebApplicationTask ApplicationPoolName(string applicationPoolName); ICreateWebApplicationTask ParentVirtualDirectoryName(string parentVirualDirectoryName); /// <summary> /// Web site name web application will be added to. /// </summary> ICreateWebApplicationTask WebsiteName(string websiteName); /// <summary> /// Mime types to be added. /// </summary> ICreateWebApplicationTask AddMimeType(params MimeType[] mimeTypes); }}
namespace FlubuCore.Tasks.Iis.Interfaces
{
public interface ICreateWebApplicationTask : ITaskOfT<int, ICreateWebApplicationTask>
{
ICreateWebApplicationTask LocalPath(string localPath);

/// <summary>
/// Application Name.
/// </summary>
/// <param name="applicationName"></param>
/// <returns></returns>
ICreateWebApplicationTask ApplicationName(string applicationName);

/// <summary>
/// Name of the application pool application will be controoler by.
/// </summary>
ICreateWebApplicationTask ApplicationPoolName(string applicationPoolName);

ICreateWebApplicationTask ParentVirtualDirectoryName(string parentVirualDirectoryName);

/// <summary>
/// Web site name web application will be added to.
/// </summary>
ICreateWebApplicationTask WebsiteName(string websiteName);

/// <summary>
/// Mime types to be added.
/// </summary>
ICreateWebApplicationTask AddMimeType(params MimeType[] mimeTypes);

ICreateWebApplicationTask ForServer(string serverName);
}
}
8 changes: 5 additions & 3 deletions src/FlubuCore/Tasks/Iis/Interfaces/ICreateWebSiteTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ public interface ICreateWebsiteTask : ITaskOfT<int, ICreateWebsiteTask>
/// </summary>
/// <param name="value">The website Mode <see cref="CreateWebApplicationMode"/> </param>
/// <returns>The Iis7CreateWebSiteTask.</returns>
CreateWebsiteTask WebsiteMode(CreateWebApplicationMode value);
ICreateWebsiteTask WebsiteMode(CreateWebApplicationMode value);

/// <summary>
/// Set web site application pool name.
/// </summary>
/// <param name="applicationPool">The application pool name</param>
/// <returns>The Iis7CreateWebSiteTask.</returns>
CreateWebsiteTask ApplicationPoolName(string applicationPool);
ICreateWebsiteTask ApplicationPoolName(string applicationPool);

/// <summary>
/// Add MimeType. Can be used multiple times.
/// </summary>
/// <param name="mimeType">The mime type</param>
/// <returns>The Iis7CreateWebSiteTask.</returns>
CreateWebsiteTask AddMimeType(MimeType mimeType);
ICreateWebsiteTask AddMimeType(MimeType mimeType);

ICreateWebsiteTask ForServer(string serverName);
}
}
2 changes: 2 additions & 0 deletions src/FlubuCore/Tasks/Iis/Interfaces/IDeleteAppPoolTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface IDeleteAppPoolTask : ITaskOfT<int, IDeleteAppPoolTask>
/// task fails with exception if application pool doesn't exists. Otherwise not.
/// </summary>
IDeleteAppPoolTask FailIfNotExist();

IDeleteAppPoolTask ForServer(string serverName);
}
}

0 comments on commit 520a931

Please sign in to comment.