Started with servers sync to daemon
This commit is contained in:
@@ -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<Server> ServerRepository;
|
||||
private readonly ILogger<RemoteServersController> Logger;
|
||||
|
||||
public RemoteServersController(
|
||||
DatabaseRepository<Server> serverRepository,
|
||||
ILogger<RemoteServersController> logger
|
||||
)
|
||||
{
|
||||
ServerRepository = serverRepository;
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<PagedData<ServerDataResponse>> 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<ServerDataResponse>();
|
||||
|
||||
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<ServerDataResponse>()
|
||||
{
|
||||
Items = serverData.ToArray(),
|
||||
CurrentPage = page,
|
||||
PageSize = pageSize,
|
||||
TotalItems = total,
|
||||
TotalPages = total == 0 ? 0 : total / pageSize
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
7
MoonlightServers.Daemon/Models/Allocation.cs
Normal file
7
MoonlightServers.Daemon/Models/Allocation.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace MoonlightServers.Daemon.Models;
|
||||
|
||||
public class Allocation
|
||||
{
|
||||
public string IpAddress { get; set; }
|
||||
public int Port { get; set; }
|
||||
}
|
||||
22
MoonlightServers.Daemon/Models/ServerData.cs
Normal file
22
MoonlightServers.Daemon/Models/ServerData.cs
Normal file
@@ -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<Allocation> Allocations { get; set; }
|
||||
public Dictionary<string, string> Variables { get; set; }
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Docker.DotNet" Version="3.125.15" />
|
||||
<PackageReference Include="MoonCore" Version="1.8.0" />
|
||||
<PackageReference Include="MoonCore" Version="1.8.1" />
|
||||
<PackageReference Include="MoonCore.Extended" Version="1.2.4" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
|
||||
</ItemGroup>
|
||||
|
||||
38
MoonlightServers.Daemon/Services/ApplicationStateService.cs
Normal file
38
MoonlightServers.Daemon/Services/ApplicationStateService.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using MoonCore.Attributes;
|
||||
|
||||
namespace MoonlightServers.Daemon.Services;
|
||||
|
||||
[Singleton]
|
||||
public class ApplicationStateService : IHostedLifecycleService
|
||||
{
|
||||
private readonly ServerService ServerService;
|
||||
private readonly ILogger<ApplicationStateService> Logger;
|
||||
|
||||
public ApplicationStateService(ServerService serverService, ILogger<ApplicationStateService> 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;
|
||||
}
|
||||
44
MoonlightServers.Daemon/Services/ServerService.cs
Normal file
44
MoonlightServers.Daemon/Services/ServerService.cs
Normal file
@@ -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<ServerData> Servers = new();
|
||||
private readonly RemoteService RemoteService;
|
||||
private readonly ILogger<ServerService> Logger;
|
||||
|
||||
public ServerService(RemoteService remoteService, ILogger<ServerService> 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<ServerDataResponse>.All(async (page, pageSize) =>
|
||||
await apiClient.GetJson<PagedData<ServerDataResponse>>(
|
||||
$"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;
|
||||
}
|
||||
}
|
||||
@@ -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<ApplicationStateService>(
|
||||
sp => sp.GetRequiredService<ApplicationStateService>()
|
||||
);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task UseServers()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace MoonlightServers.DaemonShared.Http.Responses.Statistics;
|
||||
namespace MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Statistics;
|
||||
|
||||
public class StatisticsApplicationResponse
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace MoonlightServers.DaemonShared.Http.Responses.Statistics;
|
||||
namespace MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Statistics;
|
||||
|
||||
public class StatisticsDockerResponse
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace MoonlightServers.DaemonShared.Http.Responses.Statistics;
|
||||
namespace MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Statistics;
|
||||
|
||||
public class StatisticsHostResponse
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace MoonlightServers.DaemonShared.Http.Responses.Sys;
|
||||
namespace MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Sys;
|
||||
|
||||
public class SystemStatusResponse
|
||||
{
|
||||
@@ -7,7 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Http\Requests\" />
|
||||
<Folder Include="DaemonSide\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace MoonlightServers.DaemonShared.PanelSide.Http.Responses;
|
||||
|
||||
public class AllocationDataResponse
|
||||
{
|
||||
public string IpAddress { get; set; }
|
||||
public int Port { get; set; }
|
||||
}
|
||||
@@ -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<string, string> Variables { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user