fix(cli)!: stop using the default instance for a missing container

status no longer probes the default instance for a named container with no known
endpoint, and env, services, snapshot and az setup exit 1 with the next step
instead of acting on it. wait keeps looking for the container until --timeout, and
doctor and version still run. A profile's port is used before its container
starts, and an endpoint from --endpoint, FLOCI_ENDPOINT or another host beats a
local container's published port.

BREAKING CHANGE: in status -o json, endpoint is null when the selected container
has no known endpoint. env, services, snapshot and az setup exit 1 in that case,
where they used to use the default endpoint. An --endpoint or FLOCI_ENDPOINT on
the default port is now honoured even when a local container publishes another
port.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-11-03 23:53:22 -06:00
1 parent ac52515471
commit 33367ecb9b
17 files changed
+469 -57

No files matched your search

+84 -16
View File
@@ -1,6 +1,7 @@
package io.floci.cli;
import io.floci.cli.docker.DockerClient;
import io.floci.cli.docker.DockerException;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.Glyphs;
import io.floci.cli.output.OutputFormat;
@@ -11,6 +12,8 @@ import picocli.CommandLine.Spec;
import java.net.URI;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
@@ -37,10 +40,15 @@ public class GlobalOptions {
@Spec(Spec.Target.MIXEE)
CommandSpec mixee;
private static final Set<String> LOOPBACK_HOSTS = Set.of("localhost", "127.0.0.1", "[::1]", "localhost.floci.io");
private static final Pattern PLAIN_WORD = Pattern.compile("[A-Za-z0-9._@%+=:,/-]+");
public final ProductProfile product;
// The value FLOCI_ENDPOINT supplied, if any: that is a choice, like typing --endpoint.
private final String endpointFromEnv;
@Option(names = {"--endpoint"},
description = "Floci server endpoint URL (default: ${DEFAULT-VALUE})",
paramLabel = "<url>")
@@ -88,6 +96,7 @@ public class GlobalOptions {
/** Test seam: {@code envLookup} replaces {@code System.getenv}. */
public GlobalOptions(ProductProfile product, UnaryOperator<String> envLookup) {
this.product = product;
this.endpointFromEnv = envLookup.apply(product.envVar("ENDPOINT"));
this.endpoint = envOr(envLookup, product.envVar("ENDPOINT"), product.defaultEndpoint());
this.container = envOr(envLookup, product.envVar("CONTAINER"), product.defaultContainer());
}
@@ -157,27 +166,86 @@ public class GlobalOptions {
return noEmoji || !terminal || noColorEnv;
}
// Inspects the container and derives the endpoint from its host port mapping.
// Falls back to the configured endpoint if the container is not found or has no mapping.
public String resolvedEndpoint(DockerClient docker) {
/** Whether a configured endpoint can identify this instance without a container lookup. */
public boolean canUseConfiguredEndpoint() {
return typed("--endpoint") || !product.defaultEndpoint().equals(endpoint)
|| product.defaultContainer().equals(container);
}
/**
* Whether the configured endpoint was chosen on purpose, so it beats the port a local container
* publishes: typed, from the environment, or on another host. A loopback endpoint from a
* profile is only where the instance was last known to be, so the container corrects it.
*/
public boolean configuredEndpointWins() {
return typed("--endpoint") || endpoint.equals(endpointFromEnv) || !isLoopback(endpoint);
}
private static boolean isLoopback(String url) {
try {
return docker.inspectContainer(container)
.map(info -> endpointFromPorts(info.ports(), endpoint))
.orElse(endpoint);
} catch (Exception e) {
return endpoint;
String host = URI.create(url).getHost();
return host == null || LOOPBACK_HOSTS.contains(host);
} catch (IllegalArgumentException e) {
return false;
}
}
// Matches the container-side port from the configured endpoint to find the actual host port.
public String endpointFromPorts(String ports, String fallback) {
if (ports == null || ports.isBlank()) return fallback;
/**
* For commands that act on the instance (env, services, snapshot, az setup): where it is, or
* an {@link IllegalStateException} with the next step when that cannot be known, because
* acting on the default instance instead could change the wrong state.
*/
public String resolvedEndpoint(DockerClient docker) {
return resolvedEndpoint(docker, message -> { });
}
/** As above, reporting a missing or stopped container on stderr so env's stdout stays clean. */
public String resolvedEndpoint(DockerClient docker, Consumer<String> warning) {
Lookup found = lookup(docker);
if (found.problem() != null) warning.accept(found.problem());
if (found.endpoint() != null) return found.endpoint();
throw new IllegalStateException("The endpoint for container '" + container + "' is unknown.\n"
+ (found.dockerFailed()
? "Check that Docker is running and retry, or pass --endpoint <url> explicitly."
: "Run '" + product.commandPrefix() + " start" + instanceSelector()
+ "', or pass --endpoint <url> explicitly."));
}
/**
* For commands that only report (doctor, version, wait): where the instance is, or empty when
* that cannot be known. They must still run when the instance is missing, without describing
* the default instance as if it were this one.
*/
public Optional<String> knownEndpoint(DockerClient docker) {
return Optional.ofNullable(lookup(docker).endpoint());
}
private record Lookup(String endpoint, String problem, boolean dockerFailed) { }
private Lookup lookup(DockerClient docker) {
if (configuredEndpointWins()) return new Lookup(endpoint, null, false);
String configured = canUseConfiguredEndpoint() ? endpoint : null;
try {
int containerPort = URI.create(fallback).getPort();
if (containerPort == -1) containerPort = product.defaultPort();
return publishedEndpoint(ports, containerPort).orElse(fallback);
} catch (Exception ignored) {}
return fallback;
var info = docker.inspectContainer(container);
if (info.isEmpty()) return new Lookup(configured, instanceNotRunning(), false);
String problem = "running".equals(info.get().state()) ? null : instanceNotRunning();
String published = endpointFromPorts(info.get().ports(), null);
return new Lookup(published != null ? published : configured, problem, false);
} catch (DockerException | RuntimeException e) {
return new Lookup(configured,
"Could not inspect container '" + container + "'.\nCheck that Docker is running and retry.", true);
}
}
private String instanceNotRunning() {
return "Container '" + container + "' is not running.\nRun '" + product.commandPrefix()
+ " start" + instanceSelector() + "' before using its environment.";
}
// Published mappings always name the product's internal port, even when a profile's
// configured endpoint already contains its custom host port.
public String endpointFromPorts(String ports, String fallback) {
return publishedEndpoint(ports, product.defaultPort()).orElse(fallback);
}
/**