Refactored and cleaned some stuff
This commit is contained in:
@@ -105,7 +105,7 @@ public class ServerActions : ServiceActions
|
|||||||
serverRepo.Add(server);
|
serverRepo.Add(server);
|
||||||
|
|
||||||
await serverService.Sync(server);
|
await serverService.Sync(server);
|
||||||
await serverService.SendPowerAction(server, PowerAction.Install);
|
await serverService.Console.SendAction(server, PowerAction.Install);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task Update(IServiceProvider provider, Service service)
|
public override Task Update(IServiceProvider provider, Service service)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace Moonlight.Features.Servers.Api.Requests;
|
namespace Moonlight.Features.Servers.Api.Requests;
|
||||||
|
|
||||||
public class EnterCommand
|
public class SendCommand
|
||||||
{
|
{
|
||||||
public string Command { get; set; }
|
public string Command { get; set; }
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
using MoonCore.Abstractions;
|
||||||
|
using MoonCore.Attributes;
|
||||||
|
using MoonCore.Helpers;
|
||||||
|
using Moonlight.Features.Servers.Entities;
|
||||||
|
using Moonlight.Features.Servers.Services;
|
||||||
|
using BackgroundService = MoonCore.Abstractions.BackgroundService;
|
||||||
|
|
||||||
|
namespace Moonlight.Features.Servers.BackgroundServices;
|
||||||
|
|
||||||
|
[BackgroundService]
|
||||||
|
public class InitBackgroundService : BackgroundService
|
||||||
|
{
|
||||||
|
private readonly IServiceProvider ServiceProvider;
|
||||||
|
|
||||||
|
public InitBackgroundService(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
ServiceProvider = serviceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Run()
|
||||||
|
{
|
||||||
|
Logger.Info("Booting all nodes");
|
||||||
|
|
||||||
|
using var scope = ServiceProvider.CreateScope();
|
||||||
|
|
||||||
|
var nodeRepo = scope.ServiceProvider.GetRequiredService<Repository<ServerNode>>();
|
||||||
|
var nodeService = scope.ServiceProvider.GetRequiredService<NodeService>();
|
||||||
|
|
||||||
|
foreach (var node in nodeRepo.Get().ToArray())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await nodeService.Boot(node);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Logger.Warn($"An error occured while booting node '{node.Name}'");
|
||||||
|
Logger.Warn(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.Info("Booted all nodes");
|
||||||
|
}
|
||||||
|
}
|
||||||
16
Moonlight/Features/Servers/Extensions/NodeExtensions.cs
Normal file
16
Moonlight/Features/Servers/Extensions/NodeExtensions.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using MoonCore.Helpers;
|
||||||
|
using Moonlight.Features.Servers.Entities;
|
||||||
|
using Moonlight.Features.Servers.Exceptions;
|
||||||
|
|
||||||
|
namespace Moonlight.Features.Servers.Extensions;
|
||||||
|
|
||||||
|
public static class NodeExtensions
|
||||||
|
{
|
||||||
|
public static HttpApiClient<NodeException> CreateHttpClient(this ServerNode node)
|
||||||
|
{
|
||||||
|
var protocol = node.UseSsl ? "https" : "http";
|
||||||
|
var remoteUrl = $"{protocol}://{node.Fqdn}:{node.HttpPort}/";
|
||||||
|
|
||||||
|
return new HttpApiClient<NodeException>(remoteUrl, node.Token);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using MoonCore.Abstractions;
|
||||||
|
using MoonCore.Helpers;
|
||||||
using Moonlight.Features.Servers.Entities;
|
using Moonlight.Features.Servers.Entities;
|
||||||
|
using Moonlight.Features.Servers.Exceptions;
|
||||||
using Moonlight.Features.Servers.Models.Abstractions;
|
using Moonlight.Features.Servers.Models.Abstractions;
|
||||||
|
|
||||||
namespace Moonlight.Features.Servers.Extensions;
|
namespace Moonlight.Features.Servers.Extensions;
|
||||||
@@ -71,4 +75,17 @@ public static class ServerExtensions
|
|||||||
|
|
||||||
return installConfiguration;
|
return installConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HttpApiClient<NodeException> CreateHttpClient(this Server server, IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
using var scope = serviceProvider.CreateScope();
|
||||||
|
var serverRepo = scope.ServiceProvider.GetRequiredService<Repository<Server>>();
|
||||||
|
|
||||||
|
var serverWithNode = serverRepo
|
||||||
|
.Get()
|
||||||
|
.Include(x => x.Node)
|
||||||
|
.First(x => x.Id == server.Id);
|
||||||
|
|
||||||
|
return serverWithNode.Node.CreateHttpClient();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
using MoonCore.Attributes;
|
using MoonCore.Attributes;
|
||||||
|
using Moonlight.Features.Servers.Entities;
|
||||||
|
using Moonlight.Features.Servers.Extensions;
|
||||||
using Moonlight.Features.Servers.Helpers;
|
using Moonlight.Features.Servers.Helpers;
|
||||||
using Moonlight.Features.Servers.Models.Abstractions;
|
using Moonlight.Features.Servers.Models.Abstractions;
|
||||||
|
|
||||||
@@ -8,4 +10,17 @@ namespace Moonlight.Features.Servers.Services;
|
|||||||
public class NodeService
|
public class NodeService
|
||||||
{
|
{
|
||||||
public readonly MetaCache<NodeMeta> Meta = new();
|
public readonly MetaCache<NodeMeta> Meta = new();
|
||||||
|
|
||||||
|
private readonly IServiceProvider ServiceProvider;
|
||||||
|
|
||||||
|
public NodeService(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
ServiceProvider = serviceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Boot(ServerNode node)
|
||||||
|
{
|
||||||
|
using var httpClient = node.CreateHttpClient();
|
||||||
|
await httpClient.Post("system/boot");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
40
Moonlight/Features/Servers/Services/ServerConsoleService.cs
Normal file
40
Moonlight/Features/Servers/Services/ServerConsoleService.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
using MoonCore.Attributes;
|
||||||
|
using Moonlight.Features.Servers.Api.Requests;
|
||||||
|
using Moonlight.Features.Servers.Entities;
|
||||||
|
using Moonlight.Features.Servers.Extensions;
|
||||||
|
using Moonlight.Features.Servers.Models.Enums;
|
||||||
|
|
||||||
|
namespace Moonlight.Features.Servers.Services;
|
||||||
|
|
||||||
|
[Singleton]
|
||||||
|
public class ServerConsoleService
|
||||||
|
{
|
||||||
|
private readonly IServiceProvider ServiceProvider;
|
||||||
|
|
||||||
|
public ServerConsoleService(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
ServiceProvider = serviceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SendAction(Server server, PowerAction powerAction)
|
||||||
|
{
|
||||||
|
using var httpClient = server.CreateHttpClient(ServiceProvider);
|
||||||
|
await httpClient.Post($"servers/{server.Id}/power/{powerAction.ToString().ToLower()}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Subscribe(Server server)
|
||||||
|
{
|
||||||
|
using var httpClient = server.CreateHttpClient(ServiceProvider);
|
||||||
|
await httpClient.Post($"servers/{server.Id}/subscribe");
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SendCommand(Server server, string command)
|
||||||
|
{
|
||||||
|
using var httpClient = server.CreateHttpClient(ServiceProvider);
|
||||||
|
|
||||||
|
await httpClient.Post($"servers/{server.Id}/command", new SendCommand()
|
||||||
|
{
|
||||||
|
Command = command
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,8 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using MoonCore.Abstractions;
|
|
||||||
using MoonCore.Attributes;
|
using MoonCore.Attributes;
|
||||||
using MoonCore.Helpers;
|
|
||||||
using Moonlight.Features.Servers.Api.Requests;
|
|
||||||
using Moonlight.Features.Servers.Entities;
|
using Moonlight.Features.Servers.Entities;
|
||||||
using Moonlight.Features.Servers.Exceptions;
|
using Moonlight.Features.Servers.Extensions;
|
||||||
using Moonlight.Features.Servers.Helpers;
|
using Moonlight.Features.Servers.Helpers;
|
||||||
using Moonlight.Features.Servers.Models.Abstractions;
|
using Moonlight.Features.Servers.Models.Abstractions;
|
||||||
using Moonlight.Features.Servers.Models.Enums;
|
|
||||||
|
|
||||||
namespace Moonlight.Features.Servers.Services;
|
namespace Moonlight.Features.Servers.Services;
|
||||||
|
|
||||||
@@ -15,6 +10,7 @@ namespace Moonlight.Features.Servers.Services;
|
|||||||
public class ServerService
|
public class ServerService
|
||||||
{
|
{
|
||||||
public readonly MetaCache<ServerMeta> Meta = new();
|
public readonly MetaCache<ServerMeta> Meta = new();
|
||||||
|
public ServerConsoleService Console => ServiceProvider.GetRequiredService<ServerConsoleService>();
|
||||||
|
|
||||||
private readonly IServiceProvider ServiceProvider;
|
private readonly IServiceProvider ServiceProvider;
|
||||||
|
|
||||||
@@ -25,7 +21,7 @@ public class ServerService
|
|||||||
|
|
||||||
public async Task Sync(Server server)
|
public async Task Sync(Server server)
|
||||||
{
|
{
|
||||||
using var httpClient = CreateHttpClient(server);
|
using var httpClient = server.CreateHttpClient(ServiceProvider);
|
||||||
await httpClient.Post($"servers/{server.Id}/sync");
|
await httpClient.Post($"servers/{server.Id}/sync");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,42 +29,4 @@ public class ServerService
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendPowerAction(Server server, PowerAction powerAction)
|
|
||||||
{
|
|
||||||
using var httpClient = CreateHttpClient(server);
|
|
||||||
await httpClient.Post($"servers/{server.Id}/power/{powerAction.ToString().ToLower()}");
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task SubscribeToConsole(Server server)
|
|
||||||
{
|
|
||||||
using var httpClient = CreateHttpClient(server);
|
|
||||||
await httpClient.Post($"servers/{server.Id}/subscribe");
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task SendCommand(Server server, string command)
|
|
||||||
{
|
|
||||||
using var httpClient = CreateHttpClient(server);
|
|
||||||
|
|
||||||
await httpClient.Post($"servers/{server.Id}/command", new EnterCommand()
|
|
||||||
{
|
|
||||||
Command = command
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private HttpApiClient<NodeException> CreateHttpClient(Server server)
|
|
||||||
{
|
|
||||||
using var scope = ServiceProvider.CreateScope();
|
|
||||||
var serverRepo = scope.ServiceProvider.GetRequiredService<Repository<Server>>();
|
|
||||||
|
|
||||||
var serverWithNode = serverRepo
|
|
||||||
.Get()
|
|
||||||
.Include(x => x.Node)
|
|
||||||
.First(x => x.Id == server.Id);
|
|
||||||
|
|
||||||
var protocol = serverWithNode.Node.UseSsl ? "https" : "http";
|
|
||||||
var remoteUrl = $"{protocol}://{serverWithNode.Node.Fqdn}:{serverWithNode.Node.HttpPort}/";
|
|
||||||
|
|
||||||
return new HttpApiClient<NodeException>(remoteUrl, serverWithNode.Node.Token);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -245,7 +245,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Send console subscription and add auto resubscribe for it
|
// Send console subscription and add auto resubscribe for it
|
||||||
await ServerService.SubscribeToConsole(Server);
|
await ServerService.Console.Subscribe(Server);
|
||||||
|
|
||||||
// We need this to revalidate to the daemon that we are still interested
|
// We need this to revalidate to the daemon that we are still interested
|
||||||
// in the console logs. By default the expiration time is 15 minutes from last
|
// in the console logs. By default the expiration time is 15 minutes from last
|
||||||
@@ -255,7 +255,7 @@
|
|||||||
while (!BackgroundCancel.IsCancellationRequested)
|
while (!BackgroundCancel.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromMinutes(10));
|
await Task.Delay(TimeSpan.FromMinutes(10));
|
||||||
await ServerService.SubscribeToConsole(Server);
|
await ServerService.Console.Subscribe(Server);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -276,11 +276,11 @@
|
|||||||
await InstallTerminal.WriteLine(message);
|
await InstallTerminal.WriteLine(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task Start() => await ServerService.SendPowerAction(Server, PowerAction.Start);
|
private async Task Start() => await ServerService.Console.SendAction(Server, PowerAction.Start);
|
||||||
|
|
||||||
private async Task Stop() => await ServerService.SendPowerAction(Server, PowerAction.Stop);
|
private async Task Stop() => await ServerService.Console.SendAction(Server, PowerAction.Stop);
|
||||||
|
|
||||||
private async Task Kill() => await ServerService.SendPowerAction(Server, PowerAction.Kill);
|
private async Task Kill() => await ServerService.Console.SendAction(Server, PowerAction.Kill);
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -51,7 +51,7 @@
|
|||||||
|
|
||||||
private async Task SendCommand()
|
private async Task SendCommand()
|
||||||
{
|
{
|
||||||
await ServerService.SendCommand(Server, CommandInput);
|
await ServerService.Console.SendCommand(Server, CommandInput);
|
||||||
CommandInput = "";
|
CommandInput = "";
|
||||||
|
|
||||||
await InvokeAsync(StateHasChanged);
|
await InvokeAsync(StateHasChanged);
|
||||||
|
|||||||
@@ -38,7 +38,7 @@
|
|||||||
|
|
||||||
private async Task ResetServer()
|
private async Task ResetServer()
|
||||||
{
|
{
|
||||||
await ServerService.SendPowerAction(Server, PowerAction.Install);
|
await ServerService.Console.SendAction(Server, PowerAction.Install);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task OnStateChanged()
|
private async Task OnStateChanged()
|
||||||
|
|||||||
Reference in New Issue
Block a user