From 9f8c1f6d2461716cebce46dc0bfe35d82c4179b3 Mon Sep 17 00:00:00 2001 From: ChiaraBm Date: Tue, 24 Dec 2024 00:42:17 +0100 Subject: [PATCH] Started with servers sync to daemon --- .../Remote/Servers/RemoteServersController.cs | 99 +++++++++++++++++++ .../Services/NodeService.cs | 4 +- .../StatisticsApplicationController.cs | 2 +- .../Statistics/StatisticsDockerController.cs | 2 +- .../Statistics/StatisticsHostController.cs | 2 +- .../Controllers/Sys/SystemStatusController.cs | 2 +- MoonlightServers.Daemon/Models/Allocation.cs | 7 ++ MoonlightServers.Daemon/Models/ServerData.cs | 22 +++++ .../MoonlightServers.Daemon.csproj | 2 +- .../Services/ApplicationStateService.cs | 38 +++++++ .../Services/ServerService.cs | 44 +++++++++ MoonlightServers.Daemon/Startup.cs | 20 ++++ .../StatisticsApplicationResponse.cs | 2 +- .../Statistics/StatisticsDockerResponse.cs | 2 +- .../Statistics/StatisticsHostResponse.cs | 2 +- .../Responses/Sys/SystemStatusResponse.cs | 2 +- .../MoonlightServers.DaemonShared.csproj | 2 +- .../Http/Responses/AllocationDataResponse.cs | 7 ++ .../Http/Responses/ServerDataResponse.cs | 22 +++++ 19 files changed, 271 insertions(+), 12 deletions(-) create mode 100644 MoonlightServers.ApiServer/Http/Controllers/Remote/Servers/RemoteServersController.cs create mode 100644 MoonlightServers.Daemon/Models/Allocation.cs create mode 100644 MoonlightServers.Daemon/Models/ServerData.cs create mode 100644 MoonlightServers.Daemon/Services/ApplicationStateService.cs create mode 100644 MoonlightServers.Daemon/Services/ServerService.cs rename MoonlightServers.DaemonShared/{ => DaemonSide}/Http/Responses/Statistics/StatisticsApplicationResponse.cs (68%) rename MoonlightServers.DaemonShared/{ => DaemonSide}/Http/Responses/Statistics/StatisticsDockerResponse.cs (82%) rename MoonlightServers.DaemonShared/{ => DaemonSide}/Http/Responses/Statistics/StatisticsHostResponse.cs (53%) rename MoonlightServers.DaemonShared/{ => DaemonSide}/Http/Responses/Sys/SystemStatusResponse.cs (74%) create mode 100644 MoonlightServers.DaemonShared/PanelSide/Http/Responses/AllocationDataResponse.cs create mode 100644 MoonlightServers.DaemonShared/PanelSide/Http/Responses/ServerDataResponse.cs diff --git a/MoonlightServers.ApiServer/Http/Controllers/Remote/Servers/RemoteServersController.cs b/MoonlightServers.ApiServer/Http/Controllers/Remote/Servers/RemoteServersController.cs new file mode 100644 index 0000000..97a930e --- /dev/null +++ b/MoonlightServers.ApiServer/Http/Controllers/Remote/Servers/RemoteServersController.cs @@ -0,0 +1,99 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using MoonCore.Extended.Abstractions; +using MoonCore.Models; +using MoonlightServers.ApiServer.Database.Entities; +using MoonlightServers.DaemonShared.PanelSide.Http.Responses; + +namespace MoonlightServers.ApiServer.Http.Controllers.Remote.Servers; + +[ApiController] +[Route("api/servers/remote/servers")] +public class RemoteServersController : Controller +{ + private readonly DatabaseRepository ServerRepository; + private readonly ILogger Logger; + + public RemoteServersController( + DatabaseRepository serverRepository, + ILogger logger + ) + { + ServerRepository = serverRepository; + Logger = logger; + } + + [HttpGet] + public async Task> Get([FromQuery] int page, [FromQuery] int pageSize) + { + var total = await ServerRepository + .Get() + .Where(x => x.Node.Id == 1) + .CountAsync(); + + var servers = await ServerRepository + .Get() + .Where(x => x.Node.Id == 1) + .Include(x => x.Star) + .ThenInclude(x => x.DockerImages) + .Include(x => x.Variables) + .Include(x => x.Allocations) + .Skip(page * pageSize) + .Take(pageSize) + .ToArrayAsync(); + + var serverData = new List(); + + foreach (var server in servers) + { + var dockerImage = server.Star.DockerImages + .FirstOrDefault(x => x.Id == server.DockerImageIndex); + + if (dockerImage == null) + { + dockerImage = server.Star.DockerImages + .FirstOrDefault(x => x.Id == server.Star.DefaultDockerImage); + } + + if (dockerImage == null) + dockerImage = server.Star.DockerImages.FirstOrDefault(); + + if (dockerImage == null) + { + Logger.LogWarning("Unable to map server data for server {id}: No docker image available", server.Id); + continue; + } + + serverData.Add(new ServerDataResponse() + { + Id = server.Id, + StartupCommand = server.StartupOverride ?? server.Star.StartupCommand, + Allocations = server.Allocations.Select(x => new AllocationDataResponse() + { + IpAddress = x.IpAddress, + Port = x.Port + }).ToArray(), + Variables = server.Variables.ToDictionary(x => x.Key, x => x.Value), + Bandwidth = server.Bandwidth, + Cpu = server.Cpu, + Disk = server.Disk, + Memory = server.Memory, + OnlineDetection = server.Star.OnlineDetection, + DockerImage = dockerImage.Identifier, + PullDockerImage = dockerImage.AutoPulling, + ParseConiguration = server.Star.ParseConfiguration, + StopCommand = server.Star.StopCommand, + UseVirtualDisk = server.UseVirtualDisk + }); + } + + return new PagedData() + { + Items = serverData.ToArray(), + CurrentPage = page, + PageSize = pageSize, + TotalItems = total, + TotalPages = total == 0 ? 0 : total / pageSize + }; + } +} \ No newline at end of file diff --git a/MoonlightServers.ApiServer/Services/NodeService.cs b/MoonlightServers.ApiServer/Services/NodeService.cs index a7abb32..0f7d2cc 100644 --- a/MoonlightServers.ApiServer/Services/NodeService.cs +++ b/MoonlightServers.ApiServer/Services/NodeService.cs @@ -1,8 +1,8 @@ using MoonCore.Attributes; using MoonCore.Helpers; using MoonlightServers.ApiServer.Database.Entities; -using MoonlightServers.DaemonShared.Http.Responses.Statistics; -using MoonlightServers.DaemonShared.Http.Responses.Sys; +using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Statistics; +using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Sys; namespace MoonlightServers.ApiServer.Services; diff --git a/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsApplicationController.cs b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsApplicationController.cs index 7381488..20d7b8a 100644 --- a/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsApplicationController.cs +++ b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsApplicationController.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Mvc; using MoonlightServers.Daemon.Helpers; -using MoonlightServers.DaemonShared.Http.Responses.Statistics; +using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Statistics; namespace MoonlightServers.Daemon.Http.Controllers.Statistics; diff --git a/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsDockerController.cs b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsDockerController.cs index ca01872..c00337b 100644 --- a/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsDockerController.cs +++ b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsDockerController.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Mvc; using MoonlightServers.Daemon.Services; -using MoonlightServers.DaemonShared.Http.Responses.Statistics; +using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Statistics; namespace MoonlightServers.Daemon.Http.Controllers.Statistics; diff --git a/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsHostController.cs b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsHostController.cs index 1a97a0d..18226ac 100644 --- a/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsHostController.cs +++ b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsHostController.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Mvc; using MoonlightServers.Daemon.Helpers; -using MoonlightServers.DaemonShared.Http.Responses.Statistics; +using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Statistics; namespace MoonlightServers.Daemon.Http.Controllers.Statistics; diff --git a/MoonlightServers.Daemon/Http/Controllers/Sys/SystemStatusController.cs b/MoonlightServers.Daemon/Http/Controllers/Sys/SystemStatusController.cs index a9587c9..2e7a399 100644 --- a/MoonlightServers.Daemon/Http/Controllers/Sys/SystemStatusController.cs +++ b/MoonlightServers.Daemon/Http/Controllers/Sys/SystemStatusController.cs @@ -1,7 +1,7 @@ using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using MoonlightServers.Daemon.Services; -using MoonlightServers.DaemonShared.Http.Responses.Sys; +using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Sys; namespace MoonlightServers.Daemon.Http.Controllers.Sys; diff --git a/MoonlightServers.Daemon/Models/Allocation.cs b/MoonlightServers.Daemon/Models/Allocation.cs new file mode 100644 index 0000000..028303f --- /dev/null +++ b/MoonlightServers.Daemon/Models/Allocation.cs @@ -0,0 +1,7 @@ +namespace MoonlightServers.Daemon.Models; + +public class Allocation +{ + public string IpAddress { get; set; } + public int Port { get; set; } +} \ No newline at end of file diff --git a/MoonlightServers.Daemon/Models/ServerData.cs b/MoonlightServers.Daemon/Models/ServerData.cs new file mode 100644 index 0000000..7417fcc --- /dev/null +++ b/MoonlightServers.Daemon/Models/ServerData.cs @@ -0,0 +1,22 @@ +namespace MoonlightServers.Daemon.Models; + +public class ServerData +{ + public int Id { get; set; } + public string StartupCommand { get; set; } + public string OnlineDetection { get; set; } + public string StopCommand { get; set; } + public string DockerImage { get; set; } + public bool PullDockerImage { get; set; } + public string ParseConiguration { get; set; } + + public int Cpu { get; set; } + public int Memory { get; set; } + public int Disk { get; set; } + + public bool UseVirtualDisk { get; set; } + public int Bandwidth { get; set; } + + public List Allocations { get; set; } + public Dictionary Variables { get; set; } +} \ No newline at end of file diff --git a/MoonlightServers.Daemon/MoonlightServers.Daemon.csproj b/MoonlightServers.Daemon/MoonlightServers.Daemon.csproj index 8b513fe..57507e1 100644 --- a/MoonlightServers.Daemon/MoonlightServers.Daemon.csproj +++ b/MoonlightServers.Daemon/MoonlightServers.Daemon.csproj @@ -8,7 +8,7 @@ - + diff --git a/MoonlightServers.Daemon/Services/ApplicationStateService.cs b/MoonlightServers.Daemon/Services/ApplicationStateService.cs new file mode 100644 index 0000000..e3fe587 --- /dev/null +++ b/MoonlightServers.Daemon/Services/ApplicationStateService.cs @@ -0,0 +1,38 @@ +using MoonCore.Attributes; + +namespace MoonlightServers.Daemon.Services; + +[Singleton] +public class ApplicationStateService : IHostedLifecycleService +{ + private readonly ServerService ServerService; + private readonly ILogger Logger; + + public ApplicationStateService(ServerService serverService, ILogger logger) + { + ServerService = serverService; + Logger = logger; + } + + public Task StartAsync(CancellationToken cancellationToken) + => Task.CompletedTask; + + public Task StopAsync(CancellationToken cancellationToken) + => Task.CompletedTask; + + public async Task StartedAsync(CancellationToken cancellationToken) + { + Logger.LogInformation("Performing initialization"); + + await ServerService.Initialize(); + } + + public Task StartingAsync(CancellationToken cancellationToken) + => Task.CompletedTask; + + public Task StoppedAsync(CancellationToken cancellationToken) + => Task.CompletedTask; + + public Task StoppingAsync(CancellationToken cancellationToken) + => Task.CompletedTask; +} \ No newline at end of file diff --git a/MoonlightServers.Daemon/Services/ServerService.cs b/MoonlightServers.Daemon/Services/ServerService.cs new file mode 100644 index 0000000..109ff73 --- /dev/null +++ b/MoonlightServers.Daemon/Services/ServerService.cs @@ -0,0 +1,44 @@ +using MoonCore.Attributes; +using MoonCore.Models; +using MoonlightServers.Daemon.Models; +using MoonlightServers.DaemonShared.PanelSide.Http.Responses; + +namespace MoonlightServers.Daemon.Services; + +[Singleton] +public class ServerService +{ + private readonly List Servers = new(); + private readonly RemoteService RemoteService; + private readonly ILogger Logger; + + public ServerService(RemoteService remoteService, ILogger logger) + { + RemoteService = remoteService; + Logger = logger; + } + + public async Task Initialize() //TODO: Add initialize call from panel + { + //TODO: Handle block creating servers while initializing + Logger.LogInformation("Loading servers from panel"); + + var apiClient = await RemoteService.CreateHttpClient(); + + var servers = await PagedData.All(async (page, pageSize) => + await apiClient.GetJson>( + $"api/servers/remote/servers?page={page}&pageSize={pageSize}" + ) + ); + + Logger.LogInformation("Initializing {count} servers", servers.Length); + } + + public Task ImportServer(ServerData serverData) + { + lock (Servers) + Servers.Add(serverData); + + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/MoonlightServers.Daemon/Startup.cs b/MoonlightServers.Daemon/Startup.cs index cfb1029..f42ed86 100644 --- a/MoonlightServers.Daemon/Startup.cs +++ b/MoonlightServers.Daemon/Startup.cs @@ -6,6 +6,7 @@ using MoonCore.Extensions; using MoonCore.Helpers; using MoonCore.Services; using MoonlightServers.Daemon.Configuration; +using MoonlightServers.Daemon.Services; namespace MoonlightServers.Daemon; @@ -41,6 +42,7 @@ public class Startup await RegisterLogging(); await RegisterBase(); await RegisterDocker(); + await RegisterServers(); await BuildWebApplication(); @@ -238,4 +240,22 @@ public class Startup } #endregion + + #region Servers + + private Task RegisterServers() + { + WebApplicationBuilder.Services.AddHostedService( + sp => sp.GetRequiredService() + ); + + return Task.CompletedTask; + } + + private Task UseServers() + { + return Task.CompletedTask; + } + + #endregion } \ No newline at end of file diff --git a/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsApplicationResponse.cs b/MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Statistics/StatisticsApplicationResponse.cs similarity index 68% rename from MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsApplicationResponse.cs rename to MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Statistics/StatisticsApplicationResponse.cs index 6ba6f2c..74624dd 100644 --- a/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsApplicationResponse.cs +++ b/MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Statistics/StatisticsApplicationResponse.cs @@ -1,4 +1,4 @@ -namespace MoonlightServers.DaemonShared.Http.Responses.Statistics; +namespace MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Statistics; public class StatisticsApplicationResponse { diff --git a/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsDockerResponse.cs b/MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Statistics/StatisticsDockerResponse.cs similarity index 82% rename from MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsDockerResponse.cs rename to MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Statistics/StatisticsDockerResponse.cs index 04667ab..9bc35e6 100644 --- a/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsDockerResponse.cs +++ b/MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Statistics/StatisticsDockerResponse.cs @@ -1,4 +1,4 @@ -namespace MoonlightServers.DaemonShared.Http.Responses.Statistics; +namespace MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Statistics; public class StatisticsDockerResponse { diff --git a/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsHostResponse.cs b/MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Statistics/StatisticsHostResponse.cs similarity index 53% rename from MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsHostResponse.cs rename to MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Statistics/StatisticsHostResponse.cs index c7e7dc5..f97e6dd 100644 --- a/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsHostResponse.cs +++ b/MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Statistics/StatisticsHostResponse.cs @@ -1,4 +1,4 @@ -namespace MoonlightServers.DaemonShared.Http.Responses.Statistics; +namespace MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Statistics; public class StatisticsHostResponse { diff --git a/MoonlightServers.DaemonShared/Http/Responses/Sys/SystemStatusResponse.cs b/MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Sys/SystemStatusResponse.cs similarity index 74% rename from MoonlightServers.DaemonShared/Http/Responses/Sys/SystemStatusResponse.cs rename to MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Sys/SystemStatusResponse.cs index 87feef4..505c519 100644 --- a/MoonlightServers.DaemonShared/Http/Responses/Sys/SystemStatusResponse.cs +++ b/MoonlightServers.DaemonShared/DaemonSide/Http/Responses/Sys/SystemStatusResponse.cs @@ -1,4 +1,4 @@ -namespace MoonlightServers.DaemonShared.Http.Responses.Sys; +namespace MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Sys; public class SystemStatusResponse { diff --git a/MoonlightServers.DaemonShared/MoonlightServers.DaemonShared.csproj b/MoonlightServers.DaemonShared/MoonlightServers.DaemonShared.csproj index f217468..9e728c3 100644 --- a/MoonlightServers.DaemonShared/MoonlightServers.DaemonShared.csproj +++ b/MoonlightServers.DaemonShared/MoonlightServers.DaemonShared.csproj @@ -7,7 +7,7 @@ - + diff --git a/MoonlightServers.DaemonShared/PanelSide/Http/Responses/AllocationDataResponse.cs b/MoonlightServers.DaemonShared/PanelSide/Http/Responses/AllocationDataResponse.cs new file mode 100644 index 0000000..eb8b1fd --- /dev/null +++ b/MoonlightServers.DaemonShared/PanelSide/Http/Responses/AllocationDataResponse.cs @@ -0,0 +1,7 @@ +namespace MoonlightServers.DaemonShared.PanelSide.Http.Responses; + +public class AllocationDataResponse +{ + public string IpAddress { get; set; } + public int Port { get; set; } +} \ No newline at end of file diff --git a/MoonlightServers.DaemonShared/PanelSide/Http/Responses/ServerDataResponse.cs b/MoonlightServers.DaemonShared/PanelSide/Http/Responses/ServerDataResponse.cs new file mode 100644 index 0000000..db7bae3 --- /dev/null +++ b/MoonlightServers.DaemonShared/PanelSide/Http/Responses/ServerDataResponse.cs @@ -0,0 +1,22 @@ +namespace MoonlightServers.DaemonShared.PanelSide.Http.Responses; + +public class ServerDataResponse +{ + public int Id { get; set; } + public string StartupCommand { get; set; } + public string OnlineDetection { get; set; } + public string StopCommand { get; set; } + public string DockerImage { get; set; } + public bool PullDockerImage { get; set; } + public string ParseConiguration { get; set; } + + public int Cpu { get; set; } + public int Memory { get; set; } + public int Disk { get; set; } + + public bool UseVirtualDisk { get; set; } + public int Bandwidth { get; set; } + + public AllocationDataResponse[] Allocations { get; set; } + public Dictionary Variables { get; set; } +} \ No newline at end of file