108 lines
3.6 KiB
C#
Raw Permalink Normal View History

2025-01-16 13:51:29 +01:00
using CodexClient;
using Core;
2023-09-11 16:57:57 +02:00
using KubernetesWorkflow;
using KubernetesWorkflow.Types;
2025-01-15 15:43:50 +01:00
using Utils;
namespace CodexPlugin
{
2025-01-27 09:59:19 +01:00
public class ContainerCodexStarter : ICodexStarter
{
2023-09-12 10:31:55 +02:00
private readonly IPluginTools pluginTools;
2025-01-27 10:22:34 +01:00
private readonly ProcessControlMap processControlMap;
private readonly CodexContainerRecipe recipe;
2024-03-26 14:42:47 +01:00
private readonly ApiChecker apiChecker;
2023-09-11 16:57:57 +02:00
public ContainerCodexStarter(IPluginTools pluginTools, CodexContainerRecipe recipe, ProcessControlMap processControlMap)
2023-09-11 16:57:57 +02:00
{
this.pluginTools = pluginTools;
this.recipe = recipe;
2025-01-27 10:22:34 +01:00
this.processControlMap = processControlMap;
2024-03-26 14:42:47 +01:00
apiChecker = new ApiChecker(pluginTools);
2023-09-11 16:57:57 +02:00
}
public ICodexInstance[] BringOnline(CodexSetup codexSetup)
{
LogSeparator();
Log($"Starting {codexSetup.Describe()}...");
2023-09-19 13:58:45 +02:00
var startupConfig = CreateStartupConfig(codexSetup);
var containers = StartCodexContainers(startupConfig, codexSetup.NumberOfNodes, codexSetup.Location);
2024-03-26 14:42:47 +01:00
apiChecker.CheckCompatibility(containers);
foreach (var rc in containers)
{
var podInfo = GetPodInfo(rc);
var podInfos = string.Join(", ", rc.Containers.Select(c => $"Container: '{c.Name}' PodLabel: '{c.RunningPod.StartResult.Deployment.PodLabel}' runs at '{podInfo.K8SNodeName}'={podInfo.Ip}"));
2024-07-25 10:10:11 +02:00
Log($"Started node with image '{containers.First().Containers.First().Recipe.Image}'. ({podInfos})");
LogEthAddress(rc);
}
LogSeparator();
return containers.Select(CreateInstance).ToArray();
2023-09-11 16:57:57 +02:00
}
public void Decommission()
{
}
2023-09-19 13:58:45 +02:00
private StartupConfig CreateStartupConfig(CodexSetup codexSetup)
2023-09-11 16:57:57 +02:00
{
var startupConfig = new StartupConfig();
startupConfig.NameOverride = codexSetup.NameOverride;
startupConfig.Add(codexSetup);
return startupConfig;
}
2024-04-13 17:09:17 +03:00
private RunningPod[] StartCodexContainers(StartupConfig startupConfig, int numberOfNodes, ILocation location)
2023-09-11 16:57:57 +02:00
{
var futureContainers = new List<FutureContainers>();
2023-09-11 16:57:57 +02:00
for (var i = 0; i < numberOfNodes; i++)
{
2023-09-12 10:31:55 +02:00
var workflow = pluginTools.CreateWorkflow();
futureContainers.Add(workflow.Start(1, location, recipe, startupConfig));
2023-09-11 16:57:57 +02:00
}
2024-04-09 09:30:45 +02:00
return futureContainers
.Select(f => f.WaitForOnline())
.ToArray();
2023-09-11 16:57:57 +02:00
}
2024-04-13 17:09:17 +03:00
private PodInfo GetPodInfo(RunningPod rc)
{
var workflow = pluginTools.CreateWorkflow();
return workflow.GetPodInfo(rc);
}
2025-01-15 15:43:50 +01:00
private ICodexInstance CreateInstance(RunningPod pod)
{
2025-01-16 10:15:02 +01:00
var instance = CodexInstanceContainerExtension.CreateFromPod(pod);
var processControl = new CodexContainerProcessControl(pluginTools, pod, onStop: () =>
{
2025-01-27 10:22:34 +01:00
processControlMap.Remove(instance);
});
2025-01-27 10:22:34 +01:00
processControlMap.Add(instance, processControl);
2025-01-15 15:43:50 +01:00
return instance;
}
private void LogSeparator()
{
Log("----------------------------------------------------------------------------");
}
private void LogEthAddress(RunningPod rc)
{
var account = rc.Containers.First().Recipe.Additionals.Get<EthAccount>();
if (account == null) return;
Log($"{rc.Name} = {account}");
}
private void Log(string message)
{
pluginTools.GetLog().Log(message);
}
}
}