From 0488e83a38074146c1803f812db45c8a72de4f2f Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Mon, 10 Jul 2023 22:01:10 +0200 Subject: [PATCH] Implemented pterodactyl egg import function --- Moonlight/App/Helpers/EggConverter.cs | 71 +++++++++++++++++++ .../Views/Admin/Servers/Images/Index.razor | 65 +++++++++++++++-- 2 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 Moonlight/App/Helpers/EggConverter.cs diff --git a/Moonlight/App/Helpers/EggConverter.cs b/Moonlight/App/Helpers/EggConverter.cs new file mode 100644 index 00000000..d97647c2 --- /dev/null +++ b/Moonlight/App/Helpers/EggConverter.cs @@ -0,0 +1,71 @@ +using System.Text; +using Moonlight.App.Database.Entities; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace Moonlight.App.Helpers; + +public static class EggConverter +{ + public static Image Convert(string json) + { + var result = new Image(); + + var data = new ConfigurationBuilder().AddJsonStream( + new MemoryStream(Encoding.ASCII.GetBytes(json)) + ).Build(); + + result.Allocations = 1; + result.Description = data.GetValue("description") ?? ""; + result.Uuid = Guid.NewGuid(); + result.Startup = data.GetValue("startup") ?? ""; + result.Name = data.GetValue("name") ?? "Ptero Egg"; + + foreach (var variable in data.GetSection("variables").GetChildren()) + { + result.Variables.Add(new() + { + Key = variable.GetValue("env_variable") ?? "", + DefaultValue = variable.GetValue("default_value") ?? "" + }); + } + + var configData = data.GetSection("config"); + + result.ConfigFiles = configData.GetValue("files") ?? "{}"; + + var dImagesData = JObject.Parse(json); + var dImages = (JObject)dImagesData["docker_images"]!; + + foreach (var dockerImage in dImages) + { + var di = new DockerImage() + { + Default = dockerImage.Key == dImages.Properties().Last().Name, + Name = dockerImage.Value!.ToString() + }; + + result.DockerImages.Add(di); + } + + var installSection = data.GetSection("scripts").GetSection("installation"); + + result.InstallEntrypoint = installSection.GetValue("entrypoint") ?? "bash"; + result.InstallScript = installSection.GetValue("script") ?? ""; + result.InstallDockerImage = installSection.GetValue("container") ?? ""; + + var rawJson = configData.GetValue("startup"); + + var startupData = new ConfigurationBuilder().AddJsonStream( + new MemoryStream(Encoding.ASCII.GetBytes(rawJson!)) + ).Build(); + + result.StartupDetection = startupData.GetValue("done", "") ?? ""; + result.StopCommand = configData.GetValue("stop") ?? ""; + + result.TagsJson = "[]"; + result.BackgroundImageUrl = ""; + + return result; + } +} \ No newline at end of file diff --git a/Moonlight/Shared/Views/Admin/Servers/Images/Index.razor b/Moonlight/Shared/Views/Admin/Servers/Images/Index.razor index d947bb4b..5fc6356c 100644 --- a/Moonlight/Shared/Views/Admin/Servers/Images/Index.razor +++ b/Moonlight/Shared/Views/Admin/Servers/Images/Index.razor @@ -32,7 +32,7 @@ New image