Implemented update functionallity
All checks were successful
Updater CI/CD / publish (push) Successful in 2m18s
All checks were successful
Updater CI/CD / publish (push) Successful in 2m18s
This commit is contained in:
58
UpdateWorker.cs
Normal file
58
UpdateWorker.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace ServiceUpdater;
|
||||
|
||||
public sealed class UpdateWorker(UpdaterConfig config) {
|
||||
public async IAsyncEnumerable<string> UpdateService(string serviceName) {
|
||||
yield return $"Starting update for {serviceName}";
|
||||
|
||||
await foreach (var line in RunProcess(serviceName, "compose pull")) {
|
||||
yield return line;
|
||||
}
|
||||
|
||||
yield return $"Downloaded changes for {serviceName}";
|
||||
|
||||
await foreach (var line in RunProcess(serviceName, "compose up -d --remove-orphans")) {
|
||||
yield return line;
|
||||
}
|
||||
|
||||
yield return $"Successfully updated {serviceName}";
|
||||
}
|
||||
|
||||
private async IAsyncEnumerable<string> RunProcess(string folder, string arguments) {
|
||||
var process = new Process {
|
||||
StartInfo = new() {
|
||||
FileName = "docker",
|
||||
Arguments = arguments,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
WorkingDirectory = Path.Combine(config.ServicesRoot, folder)
|
||||
}
|
||||
};
|
||||
|
||||
process.Start();
|
||||
|
||||
var output = process.StandardOutput.ReadLineAsync();
|
||||
var error = process.StandardError.ReadLineAsync();
|
||||
|
||||
while (true) {
|
||||
var completed = await Task.WhenAny(output, error);
|
||||
|
||||
if (completed == output) {
|
||||
var line = await output;
|
||||
if (line == null) break;
|
||||
yield return line;
|
||||
output = process.StandardOutput.ReadLineAsync();
|
||||
}
|
||||
else {
|
||||
var line = await error;
|
||||
if (line == null) break;
|
||||
yield return "[ERR] " + line;
|
||||
error = process.StandardError.ReadLineAsync();
|
||||
}
|
||||
}
|
||||
|
||||
await process.WaitForExitAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user