Skip to content

Commit

Permalink
feat: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-hoedl committed Mar 13, 2023
1 parent 5c99d37 commit ace7206
Show file tree
Hide file tree
Showing 36 changed files with 1,178 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,6 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/
/IISManager/local.db-wal
/IISManager/local.db-shm
/IISManager/local.db
29 changes: 29 additions & 0 deletions IISManager.Core/Domain/Common/AppPool.cs
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();
}
}
}
}
8 changes: 8 additions & 0 deletions IISManager.Core/Domain/Common/AppPoolApplication.cs
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; }
}
}
20 changes: 20 additions & 0 deletions IISManager.Core/Domain/Common/AppPoolGroup.cs
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()));
}
}
}
13 changes: 13 additions & 0 deletions IISManager.Core/Domain/Common/AppPoolGroupAppPool.cs
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; }
}
}
9 changes: 9 additions & 0 deletions IISManager.Core/Domain/Enums/AppPoolActionState.cs
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
}
}
11 changes: 11 additions & 0 deletions IISManager.Core/Domain/Enums/AppPoolStatus.cs
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
}
}
28 changes: 28 additions & 0 deletions IISManager.Core/IISManager.Core.csproj
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>
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 IISManager.Core/Infrastructure/Services/IISManagerService.cs
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 IISManager.Core/Migrations/20230313200757_Init.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ace7206

Please sign in to comment.