Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CLI Runtime selector list #622

Merged
merged 2 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions Source/CLI/Runtime/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dolittle.Runtime.ApplicationModel;
using Dolittle.Runtime.Artifacts;
using Dolittle.Runtime.CLI.Runtime.EventTypes;
using Dolittle.Runtime.Events;
Expand Down Expand Up @@ -60,7 +61,7 @@ protected IEnumerable<EventType> EventTypes
/// <returns>A <see cref="Try{TResult}"/> of type <see cref="MicroserviceAddress"/>.</returns>
protected async Task<Try<MicroserviceAddress>> SelectRuntimeToConnectTo(CommandLineApplication cli)
{
var addresses = (await _runtimes.GetAvailableRuntimeAddresses(Runtime)).ToList();
var addresses = (await _runtimes.GetAvailableRuntimeAddresses(Runtime)).OrderBy(ListTextFor).ToList();

if (!addresses.Any())
{
Expand All @@ -70,23 +71,22 @@ protected async Task<Try<MicroserviceAddress>> SelectRuntimeToConnectTo(CommandL

if (addresses.Count == 1)
{
return addresses[0];
return new MicroserviceAddress(addresses[0].Host, addresses[0].Port);
}

await cli.Out.WriteLineAsync("Found multiple available Runtimes, please select one of the following:");
while (true)
{
for (var i = 0; i < addresses.Count; i++)
{
var (host, port) = addresses[i];
await cli.Out.WriteLineAsync($"\t{i}) {host.Value}:{port.Value}");
await cli.Out.WriteLineAsync($"\t{i}) {ListTextFor(addresses[i])})");
}

var selection = Prompt.GetInt($"Select Runtime (0-{addresses.Count - 1}):");

if (selection >= 0 && selection < addresses.Count)
{
return addresses[selection];
return new MicroserviceAddress(addresses[selection].Host, addresses[selection].Port);
}

await cli.Out.WriteLineAsync("Invalid number, please select one of the following:");
Expand Down Expand Up @@ -115,4 +115,10 @@ protected string ResolveEventTypeIdentifier(ArtifactId eventTypeId)
? eventTypeId.Value.ToString()
: eventType.Alias;
}
}

static string ListTextFor(NamedRuntimeAddress address)
=> address.Name != MicroserviceName.NotSet && !string.IsNullOrWhiteSpace(address.Name)
? $"{address.Name} ({address.Host.Value}:{address.Port.Value})"
: $"{address.Host.Value}:{address.Port.Value}";

}
32 changes: 26 additions & 6 deletions Source/CLI/Runtime/DockerRuntimeAddresses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Docker.DotNet;
using Docker.DotNet.Models;
using Dolittle.Runtime.ApplicationModel;
using Dolittle.Runtime.Microservices;
using Dolittle.Runtime.Services;

Expand All @@ -31,18 +32,18 @@ public DockerRuntimeAddresses(IDockerClient client)
}

/// <inheritdoc />
public async Task<IEnumerable<MicroserviceAddress>> Discover()
public async Task<IEnumerable<NamedRuntimeAddress>> Discover()
{
try
{
var containers = await _client.Containers.ListContainersAsync(new ContainersListParameters());
var runtimesWithExposedManagement =
containers.Where(IsRuntimeContainer).Where(HasManagementPortExposed);
return runtimesWithExposedManagement.Select(ManagmentPortAddress);
return runtimesWithExposedManagement.Select(ManagementPortAddress);
}
catch
{
return Array.Empty<MicroserviceAddress>();
return Array.Empty<NamedRuntimeAddress>();
}
}

Expand All @@ -52,9 +53,28 @@ static bool IsRuntimeContainer(ContainerListResponse container)
static bool HasManagementPortExposed(ContainerListResponse container)
=> container.Ports.Any(IsManagementPort);

static MicroserviceAddress ManagmentPortAddress(ContainerListResponse container)
=> new("localhost", container.Ports.First(IsManagementPort).PublicPort);
static NamedRuntimeAddress ManagementPortAddress(ContainerListResponse container)
=> new(
GetContainerName(container),
"localhost",
container.Ports.First(IsManagementPort).PublicPort);

