2023-09-12 11:32:06 +00:00
|
|
|
|
using Core;
|
2023-09-11 14:57:57 +00:00
|
|
|
|
using KubernetesWorkflow;
|
2023-09-11 09:59:33 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
|
|
|
|
|
namespace CodexPlugin
|
|
|
|
|
{
|
2023-09-11 14:57:57 +00:00
|
|
|
|
public class CodexStarter
|
2023-09-11 09:59:33 +00:00
|
|
|
|
{
|
2023-09-12 08:31:55 +00:00
|
|
|
|
private readonly IPluginTools pluginTools;
|
2023-09-13 14:06:05 +00:00
|
|
|
|
private readonly CodexContainerRecipe recipe = new CodexContainerRecipe();
|
|
|
|
|
private CodexDebugVersionResponse? versionResponse;
|
2023-09-11 14:57:57 +00:00
|
|
|
|
|
2023-09-12 13:18:36 +00:00
|
|
|
|
public CodexStarter(IPluginTools pluginTools)
|
2023-09-11 14:57:57 +00:00
|
|
|
|
{
|
2023-09-12 13:18:36 +00:00
|
|
|
|
this.pluginTools = pluginTools;
|
2023-09-11 14:57:57 +00:00
|
|
|
|
}
|
2023-09-11 09:59:33 +00:00
|
|
|
|
|
2023-09-11 14:57:57 +00:00
|
|
|
|
public RunningContainers[] BringOnline(CodexSetup codexSetup)
|
2023-09-11 09:59:33 +00:00
|
|
|
|
{
|
2023-09-12 13:18:36 +00:00
|
|
|
|
LogSeparator();
|
|
|
|
|
Log($"Starting {codexSetup.Describe()}...");
|
2023-09-11 09:59:33 +00:00
|
|
|
|
//var gethStartResult = lifecycle.GethStarter.BringOnlineMarketplaceFor(codexSetup);
|
|
|
|
|
|
2023-09-11 14:57:57 +00:00
|
|
|
|
var startupConfig = CreateStartupConfig(/*gethStartResult,*/ codexSetup);
|
2023-09-11 09:59:33 +00:00
|
|
|
|
|
2023-09-12 13:18:36 +00:00
|
|
|
|
var containers = StartCodexContainers(startupConfig, codexSetup.NumberOfNodes, codexSetup.Location);
|
2023-09-11 09:59:33 +00:00
|
|
|
|
|
|
|
|
|
//var metricAccessFactory = CollectMetrics(codexSetup, containers);
|
|
|
|
|
|
|
|
|
|
//var codexNodeFactory = new CodexNodeFactory(lifecycle, metricAccessFactory, gethStartResult.MarketplaceAccessFactory);
|
|
|
|
|
|
|
|
|
|
//var group = CreateCodexGroup(codexSetup, containers, codexNodeFactory);
|
|
|
|
|
//lifecycle.SetCodexVersion(group.Version);
|
|
|
|
|
|
2023-09-12 13:18:36 +00:00
|
|
|
|
var podInfos = string.Join(", ", containers.Containers().Select(c => $"Container: '{c.Name}' runs at '{c.Pod.PodInfo.K8SNodeName}'={c.Pod.PodInfo.Ip}"));
|
|
|
|
|
Log($"Started {codexSetup.NumberOfNodes} nodes of image '{containers.Containers().First().Recipe.Image}'. ({podInfos})");
|
|
|
|
|
LogSeparator();
|
2023-09-11 09:59:33 +00:00
|
|
|
|
|
2023-09-12 13:18:36 +00:00
|
|
|
|
return containers;
|
2023-09-11 14:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ICodexNodeGroup WrapCodexContainers(RunningContainers[] containers)
|
|
|
|
|
{
|
|
|
|
|
//var metricAccessFactory = CollectMetrics(codexSetup, containers);
|
|
|
|
|
|
2023-09-12 09:43:46 +00:00
|
|
|
|
var codexNodeFactory = new CodexNodeFactory(pluginTools);// (lifecycle, metricAccessFactory, gethStartResult.MarketplaceAccessFactory);
|
2023-09-11 14:57:57 +00:00
|
|
|
|
|
2023-09-12 13:18:36 +00:00
|
|
|
|
var group = CreateCodexGroup(containers, codexNodeFactory);
|
2023-09-11 14:57:57 +00:00
|
|
|
|
|
2023-09-12 13:18:36 +00:00
|
|
|
|
Log($"Codex version: {group.Version}");
|
2023-09-13 14:06:05 +00:00
|
|
|
|
versionResponse = group.Version;
|
2023-09-12 13:18:36 +00:00
|
|
|
|
|
|
|
|
|
return group;
|
2023-09-11 09:59:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void BringOffline(CodexNodeGroup group)
|
|
|
|
|
{
|
2023-09-13 10:09:44 +00:00
|
|
|
|
Log($"Stopping {group.Describe()}...");
|
|
|
|
|
var workflow = pluginTools.CreateWorkflow();
|
|
|
|
|
foreach (var c in group.Containers)
|
|
|
|
|
{
|
|
|
|
|
StopCrashWatcher(c);
|
|
|
|
|
workflow.Stop(c);
|
|
|
|
|
}
|
|
|
|
|
Log("Stopped.");
|
2023-09-11 09:59:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 14:06:05 +00:00
|
|
|
|
public string GetCodexId()
|
|
|
|
|
{
|
|
|
|
|
if (versionResponse != null) return versionResponse.version;
|
|
|
|
|
return recipe.Image;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 10:09:44 +00:00
|
|
|
|
//public void DeleteAllResources()
|
|
|
|
|
//{
|
|
|
|
|
// //var workflow = CreateWorkflow();
|
|
|
|
|
// //workflow.DeleteTestResources();
|
|
|
|
|
//}
|
2023-09-11 09:59:33 +00:00
|
|
|
|
|
2023-09-13 10:09:44 +00:00
|
|
|
|
//public void DownloadLog(RunningContainer container, ILogHandler logHandler, int? tailLines)
|
|
|
|
|
//{
|
|
|
|
|
// //var workflow = CreateWorkflow();
|
|
|
|
|
// //workflow.DownloadContainerLog(container, logHandler, tailLines);
|
|
|
|
|
//}
|
2023-09-11 09:59:33 +00:00
|
|
|
|
|
|
|
|
|
//private IMetricsAccessFactory CollectMetrics(CodexSetup codexSetup, RunningContainers[] containers)
|
|
|
|
|
//{
|
|
|
|
|
// if (codexSetup.MetricsMode == MetricsMode.None) return new MetricsUnavailableAccessFactory();
|
|
|
|
|
|
|
|
|
|
// var runningContainers = lifecycle.PrometheusStarter.CollectMetricsFor(containers);
|
|
|
|
|
|
|
|
|
|
// if (codexSetup.MetricsMode == MetricsMode.Dashboard)
|
|
|
|
|
// {
|
|
|
|
|
// lifecycle.GrafanaStarter.StartDashboard(runningContainers.Containers.First(), codexSetup);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return new CodexNodeMetricsAccessFactory(lifecycle, runningContainers);
|
|
|
|
|
//}
|
|
|
|
|
|
2023-09-11 14:57:57 +00:00
|
|
|
|
private StartupConfig CreateStartupConfig(/*GethStartResult gethStartResult, */ CodexSetup codexSetup)
|
|
|
|
|
{
|
|
|
|
|
var startupConfig = new StartupConfig();
|
|
|
|
|
startupConfig.NameOverride = codexSetup.NameOverride;
|
|
|
|
|
startupConfig.CreateCrashWatcher = true;
|
|
|
|
|
startupConfig.Add(codexSetup);
|
|
|
|
|
//startupConfig.Add(gethStartResult);
|
|
|
|
|
return startupConfig;
|
|
|
|
|
}
|
2023-09-11 09:59:33 +00:00
|
|
|
|
|
2023-09-11 14:57:57 +00:00
|
|
|
|
private RunningContainers[] StartCodexContainers(StartupConfig startupConfig, int numberOfNodes, Location location)
|
|
|
|
|
{
|
|
|
|
|
var result = new List<RunningContainers>();
|
|
|
|
|
for (var i = 0; i < numberOfNodes; i++)
|
|
|
|
|
{
|
2023-09-12 08:31:55 +00:00
|
|
|
|
var workflow = pluginTools.CreateWorkflow();
|
2023-09-11 14:57:57 +00:00
|
|
|
|
result.Add(workflow.Start(1, location, recipe, startupConfig));
|
|
|
|
|
}
|
|
|
|
|
return result.ToArray();
|
|
|
|
|
}
|
2023-09-11 09:59:33 +00:00
|
|
|
|
|
2023-09-12 08:31:55 +00:00
|
|
|
|
private CodexNodeGroup CreateCodexGroup(RunningContainers[] runningContainers, CodexNodeFactory codexNodeFactory)
|
2023-09-11 14:57:57 +00:00
|
|
|
|
{
|
2023-09-13 10:09:44 +00:00
|
|
|
|
var group = new CodexNodeGroup(this, pluginTools, runningContainers, codexNodeFactory);
|
2023-09-11 14:57:57 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-13 08:23:05 +00:00
|
|
|
|
Stopwatch.Measure(pluginTools.GetLog(), "EnsureOnline", group.EnsureOnline);
|
2023-09-11 14:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
CodexNodesNotOnline(runningContainers);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return group;
|
|
|
|
|
}
|
2023-09-11 09:59:33 +00:00
|
|
|
|
|
2023-09-11 14:57:57 +00:00
|
|
|
|
private void CodexNodesNotOnline(RunningContainers[] runningContainers)
|
|
|
|
|
{
|
2023-09-13 07:12:18 +00:00
|
|
|
|
Log("Codex nodes failed to start");
|
2023-09-11 14:57:57 +00:00
|
|
|
|
// todo:
|
|
|
|
|
//foreach (var container in runningContainers.Containers()) lifecycle.DownloadLog(container);
|
|
|
|
|
}
|
2023-09-11 09:59:33 +00:00
|
|
|
|
|
|
|
|
|
//private StartupWorkflow CreateWorkflow()
|
|
|
|
|
//{
|
|
|
|
|
// return lifecycle.WorkflowCreator.CreateWorkflow();
|
|
|
|
|
//}
|
|
|
|
|
|
2023-09-12 13:18:36 +00:00
|
|
|
|
private void LogSeparator()
|
|
|
|
|
{
|
|
|
|
|
Log("----------------------------------------------------------------------------");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Log(string message)
|
|
|
|
|
{
|
2023-09-13 08:23:05 +00:00
|
|
|
|
pluginTools.GetLog().Log(message);
|
2023-09-12 13:18:36 +00:00
|
|
|
|
}
|
2023-09-11 09:59:33 +00:00
|
|
|
|
|
2023-09-13 10:09:44 +00:00
|
|
|
|
private void StopCrashWatcher(RunningContainers containers)
|
|
|
|
|
{
|
|
|
|
|
foreach (var c in containers.Containers)
|
|
|
|
|
{
|
|
|
|
|
c.CrashWatcher?.Stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-11 09:59:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|