fix(reset): reset the server a profile or FLOCI_ENDPOINT names, not a local container of the default name (#64)

* fix(reset): reset the server a profile or FLOCI_ENDPOINT names, not a local container of the default name

With no container selected, an endpoint that came from a profile or the environment was replaced by the port of a container running locally under the default name, so reset --yes could wipe the wrong instance. Such an endpoint is now used as given, like a typed one.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(reset): reset nothing when the configured endpoint and a running default container are different servers

A profile carries a container name next to its endpoint, and every created profile is seeded with the default one. Using the endpoint outright could then reset a server other than a container the profile also names. When the two disagree neither is picked; the user says which with --endpoint or --container.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(reset): require a configured container to be running and to agree with a configured endpoint

A container that a profile or FLOCI_CONTAINER supplies is selected even when it has the default name, so a missing or stopped one resets nothing instead of falling back to the endpoint. A configured container and endpoint must be the same server. For a remote Docker host the published port is on the daemon's machine, so the endpoint is compared with that host, and a remote container without an endpoint is not reset through localhost.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(reset): treat a Docker daemon on this machine reached over TCP as local

tcp://127.0.0.1:2375 is this machine's daemon, so its published ports are local and a reset goes ahead as with a socket. The README sentence about exiting 1 now covers only the cases where reset refuses.
Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(reset): keep a Docker daemon reached over SSH remote, loopback address or not

ssh://user@127.0.0.1:2222 is a forwarded port to a VM or another machine. Treating it as local would let reset --yes go to localhost on this machine.
Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-11 21:10:58 -05:00
1 parent 97eaa5e820
commit 885e49f81a
3 files changed
+499 -26

No files matched your search

@@ -2,6 +2,7 @@ package io.floci.cli.commands;
import io.floci.cli.GlobalOptions;
import io.floci.cli.ProductProfile;
import io.floci.cli.config.ProfileDefaultValueProvider;
import io.floci.cli.docker.DockerClient;
import io.floci.cli.docker.DockerException;
import io.floci.cli.http.FlociException;
@@ -11,12 +12,16 @@ import io.floci.cli.output.Glyphs;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
import picocli.CommandLine.*;
import picocli.CommandLine.Model.CommandSpec;
import java.io.Console;
import java.net.URI;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.function.UnaryOperator;
@Command(
name = "reset",
@@ -43,14 +48,24 @@ public class ResetCommand implements Callable<Integer> {
/** Test seam: {@code docker} answers the container lookup and {@code prompt} the confirmation. */
public ResetCommand(ProductProfile profile, DockerClient docker, Prompt prompt) {
this(profile, docker, prompt, System::getenv);
}
/** Test seam: {@code env} answers whether {@code <PREFIX>_CONTAINER} selected the container. */
public ResetCommand(ProductProfile profile, DockerClient docker, Prompt prompt, UnaryOperator<String> env) {
this.profile = profile;
this.global = new GlobalOptions(profile);
this.global = new GlobalOptions(profile, env);
this.docker = docker;
this.prompt = prompt;
this.env = env;
}
private final DockerClient docker;
private final Prompt prompt;
private final UnaryOperator<String> env;
@Spec
CommandSpec spec;
/** The person at the terminal, when there is one. */
public interface Prompt {
@@ -82,16 +97,28 @@ public class ResetCommand implements Callable<Integer> {
/**
* Where this reset acts. Reset deletes data, so unlike the read-only commands it never
* guesses: an {@code --endpoint} the user typed is used as given, and a container the user
* chose must be found running. Falling back to the configured endpoint, as
* {@link GlobalOptions#resolvedEndpoint} does, is right only for the default container: there
* the configured endpoint names the same default instance.
* guesses. In order:
* <ol>
* <li>A typed {@code --endpoint} is used as given.</li>
* <li>A typed {@code --container} must be found running, and is reset.</li>
* <li>A container that a profile or {@code <PREFIX>_CONTAINER} supplies must be found
* running too. If the profile or environment also names an endpoint other than the
* default, the two have to be the same server.</li>
* <li>With no container supplied, an endpoint from a profile or the environment is used,
* unless a container of the default name runs here and is a different server.</li>
* <li>With nothing selected at all, the default container is looked up, falling back to
* the default endpoint, which names the same default instance.</li>
* </ol>
* Whenever two things that were configured point at different servers, nothing is reset.
*/
public Target target() {
if (global.typed("--endpoint")) return new Target(global.endpoint, null);
boolean chosen = global.typed("--container") || !profile.defaultContainer().equals(global.container);
boolean typedContainer = global.typed("--container");
boolean chosen = typedContainer || containerSupplied();
boolean endpointNamed = namesAServer(global.endpoint, profile);
String how = "\nRun '" + profile.commandPrefix() + " status" + global.instanceSelector()
+ "' to check it, or pass --endpoint <url> to name the server to reset.";
Optional<DockerClient.ContainerInfo> info;
try {
info = docker.inspectContainer(global.container);
@@ -99,26 +126,128 @@ public class ResetCommand implements Callable<Integer> {
if (chosen) {
return new Target(null, "Could not look up container '" + global.container + "': " + e.getMessage() + how);
}
info = Optional.empty();
}
if (info.isEmpty()) {
return chosen
? new Target(null, "Container '" + global.container + "' not found, so nothing was reset." + how)
: new Target(global.endpoint, null);
}
if (chosen && !"running".equals(info.get().state())) {
return new Target(null, "Container '" + global.container + "' is not running (state: "
+ info.get().state() + "), so nothing was reset." + how);
info = Optional.empty(); // nothing known to contradict what was configured
}
boolean running = info.isPresent() && "running".equals(info.get().state());
// By the emulator's own port inside the container, which never changes; the configured
// endpoint names the host port, and that differs on every instance but the default one.
Optional<String> published = GlobalOptions.publishedEndpoint(info.get().ports(), profile.defaultPort());
if (chosen && published.isEmpty()) {
// No port to find it by: the configured endpoint may well be another instance.
return new Target(null, "Container '" + global.container
+ "' publishes no port for the emulator, so nothing was reset." + how);
Optional<String> published = running
? GlobalOptions.publishedEndpoint(info.get().ports(), profile.defaultPort())
: Optional.empty();
if (chosen) {
if (info.isEmpty()) {
return new Target(null, "Container '" + global.container + "' not found, so nothing was reset." + how);
}
if (!running) {
return new Target(null, "Container '" + global.container + "' is not running (state: "
+ info.get().state() + "), so nothing was reset." + how);
}
if (published.isEmpty()) {
// No port to find it by: the configured endpoint may well be another instance.
return new Target(null, "Container '" + global.container
+ "' publishes no port for the emulator, so nothing was reset." + how);
}
}
return new Target(published.orElse(global.endpoint), null);
if (published.isEmpty()) {
// Nothing runs here under that name: the configured endpoint is all there is.
return new Target(global.endpoint, null);
}
DockerClient.DockerHost daemon = docker.daemonHost();
boolean remote = isRemote(daemon);
if (endpointNamed && !typedContainer) {
// A container and an endpoint were both configured: they have to be one server.
if (sameServer(published.get(), global.endpoint, daemon)) return new Target(global.endpoint, null);
return new Target(null, "The configured endpoint is " + global.endpoint + ", but container '"
+ global.container + "' is running at " + whereItRuns(published.get(), daemon)
+ ". They are different servers, so nothing was reset."
+ "\nPass --endpoint <url> or --container <name> to say which one to reset.");
}
if (remote) {
// The port was published on the daemon's machine. 'localhost' here would be this
// one, and whatever answers on that port here is not the container that was found.
return new Target(null, "Container '" + global.container + "' runs on the remote Docker host "
+ daemon.raw() + ", so its address is not known here and nothing was reset."
+ "\nPass --endpoint <url> with the address the emulator is reached at.");
}
return new Target(published.get(), null);
}
// Whether something other than the default name picked the container: a profile that sets
// one (every created profile does), <PREFIX>_CONTAINER, or simply a different name.
private boolean containerSupplied() {
if (!profile.defaultContainer().equals(global.container)) return true;
if (env.apply(profile.envVar("CONTAINER")) != null) return true;
return global.profile != null
&& ProfileDefaultValueProvider.of(spec).resolved().map(p -> p.container != null).orElse(false);
}
private static String whereItRuns(String published, DockerClient.DockerHost daemon) {
return isRemote(daemon)
? "port " + URI.create(published).getPort() + " of the remote Docker host " + daemon.raw()
: published;
}
/**
* Whether the daemon runs on another machine. Docker reached over TCP usually does, but
* {@code tcp://127.0.0.1:2375} is this machine's daemon on a socket of another kind, and its
* published ports are local like any other's. SSH is always remote: a loopback address there
* ({@code ssh://user@127.0.0.1:2222}) is a forwarded port to a VM or another machine, not a
* daemon on this one.
*/
public static boolean isRemote(DockerClient.DockerHost daemon) {
if (daemon.kind() != DockerClient.Kind.TCP) return false;
try {
URI address = URI.create(daemon.raw());
if ("ssh".equalsIgnoreCase(address.getScheme())) return true;
String host = address.getHost();
return host == null || !thisMachine(host);
} catch (IllegalArgumentException | NullPointerException e) {
return true; // cannot tell: the careful answer
}
}
private static boolean thisMachine(String host) {
String name = host.toLowerCase(Locale.ROOT);
return name.equals("localhost") || name.equals("127.0.0.1") || name.equals("[::1]") || name.equals("::1")
|| name.equals("0.0.0.0") || name.equals("localhost.floci.io");
}
/**
* Whether {@code configured} is the server a container publishes at {@code published}
* ({@code http://localhost:<port>} as docker reports it). The port has to match. For a daemon
* on this machine the configured host has to mean this machine; for a remote one it has to be
* the daemon's host, since that is where the port was published.
*/
public static boolean sameServer(String published, String configured, DockerClient.DockerHost daemon) {
try {
URI there = URI.create(withoutTrailingSlashes(configured));
if (URI.create(published).getPort() != there.getPort()) return false;
String host = there.getHost() == null ? "" : there.getHost();
if (isRemote(daemon)) {
String daemonHost = URI.create(daemon.raw()).getHost();
return daemonHost != null && daemonHost.equalsIgnoreCase(host);
}
return thisMachine(host);
} catch (IllegalArgumentException | NullPointerException e) {
return false;
}
}
/**
* Whether {@code endpoint} is something other than the product's default, that is, whether a
* profile or the environment pointed this command at a particular server. A trailing slash
* does not make it a different one.
*/
public static boolean namesAServer(String endpoint, ProductProfile product) {
return endpoint != null && !withoutTrailingSlashes(endpoint).equals(product.defaultEndpoint());
}
private static String withoutTrailingSlashes(String url) {
String trimmed = url.strip();
while (trimmed.endsWith("/")) trimmed = trimmed.substring(0, trimmed.length() - 1);
return trimmed;
}
@Override