Added node server sync and delete sync. Cleaned up codebase and extracted calls to apis to services
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -14,27 +14,6 @@ namespace MoonlightServers.ApiServer.Services;
|
||||
[Singleton]
|
||||
public class NodeService
|
||||
{
|
||||
public async Task<HttpApiClient> CreateApiClient(Node node)
|
||||
{
|
||||
var url = "";
|
||||
|
||||
if (node.UseSsl)
|
||||
url += "https://";
|
||||
else
|
||||
url += "http://";
|
||||
|
||||
url += $"{node.Fqdn}:{node.HttpPort}/";
|
||||
|
||||
var httpClient = new HttpClient()
|
||||
{
|
||||
BaseAddress = new Uri(url)
|
||||
};
|
||||
|
||||
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {node.Token}");
|
||||
|
||||
return new HttpApiClient(httpClient);
|
||||
}
|
||||
|
||||
public string CreateAccessToken(Node node, Action<Dictionary<string, object>> parameters, TimeSpan duration)
|
||||
{
|
||||
var claims = new Dictionary<string, object>();
|
||||
@@ -63,7 +42,7 @@ public class NodeService
|
||||
|
||||
public async Task<SystemStatusResponse> GetSystemStatus(Node node)
|
||||
{
|
||||
using var apiClient = await CreateApiClient(node);
|
||||
using var apiClient = CreateApiClient(node);
|
||||
return await apiClient.GetJson<SystemStatusResponse>("api/system/status");
|
||||
}
|
||||
|
||||
@@ -71,21 +50,66 @@ public class NodeService
|
||||
|
||||
public async Task<StatisticsApplicationResponse> GetApplicationStatistics(Node node)
|
||||
{
|
||||
using var apiClient = await CreateApiClient(node);
|
||||
using var apiClient = CreateApiClient(node);
|
||||
return await apiClient.GetJson<StatisticsApplicationResponse>("api/statistics/application");
|
||||
}
|
||||
|
||||
public async Task<StatisticsHostResponse> GetHostStatistics(Node node)
|
||||
{
|
||||
using var apiClient = await CreateApiClient(node);
|
||||
using var apiClient = CreateApiClient(node);
|
||||
return await apiClient.GetJson<StatisticsHostResponse>("api/statistics/host");
|
||||
}
|
||||
|
||||
public async Task<StatisticsDockerResponse> GetDockerStatistics(Node node)
|
||||
{
|
||||
using var apiClient = await CreateApiClient(node);
|
||||
using var apiClient = CreateApiClient(node);
|
||||
return await apiClient.GetJson<StatisticsDockerResponse>("api/statistics/docker");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helpers
|
||||
|
||||
private string GenerateJwt(Node node)
|
||||
{
|
||||
var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
|
||||
|
||||
var securityTokenDescriptor = new SecurityTokenDescriptor()
|
||||
{
|
||||
//Expires = DateTime.UtcNow.AddYears(1),
|
||||
Expires = DateTime.UtcNow.AddMinutes(1),
|
||||
NotBefore = DateTime.UtcNow.AddSeconds(-1),
|
||||
IssuedAt = DateTime.UtcNow,
|
||||
SigningCredentials = new SigningCredentials(
|
||||
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
|
||||
node.Token
|
||||
)),
|
||||
SecurityAlgorithms.HmacSha256
|
||||
)
|
||||
};
|
||||
|
||||
var securityToken = jwtSecurityTokenHandler.CreateJwtSecurityToken(securityTokenDescriptor);
|
||||
|
||||
return jwtSecurityTokenHandler.WriteToken(securityToken);
|
||||
}
|
||||
|
||||
public HttpApiClient CreateApiClient(Node node)
|
||||
{
|
||||
var url = "";
|
||||
|
||||
url += node.UseSsl ? "https://" : "http://";
|
||||
url += $"{node.Fqdn}:{node.HttpPort}/";
|
||||
|
||||
var httpClient = new HttpClient()
|
||||
{
|
||||
BaseAddress = new Uri(url)
|
||||
};
|
||||
|
||||
var jwt = GenerateJwt(node);
|
||||
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {jwt}");
|
||||
|
||||
return new HttpApiClient(httpClient);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
165
MoonlightServers.ApiServer/Services/ServerService.cs
Normal file
165
MoonlightServers.ApiServer/Services/ServerService.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Attributes;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Helpers;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Servers;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Services;
|
||||
|
||||
[Scoped]
|
||||
public class ServerService
|
||||
{
|
||||
private readonly NodeService NodeService;
|
||||
private readonly DatabaseRepository<Server> ServerRepository;
|
||||
|
||||
public ServerService(NodeService nodeService, DatabaseRepository<Server> serverRepository)
|
||||
{
|
||||
NodeService = nodeService;
|
||||
ServerRepository = serverRepository;
|
||||
}
|
||||
|
||||
#region Power Actions
|
||||
|
||||
public async Task Start(Server server)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var apiClient = await GetApiClient(server);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Stop(Server server)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var apiClient = await GetApiClient(server);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Kill(Server server)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var apiClient = await GetApiClient(server);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public async Task Install(Server server)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var apiClient = await GetApiClient(server);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Sync(Server server)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var apiClient = await GetApiClient(server);
|
||||
await apiClient.Post($"api/servers/{server.Id}/sync");
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
throw new HttpApiException("Unable to access the node the server is running on", 502);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SyncDelete(Server server)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var apiClient = await GetApiClient(server);
|
||||
await apiClient.Delete($"api/servers/{server.Id}");
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
throw new HttpApiException("Unable to access the node the server is running on", 502);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ServerStatusResponse> GetStatus(Server server)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var apiClient = await GetApiClient(server);
|
||||
return await apiClient.GetJson<ServerStatusResponse>($"api/servers/{server.Id}/status");
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
throw new HttpApiException("Unable to access the node the server is running on", 502);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ServerLogsResponse> GetLogs(Server server)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var apiClient = await GetApiClient(server);
|
||||
return await apiClient.GetJson<ServerLogsResponse>($"api/servers/{server.Id}/logs");
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
throw new HttpApiException("Unable to access the node the server is running on", 502);
|
||||
}
|
||||
}
|
||||
|
||||
#region Helpers
|
||||
|
||||
public bool IsAllowedToAccess(User user, Server server)
|
||||
{
|
||||
if (server.OwnerId == user.Id)
|
||||
return true;
|
||||
|
||||
var permissions = JsonSerializer.Deserialize<string[]>(
|
||||
user.PermissionsJson
|
||||
) ?? [];
|
||||
|
||||
return PermissionHelper.HasPermission(permissions, "admin.servers.get");
|
||||
}
|
||||
|
||||
private async Task<HttpApiClient> GetApiClient(Server server)
|
||||
{
|
||||
var serverWithNode = server;
|
||||
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
// It can be null when its not included when loading via ef !!!
|
||||
if (server.Node == null)
|
||||
{
|
||||
serverWithNode = await ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Node)
|
||||
.FirstAsync(x => x.Id == server.Id);
|
||||
}
|
||||
|
||||
return NodeService.CreateApiClient(serverWithNode.Node);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user