Started implementing server installation

This commit is contained in:
2025-02-13 21:23:35 +01:00
parent f45699f300
commit 761ab455f0
11 changed files with 179 additions and 19 deletions

View File

@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MoonCore.Exceptions;
using MoonCore.Extended.Abstractions;
using MoonCore.Models;
using MoonlightServers.ApiServer.Database.Entities;
@@ -96,4 +97,23 @@ public class RemoteServersController : Controller
TotalPages = total == 0 ? 0 : total / pageSize
};
}
[HttpGet("{id:int}/install")]
public async Task<ServerInstallDataResponse> GetInstall([FromRoute] int id)
{
var server = await ServerRepository
.Get()
.Include(x => x.Star)
.FirstOrDefaultAsync(x => x.Id == id);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
return new ServerInstallDataResponse()
{
Script = server.Star.InstallScript,
DockerImage = server.Star.InstallDockerImage,
Shell = server.Star.InstallShell
};
}
}

View File

@@ -380,6 +380,7 @@ public class StarImportExportService
// Fix up special variables
entry.Value = entry.Value.Replace("server.allocations.default.port", "SERVER_PORT");
entry.Value = entry.Value.Replace("server.build.default.port", "SERVER_PORT");
pc.Entries.Add(entry);
}

View File

@@ -30,6 +30,8 @@ public partial class Server
await LogToConsole("Removing container");
await dockerClient.Containers.RemoveContainerAsync(container.ID, new());
RuntimeContainerId = null;
}
catch (DockerContainerNotFoundException){}

View File

@@ -53,7 +53,8 @@ public partial class Server
.OnEntryAsync(InternalStop);
StateMachine.Configure(ServerState.Installing)
.Permit(ServerTrigger.NotifyInstallContainerDied, ServerState.Offline);
.Permit(ServerTrigger.NotifyContainerDied, ServerState.Offline)
.OnEntryAsync(InternalInstall);
return Task.CompletedTask;
}

View File

@@ -0,0 +1,59 @@
using Docker.DotNet;
using MoonCore.Helpers;
using MoonlightServers.Daemon.Enums;
using MoonlightServers.Daemon.Extensions;
using MoonlightServers.Daemon.Services;
using MoonlightServers.DaemonShared.PanelSide.Http.Responses;
namespace MoonlightServers.Daemon.Abstractions;
public partial class Server
{
public async Task Install() => await StateMachine.FireAsync(ServerTrigger.Reinstall);
private async Task InternalInstall()
{
// TODO: Consider if checking for existing install containers is actually useful, because
// when the daemon is starting and a installation is still ongoing it will reattach anyways
// and the container has the auto remove flag enabled by default (maybe also consider this for the normal runtime container)
await LogToConsole("Fetching installation configuration");
// Fetching remote configuration
var remoteService = ServiceProvider.GetRequiredService<RemoteService>();
using var remoteHttpClient = await remoteService.CreateHttpClient();
var installData = await remoteHttpClient.GetJson<ServerInstallDataResponse>($"api/servers/remote/servers/{Configuration.Id}/install");
var dockerImageService = ServiceProvider.GetRequiredService<DockerImageService>();
// We call an external service for that, as we want to have a central management point of images
// for analytics and automatic deletion
await dockerImageService.Ensure(installData.DockerImage, async message => { await LogToConsole(message); });
// Ensuring storage configuration
var installationHostPath = await EnsureInstallationVolume();
var runtimeHostPath = await EnsureRuntimeVolume();
// Write installation script to path
await File.WriteAllTextAsync(PathBuilder.File(installationHostPath, "install.sh"), installData.Script.Replace("\n\r", "\n") + "\n\n");
// Creating container configuration
var parameters = Configuration.ToInstallationCreateParameters(
runtimeHostPath,
installationHostPath,
InstallationContainerName,
installData.DockerImage,
installData.Shell
);
var dockerClient = ServiceProvider.GetRequiredService<DockerClient>();
var container = await dockerClient.Containers.CreateContainerAsync(parameters);
InstallationContainerId = container.ID;
await AttachConsole(InstallationContainerId);
await dockerClient.Containers.StartContainerAsync(InstallationContainerId, new());
}
}

View File

@@ -35,11 +35,11 @@ public partial class Server
var appConfiguration = ServiceProvider.GetRequiredService<AppConfiguration>();
var hostPath = PathBuilder.Dir(
appConfiguration.Storage.Volumes,
appConfiguration.Storage.Install,
Configuration.Id.ToString()
);
await LogToConsole("Creating storage");
await LogToConsole("Creating installation storage");
// Create volume if missing
if (!Directory.Exists(hostPath))

View File

@@ -8,6 +8,5 @@ public enum ServerTrigger
Kill = 3,
Reinstall = 4,
NotifyOnline = 5,
NotifyContainerDied = 6,
NotifyInstallContainerDied = 7
NotifyContainerDied = 6
}

View File

