This commit is contained in:
Jöran Malek 2023-11-17 21:35:40 +01:00
parent 41b067cbb4
commit ef87548623
9 changed files with 107 additions and 33 deletions

View file

@ -0,0 +1,7 @@
using pdns_dhcp.Options;
namespace pdns_dhcp.Kea;
public class KeaDhcp4LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options)
{
}

View file

@ -0,0 +1,7 @@
using pdns_dhcp.Options;
namespace pdns_dhcp.Kea;
public class KeaDhcp6LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options)
{
}

View file

@ -1,5 +1,24 @@
using Microsoft.Extensions.Hosting;
using pdns_dhcp.Options;
using Stl.IO;
namespace pdns_dhcp.Kea;
public class KeaLeaseWatcher
public abstract class KeaDhcpLeaseWatcher : BackgroundService
{
private readonly FileSystemWatcher fsw;
protected KeaDhcpServerOptions Options { get; }
protected KeaDhcpLeaseWatcher(KeaDhcpServerOptions options)
{
Options = options;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
throw new NotImplementedException();
}
}

View file

@ -15,6 +15,7 @@ builder.Services.Configure<PowerDnsOptions>(builder.Configuration.GetRequiredSec
builder.Services.AddHostedService<DhcpLeaseWatcher>();
builder.Services.AddHostedService<PowerDnsBackend>();
builder.Services.AddTypedFactory<IDhcpLeaseWatcherFactory>();
builder.Services.AddTypedFactory<IPowerDnsFactory>();
builder.Build().Run();

View file

@ -1,3 +1,5 @@
using System.Collections.Immutable;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
@ -5,18 +7,52 @@ using pdns_dhcp.Options;
namespace pdns_dhcp.Services;
public class DhcpLeaseWatcher : BackgroundService
public class DhcpLeaseWatcher : IHostedService
{
public DhcpLeaseWatcher(IOptions<DhcpOptions> options)
private readonly ImmutableArray<IHostedService> _services;
public DhcpLeaseWatcher(IOptions<DhcpOptions> options, IDhcpLeaseWatcherFactory factory)
{
var dhcpOptions = options.Value;
var services = ImmutableArray.CreateBuilder<IHostedService>();
if (dhcpOptions.Kea is { } keaOptions)
{
if (keaOptions.Dhcp4 is { } dhcp4Options)
{
services.Add(factory.KeaDhcp4Watcher(dhcp4Options));
}
if (keaOptions.Dhcp6 is { } dhcp6Options)
{
services.Add(factory.KeaDhcp6Watcher(dhcp6Options));
}
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
_services = services.DrainToImmutable();
}
public Task StartAsync(CancellationToken cancellationToken)
{
throw new NotImplementedException();
Task[] tasks = new Task[_services.Length];
for (int i = 0; i < tasks.Length; i++)
{
tasks[i] = _services[i].StartAsync(cancellationToken);
}
return Task.WhenAll(tasks);
}
public async Task StopAsync(CancellationToken cancellationToken)
{
Task[] tasks = new Task[_services.Length];
for (int i = 0; i < tasks.Length; i++)
{
tasks[i] = _services[i].StopAsync(cancellationToken);
}
var waitTask = Task.WhenAll(tasks);
TaskCompletionSource taskCompletionSource = new();
using var registration = cancellationToken.Register(s => (s as TaskCompletionSource)!.SetCanceled(), taskCompletionSource);
await Task.WhenAny(waitTask, taskCompletionSource.Task).ConfigureAwait(continueOnCapturedContext: false);
}
}

View file

@ -1,8 +1,13 @@
using pdns_dhcp.Kea;
using pdns_dhcp.Options;
using Stl.Interception;
namespace pdns_dhcp.Services;
public interface IDhcpLeaseWatcherFactory : IRequiresFullProxy
{
KeaDhcp4LeaseWatcher KeaDhcp4Watcher(KeaDhcpServerOptions options);
KeaDhcp6LeaseWatcher KeaDhcp6Watcher(KeaDhcpServerOptions options);
}

View file

@ -1,9 +1,4 @@
{
"PowerDns": {
"Listener": {
"Socket": "/run/pdns-dhcp/pdns.sock"
}
},
"Dhcp": {
"Kea": {
"Dhcp4": {
@ -13,5 +8,10 @@
"Leases": "../..ext/kea/dhcp4.leases"
}
}
},
"PowerDns": {
"Listener": {
"Socket": "/run/pdns-dhcp/pdns.sock"
}
}
}

View file

@ -1,9 +1,4 @@
{
"PowerDns": {
"Listener": {
"Socket": "/run/pdns-dhcp/pdns.sock"
}
},
"Dhcp": {
"Kea": {
"Dhcp4": {
@ -13,5 +8,10 @@
"Leases": "/var/lib/kea/dhcp6.leases"
}
}
},
"PowerDns": {
"Listener": {
"Socket": "/run/pdns-dhcp/pdns.sock"
}
}
}

View file

@ -6,7 +6,6 @@
<RootNamespace>pdns_dhcp</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishTrimmed>true</PublishTrimmed>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
</PropertyGroup>
@ -14,8 +13,8 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Sep" Version="0.2.7" />
<PackageReference Include="Stl.Generators" Version="6.5.44" />
<PackageReference Include="Stl.Interception" Version="6.5.43" />
<PackageReference Include="Stl.Generators" Version="6.6.5" />
<PackageReference Include="Stl.Interception" Version="6.6.5" />
</ItemGroup>
</Project>