mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(restart): show how to recover after startup fails (#73)
When restart has removed the old container and the new one does not come up, it now prints a start command with the same port, image, persist dir, services and namespace. When the new container is running but did not become ready, it says to check logs and run wait instead, because start refuses a running container. The commands name their tree, because a bare floci follows the configured default product. They keep a typed --endpoint, and --persist prints the directory a local daemon would mount, so a retry from another directory finds the same state. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
763bb2d854
commit
a319e2d242
9 files changed
+262
-7
No files matched your search
@@ -125,7 +125,7 @@ public class GlobalOptions {
|
||||
}
|
||||
|
||||
// POSIX single quotes: the hints are shell command lines like the CLI's other 'Run ...' hints.
|
||||
private static String shellWord(String value) {
|
||||
public static String shellWord(String value) {
|
||||
if (PLAIN_WORD.matcher(value).matches()) return value;
|
||||
return "'" + value.replace("'", "'\\''") + "'";
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -96,7 +96,14 @@ public class RestartCommand implements Callable<Integer> {
|
||||
int stopResult = buildStopCommand().call();
|
||||
if (stopResult != 0) return stopResult;
|
||||
|
||||
return start.call();
|
||||
// Capture before start mutates its endpoint while waiting. This command stands alone,
|
||||
// even if the profile is edited or deleted after the failed restart.
|
||||
String recovery = start.recoveryCommand();
|
||||
int result = start.call();
|
||||
if (result != 0) {
|
||||
printer.error(recoveryMessage(recovery));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,7 +132,7 @@ public class RestartCommand implements Callable<Integer> {
|
||||
public StartCommand buildStartCommand() throws DockerException {
|
||||
// The outer parse's provider: its memoized profile is the snapshot the globals below were
|
||||
// resolved from, so the start settings cannot come from a newer version of the file.
|
||||
StartCommand start = StartCommand.resolvedFor(profile, ProfileDefaultValueProvider.of(spec), global.profile);
|
||||
StartCommand start = StartCommand.resolvedFor(profile, ProfileDefaultValueProvider.of(spec), global.profile, docker);
|
||||
|
||||
// The real invocation's globals win: they already carry the profile plus any flag the
|
||||
// user passed, resolved once by the outer parse.
|
||||
@@ -153,6 +160,31 @@ public class RestartCommand implements Callable<Integer> {
|
||||
});
|
||||
}
|
||||
|
||||
// A new container that is up but has not answered yet cannot be started again: start refuses a
|
||||
// running container. So the retry command is only for a container that did not come up.
|
||||
private String recoveryMessage(String recovery) {
|
||||
boolean running;
|
||||
try {
|
||||
running = docker.inspectContainer(global.container)
|
||||
.filter(info -> "running".equals(info.state())).isPresent();
|
||||
} catch (DockerException e) {
|
||||
running = false;
|
||||
}
|
||||
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 '" + 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;
|
||||
}
|
||||
|
||||
private boolean containerExists() {
|
||||
try {
|
||||
return docker.inspectContainer(global.container).isPresent();
|
||||
|
||||
@@ -99,7 +99,14 @@ public class StartCommand implements Callable<Integer> {
|
||||
public static StartCommand resolvedFor(ProductProfile product,
|
||||
ProfileDefaultValueProvider provider,
|
||||
String profileName) {
|
||||
StartCommand start = new StartCommand(product);
|
||||
return resolvedFor(product, provider, profileName, new DockerClient());
|
||||
}
|
||||
|
||||
/** Reuse the caller's Docker client when a restart starts the resolved instance. */
|
||||
public static StartCommand resolvedFor(ProductProfile product,
|
||||
ProfileDefaultValueProvider provider,
|
||||
String profileName, DockerClient docker) {
|
||||
StartCommand start = new StartCommand(product, docker);
|
||||
if (profileName != null) {
|
||||
new CommandLine(start)
|
||||
.setCaseInsensitiveEnumValuesAllowed(true)
|
||||
@@ -109,6 +116,25 @@ public class StartCommand implements Callable<Integer> {
|
||||
return start;
|
||||
}
|
||||
|
||||
/** A self-contained POSIX command that recovers the resolved settings after a failed restart. */
|
||||
public String recoveryCommand() {
|
||||
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));
|
||||
// --port already moves the default endpoint, so only a different one is worth naming.
|
||||
if (!profile.defaultEndpoint().equals(global.endpoint)) {
|
||||
command.append(" --endpoint ").append(GlobalOptions.shellWord(global.endpoint));
|
||||
}
|
||||
// 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();
|
||||
}
|
||||
|
||||
/** What a profile-resolved instance would start with. Read by {@code config show}. */
|
||||
public String image() { return image; }
|
||||
|
||||
@@ -433,9 +459,16 @@ public class StartCommand implements Callable<Integer> {
|
||||
return exit;
|
||||
}
|
||||
|
||||
private String readinessTimeout = "30s";
|
||||
|
||||
/** How long start waits for the emulator to answer. A seam: tests do not wait half a minute. */
|
||||
protected String readinessTimeout() {
|
||||
return "30s";
|
||||
return readinessTimeout;
|
||||
}
|
||||
|
||||
/** Test seam for a start built by {@code restart}, which cannot be subclassed from outside. */
|
||||
public void readinessTimeout(String timeout) {
|
||||
this.readinessTimeout = timeout;
|
||||
}
|
||||
|
||||
// What -o json/yaml reports for a start: 'ready' is left out when --detach did not wait for it.
|
||||
|
||||
Reference in new issue
Block a user