Started implementing server share backend. Redesigned server authorization for api calls. Refactored controller names for servers. Moved some responses to correct namespace

This commit is contained in:
2025-06-05 23:35:39 +02:00
parent 4b1045d629
commit 1ec4450040
37 changed files with 1169 additions and 139 deletions

View File

@@ -64,4 +64,25 @@ public class ServersController : Controller
Messages = messages
};
}
[HttpGet("{serverId:int}/stats")]
public Task<ServerStatsResponse> GetStats([FromRoute] int serverId)
{
var server = ServerService.Find(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
var statsSubSystem = server.GetRequiredSubSystem<StatsSubSystem>();
return Task.FromResult<ServerStatsResponse>(new()
{
CpuUsage = statsSubSystem.CurrentStats.CpuUsage,
MemoryUsage = statsSubSystem.CurrentStats.MemoryUsage,
NetworkRead = statsSubSystem.CurrentStats.NetworkRead,
NetworkWrite = statsSubSystem.CurrentStats.NetworkWrite,
IoRead = statsSubSystem.CurrentStats.IoRead,
IoWrite = statsSubSystem.CurrentStats.IoWrite
});
}
}

View File

@@ -8,6 +8,8 @@ namespace MoonlightServers.Daemon.ServerSystem.SubSystems;
public class StatsSubSystem : ServerSubSystem
{
public ServerStats CurrentStats { get; private set; }
private readonly DockerClient DockerClient;
private readonly IHubContext<ServerWebSocketHub> HubContext;
@@ -20,6 +22,8 @@ public class StatsSubSystem : ServerSubSystem
{
DockerClient = dockerClient;
HubContext = hubContext;
CurrentStats = new();
}
public Task Attach(string containerId)
@@ -44,6 +48,9 @@ public class StatsSubSystem : ServerSubSystem
{
var stats = ConvertToStats(response);
// Update current stats for usage of other components
CurrentStats = stats;
await HubContext.Clients
.Group(Configuration.Id.ToString())
.SendAsync("StatsUpdated", stats);
@@ -66,6 +73,9 @@ public class StatsSubSystem : ServerSubSystem
}
}
// Reset current stats
CurrentStats = new();
Logger.LogDebug("Stopped fetching container stats");
});
@@ -76,20 +86,16 @@ public class StatsSubSystem : ServerSubSystem
{
var result = new ServerStats();
// When killed this field will be null so we just return
if (response.CPUStats.CPUUsage == null)
return result;
#region CPU
if(response.CPUStats is { CPUUsage.PercpuUsage: not null }) // Sometimes some values are just null >:/
if(response.CPUStats != null && response.PreCPUStats.CPUUsage != null) // Sometimes some values are just null >:/
{
var cpuDelta = (float)response.CPUStats.CPUUsage.TotalUsage - response.PreCPUStats.CPUUsage.TotalUsage;
var cpuSystemDelta = (float)response.CPUStats.SystemUsage - response.PreCPUStats.SystemUsage;
var cpuCoreCount = (int)response.CPUStats.OnlineCPUs;
if (cpuCoreCount == 0)
if (cpuCoreCount == 0 && response.CPUStats.CPUUsage.PercpuUsage != null)
cpuCoreCount = response.CPUStats.CPUUsage.PercpuUsage.Count;
var cpuPercent = 0f;
@@ -115,10 +121,13 @@ public class StatsSubSystem : ServerSubSystem
#region Network
foreach (var network in response.Networks)
if (response.Networks != null)
{
result.NetworkRead += network.Value.RxBytes;
result.NetworkWrite += network.Value.TxBytes;
foreach (var network in response.Networks)
{
result.NetworkRead += network.Value.RxBytes;
result.NetworkWrite += network.Value.TxBytes;
}
}
#endregion

View File

@@ -92,6 +92,14 @@ public class StorageSubSystem : ServerSubSystem
public async Task Reinitialize()
{
if (IsInitialized && StateMachine.State != ServerState.Offline)
{
throw new HttpApiException(
"Unable to reinitialize storage sub system while the server is not offline",
400
);
}
IsInitialized = false;
await EnsureRuntimeVolumeCreated();
@@ -303,6 +311,22 @@ public class StorageSubSystem : ServerSubSystem
if (existingDiskInfo.Exists)
{
var expectedSize = ByteConverter.FromMegaBytes(Configuration.Disk).Bytes;
// If the disk size matches, we are done here
if (expectedSize == existingDiskInfo.Length)
{
Logger.LogDebug("Virtual disk size matches expected size");
return;
}
// We cant resize while the server is running as this would lead to possible file corruptions
// and crashes of the software the server is running
if (StateMachine.State != ServerState.Offline)
{
Logger.LogDebug("Skipping disk resizing while server is not offline");
await ConsoleSubSystem.WriteMoonlight("Skipping disk resizing as the server is not offline");
return;
}
if (expectedSize > existingDiskInfo.Length)
{

View File

@@ -78,12 +78,14 @@ public class ServerService : IHostedLifecycleService
Type[] subSystems =
[
// The restore sub system needs to be on top in order for the state machine having the
// correct state when all other sub systems initialize
typeof(RestoreSubSystem),
typeof(ProvisionSubSystem),
typeof(StorageSubSystem),
typeof(DebugSubSystem),
typeof(ShutdownSubSystem),
typeof(ConsoleSubSystem),
typeof(RestoreSubSystem),
typeof(OnlineDetectionService),
typeof(InstallationSubSystem),
typeof(StatsSubSystem)