mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(restart): make the recovery commands work from any shell and directory
They name floci aws, because a bare floci follows the configured default product. The suggested wait keeps a typed --endpoint with the port start bound, and --persist prints the directory a local daemon would mount, so a retry from another directory finds the same state. Addresses the review of #73. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
5 files changed
+79
-5
No files matched your search
@@ -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
|
||||
|
||||
@@ -170,11 +170,16 @@ public class RestartCommand implements Callable<Integer> {
|
||||
} 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;
|
||||
|
||||
@@ -118,7 +118,7 @@ public class StartCommand implements Callable<Integer> {
|
||||
|
||||
/** 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<Integer> {
|
||||
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();
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String> 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;
|
||||
|
||||
Reference in new issue
Block a user