2025-01-16 13:51:29 +01:00
|
|
|
|
using CodexClient;
|
2024-07-26 08:39:27 +02:00
|
|
|
|
using Core;
|
2023-09-11 16:57:57 +02:00
|
|
|
|
using KubernetesWorkflow;
|
2023-11-12 10:07:23 +01:00
|
|
|
|
using KubernetesWorkflow.Types;
|
2025-01-15 15:43:50 +01:00
|
|
|
|
using Utils;
|
2023-09-11 11:59:33 +02:00
|
|
|
|
|
|
|
|
|
|
namespace CodexPlugin
|
|
|
|
|
|
{
|
2025-01-27 09:59:19 +01:00
|
|
|
|
public class ContainerCodexStarter : ICodexStarter
|
2023-09-11 11:59:33 +02:00
|
|
|
|
{
|
2023-09-12 10:31:55 +02:00
|
|
|
|
private readonly IPluginTools pluginTools;
|
2025-01-27 10:22:34 +01:00
|
|
|
|
private readonly ProcessControlMap processControlMap;
|
2025-04-18 15:47:44 +02:00
|
|
|
|
private readonly CodexContainerRecipe recipe;
|
2024-03-26 14:42:47 +01:00
|
|
|
|
private readonly ApiChecker apiChecker;
|
2023-09-11 16:57:57 +02:00
|
|
|
|
|
2025-04-18 15:47:44 +02:00
|
|
|
|
public ContainerCodexStarter(IPluginTools pluginTools, CodexContainerRecipe recipe, ProcessControlMap processControlMap)
|
2023-09-11 16:57:57 +02:00
|
|
|
|
{
|
2023-09-12 15:18:36 +02:00
|
|
|
|
this.pluginTools = pluginTools;
|
2025-04-18 15:47:44 +02:00
|
|
|
|
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
|
|
|
|
}
|
2023-09-11 11:59:33 +02:00
|
|
|
|
|
2025-01-27 09:49:21 +01:00
|
|
|
|
public ICodexInstance[] BringOnline(CodexSetup codexSetup)
|
2023-09-11 11:59:33 +02:00
|
|
|
|
{
|
2023-09-12 15:18:36 +02:00
|
|
|
|
LogSeparator();
|
|
|
|
|
|
Log($"Starting {codexSetup.Describe()}...");
|
2023-09-11 11:59:33 +02:00
|
|
|
|
|
2023-09-19 13:58:45 +02:00
|
|
|
|
var startupConfig = CreateStartupConfig(codexSetup);
|
2023-09-11 11:59:33 +02:00
|
|
|
|
|
2023-09-12 15:18:36 +02:00
|
|
|
|
var containers = StartCodexContainers(startupConfig, codexSetup.NumberOfNodes, codexSetup.Location);
|
2023-09-11 11:59:33 +02:00
|
|
|
|
|
2024-03-26 14:42:47 +01:00
|
|
|
|
apiChecker.CheckCompatibility(containers);
|
|
|
|
|
|
|
2023-11-06 14:33:47 +01:00
|
|
|
|
foreach (var rc in containers)
|
|
|
|
|
|
{
|
|
|
|
|
|
var podInfo = GetPodInfo(rc);
|
2024-06-12 10:48:52 +02:00
|
|
|
|
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})");
|
2024-08-21 10:45:17 +02:00
|
|
|
|
LogEthAddress(rc);
|
2023-11-06 14:33:47 +01:00
|
|
|
|
}
|
2023-09-12 15:18:36 +02:00
|
|
|
|
LogSeparator();
|
2023-09-11 11:59:33 +02:00
|
|
|
|
|
2025-01-27 09:49:21 +01:00
|
|
|
|
return containers.Select(CreateInstance).ToArray();
|
2023-09-11 16:57:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-28 11:21:47 +01: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;
|
|
|
|
|
|
}
|
2023-09-11 11:59:33 +02:00
|
|
|
|
|
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
|
|
|
|
{
|
2024-04-09 10:23:07 +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();
|
2024-04-09 10:23:07 +02:00
|
|
|
|
futureContainers.Add(workflow.Start(1, location, recipe, startupConfig));
|
2023-09-11 16:57:57 +02:00
|
|
|
|
}
|
2024-04-09 09:30:45 +02:00
|
|
|
|
|
2024-04-09 10:23:07 +02:00
|
|
|
|
return futureContainers
|
|
|
|
|
|
.Select(f => f.WaitForOnline())
|
|
|
|
|
|
.ToArray();
|
2023-09-11 16:57:57 +02:00
|
|
|
|
}
|
2023-09-11 11:59:33 +02:00
|
|
|
|
|
2024-04-13 17:09:17 +03:00
|
|
|
|
private PodInfo GetPodInfo(RunningPod rc)
|
2023-11-06 14:33:47 +01:00
|
|
|
|
{
|
|
|
|
|
|
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);
|
2025-01-27 09:49:21 +01:00
|
|
|
|
var processControl = new CodexContainerProcessControl(pluginTools, pod, onStop: () =>
|
|
|
|
|
|
{
|
2025-01-27 10:22:34 +01:00
|
|
|
|
processControlMap.Remove(instance);
|
2025-01-27 09:49:21 +01:00
|
|
|
|
});
|
2025-01-27 10:22:34 +01:00
|
|
|
|
processControlMap.Add(instance, processControl);
|
2025-01-15 15:43:50 +01:00
|
|
|
|
return instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-12 15:18:36 +02:00
|
|
|
|
private void LogSeparator()
|
|
|
|
|
|
{
|
|
|
|
|
|
Log("----------------------------------------------------------------------------");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-21 10:45:17 +02:00
|
|
|
|
private void LogEthAddress(RunningPod rc)
|
|
|
|
|
|
{
|
|
|
|
|
|
var account = rc.Containers.First().Recipe.Additionals.Get<EthAccount>();
|
|
|
|
|
|
if (account == null) return;
|
|
|
|
|
|
Log($"{rc.Name} = {account}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-12 15:18:36 +02:00
|
|
|
|
private void Log(string message)
|
|
|
|
|
|
{
|
2023-09-13 10:23:05 +02:00
|
|
|
|
pluginTools.GetLog().Log(message);
|
2023-09-12 15:18:36 +02:00
|
|
|
|
}
|
2023-09-11 11:59:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|