127 lines
3.6 KiB
C#
127 lines
3.6 KiB
C#
using Sharp.Augeas;
|
|
|
|
namespace CodeLiturgy.Views.Augeas
|
|
{
|
|
/// <summary>
|
|
/// Manager class for Augeas.
|
|
/// </summary>
|
|
public class AugeasManager
|
|
{
|
|
private readonly Sharp.Augeas.Augeas _augeas;
|
|
|
|
private AugSettings _augSettings;
|
|
|
|
/// <summary>
|
|
/// Cached List with the current apache configurations.
|
|
/// </summary>
|
|
public List<string> CurrentApacheConfigurations => _currentApacheConfigurations;
|
|
|
|
private List<string> _currentApacheConfigurations;
|
|
|
|
private const string ApachePath = "/etc/apache2/sites-available";
|
|
|
|
|
|
/// <summary>
|
|
/// Augeas Manager constructor.
|
|
/// </summary>
|
|
public AugeasManager()
|
|
{
|
|
_augSettings = AugeasExtensions.GetSettings();
|
|
_augeas = new Sharp.Augeas.Augeas(_augSettings);
|
|
RefreshApacheConfigurations();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write Changes to the disk
|
|
/// </summary>
|
|
/// <returns>Returns true if the save was successful.</returns>
|
|
public bool Save()
|
|
{
|
|
return _augeas.Save();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a preview of the configuration.
|
|
/// </summary>
|
|
/// <param name="site"></param>
|
|
/// <returns></returns>
|
|
public string GetPreview(string site)
|
|
{
|
|
|
|
if (!_currentApacheConfigurations.Contains(site))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
var siteFullPath = $"{ApachePath}/{site}";
|
|
_augeas.LoadFile(siteFullPath);
|
|
var preview = _augeas.GetPreview($"/files{siteFullPath}");
|
|
return preview;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets available Apache configurations.
|
|
/// </summary>
|
|
/// <param name="skip"></param>
|
|
/// <param name="take"></param>
|
|
/// <returns></returns>
|
|
public List<string> GetApacheAvailableConfigurations(int skip, int take)
|
|
{
|
|
var result = _currentApacheConfigurations
|
|
.Skip(skip)
|
|
.Take(take)
|
|
.ToList();
|
|
|
|
return _currentApacheConfigurations;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets virtual host tree of a site.
|
|
/// </summary>
|
|
/// <param name="site">Name of the site.</param>
|
|
/// <returns></returns>
|
|
public SuperNode GetVirtualHostTree(string site)
|
|
{
|
|
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;
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
}
|
|
}
|
|
|