pretty sure commands all work

just need checks
This commit is contained in:
Adam R. Grey 2021-07-23 04:18:21 -04:00
parent 5b77672ac7
commit 5454333ac4
5 changed files with 49 additions and 29 deletions

View File

@ -4,17 +4,11 @@ public class Config
{
public string name { get; set; } = "guy who didn't configure";
public string bootstrap_servers { get; set; } = "localhost:9092";
public string handling_group { get; set; } = null;
public string kafka_location { get; set; } = "/usr/lib/librdkafka.so";
public Dictionary<string, string> topics { get; set; } = new Dictionary<string, string>() {
{"global", "silver_meddlists.global"},
{"directorial", "silver_meddlists.directorial"}
};
public class Command
{
public string name { get; set; }
public string shell { get; set; }
}
public List<Command> commands { get; set; } = new List<Command>() { new Command() { name = "echo", shell = "echo" } };
public List<Command> commands { get; set; }
public List<Command> checks { get; set; }
}

View File

@ -21,17 +21,18 @@ namespace directors_assistant
static async Task Main(string[] args)
{
conf = JsonConvert.DeserializeObject<Config>(File.ReadAllText("appsettings.json"));
telefranz = new Telefranz(conf.name, conf.bootstrap_servers, new List<string>() { "proof_of_concept" });
telefranz = new Telefranz(conf.name, conf.bootstrap_servers,
conf.commands.Select(c => c.name).ToList(),
conf.checks.Select(c => c.name).ToList());
telefranz.addHandler((silver_messages.directorial.execute_command ec) =>
{
Console.WriteLine(JsonConvert.SerializeObject(ec));
var matchedCommands = conf.commands.Where(c => c.name == ec.command)?.ToList();
//I swear I have a vague memory of foreach in linq'd query throwing an exception when results empty.
if (matchedCommands != null && matchedCommands.Count > 0)
{
foreach (var cmd in matchedCommands)
{
Console.WriteLine($"executing {cmd.name}, given args {ec.args}" + cmd.name);
Console.WriteLine($"executing {cmd.name}{(ec.args != null ? ", given args " + string.Join(' ', ec.args) : "")} with timeout {ec.timeout}");
var cmdAndArgs = cmd.shell.Split(' ');
var justArgs = cmdAndArgs.Skip(1).ToList();
@ -52,13 +53,13 @@ namespace directors_assistant
}
}
});
Console.WriteLine("off we go");
telefranz.addHandler((silver_messages.directorial.execute_check ec) => {
//TODO
});
await Task.Delay(1000);
Console.WriteLine("im a strong independent director's assistant who don't need no director");
telefranz.ProduceMessage(new silver_messages.global.sound_off());
await Task.Delay(120000);
Console.WriteLine("alright fuck it");
await Task.Delay(-1);
}
private static void executeTimed(string commandName, string commandPath, string commandArguments, int timeout)
@ -84,10 +85,11 @@ namespace directors_assistant
if (process.HasExited)
{
stopwatch.Stop();
Console.WriteLine($"{commandName} completed");
telefranz.ProduceMessage(new silver_messages.directorial.command_completed()
{
command = commandName,
runtime = (uint)stopwatch.ElapsedMilliseconds,
runtimeMilliseconds = stopwatch.ElapsedMilliseconds,
exit_code = process.ExitCode,
stdout = string.Join('\n', outputs),
stderr = string.Join('\n', errors)
@ -96,38 +98,56 @@ namespace directors_assistant
else
{
process.Kill();
Console.WriteLine($"{commandName} expired");
telefranz.ProduceMessage(new silver_messages.directorial.command_expired()
{
command = commandName,
runtime = (uint)timeout
stdout = string.Join('\n', outputs),
stderr = string.Join('\n', errors)
});
}
}
private static void executeUntimed(string commandName, string commandPath, string commandArguments)
{
var stopwatch = new Stopwatch();
var process = readableProcess(commandPath, commandArguments);
process.OutputDataReceived += new DataReceivedEventHandler((s, e) => {
Console.WriteLine($"{commandName} output: {e.Data}");
if(string.IsNullOrWhiteSpace(e.Data))
{
return;
}
telefranz.ProduceMessage(new silver_messages.directorial.command_output() {
stdout = e.Data
stdout = e.Data,
command = commandName,
runtime = (uint)stopwatch.ElapsedMilliseconds
});
});
process.ErrorDataReceived += new DataReceivedEventHandler((s, e) => {
if(string.IsNullOrWhiteSpace(e.Data))
{
return;
}
Console.Error.WriteLine($"{commandName} err (but not necessarily dead?): {e.Data}");
telefranz.ProduceMessage(new silver_messages.directorial.command_error() {
stderr = e.Data
stderr = e.Data,
command = commandName,
runtime = (uint)stopwatch.ElapsedMilliseconds
});
});
var stopwatch = new Stopwatch();
stopwatch.Start();
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
stopwatch.Stop();
Console.WriteLine($"{commandName} returned {process.ExitCode} after {stopwatch.ElapsedMilliseconds}ms");
telefranz.ProduceMessage(new silver_messages.directorial.command_completed()
{
command = commandName,
runtime = (uint)stopwatch.ElapsedMilliseconds,
runtimeMilliseconds = stopwatch.ElapsedMilliseconds,
exit_code = process.ExitCode
});
}

View File

@ -1,14 +1,10 @@
{
"name": "guy who didn't configure",
"bootstrap servers": "localhost:9092",
"handling group": "directors_assistant.foo",
"kafka location": "/usr/lib/librdkafka.so",
"topics": [
"silver_meddlists.global",
"silver_meddlists.directorial"
],
"commands": [
{"name":"echo", "shell": "echo"}
],
"checks": []
"checks": [
{"name": "echo", "shell": "echo 1"}
]
}

View File

@ -13,12 +13,15 @@
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="silvermeddlists.franz" Version="0.0.2" />
<PackageReference Include="silvermeddlists.franz" Version="0.0.3" />
</ItemGroup>
<ItemGroup>
<None Update="AppSettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="AppSettings.example.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

7
loopy.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
for i in {1..10}
do
echo "$i: $(date)"
sleep 3
done