From 78d8c8d0bd7edee8ac285d8f418e7f2e51c55890 Mon Sep 17 00:00:00 2001 From: code liturgy Date: Tue, 6 Dec 2022 14:33:12 +0000 Subject: [PATCH] Add methods for the Apache Manager --- CodeLiturgy.Views/Augeas/AugeasExtensions.cs | 4 +- CodeLiturgy.Views/Augeas/AugeasManager.cs | 63 ++++++++++++--- .../00-ci.codeliturgy.com-le-ssl.conf | 2 +- .../01-git.codeliturgy.com.conf | 2 +- CodeLiturgy.Views/CodeLiturgy.Views.csproj | 1 + .../Controllers/ApacheController.cs | 76 +++++++++++++++++++ CodeLiturgy.Views/Startup.cs | 4 +- include/Sharp.Augeas | 2 +- 8 files changed, 139 insertions(+), 15 deletions(-) create mode 100644 CodeLiturgy.Views/Controllers/ApacheController.cs diff --git a/CodeLiturgy.Views/Augeas/AugeasExtensions.cs b/CodeLiturgy.Views/Augeas/AugeasExtensions.cs index 5524ed9..249fe41 100644 --- a/CodeLiturgy.Views/Augeas/AugeasExtensions.cs +++ b/CodeLiturgy.Views/Augeas/AugeasExtensions.cs @@ -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); } diff --git a/CodeLiturgy.Views/Augeas/AugeasManager.cs b/CodeLiturgy.Views/Augeas/AugeasManager.cs index 8222827..5dd2e58 100644 --- a/CodeLiturgy.Views/Augeas/AugeasManager.cs +++ b/CodeLiturgy.Views/Augeas/AugeasManager.cs @@ -5,29 +5,74 @@ namespace CodeLiturgy.Views.Augeas public class AugeasManager { private readonly Sharp.Augeas.Augeas _augeas; + private AugSettings _augSettings; + public List CurrentApacheConfigurations => _currentApacheConfigurations; + + private List _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 GetApacheAvailableConfigurations() + public List GetApacheAvailableConfigurations(int skip, int take) { - DirectoryInfo d = new DirectoryInfo(_augSettings.); //Assuming Test is your Folder + var result = _currentApacheConfigurations + .Skip(skip) + .Take(take) + .ToList(); + + return _currentApacheConfigurations; - FileInfo[] Files = d.GetFiles("*.txt"); //Getting Text files - string str = ""; + } - foreach(FileInfo file in Files ) + public SuperNode GetVirtualHostTree(string site) + { + if (!_currentApacheConfigurations.Contains(site)) { - str = str + ", " + + 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; } } } diff --git a/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/00-ci.codeliturgy.com-le-ssl.conf b/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/00-ci.codeliturgy.com-le-ssl.conf index c209ed3..76391eb 100644 --- a/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/00-ci.codeliturgy.com-le-ssl.conf +++ b/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/00-ci.codeliturgy.com-le-ssl.conf @@ -1,5 +1,5 @@ - + ServerName www.example.com ServerAlias example.com #SSLEngine on diff --git a/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/01-git.codeliturgy.com.conf b/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/01-git.codeliturgy.com.conf index cbd8e1f..192b260 100644 --- a/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/01-git.codeliturgy.com.conf +++ b/CodeLiturgy.Views/Augeas/root/etc/apache2/sites-available/01-git.codeliturgy.com.conf @@ -1,4 +1,4 @@ - + ServerName git.example2.com ServerAlias www.git.example2.com SSLEngine on diff --git a/CodeLiturgy.Views/CodeLiturgy.Views.csproj b/CodeLiturgy.Views/CodeLiturgy.Views.csproj index d22ffe7..5ced886 100644 --- a/CodeLiturgy.Views/CodeLiturgy.Views.csproj +++ b/CodeLiturgy.Views/CodeLiturgy.Views.csproj @@ -29,6 +29,7 @@ + <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.css" /> diff --git a/CodeLiturgy.Views/Controllers/ApacheController.cs b/CodeLiturgy.Views/Controllers/ApacheController.cs new file mode 100644 index 0000000..f8017d4 --- /dev/null +++ b/CodeLiturgy.Views/Controllers/ApacheController.cs @@ -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 _logger; + private readonly SiteDbContext _siteDbContext; + private readonly AugeasManager _augeasManager; + + public ApacheController( + ApplicationUserManager userManager, + ILogger 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 GetSiteTree( + string site) + { + var tree = _augeasManager + .GetVirtualHostTree(site); + + return Ok(tree); + } + + + } +} + diff --git a/CodeLiturgy.Views/Startup.cs b/CodeLiturgy.Views/Startup.cs index 20a36d5..aff8878 100644 --- a/CodeLiturgy.Views/Startup.cs +++ b/CodeLiturgy.Views/Startup.cs @@ -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,7 +51,8 @@ public class Startup services.ConfigureSwagger(); } - + services.AddSingleton(); + services.AddControllersWithViews(x => x.EnableEndpointRouting = false); diff --git a/include/Sharp.Augeas b/include/Sharp.Augeas index f5c1368..eae24d1 160000 --- a/include/Sharp.Augeas +++ b/include/Sharp.Augeas @@ -1 +1 @@ -Subproject commit f5c1368b8fbcc0fa293e6d47ab3437fd060e290c +Subproject commit eae24d1b142738f40597284a24d637ad51506c0c