-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor broker and add management commands (#70)
* Refactor into a broker abstraction * Clean up tidbits * Idk wtf is happening here
- Loading branch information
1 parent
06b98c9
commit 781df86
Showing
12 changed files
with
162 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
namespace UnMango.Tdl.Broker.Tests; | ||
|
||
public class UnitTest1 | ||
{ | ||
[Fact] | ||
public void Test1() | ||
{ | ||
|
||
} | ||
{ | ||
[Fact] | ||
public void Test1() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System.CommandLine; | ||
using System.Text.RegularExpressions; | ||
using Serilog; | ||
using UnMango.Tdl.Cli.Docker; | ||
|
||
namespace UnMango.Tdl.Cli.Broker; | ||
|
||
internal static partial class Patterns | ||
{ | ||
[GeneratedRegex(".*Application started.*")] | ||
public static partial Regex ApplicationStarted(); | ||
} | ||
|
||
internal sealed class DockerBroker(IDocker docker) : IBroker | ||
{ | ||
private const string OwnerLabel = "tdl.owner", Owner = "tdl-cli"; | ||
private static readonly Regex ApplicationStarted = Patterns.ApplicationStarted(); | ||
|
||
private static readonly IDictionary<string, string> Labels = new Dictionary<string, string> { | ||
[Config.OwnerLabel] = Owner, | ||
}; | ||
|
||
public async ValueTask<bool> Running(CancellationToken cancellationToken) { | ||
if (File.Exists(Config.Socket)) { | ||
return true; | ||
} | ||
|
||
var container = await docker.FindMatching(Labels, cancellationToken); | ||
return container != null; | ||
} | ||
|
||
public async ValueTask Start(CancellationToken cancellationToken) { | ||
if (await Running(cancellationToken)) { | ||
Log.Verbose("Broker is already running"); | ||
return; | ||
} | ||
|
||
var uid = await Config.Uid(); | ||
var gid = await Config.Gid(); | ||
|
||
Log.Debug("Starting broker"); | ||
var container = await docker.Start(new StartArgs { | ||
Image = $"{Config.ContainerRepo}/tdl-broker", | ||
Tag = Config.ContainerTag, | ||
User = $"{uid}:{gid}", | ||
Volumes = [$"{Config.SocketDir}:/var/run/tdl"], | ||
Labels = { [OwnerLabel] = Owner }, | ||
}, cancellationToken); | ||
Log.Verbose("Started broker"); | ||
|
||
_ = docker.FollowLogs(container, cancellationToken); | ||
await docker.WaitFor(container, ApplicationStarted.IsMatch, cancellationToken); | ||
} | ||
|
||
public async Task<BrokerStatus> Status(CancellationToken cancellationToken) { | ||
var container = await docker.FindMatching(Labels, cancellationToken); | ||
if (container is null) { | ||
return new BrokerStatus { | ||
State = "not found", | ||
Version = "unknown", | ||
}; | ||
} | ||
|
||
var inspection = await docker.Inspect(container, cancellationToken); | ||
return new BrokerStatus { | ||
Version = inspection.Version, | ||
State = inspection.State, | ||
}; | ||
} | ||
|
||
public async ValueTask Stop(CancellationToken cancellationToken) { | ||
if (!await Running(cancellationToken)) | ||
return; | ||
|
||
var container = await docker.FindMatching(Labels, cancellationToken); | ||
if (container is null) | ||
return; | ||
|
||
await docker.Stop(container, cancellationToken); | ||
} | ||
|
||
public Task Upgrade(string version, CancellationToken cancellationToken) { | ||
throw new NotImplementedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,18 @@ | ||
using System.CommandLine.Invocation; | ||
using System.Text.RegularExpressions; | ||
using System.CommandLine.Parsing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Serilog; | ||
using UnMango.Tdl.Cli.Docker; | ||
|
||
namespace UnMango.Tdl.Cli.Broker; | ||
|
||
internal static partial class Patterns | ||
{ | ||
[GeneratedRegex(".*Application started.*")] | ||
public static partial Regex ApplicationStarted(); | ||
} | ||
|
||
internal static class EnsureBroker | ||
{ | ||
private const string OwnerLabel = "tdl.owner", Owner = "tdl-cli"; | ||
private static readonly Regex ApplicationStarted = Patterns.ApplicationStarted(); | ||
|
||
public static InvocationMiddleware Middleware => async (context, next) => { | ||
var docker = context.BindingContext.GetRequiredService<IDocker>(); | ||
await EnsureStarted(docker, context.GetCancellationToken()); | ||
var parentCommand = context.ParseResult.CommandResult.Parent; | ||
if (parentCommand is not CommandResult { Command.Name: "broker" }) { | ||
var broker = context.BindingContext.GetRequiredService<IBroker>(); | ||
await broker.Start(context.GetCancellationToken()); | ||
} | ||
|
||
await next(context); | ||
}; | ||
|
||
private static Task EnsureStarted(IDocker docker, CancellationToken cancellationToken) { | ||
Log.Verbose("Checking for socket existence"); | ||
if (!File.Exists(Config.Socket)) | ||
return Start(docker, cancellationToken); | ||
|
||
Log.Debug("Socket exists"); | ||
return Task.CompletedTask; | ||
} | ||
|
||
private static async Task Start(IDocker docker, CancellationToken cancellationToken) { | ||
var uid = await Config.Uid(); | ||
var gid = await Config.Gid(); | ||
|
||
Log.Debug("Starting broker"); | ||
var container = await docker.Start(new StartArgs { | ||
Image = $"{Config.ContainerRepo}/tdl-broker", | ||
Tag = Config.ContainerTag, | ||
User = $"{uid}:{gid}", | ||
Volumes = [$"{Config.SocketDir}:/var/run/tdl"], | ||
Labels = { [OwnerLabel] = Owner }, | ||
}, cancellationToken); | ||
Log.Verbose("Started broker"); | ||
|
||
_ = docker.FollowLogs(container, cancellationToken); | ||
await docker.WaitFor(container, ApplicationStarted.IsMatch, cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
using System.CommandLine; | ||
using UnMango.Tdl.Cli.Docker; | ||
using UnMango.Tdl.Cli.Internal; | ||
using System.Reflection; | ||
using Serilog; | ||
|
||
namespace UnMango.Tdl.Cli.Broker; | ||
|
||
internal static class Handlers | ||
{ | ||
public static Task Start(CancellationToken cancellationToken) { | ||
Console.WriteLine("Not implemented"); | ||
return Task.CompletedTask; | ||
public static Task Start(IBroker broker, CancellationToken cancellationToken) { | ||
return broker.Start(cancellationToken).AsTask(); | ||
} | ||
|
||
public static async Task Status(IDocker docker, IConsole console, CancellationToken cancellationToken) { | ||
var labels = new Dictionary<string, string> { | ||
[Config.OwnerLabel] = Constants.Owner, | ||
}; | ||
|
||
var container = await docker.FindMatching(labels, cancellationToken); | ||
if (container is null) { | ||
console.WriteLine($"Unable to find matching container: {labels}"); | ||
return; | ||
} | ||
|
||
var inspection = await docker.Inspect(container, cancellationToken); | ||
console.WriteLine($"Version : {inspection.Version}"); | ||
console.WriteLine($"State : {inspection.State}"); | ||
public static async Task Status(IBroker broker, IConsole console, CancellationToken cancellationToken) { | ||
var status = await broker.Status(cancellationToken); | ||
console.WriteLine($"Version: {status.Version}"); | ||
console.WriteLine($"State: {status.State}"); | ||
} | ||
|
||
public static Task Stop(CancellationToken cancellationToken) { | ||
Console.WriteLine("Not implemented"); | ||
return Task.CompletedTask; | ||
public static Task Stop(IBroker broker, CancellationToken cancellationToken) { | ||
return broker.Stop(cancellationToken).AsTask(); | ||
} | ||
|
||
public static Task Upgrade(CancellationToken cancellationToken) { | ||
Console.WriteLine("Not implemented"); | ||
return Task.CompletedTask; | ||
public static Task Upgrade(IBroker broker, CancellationToken cancellationToken) { | ||
var entryAssembly = Assembly.GetEntryAssembly(); | ||
if (entryAssembly is null) { | ||
Log.Error("Failed to retrieve entry assembly"); | ||
return Task.CompletedTask; | ||
} | ||
|
||
var version = entryAssembly.GetName().Version; | ||
if (version is null) { | ||
Log.Error("Failed to retrieve version"); | ||
return Task.CompletedTask; | ||
} | ||
|
||
Log.Verbose("Upgrading broker"); | ||
return broker.Upgrade(version.ToString(), cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace UnMango.Tdl.Cli.Broker; | ||
|
||
public sealed record BrokerStatus | ||
{ | ||
public required string State { get; init; } | ||
|
||
public required string Version { get; init; } | ||
} | ||
|
||
public interface IBroker | ||
{ | ||
ValueTask<bool> Running(CancellationToken cancellationToken); | ||
|
||
ValueTask Start(CancellationToken cancellationToken); | ||
|
||
Task<BrokerStatus> Status(CancellationToken cancellationToken); | ||
|
||
ValueTask Stop(CancellationToken cancellationToken); | ||
|
||
Task Upgrade(string version, CancellationToken cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
13 changes: 9 additions & 4 deletions
13
src/Cli/Docker/AddDocker.cs → src/Cli/Internal/Middleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
using System.CommandLine.Invocation; | ||
using Docker.DotNet; | ||
using UnMango.Tdl.Cli.Broker; | ||
using UnMango.Tdl.Cli.Docker; | ||
|
||
namespace UnMango.Tdl.Cli.Docker; | ||
namespace UnMango.Tdl.Cli.Internal; | ||
|
||
internal static class AddDocker | ||
internal static class Middleware | ||
{ | ||
public static InvocationMiddleware Middleware => async (context, next) => { | ||
public static InvocationMiddleware Services => async (context, next) => { | ||
// var progress = new ConsoleProgress(context.Console); | ||
var progress = new SerilogProgress(); | ||
|
||
// Either need to await this method or dispose of this differently | ||
using var client = new DockerClientConfiguration().CreateClient(); | ||
var docker = new Docker(client, progress); | ||
var docker = new Docker.Docker(client, progress); | ||
context.BindingContext.AddService<IDocker>(_ => docker); | ||
context.BindingContext.AddService<IBroker>(_ => new DockerBroker(docker)); | ||
|
||
await next(context); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters