Add methods for the Apache Manager

This commit is contained in:
code liturgy 2022-12-06 14:33:12 +00:00
parent ce13a75aaa
commit 78d8c8d0bd
8 changed files with 139 additions and 15 deletions

View File

@ -7,8 +7,8 @@ public static class AugeasExtensions
public static AugSettings GetSettings()
{
// if DEBUG we use this
var rootDir = Environment.CurrentDirectory + "/root";
var lensDir = Environment.CurrentDirectory + "/lens";
var rootDir = Environment.CurrentDirectory + "/Augeas/root";
var lensDir = Environment.CurrentDirectory + "/Augeas/lens";
return new AugSettings(rootDir, lensDir);
}

View File

@ -5,29 +5,74 @@ namespace CodeLiturgy.Views.Augeas
public class AugeasManager
{
private readonly Sharp.Augeas.Augeas _augeas;
private AugSettings _augSettings;
public List<string> CurrentApacheConfigurations => _currentApacheConfigurations;
private List<string> _currentApacheConfigurations;
private const string _apachePath = "/etc/apache2/sites-available";
public AugeasManager()
{
_augeas = new Sharp.Augeas.Augeas(AugeasExtensions.GetSettings());
_augSettings = AugeasExtensions.GetSettings();
_augeas = new Sharp.Augeas.Augeas(_augSettings);
RefreshApacheConfigurations();
}
public string GetPreview()
public string GetPreview(string site)
{
var preview = _augeas.GetPreview("/files/etc/apache2/sites-available/example.com.conf");
if (!_currentApacheConfigurations.Contains(site))
{
return string.Empty;
}
var siteFullPath = $"{_apachePath}/{site}";
_augeas.LoadFile(siteFullPath);
var preview = _augeas.GetPreview($"/files{siteFullPath}");
return preview;
}
public List<string> GetApacheAvailableConfigurations()
public List<string> GetApacheAvailableConfigurations(int skip, int take)
{
DirectoryInfo d = new DirectoryInfo(_augSettings.); //Assuming Test is your Folder
var result = _currentApacheConfigurations
.Skip(skip)
.Take(take)
.ToList();
FileInfo[] Files = d.GetFiles("*.txt"); //Getting Text files
string str = "";
return _currentApacheConfigurations;
foreach(FileInfo file in Files )
}
public SuperNode GetVirtualHostTree(string site)
{
str = str + ", " +
if (!_currentApacheConfigurations.Contains(site))
{
return null;
}
var siteFullPath = $"{_apachePath}/{site}";
_augeas.LoadFile(siteFullPath);
var tree = _augeas.GetTree("VirtualHost",$"/files{siteFullPath}/VirtualHost/*");
return tree;
}
private void RefreshApacheConfigurations()
{
var apacheDir = _augSettings.root + _apachePath;
DirectoryInfo d = new DirectoryInfo(apacheDir);
FileInfo[] files = d.GetFiles("*.conf");
var result = files
.Select(x => x.Name)
.ToList();
_currentApacheConfigurations = result;
}
}
}

View File

@ -1,5 +1,5 @@
<IfModule mod_ssl.c>
<VirtualHost 91.218.230.167:443>
<VirtualHost 91.99.99.99:443>
ServerName www.example.com
ServerAlias example.com
#SSLEngine on

View File

@ -1,4 +1,4 @@
<VirtualHost 95.181.226.174:443>
<VirtualHost 10.10.10.10:443>
ServerName git.example2.com
ServerAlias www.git.example2.com
SSLEngine on

View File

@ -29,6 +29,7 @@
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.4.0" />
<PackageReference Include="System.Json" Version="4.7.1" />
</ItemGroup>
<ItemGroup>
<_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.css" />

View File

@ -0,0 +1,76 @@
using CodeLiturgy.Data.Auth.Context.Users;
using CodeLiturgy.Domain;
using CodeLiturgy.Views.Augeas;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Sharp.Augeas;
using System.Text.Json;
namespace CodeLiturgy.Views.Controllers
{
[ApiController]
[Authorize]
public class ApacheController : ControllerBase
{
private ILogger<SitesController> _logger;
private readonly SiteDbContext _siteDbContext;
private readonly AugeasManager _augeasManager;
public ApacheController(
ApplicationUserManager userManager,
ILogger<SitesController> logger,
SiteDbContext siteDbContext,
AugeasManager augeasManager)
{
_logger = logger;
_siteDbContext = siteDbContext;
_augeasManager = augeasManager;
}
[HttpGet("/api/apache")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public ActionResult GetSites(
int skip = 0, int take = 50, int orderDir = 1)
{
var sites = _augeasManager
.GetApacheAvailableConfigurations(skip, take);
return Ok(sites);
}
[HttpGet("/api/apache/preview/{site}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public ActionResult GetSitePreview(
string site)
{
var preview = _augeasManager
.GetPreview(site);
return Ok(preview);
}
[HttpGet("/api/apache/tree/{site}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public ActionResult<SuperNode> GetSiteTree(
string site)
{
var tree = _augeasManager
.GetVirtualHostTree(site);
return Ok(tree);
}
}
}

View File

@ -2,6 +2,7 @@ using System.Globalization;
using System.Reflection;
using CodeLiturgy.Data.Auth;
using CodeLiturgy.Startup.Application;
using CodeLiturgy.Views.Augeas;
using CodeLiturgy.Views.Utils;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Localization;
@ -50,6 +51,7 @@ public class Startup
services.ConfigureSwagger();
}
services.AddSingleton<AugeasManager>();
services.AddControllersWithViews(x => x.EnableEndpointRouting = false);

@ -1 +1 @@
Subproject commit f5c1368b8fbcc0fa293e6d47ab3437fd060e290c
Subproject commit eae24d1b142738f40597284a24d637ad51506c0c