diff --git a/src/main/java/io/floci/cli/ProductProfile.java b/src/main/java/io/floci/cli/ProductProfile.java index 9071412..bb18ceb 100644 --- a/src/main/java/io/floci/cli/ProductProfile.java +++ b/src/main/java/io/floci/cli/ProductProfile.java @@ -36,6 +36,11 @@ public record ProductProfile( return "aws".equals(name) ? "floci" : "floci " + name; } + /** Always names the tree: a bare {@code floci} goes to whatever default product is configured. */ + public String explicitCommandPrefix() { + return "floci " + name; + } + /** * The Docker resource namespace an instance in {@code container} gets when none is set: none * for the default container, so its child containers keep their names; otherwise the container diff --git a/src/main/java/io/floci/cli/commands/RestartCommand.java b/src/main/java/io/floci/cli/commands/RestartCommand.java index 79aae27..9d7550b 100644 --- a/src/main/java/io/floci/cli/commands/RestartCommand.java +++ b/src/main/java/io/floci/cli/commands/RestartCommand.java @@ -170,11 +170,16 @@ public class RestartCommand implements Callable { } catch (DockerException e) { running = false; } - String selector = global.instanceSelector(); if (running) { + String prefix = profile.explicitCommandPrefix(); + String selector = global.instanceSelector(); + // By now this is the endpoint start waited on, with the port it bound. + String wait = global.typed("--endpoint") && !selector.contains(" --endpoint ") + ? selector + " --endpoint " + GlobalOptions.shellWord(global.endpoint) + : selector; return "The new container is running but did not become ready.\n" - + "Check it with '" + profile.commandPrefix() + " logs" + selector + "', then run '" - + profile.commandPrefix() + " wait" + selector + "' to wait again."; + + "Check it with '" + prefix + " logs" + selector + "', then run '" + + prefix + " wait" + wait + "' to wait again."; } return "Restart did not finish after the old container was removed.\n" + "To retry with the same settings, run:\n" + recovery; diff --git a/src/main/java/io/floci/cli/commands/StartCommand.java b/src/main/java/io/floci/cli/commands/StartCommand.java index 138d6e2..d529b89 100644 --- a/src/main/java/io/floci/cli/commands/StartCommand.java +++ b/src/main/java/io/floci/cli/commands/StartCommand.java @@ -118,7 +118,7 @@ public class StartCommand implements Callable { /** A self-contained POSIX command that recovers the resolved settings after a failed restart. */ public String recoveryCommand() { - StringBuilder command = new StringBuilder(profile.commandPrefix()).append(" start"); + StringBuilder command = new StringBuilder(profile.explicitCommandPrefix()).append(" start"); command.append(" --container ").append(GlobalOptions.shellWord(global.container)); command.append(" --port ").append(port); command.append(" --image ").append(GlobalOptions.shellWord(image)); @@ -126,7 +126,10 @@ public class StartCommand implements Callable { if (!profile.defaultEndpoint().equals(global.endpoint)) { command.append(" --endpoint ").append(GlobalOptions.shellWord(global.endpoint)); } - if (persistDir != null) command.append(" --persist ").append(GlobalOptions.shellWord(persistDir)); + // Resolved, so the retry mounts the same directory from wherever it is run. + if (persistDir != null && !persistDir.isBlank()) { + command.append(" --persist ").append(GlobalOptions.shellWord(bindSource(docker.daemonHost()))); + } if (services != null) command.append(" --services ").append(GlobalOptions.shellWord(services)); if (namespace != null) command.append(" --namespace ").append(GlobalOptions.shellWord(namespace)); return command.toString(); diff --git a/src/test/java/io/floci/cli/unit/ProductProfileTest.java b/src/test/java/io/floci/cli/unit/ProductProfileTest.java index 55758d1..04d3fa0 100644 --- a/src/test/java/io/floci/cli/unit/ProductProfileTest.java +++ b/src/test/java/io/floci/cli/unit/ProductProfileTest.java @@ -22,6 +22,7 @@ class ProductProfileTest { assertEquals("FLOCI_ENDPOINT", ProductProfile.AWS.envVar("ENDPOINT")); assertEquals("/_floci", ProductProfile.AWS.controlPrefix()); assertEquals("floci", ProductProfile.AWS.commandPrefix()); + assertEquals("floci aws", ProductProfile.AWS.explicitCommandPrefix()); assertEquals(Map.of(), ProductProfile.AWS.startEnv()); } @@ -35,6 +36,7 @@ class ProductProfileTest { assertEquals("FLOCI_GCP_CONTAINER", ProductProfile.GCP.envVar("CONTAINER")); assertEquals("/_floci-gcp", ProductProfile.GCP.controlPrefix()); assertEquals("floci gcp", ProductProfile.GCP.commandPrefix()); + assertEquals("floci gcp", ProductProfile.GCP.explicitCommandPrefix()); assertEquals(Map.of(), ProductProfile.GCP.startEnv()); } @@ -49,6 +51,7 @@ class ProductProfileTest { // The floci-az server exposes /_floci/* (same as AWS), NOT /_floci-az. assertEquals("/_floci", ProductProfile.AZ.controlPrefix()); assertEquals("floci az", ProductProfile.AZ.commandPrefix()); + assertEquals("floci az", ProductProfile.AZ.explicitCommandPrefix()); assertEquals(Map.of("TLS_ENABLED", "true"), ProductProfile.AZ.startEnv()); } @@ -62,6 +65,7 @@ class ProductProfileTest { assertEquals("FLOCI_OCI_ENDPOINT", ProductProfile.OCI.envVar("ENDPOINT")); assertEquals("/_floci-oci", ProductProfile.OCI.controlPrefix()); assertEquals("floci oci", ProductProfile.OCI.commandPrefix()); + assertEquals("floci oci", ProductProfile.OCI.explicitCommandPrefix()); assertEquals(Map.of(), ProductProfile.OCI.startEnv()); } diff --git a/src/test/java/io/floci/cli/unit/RestartRecoveryTest.java b/src/test/java/io/floci/cli/unit/RestartRecoveryTest.java index c41317e..99506e3 100644 --- a/src/test/java/io/floci/cli/unit/RestartRecoveryTest.java +++ b/src/test/java/io/floci/cli/unit/RestartRecoveryTest.java @@ -102,6 +102,63 @@ class RestartRecoveryTest { assertFalse(text.contains("floci gcp start --container"), text); } + private String restartStderr(ProductProfile product, FailedStart docker, String... args) throws Exception { + var restart = new RestartCommand(product, docker) { + @Override public StartCommand buildStartCommand() throws DockerException { + StartCommand start = super.buildStartCommand(); + start.readinessTimeout("1ms"); + return start; + } + }; + CommandLine command = new CommandLine(restart) + .setDefaultValueProvider(new ProfileDefaultValueProvider(new ProfileStore(profiles))); + PrintStream out = System.out, err = System.err; + ByteArrayOutputStream stderr = new ByteArrayOutputStream(); + try { + System.setErr(new PrintStream(stderr)); System.setOut(new PrintStream(new ByteArrayOutputStream())); + assertEquals(1, command.execute(args)); + } finally { System.setErr(err); System.setOut(out); } + return stderr.toString(); + } + + private static FailedStart comesUpRunning() { + return new FailedStart() { + @Override public String startContainer(List args) { + actions.add("run"); state = "running"; return "new-id"; + } + }; + } + + @Test + void awsRecoveryNamesItsTreeBecauseABareCommandFollowsTheDefaultProduct() throws Exception { + String refused = restartStderr(ProductProfile.AWS, new FailedStart(), "--container", "other"); + assertTrue(refused.contains("floci aws start --container other"), refused); + String unready = restartStderr(ProductProfile.AWS, comesUpRunning(), "--container", "other"); + assertTrue(unready.contains("floci aws logs --container other"), unready); + assertTrue(unready.contains("floci aws wait --container other"), unready); + } + + @Test + void theSuggestedWaitChecksTheEndpointStartWaitedOn() throws Exception { + String text = restartStderr(ProductProfile.GCP, comesUpRunning(), + "--container", "floci-gcp-team", "--endpoint", "http://build-host:4588"); + assertTrue(text.contains("floci gcp wait --container floci-gcp-team --endpoint http://build-host:14588"), text); + } + + @Test + void theRecoveryCommandPrintsTheDirectoryALocalDaemonWouldMount() { + FailedStart local = new FailedStart() { + @Override public DockerHost daemonHost() { return new DockerHost(Kind.UNIX, null, "unix:///var/run/docker.sock"); } + }; + StartCommand onLocal = new StartCommand(ProductProfile.GCP, local); + new CommandLine(onLocal).parseArgs("--persist", "rel/data"); + assertTrue(onLocal.recoveryCommand().contains("--persist " + Path.of("rel/data").toAbsolutePath()), onLocal.recoveryCommand()); + + StartCommand onRemote = new StartCommand(ProductProfile.GCP, new FailedStart()); + new CommandLine(onRemote).parseArgs("--persist", "/srv/data"); + assertTrue(onRemote.recoveryCommand().contains("--persist /srv/data"), onRemote.recoveryCommand()); + } + @Test void failedStopDoesNotClaimTheOldContainerWasRemoved() { FailedStart docker = new FailedStart(); docker.failStop = true;