207 lines
6.9 KiB
C#
207 lines
6.9 KiB
C#
using System.Text;
|
|
using MoonlightServers.Api.Infrastructure.Database;
|
|
using MoonlightServers.Api.Infrastructure.Database.Entities;
|
|
using MoonlightServers.Api.Infrastructure.Database.Json;
|
|
using VYaml.Annotations;
|
|
using VYaml.Serialization;
|
|
|
|
namespace MoonlightServers.Api.Admin.Templates;
|
|
|
|
public class PelicanEggImportService
|
|
{
|
|
private readonly DatabaseRepository<Template> TemplateRepository;
|
|
private readonly DatabaseRepository<TemplateDockerImage> DockerImageRepository;
|
|
|
|
public PelicanEggImportService(
|
|
DatabaseRepository<Template> templateRepository,
|
|
DatabaseRepository<TemplateDockerImage> dockerImageRepository
|
|
)
|
|
{
|
|
TemplateRepository = templateRepository;
|
|
DockerImageRepository = dockerImageRepository;
|
|
}
|
|
|
|
public async Task<Template> ImportAsync(string content)
|
|
{
|
|
var egg = YamlSerializer.Deserialize<Egg>(Encoding.UTF8.GetBytes(content));
|
|
|
|
var template = new Template()
|
|
{
|
|
AllowUserDockerImageChange = true,
|
|
Author = egg.Author,
|
|
Description = egg.Description,
|
|
DonateUrl = null,
|
|
Name = egg.Name,
|
|
UpdateUrl = egg.Meta.UpdateUrl,
|
|
Version = "1.0.0",
|
|
FilesConfig = new FilesConfig()
|
|
{
|
|
ConfigurationFiles = egg.Config.Files.Select(file => new ConfigurationFile()
|
|
{
|
|
Path = file.Key,
|
|
Parser = file.Value.Parser,
|
|
Mappings = file.Value.Find.Select(pair => new ConfigurationFileMapping()
|
|
{
|
|
Key = pair.Key,
|
|
Value = pair.Value
|
|
}).ToList()
|
|
}).ToList()
|
|
},
|
|
InstallationConfig = new InstallationConfig()
|
|
{
|
|
DockerImage = egg.Scripts.Installation.Container,
|
|
Script = egg.Scripts.Installation.Script,
|
|
Shell = egg.Scripts.Installation.Entrypoint
|
|
},
|
|
LifecycleConfig = new LifecycleConfig()
|
|
{
|
|
OnlineLogPatterns = egg.Config.Startup.Values.ToList(),
|
|
StopCommand = egg.Config.Stop,
|
|
StartupCommands = egg.StartupCommands.Select(x => new StartupCommand()
|
|
{
|
|
DisplayName = x.Key,
|
|
Command = x.Value
|
|
}).ToList()
|
|
},
|
|
MiscellaneousConfig = new MiscellaneousConfig()
|
|
{
|
|
UseLegacyStartup = true
|
|
},
|
|
Variables = egg.Variables.Select(variable => new TemplateVariable()
|
|
{
|
|
Description = variable.Description,
|
|
DisplayName = variable.Name,
|
|
DefaultValue = variable.DefaultValue,
|
|
EnvName = variable.EnvVariable
|
|
}).ToList()
|
|
};
|
|
|
|
var finalTemplate = await TemplateRepository.AddAsync(template);
|
|
|
|
var isFirst = true;
|
|
TemplateDockerImage? defaultDockerImage = null;
|
|
|
|
foreach (var dockerImage in egg.DockerImages)
|
|
{
|
|
var finalDockerImage = await DockerImageRepository.AddAsync(new TemplateDockerImage()
|
|
{
|
|
DisplayName = dockerImage.Key,
|
|
ImageName = dockerImage.Value,
|
|
SkipPulling = false,
|
|
Template = finalTemplate
|
|
});
|
|
|
|
if (isFirst)
|
|
{
|
|
isFirst = false;
|
|
defaultDockerImage = finalDockerImage;
|
|
}
|
|
}
|
|
|
|
finalTemplate.DefaultDockerImage = defaultDockerImage;
|
|
|
|
await TemplateRepository.UpdateAsync(finalTemplate);
|
|
|
|
return finalTemplate;
|
|
}
|
|
}
|
|
|
|
[YamlObject]
|
|
public partial class Egg
|
|
{
|
|
[YamlMember("_comment")] public string? Comment { get; set; }
|
|
|
|
[YamlMember("meta")] public EggMeta Meta { get; set; } = new();
|
|
|
|
[YamlMember("exported_at")] public string? ExportedAt { get; set; }
|
|
|
|
[YamlMember("name")] public string Name { get; set; } = string.Empty;
|
|
|
|
[YamlMember("author")] public string Author { get; set; } = string.Empty;
|
|
|
|
[YamlMember("uuid")] public string Uuid { get; set; } = string.Empty;
|
|
|
|
[YamlMember("description")] public string Description { get; set; } = string.Empty;
|
|
|
|
[YamlMember("image")] public string? Image { get; set; }
|
|
|
|
[YamlMember("tags")] public List<string> Tags { get; set; } = new();
|
|
|
|
[YamlMember("features")] public List<string> Features { get; set; } = new();
|
|
|
|
[YamlMember("docker_images")] public Dictionary<string, string> DockerImages { get; set; } = new();
|
|
|
|
[YamlMember("file_denylist")] public Dictionary<string, string> FileDenylist { get; set; } = new();
|
|
|
|
[YamlMember("startup_commands")] public Dictionary<string, string> StartupCommands { get; set; } = new();
|
|
|
|
[YamlMember("config")] public EggConfig Config { get; set; } = new();
|
|
|
|
[YamlMember("scripts")] public EggScripts Scripts { get; set; } = new();
|
|
|
|
[YamlMember("variables")] public List<EggVariable> Variables { get; set; } = new();
|
|
}
|
|
|
|
[YamlObject]
|
|
public partial class EggMeta
|
|
{
|
|
[YamlMember("version")] public string Version { get; set; } = string.Empty;
|
|
|
|
[YamlMember("update_url")] public string? UpdateUrl { get; set; }
|
|
}
|
|
|
|
[YamlObject]
|
|
public partial class EggConfig
|
|
{
|
|
[YamlMember("files")] public Dictionary<string, EggConfigFile> Files { get; set; } = new();
|
|
|
|
[YamlMember("startup")] public Dictionary<string, string> Startup { get; set; } = new();
|
|
|
|
[YamlMember("logs")] public Dictionary<string, string> Logs { get; set; } = new();
|
|
|
|
[YamlMember("stop")] public string Stop { get; set; } = string.Empty;
|
|
}
|
|
|
|
[YamlObject]
|
|
public partial class EggConfigFile
|
|
{
|
|
[YamlMember("parser")] public string Parser { get; set; } = string.Empty;
|
|
|
|
[YamlMember("find")] public Dictionary<string, string> Find { get; set; } = new();
|
|
}
|
|
|
|
[YamlObject]
|
|
public partial class EggScripts
|
|
{
|
|
[YamlMember("installation")] public EggInstallationScript Installation { get; set; } = new();
|
|
}
|
|
|
|
[YamlObject]
|
|
public partial class EggInstallationScript
|
|
{
|
|
[YamlMember("script")] public string Script { get; set; } = string.Empty;
|
|
|
|
[YamlMember("container")] public string Container { get; set; } = string.Empty;
|
|
|
|
[YamlMember("entrypoint")] public string Entrypoint { get; set; } = string.Empty;
|
|
}
|
|
|
|
[YamlObject]
|
|
public partial class EggVariable
|
|
{
|
|
[YamlMember("name")] public string Name { get; set; } = string.Empty;
|
|
|
|
[YamlMember("description")] public string Description { get; set; } = string.Empty;
|
|
|
|
[YamlMember("env_variable")] public string EnvVariable { get; set; } = string.Empty;
|
|
|
|
[YamlMember("default_value")] public string DefaultValue { get; set; } = string.Empty;
|
|
|
|
[YamlMember("user_viewable")] public bool UserViewable { get; set; }
|
|
|
|
[YamlMember("user_editable")] public bool UserEditable { get; set; }
|
|
|
|
[YamlMember("rules")] public List<string> Rules { get; set; } = new();
|
|
|
|
[YamlMember("sort")] public int Sort { get; set; }
|
|
} |