ContainerCodexStarter.cs
1 using CodexClient; 2 using Core; 3 using KubernetesWorkflow; 4 using KubernetesWorkflow.Types; 5 using Utils; 6 7 namespace CodexPlugin 8 { 9 public class ContainerCodexStarter : ICodexStarter 10 { 11 private readonly IPluginTools pluginTools; 12 private readonly ProcessControlMap processControlMap; 13 private readonly CodexContainerRecipe recipe; 14 private readonly ApiChecker apiChecker; 15 16 public ContainerCodexStarter(IPluginTools pluginTools, CodexContainerRecipe recipe, ProcessControlMap processControlMap) 17 { 18 this.pluginTools = pluginTools; 19 this.recipe = recipe; 20 this.processControlMap = processControlMap; 21 apiChecker = new ApiChecker(pluginTools); 22 } 23 24 public ICodexInstance[] BringOnline(CodexSetup codexSetup) 25 { 26 LogSeparator(); 27 Log($"Starting {codexSetup.Describe()}..."); 28 29 var startupConfig = CreateStartupConfig(codexSetup); 30 31 var containers = StartCodexContainers(startupConfig, codexSetup.NumberOfNodes, codexSetup.Location); 32 33 apiChecker.CheckCompatibility(containers); 34 35 foreach (var rc in containers) 36 { 37 var podInfo = GetPodInfo(rc); 38 var podInfos = string.Join(", ", rc.Containers.Select(c => $"Container: '{c.Name}' PodLabel: '{c.RunningPod.StartResult.Deployment.PodLabel}' runs at '{podInfo.K8SNodeName}'={podInfo.Ip}")); 39 Log($"Started node with image '{containers.First().Containers.First().Recipe.Image}'. ({podInfos})"); 40 LogEthAddress(rc); 41 } 42 LogSeparator(); 43 44 return containers.Select(CreateInstance).ToArray(); 45 } 46 47 public void Decommission() 48 { 49 } 50 51 private StartupConfig CreateStartupConfig(CodexSetup codexSetup) 52 { 53 var startupConfig = new StartupConfig(); 54 startupConfig.NameOverride = codexSetup.NameOverride; 55 startupConfig.Add(codexSetup); 56 return startupConfig; 57 } 58 59 private RunningPod[] StartCodexContainers(StartupConfig startupConfig, int numberOfNodes, ILocation location) 60 { 61 var futureContainers = new List<FutureContainers>(); 62 for (var i = 0; i < numberOfNodes; i++) 63 { 64 var workflow = pluginTools.CreateWorkflow(); 65 futureContainers.Add(workflow.Start(1, location, recipe, startupConfig)); 66 } 67 68 return futureContainers 69 .Select(f => f.WaitForOnline()) 70 .ToArray(); 71 } 72 73 private PodInfo GetPodInfo(RunningPod rc) 74 { 75 var workflow = pluginTools.CreateWorkflow(); 76 return workflow.GetPodInfo(rc); 77 } 78 79 private ICodexInstance CreateInstance(RunningPod pod) 80 { 81 var instance = CodexInstanceContainerExtension.CreateFromPod(pod); 82 var processControl = new CodexContainerProcessControl(pluginTools, pod, onStop: () => 83 { 84 processControlMap.Remove(instance); 85 }); 86 processControlMap.Add(instance, processControl); 87 return instance; 88 } 89 90 private void LogSeparator() 91 { 92 Log("----------------------------------------------------------------------------"); 93 } 94 95 private void LogEthAddress(RunningPod rc) 96 { 97 var account = rc.Containers.First().Recipe.Additionals.Get<EthAccount>(); 98 if (account == null) return; 99 Log($"{rc.Name} = {account}"); 100 } 101 102 private void Log(string message) 103 { 104 pluginTools.GetLog().Log(message); 105 } 106 } 107 }