static MicroserviceName GetContainerName(ContainerListResponse container)
{
if (container.Labels.TryGetValue("com.docker.compose.service", out var dockerComposeName))
{
return dockerComposeName;
}

var name = container.Names.FirstOrDefault(_ => !string.IsNullOrWhiteSpace(_));
if (name != null)
{
return name.TrimStart('/');
}

return MicroserviceName.NotSet;
}

static bool IsManagementPort(Port port)
=> port.Type == "tcp" && port.PrivatePort == EndpointsConfigurationDefaultProvider.DefaultManagementPort;
}
}
7 changes: 3 additions & 4 deletions Source/CLI/Runtime/ICanDiscoverRuntimeAddresses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Collections.Generic;
using System.Threading.Tasks;
using Dolittle.Runtime.Microservices;

namespace Dolittle.Runtime.CLI.Runtime;

Expand All @@ -15,6 +14,6 @@ public interface ICanDiscoverRuntimeAddresses
/// <summary>
/// Discovers addresses of available Runtimes.
/// </summary>
/// <returns>An <see cref="IEnumerable{T}"/> of type <see cref="MicroserviceAddress"/> to available Runtimes.</returns>
Task<IEnumerable<MicroserviceAddress>> Discover();
}
/// <returns>An <see cref="IEnumerable{T}"/> of type <see cref="NamedRuntimeAddress"/> to available Runtimes.</returns>
Task<IEnumerable<NamedRuntimeAddress>> Discover();
}
6 changes: 3 additions & 3 deletions Source/CLI/Runtime/ICanLocateRuntimes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public interface ICanLocateRuntimes
/// Gets the addresses of Runtimes that are available to connect to, or the address provided in the argument.
/// </summary>
/// <param name="argument">An optional address provided to the CLI as an argument.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of type <see cref="MicroserviceAddress"/> to available Runtimes.</returns>
Task<IEnumerable<MicroserviceAddress>> GetAvailableRuntimeAddresses(MicroserviceAddress argument = null);
}
/// <returns>An <see cref="IEnumerable{T}"/> of type <see cref="NamedRuntimeAddress"/> to available Runtimes.</returns>
Task<IEnumerable<NamedRuntimeAddress>> GetAvailableRuntimeAddresses(MicroserviceAddress argument = null);
}
18 changes: 18 additions & 0 deletions Source/CLI/Runtime/NamedRuntimeAddress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Dolittle.Runtime.ApplicationModel;
using Dolittle.Runtime.Microservices;

namespace Dolittle.Runtime.CLI.Runtime;

/// <summary>
/// Represents the address to a named instance of a Runtime for a Microservice.
/// </summary>
/// <param name="Name">The name of the Microservice.</param>
/// <param name="Host">The host of a microservice.</param>
/// <param name="Port">The host of a microservice.</param>
public record NamedRuntimeAddress(
MicroserviceName Name,
MicroserviceHost Host,
MicroservicePort Port);
8 changes: 5 additions & 3 deletions Source/CLI/Runtime/RuntimeLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dolittle.Runtime.ApplicationModel;
using Dolittle.Runtime.Microservices;
using Dolittle.Runtime.Services;

Expand All @@ -27,18 +28,19 @@ public RuntimeLocator(IEnumerable<ICanDiscoverRuntimeAddresses> addressProviders
}

/// <inheritdoc />
public async Task<IEnumerable<MicroserviceAddress>> GetAvailableRuntimeAddresses(MicroserviceAddress argument = null)
public async Task<IEnumerable<NamedRuntimeAddress>> GetAvailableRuntimeAddresses(MicroserviceAddress argument = null)
{
if (argument == null)
{
var results = await Task.WhenAll(_addressProviders.Select(_ => _.Discover()));
return results.SelectMany(_ => _);
}

var address = new MicroserviceAddress(
var address = new NamedRuntimeAddress(
MicroserviceName.NotSet,
string.IsNullOrWhiteSpace(argument.Host) ? DefaultRuntimeHost : argument.Host,
argument.Port == 0 ? EndpointsConfigurationDefaultProvider.DefaultManagementPort : argument.Port);

return new[] {address};
}
}
}