2025-01-16 13:51:29 +01:00
|
|
|
|
using CodexClient;
|
|
|
|
|
|
using CodexClient.Hooks;
|
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;
|
2023-09-11 11:59:33 +02:00
|
|
|
|
using Logging;
|
2025-01-15 15:43:50 +01:00
|
|
|
|
using Utils;
|
2023-09-11 11:59:33 +02:00
|
|
|
|
|
|
|
|
|
|
namespace CodexPlugin
|
|
|
|
|
|
{
|
2025-01-16 13:51:29 +01:00
|
|
|
|
public class CodexStarter : IIProcessControlFactory
|
2023-09-11 11:59:33 +02:00
|
|
|
|
{
|
2023-09-12 10:31:55 +02:00
|
|
|
|
private readonly IPluginTools pluginTools;
|
2023-09-13 16:06:05 +02:00
|
|
|
|
private readonly CodexContainerRecipe recipe = new CodexContainerRecipe();
|
2024-03-26 14:42:47 +01:00
|
|
|
|
private readonly ApiChecker apiChecker;
|
2025-01-15 15:43:50 +01:00
|
|
|
|
private readonly Dictionary<ICodexInstance, RunningPod> podMap = new Dictionary<ICodexInstance, RunningPod>();
|
2024-03-26 10:03:52 +01:00
|
|
|
|
private DebugInfoVersion? versionResponse;
|
2023-09-11 16:57:57 +02:00
|
|
|
|
|
2023-09-12 15:18:36 +02:00
|
|
|
|
public CodexStarter(IPluginTools pluginTools)
|
2023-09-11 16:57:57 +02:00
|
|
|
|
{
|
2023-09-12 15:18:36 +02:00
|
|
|
|
this.pluginTools = pluginTools;
|
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
|
|
|
|
|
2024-07-26 08:39:27 +02:00
|
|
|
|
public CodexHooksFactory HooksFactory { get; } = new CodexHooksFactory();
|
|
|
|
|
|
|
2025-01-16 13:51:29 +01:00
|
|
|
|
public IProcessControl CreateProcessControl(ICodexInstance instance)
|
|
|
|
|
|
{
|
|
|
|
|
|
var pod = podMap[instance];
|
|
|
|
|
|
return new CodexContainerProcessControl(pluginTools, pod, onStop: () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
podMap.Remove(instance);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-13 17:09:17 +03:00
|
|
|
|
public RunningPod[] 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
|
|
|
|
|
2023-09-12 15:18:36 +02:00
|
|
|
|
return containers;
|
2023-09-11 16:57:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-13 17:09:17 +03:00
|
|
|
|
public ICodexNodeGroup WrapCodexContainers(CoreInterface coreInterface, RunningPod[] containers)
|
2023-09-11 16:57:57 +02:00
|
|
|
|
{
|
2025-01-16 13:51:29 +01:00
|
|
|
|
var codexNodeFactory = new CodexNodeFactory(
|
|
|
|
|
|
log: pluginTools.GetLog(),
|
|
|
|
|
|
fileManager: pluginTools.GetFileManager(),
|
|
|
|
|
|
hooksFactory: HooksFactory,
|
|
|
|
|
|
httpFactory: pluginTools,
|
|
|
|
|
|
processControlFactory: this);
|
2023-09-11 16:57:57 +02:00
|
|
|
|
|
2023-09-19 13:58:45 +02:00
|
|
|
|
var group = CreateCodexGroup(coreInterface, containers, codexNodeFactory);
|
2023-09-11 16:57:57 +02:00
|
|
|
|
|
2023-09-12 15:18:36 +02:00
|
|
|
|
Log($"Codex version: {group.Version}");
|
2023-09-13 16:06:05 +02:00
|
|
|
|
versionResponse = group.Version;
|
2023-09-12 15:18:36 +02:00
|
|
|
|
|
|
|
|
|
|
return group;
|
2023-09-11 11:59:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-13 16:06:05 +02:00
|
|
|
|
public string GetCodexId()
|
|
|
|
|
|
{
|
2024-03-26 08:58:16 +01:00
|
|
|
|
if (versionResponse != null) return versionResponse.Version;
|
2023-09-13 16:06:05 +02:00
|
|
|
|
return recipe.Image;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-13 09:37:45 +01:00
|
|
|
|
public string GetCodexRevision()
|
|
|
|
|
|
{
|
2024-03-26 08:58:16 +01:00
|
|
|
|
if (versionResponse != null) return versionResponse.Revision;
|
2023-11-13 09:37:45 +01:00
|
|
|
|
return "unknown";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-13 17:09:17 +03:00
|
|
|
|
private CodexNodeGroup CreateCodexGroup(CoreInterface coreInterface, RunningPod[] runningContainers, CodexNodeFactory codexNodeFactory)
|
2023-09-11 16:57:57 +02:00
|
|
|
|
{
|
2025-01-15 15:43:50 +01:00
|
|
|
|
var instances = runningContainers.Select(CreateInstance).ToArray();
|
2025-01-16 13:51:29 +01:00
|
|
|
|
var nodes = instances.Select(codexNodeFactory.CreateCodexNode).ToArray();
|
2025-01-15 15:43:50 +01:00
|
|
|
|
var group = new CodexNodeGroup(pluginTools, nodes);
|
2023-09-11 16:57:57 +02:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-09-13 10:23:05 +02:00
|
|
|
|
Stopwatch.Measure(pluginTools.GetLog(), "EnsureOnline", group.EnsureOnline);
|
2023-09-11 16:57:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
2023-09-19 13:58:45 +02:00
|
|
|
|
CodexNodesNotOnline(coreInterface, runningContainers);
|
2023-09-11 16:57:57 +02:00
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return group;
|
|
|
|
|
|
}
|
2023-09-11 11:59:33 +02:00
|
|
|
|
|
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-15 15:43:50 +01:00
|
|
|
|
podMap.Add(instance, pod);
|
|
|
|
|
|
return instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-13 17:09:17 +03:00
|
|
|
|
private void CodexNodesNotOnline(CoreInterface coreInterface, RunningPod[] runningContainers)
|
2023-09-11 16:57:57 +02:00
|
|
|
|
{
|
2023-09-13 09:12:18 +02:00
|
|
|
|
Log("Codex nodes failed to start");
|
2024-04-13 17:09:17 +03:00
|
|
|
|
foreach (var container in runningContainers.First().Containers) coreInterface.DownloadLog(container);
|
2023-09-11 16:57:57 +02:00
|
|
|
|
}
|
2023-09-11 11:59:33 +02:00
|
|
|
|
|
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
|
|
|
|
}
|
2025-01-16 13:51:29 +01:00
|
|
|
|
|
|
|
|
|
|
public class CodexContainerProcessControl : IProcessControl
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IPluginTools tools;
|
|
|
|
|
|
private readonly RunningPod pod;
|
|
|
|
|
|
private readonly Action onStop;
|
|
|
|
|
|
private readonly ContainerCrashWatcher crashWatcher;
|
|
|
|
|
|
|
|
|
|
|
|
public CodexContainerProcessControl(IPluginTools tools, RunningPod pod, Action onStop)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.tools = tools;
|
|
|
|
|
|
this.pod = pod;
|
|
|
|
|
|
this.onStop = onStop;
|
|
|
|
|
|
|
|
|
|
|
|
crashWatcher = tools.CreateWorkflow().CreateCrashWatcher(pod.Containers.Single());
|
|
|
|
|
|
crashWatcher.Start();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Stop(bool waitTillStopped)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log($"Stopping node...");
|
|
|
|
|
|
var workflow = tools.CreateWorkflow();
|
|
|
|
|
|
workflow.Stop(pod, waitTillStopped);
|
|
|
|
|
|
crashWatcher.Stop();
|
|
|
|
|
|
onStop();
|
|
|
|
|
|
Log("Stopped.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IDownloadedLog DownloadLog(LogFile file)
|
|
|
|
|
|
{
|
|
|
|
|
|
var workflow = tools.CreateWorkflow();
|
|
|
|
|
|
return workflow.DownloadContainerLog(pod.Containers.Single());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteDataDirFolder()
|
|
|
|
|
|
{
|
|
|
|
|
|
var container = pod.Containers.Single();
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var dataDirVar = container.Recipe.EnvVars.Single(e => e.Name == "CODEX_DATA_DIR");
|
|
|
|
|
|
var dataDir = dataDirVar.Value;
|
|
|
|
|
|
var workflow = tools.CreateWorkflow();
|
|
|
|
|
|
workflow.ExecuteCommand(container, "rm", "-Rfv", $"/codex/{dataDir}/repo");
|
|
|
|
|
|
Log("Deleted repo folder.");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log("Unable to delete repo folder: " + e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool HasCrashed()
|
|
|
|
|
|
{
|
|
|
|
|
|
return crashWatcher.HasCrashed();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Log(string message)
|
|
|
|
|
|
{
|
|
|
|
|
|
tools.GetLog().Log(message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-09-11 11:59:33 +02:00
|
|
|
|
}
|