CodeLiturgy.Dashboard/CodeLiturgy.Views/Augeas/AugeasManager.cs

127 lines
3.6 KiB
C#
Raw Permalink Normal View History

2022-12-05 02:07:41 +03:00
using Sharp.Augeas;
namespace CodeLiturgy.Views.Augeas
{
2022-12-09 03:27:00 +03:00
/// <summary>
/// Manager class for Augeas.
/// </summary>
2022-12-05 02:07:41 +03:00
public class AugeasManager
{
private readonly Sharp.Augeas.Augeas _augeas;
2022-12-06 17:33:12 +03:00
2022-12-05 02:07:41 +03:00
private AugSettings _augSettings;
2022-12-09 03:27:00 +03:00
/// <summary>
/// Cached List with the current apache configurations.
/// </summary>
2022-12-06 17:33:12 +03:00
public List<string> CurrentApacheConfigurations => _currentApacheConfigurations;
private List<string> _currentApacheConfigurations;
2022-12-09 03:27:00 +03:00
private const string ApachePath = "/etc/apache2/sites-available";
2022-12-06 17:33:12 +03:00
2022-12-09 03:27:00 +03:00
/// <summary>
/// Augeas Manager constructor.
/// </summary>
2022-12-05 02:07:41 +03:00
public AugeasManager()
{
2022-12-06 17:33:12 +03:00
_augSettings = AugeasExtensions.GetSettings();
_augeas = new Sharp.Augeas.Augeas(_augSettings);
RefreshApacheConfigurations();
2022-12-05 02:07:41 +03:00
}
2022-12-09 03:27:00 +03:00
/// <summary>
/// Write Changes to the disk
/// </summary>
/// <returns>Returns true if the save was successful.</returns>
public bool Save()
{
return _augeas.Save();
}
2022-12-05 02:07:41 +03:00
2022-12-09 03:27:00 +03:00
/// <summary>
/// Gets a preview of the configuration.
/// </summary>
/// <param name="site"></param>
/// <returns></returns>
2022-12-06 17:33:12 +03:00
public string GetPreview(string site)
2022-12-05 02:07:41 +03:00
{
2022-12-06 17:33:12 +03:00
if (!_currentApacheConfigurations.Contains(site))
{
return string.Empty;
}
2022-12-09 03:27:00 +03:00
var siteFullPath = $"{ApachePath}/{site}";
2022-12-06 17:33:12 +03:00
_augeas.LoadFile(siteFullPath);
var preview = _augeas.GetPreview($"/files{siteFullPath}");
2022-12-05 02:07:41 +03:00
return preview;
}
2022-12-09 03:27:00 +03:00
/// <summary>
/// Gets available Apache configurations.
/// </summary>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <returns></returns>
2022-12-06 17:33:12 +03:00
public List<string> GetApacheAvailableConfigurations(int skip, int take)
2022-12-05 02:07:41 +03:00
{
2022-12-06 17:33:12 +03:00
var result = _currentApacheConfigurations
.Skip(skip)
.Take(take)
.ToList();
return _currentApacheConfigurations;
2022-12-05 02:07:41 +03:00
2022-12-06 17:33:12 +03:00
}
2022-12-05 02:07:41 +03:00
2022-12-09 03:27:00 +03:00
/// <summary>
/// Gets virtual host tree of a site.
/// </summary>
/// <param name="site">Name of the site.</param>
/// <returns></returns>
2022-12-06 17:33:12 +03:00
public SuperNode GetVirtualHostTree(string site)
{
if (!_currentApacheConfigurations.Contains(site))
2022-12-05 02:07:41 +03:00
{
2022-12-06 17:33:12 +03:00
return null;
}
2022-12-09 03:27:00 +03:00
var siteFullPath = $"{ApachePath}/{site}";
2022-12-06 17:33:12 +03:00
_augeas.LoadFile(siteFullPath);
var tree = _augeas.GetTree("VirtualHost",$"/files{siteFullPath}/VirtualHost/*");
return tree;
}
private void RefreshApacheConfigurations()
{
2022-12-09 03:27:00 +03:00
var apacheDir = _augSettings.root + ApachePath;
2022-12-06 17:33:12 +03:00
DirectoryInfo d = new DirectoryInfo(apacheDir);
FileInfo[] files = d.GetFiles("*.conf");
var result = files
.Select(x => x.Name)
.ToList();
_currentApacheConfigurations = result;
2022-12-05 02:07:41 +03:00
}
2022-12-09 03:27:00 +03:00
/// <summary>
/// Gets a directive.
/// </summary>
/// <param name="siteName"></param>
/// <param name="directiveName"></param>
/// <returns></returns>
public Directive GetDirective(string siteName, string directiveName)
{
var tree = GetVirtualHostTree(siteName);
var directive = tree.GetDirective(directiveName);
return directive;
}
2022-12-05 02:07:41 +03:00
}
}