-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c99d37
commit ace7206
Showing
36 changed files
with
1,178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using IISManager.Core.Domain.Enums; | ||
using Microsoft.Web.Administration; | ||
|
||
namespace IISManager.Core.Domain.Common | ||
{ | ||
public class AppPool | ||
{ | ||
public string Name { get; set; } | ||
public AppPoolStatus Status { get; set; } | ||
public List<AppPoolApplication> Applications { get; set; } = new List<AppPoolApplication>(); | ||
|
||
public static AppPool Create(ApplicationPool pool) | ||
{ | ||
try | ||
{ | ||
|
||
return new AppPool | ||
{ | ||
Name = pool.Name, | ||
Status = (AppPoolStatus)pool.State | ||
}; | ||
} | ||
catch | ||
{ | ||
return new AppPool(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace IISManager.Core.Domain.Common | ||
{ | ||
public class AppPoolApplication | ||
{ | ||
public string Path { get; set; } | ||
public string PhysicalPath { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using IISManager.Core.Infrastructure.Services; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace IISManager.Core.Domain.Common | ||
{ | ||
public class AppPoolGroup | ||
{ | ||
[Key] | ||
public string Name { get; set; } | ||
|
||
public ICollection<AppPoolGroupAppPool> AssignedAppPoolNames { get; set; } = new List<AppPoolGroupAppPool>(); | ||
|
||
public IEnumerable<AppPool> GetAppPools(IISManagerService manager) | ||
{ | ||
var names = AssignedAppPoolNames.Select(x => x.Name.ToLower()); | ||
return manager.GetAppPools().Where(x => names.Contains(x.Name.ToLower())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace IISManager.Core.Domain.Common | ||
{ | ||
public class AppPoolGroupAppPool | ||
{ | ||
[Key] | ||
public int ID { get; set; } | ||
|
||
public string Name { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace IISManager.Core.Domain.Enums | ||
{ | ||
public enum AppPoolActionState | ||
{ | ||
Start, | ||
Stop, | ||
Recycle | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace IISManager.Core.Domain.Enums | ||
{ | ||
public enum AppPoolStatus | ||
{ | ||
Starting, | ||
Started, | ||
Stopping, | ||
Stopped, | ||
Unknown | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>disable</Nullable> | ||
<GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.14" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.14"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.14" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.14"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.Web.Administration" Version="11.1.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Migrations\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
19 changes: 19 additions & 0 deletions
19
IISManager.Core/Infrastructure/Infrastructure/Persistance/ApplicationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using IISManager.Core.Domain.Common; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace IISManager.Core.Infrastructure.Infrastructure.Persistance | ||
{ | ||
public class ApplicationContext : DbContext | ||
{ | ||
public DbSet<AppPoolGroup> AppPoolGroups { get; set; } | ||
|
||
public ApplicationContext(DbContextOptions options) : base(options) | ||
{ | ||
} | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
IISManager.Core/Infrastructure/Services/IISManagerService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
using IISManager.Core.Domain.Common; | ||
using IISManager.Core.Domain.Enums; | ||
using IISManager.Core.Infrastructure.Infrastructure.Persistance; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Web.Administration; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace IISManager.Core.Infrastructure.Services | ||
{ | ||
public class IISManagerService | ||
{ | ||
private readonly ServerManager manager; | ||
private readonly IDbContextFactory<ApplicationContext> contextFactory; | ||
|
||
public IISManagerService(IDbContextFactory<ApplicationContext> contextFactory) | ||
{ | ||
this.manager = new ServerManager(); | ||
this.contextFactory = contextFactory; | ||
} | ||
|
||
public IEnumerable<AppPool> GetAppPools() | ||
{ | ||
var applications = manager.Sites.SelectMany(x => x.Applications).GroupBy(x => x.ApplicationPoolName).ToDictionary(x => x.Key.ToLower(), y => y.ToList()); | ||
|
||
var retVal = new List<AppPool>(); | ||
|
||
foreach (var a in manager.ApplicationPools) | ||
{ | ||
var toAdd = new AppPool() | ||
{ | ||
Name = a.Name, | ||
Status = (AppPoolStatus)a.State | ||
}; | ||
|
||
if (applications.TryGetValue(toAdd.Name.ToLower(), out var apps)) | ||
{ | ||
toAdd.Applications = apps.Select(x => new AppPoolApplication() | ||
{ | ||
Path = x.Path, | ||
PhysicalPath = x.VirtualDirectories["/"].PhysicalPath | ||
}).ToList(); | ||
} | ||
|
||
retVal.Add(toAdd); | ||
} | ||
|
||
return retVal; | ||
} | ||
|
||
public async Task<IEnumerable<AppPoolGroup>> GetAppPoolGroups() | ||
{ | ||
using var context = contextFactory.CreateDbContext(); | ||
return await context.AppPoolGroups | ||
.Include(x => x.AssignedAppPoolNames) | ||
.ToListAsync(); | ||
} | ||
|
||
public void SetState(AppPoolGroup group, AppPoolActionState state) | ||
{ | ||
foreach (var pool in group.GetAppPools(this)) | ||
{ | ||
SetState(pool, state); | ||
} | ||
} | ||
|
||
public void SetState(AppPool pool, AppPoolActionState state) | ||
{ | ||
var foundPool = manager.ApplicationPools.FirstOrDefault(x => x.Name == pool.Name); | ||
|
||
if (foundPool is not null) | ||
{ | ||
switch (state) | ||
{ | ||
case AppPoolActionState.Start when foundPool.State == ObjectState.Stopped: | ||
foundPool.Start(); | ||
break; | ||
case AppPoolActionState.Stop when foundPool.State == ObjectState.Started: | ||
foundPool.Stop(); | ||
break; | ||
case AppPoolActionState.Recycle when foundPool.State == ObjectState.Started: | ||
foundPool.Recycle(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public bool AppPoolExists(string name) | ||
{ | ||
if(manager.ApplicationPools.FirstOrDefault(x => string.Compare(x.Name, name, true) == 0) is null) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public void CreateAppPool(string name, bool startImmediatley) | ||
{ | ||
if (manager.ApplicationPools.FirstOrDefault(x => string.Compare(x.Name, name, true) == 0) is null) | ||
{ | ||
var addedPool = manager.ApplicationPools.Add(name); | ||
manager.CommitChanges(); | ||
|
||
if (!startImmediatley) | ||
{ | ||
addedPool.Stop(); | ||
} | ||
} | ||
|
||
} | ||
|
||
public async Task CreateLogicalGroup(string name) | ||
{ | ||
using var context = await contextFactory.CreateDbContextAsync(); | ||
await context.AppPoolGroups.AddAsync(new AppPoolGroup { Name = name }); | ||
await context.SaveChangesAsync(); | ||
} | ||
|
||
public void DeleteAppPool(AppPool pool) | ||
{ | ||
var foundElement = manager.ApplicationPools.FirstOrDefault(x => x.Name == pool.Name); | ||
|
||
if(foundElement is not null) | ||
{ | ||
manager.ApplicationPools.Remove(foundElement); | ||
} | ||
} | ||
|
||
public async Task UpdateAppPoolGroup(AppPoolGroup group) | ||
{ | ||
using var context = await contextFactory.CreateDbContextAsync(); | ||
var x = context.AppPoolGroups.Update(group); | ||
await context.SaveChangesAsync(); | ||
} | ||
|
||
public async Task DeleteAppPoolGroup(AppPoolGroup group) | ||
{ | ||
using var context = await contextFactory.CreateDbContextAsync(); | ||
context.AppPoolGroups.Remove(group); | ||
await context.SaveChangesAsync(); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
IISManager.Core/Migrations/20230313200757_Init.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.