Very simple log appender

This commit is contained in:
benbierens 2023-07-20 10:52:19 +02:00
parent f2517b2ade
commit 2dd5775296
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
3 changed files with 99 additions and 3 deletions

View File

@ -15,6 +15,9 @@ namespace CodexNetDownloader
[Uniform("kube-config", "kc", "KUBECONFIG", true, "Path to Kubeconfig file. Use 'null' (default) to use local cluster.")] [Uniform("kube-config", "kc", "KUBECONFIG", true, "Path to Kubeconfig file. Use 'null' (default) to use local cluster.")]
public string KubeConfigFile { get; set; } = "null"; public string KubeConfigFile { get; set; } = "null";
[Uniform("continuous", "c", "CONTINUOUS", false, "If true, will continuously download and append log files.")]
public bool Continuous { get; set; } = false;
public CodexDeployment CodexDeployment { get; set; } = null!; public CodexDeployment CodexDeployment { get; set; } = null!;
public TestRunnerLocation RunnerLocation { get; set; } = TestRunnerLocation.InternalToCluster; public TestRunnerLocation RunnerLocation { get; set; } = TestRunnerLocation.InternalToCluster;

View File

@ -0,0 +1,84 @@
using DistTestCore;
using KubernetesWorkflow;
namespace CodexNetDownloader
{
public class ContinuousLogDownloader
{
private readonly TestLifecycle lifecycle;
private readonly Configuration config;
public ContinuousLogDownloader(TestLifecycle lifecycle, Configuration config)
{
this.lifecycle = lifecycle;
this.config = config;
}
public void Run()
{
while (true)
{
UpdateLogs();
Thread.Sleep(TimeSpan.FromSeconds(30));
}
}
private void UpdateLogs()
{
Console.WriteLine("Updating logs...");
foreach (var container in config.CodexDeployment.CodexContainers)
{
UpdateLog(container);
}
}
private void UpdateLog(RunningContainer container)
{
var filepath = Path.Combine(config.OutputPath, GetLogName(container));
if (!File.Exists(filepath)) File.WriteAllLines(filepath, new[] { "" });
var appender = new LogAppender(filepath);
lifecycle.CodexStarter.DownloadLog(container, appender);
}
private static string GetLogName(RunningContainer container)
{
return container.Name
.Replace("<","")
.Replace(">", "")
+ ".log";
}
}
public class LogAppender : ILogHandler
{
private readonly string filename;
public LogAppender(string filename)
{
this.filename = filename;
}
public void Log(Stream log)
{
using var reader = new StreamReader(log);
var currentLines = File.ReadAllLines(filename);
var line = reader.ReadLine();
while (line != null)
{
AppendLineIfNew(line, currentLines);
line = reader.ReadLine();
}
}
private void AppendLineIfNew(string line, string[] currentLines)
{
if (!currentLines.Contains(line))
{
File.AppendAllLines(filename, new[] { line });
}
}
}
}

View File

@ -1,4 +1,5 @@
using ArgsUniform; using ArgsUniform;
using CodexNetDownloader;
using ContinuousTests; using ContinuousTests;
using DistTestCore; using DistTestCore;
using DistTestCore.Codex; using DistTestCore.Codex;
@ -27,12 +28,20 @@ public class Program
var k8sFactory = new K8sFactory(); var k8sFactory = new K8sFactory();
var (_, lifecycle) = k8sFactory.CreateFacilities(config.KubeConfigFile, config.OutputPath, "dataPath", config.CodexDeployment.Metadata.KubeNamespace, new DefaultTimeSet(), new NullLog(), config.RunnerLocation); var (_, lifecycle) = k8sFactory.CreateFacilities(config.KubeConfigFile, config.OutputPath, "dataPath", config.CodexDeployment.Metadata.KubeNamespace, new DefaultTimeSet(), new NullLog(), config.RunnerLocation);
foreach (var container in config.CodexDeployment.CodexContainers) if (config.Continuous)
{ {
lifecycle.DownloadLog(container); var dl = new ContinuousLogDownloader(lifecycle, config);
dl.Run();
} }
else
{
foreach (var container in config.CodexDeployment.CodexContainers)
{
lifecycle.DownloadLog(container);
}
Console.WriteLine("Done!"); Console.WriteLine("Done!");
}
} }
private static CodexDeployment ParseCodexDeploymentJson(string filename) private static CodexDeployment ParseCodexDeploymentJson(string filename)