Added node server sync and delete sync. Cleaned up codebase and extracted calls to apis to services

This commit is contained in:
2025-03-02 19:24:24 +01:00
parent ef7f866ded
commit 30390dab71
25 changed files with 751 additions and 282 deletions

View File

@@ -8,6 +8,7 @@ using MoonCore.Helpers;
using MoonCore.Models;
using Moonlight.ApiServer.Database.Entities;
using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.ApiServer.Services;
using MoonlightServers.Shared.Http.Requests.Admin.Servers;
using MoonlightServers.Shared.Http.Responses.Admin.Servers;
@@ -24,6 +25,8 @@ public class ServersController : Controller
private readonly DatabaseRepository<ServerVariable> VariableRepository;
private readonly DatabaseRepository<Server> ServerRepository;
private readonly DatabaseRepository<User> UserRepository;
private readonly ILogger<ServersController> Logger;
private readonly ServerService ServerService;
public ServersController(
CrudHelper<Server, ServerDetailResponse> crudHelper,
@@ -32,7 +35,10 @@ public class ServersController : Controller
DatabaseRepository<Allocation> allocationRepository,
DatabaseRepository<ServerVariable> variableRepository,
DatabaseRepository<Server> serverRepository,
DatabaseRepository<User> userRepository)
DatabaseRepository<User> userRepository,
ILogger<ServersController> logger,
ServerService serverService
)
{
CrudHelper = crudHelper;
StarRepository = starRepository;
@@ -41,6 +47,8 @@ public class ServersController : Controller
VariableRepository = variableRepository;
ServerRepository = serverRepository;
UserRepository = userRepository;
ServerService = serverService;
Logger = logger;
CrudHelper.QueryModifier = servers => servers
.Include(x => x.Node)
@@ -146,7 +154,7 @@ public class ServersController : Controller
foreach (var variable in star.Variables)
{
var requestVar = request.Variables.FirstOrDefault(x => x.Key == variable.Key);
var serverVar = new ServerVariable()
{
Key = variable.Key,
@@ -162,10 +170,23 @@ public class ServersController : Controller
server.Node = node;
server.Star = star;
// TODO: Call node
var finalServer = await ServerRepository.Add(server);
try
{
await ServerService.Sync(finalServer);
}
catch (Exception e)
{
Logger.LogError("Unable to sync server to node the server is assigned to: {e}", e);
// We are deleting the server from the database after the creation has failed
// to ensure we wont have a bugged server in the database which doesnt exist on the node
await ServerRepository.Remove(finalServer);
throw;
}
return CrudHelper.MapToResult(finalServer);
}
@@ -186,7 +207,7 @@ public class ServersController : Controller
.Where(x => x.Server == null || x.Server.Id == server.Id)
.Where(x => x.Node.Id == server.Node.Id)
.FirstOrDefaultAsync(x => x.Id == allocationId);
// ^ This loads the allocations specified in the request.
// Valid allocations are either free ones or ones which are already allocated to this server
@@ -207,31 +228,53 @@ public class ServersController : Controller
// Set allocations
server.Allocations = allocations;
// Process variables
foreach (var variable in request.Variables)
{
// Search server variable associated to the variable in the request
var serverVar = server.Variables
.FirstOrDefault(x => x.Key == variable.Key);
if(serverVar == null)
if (serverVar == null)
continue;
// Update value
serverVar.Value = variable.Value;
}
// TODO: Call node
await ServerRepository.Update(server);
// Notify the node about the changes
await ServerService.Sync(server);
return CrudHelper.MapToResult(server);
}
[HttpDelete("{id:int}")]
public async Task Delete([FromRoute] int id)
public async Task Delete([FromRoute] int id, [FromQuery] bool force = false)
{
var server = await CrudHelper.GetSingleModel(id);
try
{
// If the sync fails on the node and we aren't forcing the deletion,
// we don't want to delete it from the database yet
await ServerService.SyncDelete(server);
}
catch (Exception e)
{
if (force)
{
Logger.LogWarning(
"An error occured while syncing deletion of a server to the node. Continuing anyways. Error: {e}",
e
);
}
else
throw;
}
await CrudHelper.Delete(id);
}
}

View File