@@ -7,10 +7,11 @@ namespace MoonlightServers.Daemon.Extensions;
public static class ServerConfigurationExtensions
{
public static CreateContainerParameters ToRuntimeCreateParameters(this ServerConfiguration configuration, string hostPath, string containerName)
public static CreateContainerParameters ToRuntimeCreateParameters(this ServerConfiguration configuration,
string hostPath, string containerName)
{
var parameters = configuration.ToSharedCreateParameters();
#region Security
parameters.HostConfig.CapDrop = new List<string>()
@@ -75,7 +76,7 @@ public static class ServerConfigurationExtensions
#region Mounts
parameters.HostConfig.Mounts = new List<Mount>();
parameters.HostConfig.Mounts.Add(new()
{
Source = hostPath,
@@ -85,7 +86,7 @@ public static class ServerConfigurationExtensions
});
#endregion
#region Port Bindings
if (true) // TODO: Add network toggle
@@ -106,7 +107,7 @@ public static class ServerConfigurationExtensions
HostIP = allocation.IpAddress
}
});
parameters.HostConfig.PortBindings.Add($"{allocation.Port}/udp", new List<PortBinding>
{
new()
@@ -123,6 +124,63 @@ public static class ServerConfigurationExtensions
return parameters;
}
public static CreateContainerParameters ToInstallationCreateParameters(
this ServerConfiguration configuration,
string runtimeHostPath,
string installationHostPath,
string containerName,
string installDockerImage,
string installShell
)
{
var parameters = configuration.ToSharedCreateParameters();
// - Name
parameters.Name = containerName;
parameters.Hostname = containerName;
// - Image
parameters.Image = installDockerImage;
// - Env
parameters.Env = configuration
.ToEnvironmentVariables()
.Select(x => $"{x.Key}={x.Value}")
.ToList();
// -- Working directory
parameters.WorkingDir = "/mnt/server";
// - User
// Note: Some images might not work if we set a user here
parameters.User = "0:0";
// -- Mounts
parameters.HostConfig.Mounts = new List<Mount>();
parameters.HostConfig.Mounts.Add(new()
{
Source = runtimeHostPath,
Target = "/mnt/server",
ReadOnly = false,
Type = "bind"
});
parameters.HostConfig.Mounts.Add(new()
{
Source = installationHostPath,
Target = "/mnt/install",
ReadOnly = false,
Type = "bind"
});
parameters.Cmd = [installShell, "/mnt/install/install.sh"];
parameters.HostConfig.AutoRemove = true;
return parameters;
}
private static CreateContainerParameters ToSharedCreateParameters(this ServerConfiguration configuration)
{
var parameters = new CreateContainerParameters()
@@ -158,7 +216,7 @@ public static class ServerConfigurationExtensions
long swapLimit = -1;
/*
// If swap is enabled globally and not disabled on this server, set swap
if (!configuration.Limits.DisableSwap && config.Server.EnableSwap)
swapLimit = (long)(memoryOverhead + memoryOverhead * config.Server.SwapMultiplier);
@@ -168,7 +226,8 @@ public static class ServerConfigurationExtensions
// Finalize limits by converting and updating the host config
parameters.HostConfig.Memory = ByteConverter.FromMegaBytes((long)memoryOverhead, 1000).Bytes;
parameters.HostConfig.MemoryReservation = ByteConverter.FromMegaBytes(memoryLimit, 1000).Bytes;
parameters.HostConfig.MemorySwap = swapLimit == -1 ? swapLimit : ByteConverter.FromMegaBytes(swapLimit, 1000).Bytes;
parameters.HostConfig.MemorySwap =
swapLimit == -1 ? swapLimit : ByteConverter.FromMegaBytes(swapLimit, 1000).Bytes;
#endregion
@@ -184,7 +243,7 @@ public static class ServerConfigurationExtensions
#region DNS
// TODO: Read hosts dns settings?
parameters.HostConfig.DNS = /*config.Docker.DnsServers.Any() ? config.Docker.DnsServers :*/ new List<string>()
{
"1.1.1.1",
@@ -213,9 +272,9 @@ public static class ServerConfigurationExtensions
#endregion
#region Labels
parameters.Labels = new Dictionary<string, string>();
parameters.Labels.Add("Software", "Moonlight-Panel");
parameters.Labels.Add("ServerId", configuration.Id.ToString());
@@ -223,7 +282,7 @@ public static class ServerConfigurationExtensions
return parameters;
}
public static Dictionary<string, string> ToEnvironmentVariables(this ServerConfiguration configuration)
{
var result = new Dictionary<string, string>
@@ -236,11 +295,11 @@ public static class ServerConfigurationExtensions
if (configuration.Allocations.Length > 0)
{
var mainAllocation = configuration.Allocations.First();
result.Add("SERVER_IP", mainAllocation.IpAddress);
result.Add("SERVER_PORT", mainAllocation.Port.ToString());
}
// Handle allocation variables
var i = 1;
foreach (var allocation in configuration.Allocations)

View File

@@ -37,4 +37,15 @@ public class ServerPowerController : Controller
await server.Stop();
}
[HttpPost("{serverId:int}/install")]
public async Task Install(int serverId, [FromQuery] bool runAsync = true)
{
var server = ServerService.GetServer(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.Install();
}
}

View File

@@ -116,7 +116,7 @@ public class ServerService : IHostedLifecycleService
Server? server;
lock (Servers)
server = Servers.FirstOrDefault(x => x.RuntimeContainerId == message.ID);
server = Servers.FirstOrDefault(x => x.RuntimeContainerId == message.ID || x.InstallationContainerId == message.ID);
// TODO: Maybe implement a lookup for containers which id isn't set in the cache

View File

@@ -0,0 +1,8 @@
namespace MoonlightServers.DaemonShared.PanelSide.Http.Responses;
public class ServerInstallDataResponse
{
public string Shell { get; set; }
public string DockerImage { get; set; }
public string Script { get; set; }
}