mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-24 22:37:56 -05:00
fix(start): mount a relative --persist path as a directory and validate --port and --pull
--persist ./data is resolved to an absolute path and created before docker run, so state lands in that directory instead of a Docker named volume; --port outside 1-65535 and an unknown --pull policy are rejected with exit 2 (restart checks them before stopping anything). start creates the persist directory itself (preparePersistDir). restart now does that before it stops anything, so an unwritable directory fails the restart with the instance still running, instead of after the container was already removed.
This commit is contained in:
4 files changed
+102
-1
No files matched your search
@@ -55,6 +55,11 @@ public class RestartCommand implements Callable<Integer> {
|
||||
printer.error(invalid);
|
||||
return 2;
|
||||
}
|
||||
String unwritable = start.preparePersistDir();
|
||||
if (unwritable != null) {
|
||||
printer.error(unwritable);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int stopResult = buildStopCommand().call();
|
||||
if (stopResult != 0) return stopResult;
|
||||
|
||||
@@ -11,6 +11,8 @@ import picocli.CommandLine;
|
||||
import picocli.CommandLine.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.TreeMap;
|
||||
@@ -114,9 +116,45 @@ public class StartCommand implements Callable<Integer> {
|
||||
if (namespace != null && !namespace.isBlank() && !NAMESPACE.matcher(namespace).matches()) {
|
||||
return "Invalid namespace '" + namespace + "'.\nUse letters, digits, '_', '.' and '-', starting with a letter or digit.";
|
||||
}
|
||||
if (port < 1 || port > 65535) {
|
||||
return "--port must be between 1 and 65535, but was " + port + ".\nPick a free port, for example "
|
||||
+ (profile.defaultPort() + 10000) + ".";
|
||||
}
|
||||
if (pull == null || !PULL_POLICIES.contains(pull.toLowerCase())) {
|
||||
// Anything else used to fall through to a pull, so a typo silently meant "always".
|
||||
return "Invalid --pull '" + pull + "'.\nUse always, missing or never.";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static final List<String> PULL_POLICIES = List.of("always", "missing", "never");
|
||||
|
||||
/**
|
||||
* {@code persistDir} as an absolute, normalized path. Docker reads a bind source without a
|
||||
* leading slash as a named volume, so {@code --persist ./data} would otherwise keep the state
|
||||
* in an anonymous Docker volume instead of the directory the user named.
|
||||
*/
|
||||
public String persistPath() {
|
||||
return Path.of(persistDir).toAbsolutePath().normalize().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the state directory, returning why it could not be created, or {@code null}. Done by
|
||||
* the CLI rather than docker: a bind source docker creates is owned by root on Linux, and the
|
||||
* emulator then cannot write to it. Restart calls this before it stops anything, so a
|
||||
* directory that cannot be created leaves the running instance alone.
|
||||
*/
|
||||
public String preparePersistDir() {
|
||||
if (persistDir == null || persistDir.isBlank()) return null;
|
||||
try {
|
||||
Files.createDirectories(Path.of(persistPath()));
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
return "Could not create the persist directory " + persistPath() + ": " + e.getMessage()
|
||||
+ "\nPass a --persist directory you can write to.";
|
||||
}
|
||||
}
|
||||
|
||||
public String resourceNamespace() {
|
||||
return resourceNamespaceFor(global.container);
|
||||
}
|
||||
@@ -139,7 +177,7 @@ public class StartCommand implements Callable<Integer> {
|
||||
args.addAll(List.of("-p", port + ":" + profile.defaultPort()));
|
||||
args.addAll(socketArgs);
|
||||
if (persistDir != null && !persistDir.isBlank()) {
|
||||
args.addAll(List.of("-v", persistDir + ":/app/data"));
|
||||
args.addAll(List.of("-v", persistPath() + ":/app/data"));
|
||||
// The server defaults to in-memory storage; enable persistent mode so
|
||||
// state is actually written to the mounted directory and survives restarts.
|
||||
args.addAll(List.of("-e", profile.envVar("STORAGE_MODE") + "=persistent"));
|
||||
@@ -200,6 +238,12 @@ public class StartCommand implements Callable<Integer> {
|
||||
return 2;
|
||||
}
|
||||
|
||||
String unwritable = preparePersistDir();
|
||||
if (unwritable != null) {
|
||||
printer.error(unwritable);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if container already exists. This is also the first docker call, so a missing
|
||||
// binary shows up here, without a separate `docker --version` probe on every start.
|
||||
try {
|
||||
|
||||
@@ -5,6 +5,8 @@ import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.RestartCommand;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.DisabledOnOs;
|
||||
import org.junit.jupiter.api.condition.OS;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.ParseResult;
|
||||
@@ -134,6 +136,35 @@ class RestartCommandTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A persist directory that cannot be created fails the restart before the stop, so the running
|
||||
* instance is left alone instead of being removed and never started again.
|
||||
*/
|
||||
@Test
|
||||
@DisabledOnOs(OS.WINDOWS)
|
||||
void anUnwritablePersistDirFailsBeforeTheStop() throws Exception {
|
||||
Path readOnly = Files.createDirectories(tempDir.resolve("read-only"));
|
||||
assertTrue(readOnly.toFile().setWritable(false));
|
||||
writeProfile("ro", "container: floci-ro-none\npersistDir: " + readOnly.resolve("state") + "\n");
|
||||
RestartCommand restart = parse(ProductProfile.AWS, "--profile", "ro");
|
||||
|
||||
PrintStream out = System.out;
|
||||
PrintStream err = System.err;
|
||||
ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(outBuf));
|
||||
System.setErr(new PrintStream(errBuf));
|
||||
try {
|
||||
assertEquals(1, restart.call());
|
||||
} finally {
|
||||
System.setOut(out);
|
||||
System.setErr(err);
|
||||
readOnly.toFile().setWritable(true);
|
||||
}
|
||||
assertTrue(errBuf.toString().contains("Could not create the persist directory"), errBuf.toString());
|
||||
assertFalse(outBuf.toString().contains("Stopping"), "nothing was stopped: " + outBuf);
|
||||
}
|
||||
|
||||
/**
|
||||
* One snapshot: restart applies the profile the parse read, not a second read of the file, so
|
||||
* an edit or delete in between cannot mix two versions into one restart.
|
||||
|
||||
@@ -191,6 +191,27 @@ class StartCommandArgsTest {
|
||||
.dockerRunArgs(SOCKET).contains("FLOCI_BASE_URL=http://floci.internal:14566"));
|
||||
}
|
||||
|
||||
/** BL-034: Docker reads "./data:/app/data" as a named volume, so the path goes in absolute. */
|
||||
@Test
|
||||
void aRelativePersistDirIsMountedAsAnAbsoluteBindPath() {
|
||||
String expected = Path.of("data").toAbsolutePath().normalize() + ":/app/data";
|
||||
|
||||
List<String> args = parse("start", "--persist", "./data").dockerRunArgs(SOCKET);
|
||||
|
||||
assertTrue(args.contains(expected), args.toString());
|
||||
assertFalse(args.contains("./data:/app/data"), args.toString());
|
||||
}
|
||||
|
||||
/** BL-035: an out-of-range port or an unknown pull policy is refused before docker runs. */
|
||||
@Test
|
||||
void portAndPullPolicyAreValidated() {
|
||||
assertNull(parse("start").validationError());
|
||||
assertNull(parse("start", "--pull", "ALWAYS").validationError());
|
||||
assertTrue(parse("start", "--port", "0").validationError().contains("--port must be between 1 and 65535"));
|
||||
assertTrue(parse("start", "--port", "70000").validationError().contains("--port must be between 1 and 65535"));
|
||||
assertTrue(parse("start", "--pull", "alwayz").validationError().contains("Invalid --pull 'alwayz'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readinessPollKeepsTheHostFromTheEndpointWhenSwappingThePort() {
|
||||
assertEquals("http://floci.internal:4599", StartCommand.withPort("http://floci.internal:4566", 4599));
|
||||
|
||||
Reference in new issue
Block a user