@@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
using MoonCore.Exceptions;
using MoonCore.Extended.Abstractions;
using MoonCore.Helpers;
using Moonlight.ApiServer.Database.Entities;
using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.ApiServer.Services;
@@ -15,113 +16,69 @@ namespace MoonlightServers.ApiServer.Http.Controllers.Client;
public class ServerPowerController : Controller
{
private readonly DatabaseRepository<Server> ServerRepository;
private readonly NodeService NodeService;
private readonly DatabaseRepository<User> UserRepository;
private readonly ServerService ServerService;
public ServerPowerController(DatabaseRepository<Server> serverRepository, NodeService nodeService)
public ServerPowerController(
DatabaseRepository<Server> serverRepository,
DatabaseRepository<User> userRepository,
ServerService serverService
)
{
ServerRepository = serverRepository;
NodeService = nodeService;
UserRepository = userRepository;
ServerService = serverService;
}
[HttpPost("{serverId:int}/start")]
[Authorize]
public async Task Start([FromRoute] int serverId)
{
var server = await GetServerWithPermCheck(serverId);
using var apiClient = await NodeService.CreateApiClient(server.Node);
try
{
await apiClient.Post($"api/servers/{server.Id}/start");
}
catch (HttpRequestException e)
{
throw new HttpApiException("Unable to access the node the server is running on", 502);
}
var server = await GetServerById(serverId);
await ServerService.Start(server);
}
[HttpPost("{serverId:int}/stop")]
[Authorize]
public async Task Stop([FromRoute] int serverId)
{
var server = await GetServerWithPermCheck(serverId);
using var apiClient = await NodeService.CreateApiClient(server.Node);
try
{
await apiClient.Post($"api/servers/{server.Id}/stop");
}
catch (HttpRequestException e)
{
throw new HttpApiException("Unable to access the node the server is running on", 502);
}
var server = await GetServerById(serverId);
await ServerService.Stop(server);
}
[HttpPost("{serverId:int}/kill")]
[Authorize]
public async Task Kill([FromRoute] int serverId)
{
var server = await GetServerWithPermCheck(serverId);
using var apiClient = await NodeService.CreateApiClient(server.Node);
try
{
await apiClient.Post($"api/servers/{server.Id}/kill");
}
catch (HttpRequestException e)
{
throw new HttpApiException("Unable to access the node the server is running on", 502);
}
var server = await GetServerById(serverId);
await ServerService.Kill(server);
}
[HttpPost("{serverId:int}/install")]
[Authorize]
public async Task Install([FromRoute] int serverId)
{
var server = await GetServerWithPermCheck(serverId);
using var apiClient = await NodeService.CreateApiClient(server.Node);
try
{
await apiClient.Post($"api/servers/{server.Id}/install");
}
catch (HttpRequestException e)
{
throw new HttpApiException("Unable to access the node the server is running on", 502);
}
var server = await GetServerById(serverId);
await ServerService.Install(server);
}
private async Task<Server> GetServerWithPermCheck(int serverId,
Func<IQueryable<Server>, IQueryable<Server>>? queryModifier = null)
private async Task<Server> GetServerById(int serverId)
{
var userIdClaim = User.Claims.First(x => x.Type == "userId");
var userId = int.Parse(userIdClaim.Value);
var query = ServerRepository
var server = await ServerRepository
.Get()
.Include(x => x.Node) as IQueryable<Server>;
if (queryModifier != null)
query = queryModifier.Invoke(query);
var server = await query
.Include(x => x.Node)
.FirstOrDefaultAsync(x => x.Id == serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
if (server.OwnerId == userId) // The current user is the owner
return server;
var userIdClaim = User.Claims.First(x => x.Type == "userId");
var userId = int.Parse(userIdClaim.Value);
var user = await UserRepository.Get().FirstAsync(x => x.Id == userId);
var permissions = User.Claims.First(x => x.Type == "permissions").Value.Split(";", StringSplitOptions.RemoveEmptyEntries);
if (PermissionHelper.HasPermission(permissions, "admin.servers.get")) // The current user is an admin
return server;
if (!ServerService.IsAllowedToAccess(user, server))
throw new HttpApiException("No server with this id found", 404);
throw new HttpApiException("No server with this id found", 404);
return server;
}
}

View File

@@ -3,8 +3,8 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MoonCore.Exceptions;
using MoonCore.Extended.Abstractions;
using MoonCore.Helpers;
using MoonCore.Models;
using Moonlight.ApiServer.Database.Entities;
using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.ApiServer.Extensions;
using MoonlightServers.ApiServer.Services;
@@ -17,13 +17,17 @@ namespace MoonlightServers.ApiServer.Http.Controllers.Client;
[Route("api/client/servers")]
public class ServersController : Controller
{
private readonly ServerService ServerService;
private readonly DatabaseRepository<Server> ServerRepository;
private readonly DatabaseRepository<User> UserRepository;
private readonly NodeService NodeService;
public ServersController(DatabaseRepository<Server> serverRepository, NodeService nodeService)
public ServersController(DatabaseRepository<Server> serverRepository, NodeService nodeService, ServerService serverService, DatabaseRepository<User> userRepository)
{
ServerRepository = serverRepository;
NodeService = nodeService;
ServerService = serverService;
UserRepository = userRepository;
}
[HttpGet]
@@ -71,13 +75,22 @@ public class ServersController : Controller
[Authorize]
public async Task<ServerDetailResponse> Get([FromRoute] int serverId)
{
var server = await GetServerWithPermCheck(
serverId,
query =>
query
.Include(x => x.Allocations)
.Include(x => x.Star)
);
var server = await ServerRepository
.Get()
.Include(x => x.Allocations)
.Include(x => x.Star)
.Include(x => x.Node)
.FirstOrDefaultAsync(x => x.Id == serverId);
if(server == null)
throw new HttpApiException("No server with this id found", 404);
var userIdClaim = User.Claims.First(x => x.Type == "userId");
var userId = int.Parse(userIdClaim.Value);
var user = await UserRepository.Get().FirstAsync(x => x.Id == userId);
if(!ServerService.IsAllowedToAccess(user, server))
throw new HttpApiException("No server with this id found", 404);
return new ServerDetailResponse()
{
@@ -98,32 +111,20 @@ public class ServersController : Controller
[Authorize]
public async Task<ServerStatusResponse> GetStatus([FromRoute] int serverId)
{
var server = await GetServerWithPermCheck(serverId);
var server = await GetServerById(serverId);
var status = await ServerService.GetStatus(server);
var apiClient = await NodeService.CreateApiClient(server.Node);
try
return new ServerStatusResponse()
{
var data = await apiClient.GetJson<DaemonShared.DaemonSide.Http.Responses.Servers.ServerStatusResponse>(
$"api/servers/{server.Id}/status"
);
return new ServerStatusResponse()
{
State = data.State.ToServerPowerState()
};
}
catch (HttpRequestException e)
{
throw new HttpApiException("Unable to access the node the server is running on", 502);
}
State = status.State.ToServerPowerState()
};
}
[HttpGet("{serverId:int}/ws")]
[Authorize]
public async Task<ServerWebSocketResponse> GetWebSocket([FromRoute] int serverId)
{
var server = await GetServerWithPermCheck(serverId);
var server = await GetServerById(serverId);
// TODO: Handle transparent node proxy
@@ -135,11 +136,7 @@ public class ServersController : Controller
var url = "";
if (server.Node.UseSsl)
url += "https://";
else
url += "http://";
url += server.Node.UseSsl ? "https://" : "http://";
url += $"{server.Node.Fqdn}:{server.Node.HttpPort}/api/servers/ws";
return new ServerWebSocketResponse()
@@ -153,54 +150,33 @@ public class ServersController : Controller
[Authorize]
public async Task<ServerLogsResponse> GetLogs([FromRoute] int serverId)
{
var server = await GetServerWithPermCheck(serverId);
var server = await GetServerById(serverId);
var apiClient = await NodeService.CreateApiClient(server.Node);
try
var logs = await ServerService.GetLogs(server);
return new ServerLogsResponse()
{
var data = await apiClient.GetJson<DaemonShared.DaemonSide.Http.Responses.Servers.ServerLogsResponse>(
$"api/servers/{server.Id}/logs"
);
return new ServerLogsResponse()
{
Messages = data.Messages
};
}
catch (HttpRequestException e)
{
throw new HttpApiException("Unable to access the node the server is running on", 502);
}
Messages = logs.Messages
};
}
private async Task<Server> GetServerWithPermCheck(int serverId,
Func<IQueryable<Server>, IQueryable<Server>>? queryModifier = null)
private async Task<Server> GetServerById(int serverId)
{
var server = await ServerRepository
.Get()
.Include(x => x.Node)
.FirstOrDefaultAsync(x => x.Id == serverId);
if(server == null)
throw new HttpApiException("No server with this id found", 404);
var userIdClaim = User.Claims.First(x => x.Type == "userId");
var userId = int.Parse(userIdClaim.Value);
var query = ServerRepository
.Get()
.Include(x => x.Node) as IQueryable<Server>;
if (queryModifier != null)
query = queryModifier.Invoke(query);
var server = await query
.FirstOrDefaultAsync(x => x.Id == serverId);
if (server == null)
var user = await UserRepository.Get().FirstAsync(x => x.Id == userId);
if(!ServerService.IsAllowedToAccess(user, server))
throw new HttpApiException("No server with this id found", 404);
if (server.OwnerId == userId) // The current user is the owner
return server;
var permissions = User.Claims.First(x => x.Type == "permissions").Value.Split(";", StringSplitOptions.RemoveEmptyEntries);
if (PermissionHelper.HasPermission(permissions, "admin.servers.get")) // The current user is an admin
return server;
throw new HttpApiException("No server with this id found", 404);
return server;
}
}

View File

@@ -37,7 +37,7 @@ public class ServersController : Controller
var node = await NodeRepository
.Get()
.FirstAsync(x => x.TokenId == tokenId);
var total = await ServerRepository
.Get()
.Where(x => x.Node.Id == node.Id)
@@ -58,47 +58,12 @@ public class ServersController : Controller
foreach (var server in servers)
{
var dockerImage = server.Star.DockerImages
.Skip(server.DockerImageIndex)
.FirstOrDefault();
var convertedData = ConvertToServerData(server);
if (dockerImage == null)
{
dockerImage = server.Star.DockerImages
.Skip(server.Star.DefaultDockerImage)
.FirstOrDefault();
}
if (dockerImage == null)
dockerImage = server.Star.DockerImages.LastOrDefault();
if (dockerImage == null)
{
Logger.LogWarning("Unable to map server data for server {id}: No docker image available", server.Id);
if (convertedData == null)
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
});
serverData.Add(convertedData);
}
return new PagedData<ServerDataResponse>()
@@ -111,6 +76,38 @@ public class ServersController : Controller
};
}
[HttpGet("{id:int}")]
public async Task<ServerDataResponse> Get([FromRoute] int id)
{
// Load the node via the token id
var tokenId = User.Claims.First(x => x.Type == "iss").Value;
var node = await NodeRepository
.Get()
.FirstAsync(x => x.TokenId == tokenId);
// Load the server with the star data attached. We filter by the node to ensure the node can only access
// servers linked to it
var server = await ServerRepository
.Get()
.Where(x => x.Node.Id == node.Id)
.Include(x => x.Star)
.ThenInclude(x => x.DockerImages)
.Include(x => x.Variables)
.Include(x => x.Allocations)
.FirstOrDefaultAsync(x => x.Id == id);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
var convertedData = ConvertToServerData(server);
if (convertedData == null)
throw new HttpApiException("An error occured while creating the server data model", 500);
return convertedData;
}
[HttpGet("{id:int}/install")]
public async Task<ServerInstallDataResponse> GetInstall([FromRoute] int id)
{
@@ -120,7 +117,7 @@ public class ServersController : Controller
var node = await NodeRepository
.Get()
.FirstAsync(x => x.TokenId == tokenId);
// Load the server with the star data attached. We filter by the node to ensure the node can only access
// servers linked to it
var server = await ServerRepository
@@ -139,4 +136,58 @@ public class ServersController : Controller
Shell = server.Star.InstallShell
};
}
private ServerDataResponse? ConvertToServerData(Server server)
{
// Find the docker image to use for this server
StarDockerImage? dockerImage = null;
// Handle server set image if specified
if (server.DockerImageIndex != -1)
{
dockerImage = server.Star.DockerImages
.Skip(server.DockerImageIndex)
.FirstOrDefault();
}
// Handle star default image if set
if (dockerImage == null && server.Star.DefaultDockerImage != -1)
{
dockerImage = server.Star.DockerImages
.Skip(server.Star.DefaultDockerImage)
.FirstOrDefault();
}
if (dockerImage == null)
dockerImage = server.Star.DockerImages.LastOrDefault();
if (dockerImage == null)
{
Logger.LogWarning("Unable to map server data for server {id}: No docker image available", server.Id);
return null;
}
// Convert model
return 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
};
}
}