mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-23 14:10:50 -05:00
feat(start): check the host port before starting and explain a missing Docker socket
floci start now stops with 'Port N is already in use' and how to pick another port, instead of failing inside docker run, and when Docker cannot be reached because its local socket is missing it says how to bring Docker up. The port probe is skipped for a remote daemon (DOCKER_HOST=tcp://...).
This commit is contained in:
3 files changed
+93
-3
No files matched your search
@@ -5,6 +5,10 @@ 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.doctor.CheckResult;
|
||||
import io.floci.cli.doctor.CheckStatus;
|
||||
import io.floci.cli.doctor.checks.DockerSocketCheck;
|
||||
import io.floci.cli.doctor.checks.PortAvailableCheck;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.Printer;
|
||||
import picocli.CommandLine;
|
||||
@@ -29,11 +33,19 @@ public class StartCommand implements Callable<Integer> {
|
||||
@Mixin
|
||||
protected GlobalOptions global;
|
||||
|
||||
private final DockerClient docker;
|
||||
|
||||
public StartCommand() {
|
||||
this(ProductProfile.AWS);
|
||||
}
|
||||
|
||||
protected StartCommand(ProductProfile profile) {
|
||||
this(profile, new DockerClient());
|
||||
}
|
||||
|
||||
/** Test seam: {@code docker} answers every docker call this start makes. */
|
||||
public StartCommand(ProductProfile profile, DockerClient docker) {
|
||||
this.docker = docker;
|
||||
this.profile = profile;
|
||||
this.global = new GlobalOptions(profile);
|
||||
this.port = profile.defaultPort();
|
||||
@@ -192,7 +204,6 @@ public class StartCommand implements Callable<Integer> {
|
||||
@Override
|
||||
public Integer call() {
|
||||
Printer printer = global.printer();
|
||||
DockerClient docker = new DockerClient();
|
||||
|
||||
String invalid = validationError();
|
||||
if (invalid != null) {
|
||||
@@ -219,7 +230,16 @@ public class StartCommand implements Callable<Integer> {
|
||||
printer.error("docker binary not found in PATH.\nInstall Docker Desktop from https://docs.docker.com/get-docker/");
|
||||
return 1;
|
||||
}
|
||||
printer.error("Failed to inspect container: " + e.getMessage());
|
||||
printer.error("Failed to inspect container: " + e.getMessage() + socketGuidance());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Checked here rather than left to 'docker run', whose bind error names neither the port
|
||||
// flag nor how to find what holds the port. A remote daemon publishes on another machine,
|
||||
// so a local probe would say nothing about it.
|
||||
if (DockerClient.dockerHost().kind() != DockerClient.Kind.TCP && !PortAvailableCheck.isPortFree(port)) {
|
||||
printer.error("Port " + port + " is already in use by another process.\n"
|
||||
+ "Run 'lsof -i :" + port + "' to find it, or pass --port <other> to use a different port.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -260,4 +280,13 @@ public class StartCommand implements Callable<Integer> {
|
||||
wait.timeout = "30s";
|
||||
return wait.call();
|
||||
}
|
||||
|
||||
// When the daemon is unreachable because its local socket is missing, say so: docker's own
|
||||
// error ("Cannot connect to the Docker daemon") does not say what to do about it.
|
||||
private static String socketGuidance() {
|
||||
CheckResult socket = new DockerSocketCheck().run(null, null);
|
||||
return socket.status() == CheckStatus.fail
|
||||
? "\n" + socket.message() + ".\n" + socket.fix() + ", then re-run the command."
|
||||
: "";
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ public class PortAvailableCheck implements Check {
|
||||
"Run 'lsof -i :" + port + "' to identify the process, or start Floci with --port <other>");
|
||||
}
|
||||
|
||||
private static boolean isPortFree(int port) {
|
||||
public static boolean isPortFree(int port) {
|
||||
try (ServerSocket s = new ServerSocket(port)) {
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.StartCommand;
|
||||
import io.floci.cli.docker.DockerClient;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
/** BL-058: start reports a busy host port itself, before pulling or running anything. */
|
||||
class StartPreflightTest {
|
||||
|
||||
@Test
|
||||
void aBusyPortStopsTheStartWithAHint() throws Exception {
|
||||
assumeTrue(DockerClient.dockerHost().kind() != DockerClient.Kind.TCP, "a remote daemon skips the local probe");
|
||||
AtomicBoolean pulled = new AtomicBoolean();
|
||||
DockerClient docker = new DockerClient() {
|
||||
@Override
|
||||
public Optional<ContainerInfo> inspectContainer(String name) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pull(String image, String policy) {
|
||||
pulled.set(true);
|
||||
}
|
||||
};
|
||||
|
||||
try (ServerSocket taken = new ServerSocket()) {
|
||||
taken.bind(new InetSocketAddress(0));
|
||||
int port = taken.getLocalPort();
|
||||
PrintStream out = System.out;
|
||||
PrintStream err = System.err;
|
||||
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(new ByteArrayOutputStream()));
|
||||
System.setErr(new PrintStream(errBuf));
|
||||
int exit;
|
||||
try {
|
||||
exit = new CommandLine(new StartCommand(ProductProfile.GCP, docker))
|
||||
.execute("--port", String.valueOf(port), "--container", "floci-gcp-preflight");
|
||||
} finally {
|
||||
System.setOut(out);
|
||||
System.setErr(err);
|
||||
}
|
||||
|
||||
assertEquals(1, exit);
|
||||
assertTrue(errBuf.toString().contains("Port " + port + " is already in use"), errBuf.toString());
|
||||
assertTrue(errBuf.toString().contains("--port <other>"), errBuf.toString());
|
||||
assertFalse(pulled.get(), "nothing is pulled for a start that cannot bind");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user