Extracted all TODO static values to configuration parameters
This commit is contained in:
@@ -21,6 +21,7 @@ public partial class Server
|
|||||||
var dockerClient = ServiceProvider.GetRequiredService<DockerClient>();
|
var dockerClient = ServiceProvider.GetRequiredService<DockerClient>();
|
||||||
|
|
||||||
var parameters = Configuration.ToRuntimeCreateParameters(
|
var parameters = Configuration.ToRuntimeCreateParameters(
|
||||||
|
appConfiguration: AppConfiguration,
|
||||||
hostPath: RuntimeVolumePath,
|
hostPath: RuntimeVolumePath,
|
||||||
containerName: RuntimeContainerName
|
containerName: RuntimeContainerName
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Docker.DotNet;
|
using Docker.DotNet;
|
||||||
|
using MoonlightServers.Daemon.Configuration;
|
||||||
|
|
||||||
namespace MoonlightServers.Daemon.Abstractions;
|
namespace MoonlightServers.Daemon.Abstractions;
|
||||||
|
|
||||||
@@ -19,12 +20,11 @@ public partial class Server
|
|||||||
if (container.State.Running)
|
if (container.State.Running)
|
||||||
{
|
{
|
||||||
// Stop container when running
|
// Stop container when running
|
||||||
|
|
||||||
await LogToConsole("Stopping container");
|
await LogToConsole("Stopping container");
|
||||||
|
|
||||||
await dockerClient.Containers.StopContainerAsync(container.ID, new()
|
await dockerClient.Containers.StopContainerAsync(container.ID, new()
|
||||||
{
|
{
|
||||||
WaitBeforeKillSeconds = 30 // TODO: Config
|
WaitBeforeKillSeconds = (uint)AppConfiguration.Server.WaitBeforeKillSeconds
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ public partial class Server
|
|||||||
|
|
||||||
// Creating container configuration
|
// Creating container configuration
|
||||||
var parameters = Configuration.ToInstallationCreateParameters(
|
var parameters = Configuration.ToInstallationCreateParameters(
|
||||||
|
appConfiguration: AppConfiguration,
|
||||||
RuntimeVolumePath,
|
RuntimeVolumePath,
|
||||||
InstallationVolumePath,
|
InstallationVolumePath,
|
||||||
InstallationContainerName,
|
InstallationContainerName,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Docker.DotNet.Models;
|
using Docker.DotNet.Models;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using MoonlightServers.Daemon.Configuration;
|
||||||
using MoonlightServers.Daemon.Enums;
|
using MoonlightServers.Daemon.Enums;
|
||||||
using MoonlightServers.Daemon.Http.Hubs;
|
using MoonlightServers.Daemon.Http.Hubs;
|
||||||
using MoonlightServers.Daemon.Models;
|
using MoonlightServers.Daemon.Models;
|
||||||
@@ -36,20 +37,23 @@ public partial class Server
|
|||||||
private StateMachine<ServerState, ServerTrigger> StateMachine;
|
private StateMachine<ServerState, ServerTrigger> StateMachine;
|
||||||
private ServerConfiguration Configuration;
|
private ServerConfiguration Configuration;
|
||||||
private CancellationTokenSource Cancellation;
|
private CancellationTokenSource Cancellation;
|
||||||
|
private AppConfiguration AppConfiguration;
|
||||||
|
|
||||||
public Server(
|
public Server(
|
||||||
ILogger logger,
|
ILogger logger,
|
||||||
IServiceProvider serviceProvider,
|
IServiceProvider serviceProvider,
|
||||||
ServerConfiguration configuration,
|
ServerConfiguration configuration,
|
||||||
IHubContext<ServerWebSocketHub> webSocketHub
|
IHubContext<ServerWebSocketHub> webSocketHub,
|
||||||
|
AppConfiguration appConfiguration
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
Logger = logger;
|
Logger = logger;
|
||||||
ServiceProvider = serviceProvider;
|
ServiceProvider = serviceProvider;
|
||||||
Configuration = configuration;
|
Configuration = configuration;
|
||||||
WebSocketHub = webSocketHub;
|
WebSocketHub = webSocketHub;
|
||||||
|
AppConfiguration = appConfiguration;
|
||||||
|
|
||||||
Console = new();
|
Console = new(AppConfiguration.Server.ConsoleMessageCacheLimit);
|
||||||
Cancellation = new();
|
Cancellation = new();
|
||||||
|
|
||||||
RuntimeContainerName = $"moonlight-runtime-{Configuration.Id}";
|
RuntimeContainerName = $"moonlight-runtime-{Configuration.Id}";
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ public class AppConfiguration
|
|||||||
public SecurityData Security { get; set; } = new();
|
public SecurityData Security { get; set; } = new();
|
||||||
public RemoteData Remote { get; set; } = new();
|
public RemoteData Remote { get; set; } = new();
|
||||||
public FilesData Files { get; set; } = new();
|
public FilesData Files { get; set; } = new();
|
||||||
|
public ServerData Server { get; set; } = new();
|
||||||
|
|
||||||
public class RemoteData
|
public class RemoteData
|
||||||
{
|
{
|
||||||
@@ -45,6 +46,15 @@ public class AppConfiguration
|
|||||||
|
|
||||||
public class FilesData
|
public class FilesData
|
||||||
{
|
{
|
||||||
public int UploadLimit { get; set; } = 500;
|
public int UploadSizeLimit { get; set; } = 1024 * 2;
|
||||||
|
public int UploadChunkSize { get; set; } = 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ServerData
|
||||||
|
{
|
||||||
|
public int WaitBeforeKillSeconds { get; set; } = 30;
|
||||||
|
public int TmpFsSize { get; set; } = 100;
|
||||||
|
public float MemoryOverheadMultiplier { get; set; } = 0.05f;
|
||||||
|
public int ConsoleMessageCacheLimit { get; set; } = 250;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using Docker.DotNet.Models;
|
using Docker.DotNet.Models;
|
||||||
using Mono.Unix.Native;
|
using Mono.Unix.Native;
|
||||||
using MoonCore.Helpers;
|
using MoonCore.Helpers;
|
||||||
|
using MoonlightServers.Daemon.Configuration;
|
||||||
using MoonlightServers.Daemon.Models.Cache;
|
using MoonlightServers.Daemon.Models.Cache;
|
||||||
using MoonlightServers.DaemonShared.PanelSide.Http.Responses;
|
using MoonlightServers.DaemonShared.PanelSide.Http.Responses;
|
||||||
|
|
||||||
@@ -31,10 +32,14 @@ public static class ServerConfigurationExtensions
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CreateContainerParameters ToRuntimeCreateParameters(this ServerConfiguration configuration,
|
public static CreateContainerParameters ToRuntimeCreateParameters(
|
||||||
string hostPath, string containerName)
|
this ServerConfiguration configuration,
|
||||||
|
AppConfiguration appConfiguration,
|
||||||
|
string hostPath,
|
||||||
|
string containerName
|
||||||
|
)
|
||||||
{
|
{
|
||||||
var parameters = configuration.ToSharedCreateParameters();
|
var parameters = configuration.ToSharedCreateParameters(appConfiguration);
|
||||||
|
|
||||||
#region Security
|
#region Security
|
||||||
|
|
||||||
@@ -150,6 +155,7 @@ public static class ServerConfigurationExtensions
|
|||||||
|
|
||||||
public static CreateContainerParameters ToInstallationCreateParameters(
|
public static CreateContainerParameters ToInstallationCreateParameters(
|
||||||
this ServerConfiguration configuration,
|
this ServerConfiguration configuration,
|
||||||
|
AppConfiguration appConfiguration,
|
||||||
string runtimeHostPath,
|
string runtimeHostPath,
|
||||||
string installationHostPath,
|
string installationHostPath,
|
||||||
string containerName,
|
string containerName,
|
||||||
@@ -157,7 +163,7 @@ public static class ServerConfigurationExtensions
|
|||||||
string installShell
|
string installShell
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var parameters = configuration.ToSharedCreateParameters();
|
var parameters = configuration.ToSharedCreateParameters(appConfiguration);
|
||||||
|
|
||||||
// - Name
|
// - Name
|
||||||
parameters.Name = containerName;
|
parameters.Name = containerName;
|
||||||
@@ -212,7 +218,8 @@ public static class ServerConfigurationExtensions
|
|||||||
return parameters;
|
return parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static CreateContainerParameters ToSharedCreateParameters(this ServerConfiguration configuration)
|
private static CreateContainerParameters ToSharedCreateParameters(this ServerConfiguration configuration,
|
||||||
|
AppConfiguration appConfiguration)
|
||||||
{
|
{
|
||||||
var parameters = new CreateContainerParameters()
|
var parameters = new CreateContainerParameters()
|
||||||
{
|
{
|
||||||
@@ -242,7 +249,7 @@ public static class ServerConfigurationExtensions
|
|||||||
var memoryLimit = configuration.Memory;
|
var memoryLimit = configuration.Memory;
|
||||||
|
|
||||||
// The overhead multiplier gives the container a little bit more memory to prevent crashes
|
// The overhead multiplier gives the container a little bit more memory to prevent crashes
|
||||||
var memoryOverhead = memoryLimit + (memoryLimit * 0.05f); // TODO: Config
|
var memoryOverhead = memoryLimit + (memoryLimit * appConfiguration.Server.MemoryOverheadMultiplier);
|
||||||
|
|
||||||
long swapLimit = -1;
|
long swapLimit = -1;
|
||||||
|
|
||||||
@@ -287,7 +294,7 @@ public static class ServerConfigurationExtensions
|
|||||||
|
|
||||||
parameters.HostConfig.Tmpfs = new Dictionary<string, string>()
|
parameters.HostConfig.Tmpfs = new Dictionary<string, string>()
|
||||||
{
|
{
|
||||||
{ "/tmp", $"rw,exec,nosuid,size=100M" } // TODO: Config
|
{ "/tmp", $"rw,exec,nosuid,size={appConfiguration.Server.TmpFsSize}M" }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -16,8 +16,6 @@ public class UploadController : Controller
|
|||||||
private readonly AppConfiguration Configuration;
|
private readonly AppConfiguration Configuration;
|
||||||
private readonly ServerService ServerService;
|
private readonly ServerService ServerService;
|
||||||
|
|
||||||
private readonly long ChunkSize = ByteConverter.FromMegaBytes(20).Bytes; // TODO config
|
|
||||||
|
|
||||||
public UploadController(
|
public UploadController(
|
||||||
ServerService serverService,
|
ServerService serverService,
|
||||||
AppConfiguration configuration
|
AppConfiguration configuration
|
||||||
@@ -29,11 +27,14 @@ public class UploadController : Controller
|
|||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task Upload(
|
public async Task Upload(
|
||||||
[FromQuery] long totalSize, // TODO: Add limit in config
|
[FromQuery] long totalSize,
|
||||||
[FromQuery] int chunkId,
|
[FromQuery] int chunkId,
|
||||||
[FromQuery] string path
|
[FromQuery] string path
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
var chunkSize = ByteConverter.FromMegaBytes(Configuration.Files.UploadChunkSize).Bytes;
|
||||||
|
var uploadLimit = ByteConverter.FromMegaBytes(Configuration.Files.UploadSizeLimit).Bytes;
|
||||||
|
|
||||||
#region File validation
|
#region File validation
|
||||||
|
|
||||||
if (Request.Form.Files.Count != 1)
|
if (Request.Form.Files.Count != 1)
|
||||||
@@ -41,7 +42,7 @@ public class UploadController : Controller
|
|||||||
|
|
||||||
var file = Request.Form.Files[0];
|
var file = Request.Form.Files[0];
|
||||||
|
|
||||||
if (file.Length > ChunkSize)
|
if (file.Length > chunkSize)
|
||||||
throw new HttpApiException("The provided data exceeds the chunk size limit", 400);
|
throw new HttpApiException("The provided data exceeds the chunk size limit", 400);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -50,13 +51,16 @@ public class UploadController : Controller
|
|||||||
|
|
||||||
#region Chunk calculation and validation
|
#region Chunk calculation and validation
|
||||||
|
|
||||||
var chunks = totalSize / ChunkSize;
|
if(totalSize > uploadLimit)
|
||||||
chunks += totalSize % ChunkSize > 0 ? 1 : 0;
|
throw new HttpApiException("Invalid upload request: Exceeding upload limit", 400);
|
||||||
|
|
||||||
|
var chunks = totalSize / chunkSize;
|
||||||
|
chunks += totalSize % chunkSize > 0 ? 1 : 0;
|
||||||
|
|
||||||
if (chunkId > chunks)
|
if (chunkId > chunks)
|
||||||
throw new HttpApiException("Invalid chunk id: Out of bounds", 400);
|
throw new HttpApiException("Invalid chunk id: Out of bounds", 400);
|
||||||
|
|
||||||
var positionToSkipTo = ChunkSize * chunkId;
|
var positionToSkipTo = chunkSize * chunkId;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,17 @@ namespace MoonlightServers.Daemon.Models;
|
|||||||
|
|
||||||
public class ServerConsole
|
public class ServerConsole
|
||||||
{
|
{
|
||||||
public event Func<string, Task> OnOutput;
|
public event Func<string, Task>? OnOutput;
|
||||||
public event Func<string, Task> OnInput;
|
public event Func<string, Task>? OnInput;
|
||||||
|
|
||||||
public string[] Messages => GetMessages();
|
public string[] Messages => GetMessages();
|
||||||
private readonly Queue<string> MessageCache = new();
|
private readonly Queue<string> MessageCache = new();
|
||||||
private const int MaxMessagesInCache = 250; //TODO: Config
|
private int MaxMessagesInCache;
|
||||||
|
|
||||||
|
public ServerConsole(int maxMessagesInCache)
|
||||||
|
{
|
||||||
|
MaxMessagesInCache = maxMessagesInCache;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task WriteToOutput(string content)
|
public async Task WriteToOutput(string content)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using MoonCore.Attributes;
|
|||||||
using MoonCore.Exceptions;
|
using MoonCore.Exceptions;
|
||||||
using MoonCore.Models;
|
using MoonCore.Models;
|
||||||
using MoonlightServers.Daemon.Abstractions;
|
using MoonlightServers.Daemon.Abstractions;
|
||||||
|
using MoonlightServers.Daemon.Configuration;
|
||||||
using MoonlightServers.Daemon.Enums;
|
using MoonlightServers.Daemon.Enums;
|
||||||
using MoonlightServers.Daemon.Extensions;
|
using MoonlightServers.Daemon.Extensions;
|
||||||
using MoonlightServers.Daemon.Http.Hubs;
|
using MoonlightServers.Daemon.Http.Hubs;
|
||||||
@@ -22,6 +23,7 @@ public class ServerService : IHostedLifecycleService
|
|||||||
private readonly IServiceProvider ServiceProvider;
|
private readonly IServiceProvider ServiceProvider;
|
||||||
private readonly ILoggerFactory LoggerFactory;
|
private readonly ILoggerFactory LoggerFactory;
|
||||||
private readonly IHubContext<ServerWebSocketHub> WebSocketHub;
|
private readonly IHubContext<ServerWebSocketHub> WebSocketHub;
|
||||||
|
private readonly AppConfiguration Configuration;
|
||||||
private CancellationTokenSource Cancellation = new();
|
private CancellationTokenSource Cancellation = new();
|
||||||
private bool IsInitialized = false;
|
private bool IsInitialized = false;
|
||||||
|
|
||||||
@@ -30,7 +32,8 @@ public class ServerService : IHostedLifecycleService
|
|||||||
ILogger<ServerService> logger,
|
ILogger<ServerService> logger,
|
||||||
IServiceProvider serviceProvider,
|
IServiceProvider serviceProvider,
|
||||||
ILoggerFactory loggerFactory,
|
ILoggerFactory loggerFactory,
|
||||||
IHubContext<ServerWebSocketHub> webSocketHub
|
IHubContext<ServerWebSocketHub> webSocketHub,
|
||||||
|
AppConfiguration configuration
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
RemoteService = remoteService;
|
RemoteService = remoteService;
|
||||||
@@ -38,6 +41,7 @@ public class ServerService : IHostedLifecycleService
|
|||||||
ServiceProvider = serviceProvider;
|
ServiceProvider = serviceProvider;
|
||||||
LoggerFactory = loggerFactory;
|
LoggerFactory = loggerFactory;
|
||||||
WebSocketHub = webSocketHub;
|
WebSocketHub = webSocketHub;
|
||||||
|
Configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Initialize() //TODO: Add initialize call from panel
|
public async Task Initialize() //TODO: Add initialize call from panel
|
||||||
@@ -196,7 +200,8 @@ public class ServerService : IHostedLifecycleService
|
|||||||
LoggerFactory.CreateLogger($"Server {serverConfiguration.Id}"),
|
LoggerFactory.CreateLogger($"Server {serverConfiguration.Id}"),
|
||||||
ServiceProvider,
|
ServiceProvider,
|
||||||
serverConfiguration,
|
serverConfiguration,
|
||||||
WebSocketHub
|
WebSocketHub,
|
||||||
|
Configuration
|
||||||
);
|
);
|
||||||
|
|
||||||
await server.Initialize(existingContainers);
|
await server.Initialize(existingContainers);
|
||||||
|
|||||||
Reference in New Issue
Block a user