mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(reset): treat a Docker daemon on this machine reached over TCP as local
tcp://127.0.0.1:2375 is this machine's daemon, so its published ports are local and a reset goes ahead as with a socket. The README sentence about exiting 1 now covers only the cases where reset refuses. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
3 files changed
+61
-12
No files matched your search
@@ -733,10 +733,11 @@ Reset never guesses which server to wipe:
|
||||
container that happens to run locally under the default name never takes its place.
|
||||
- When both a container and an endpoint are configured, they have to be the same server. If they
|
||||
are not, nothing is reset and you are asked to pass `--endpoint` or `--container`.
|
||||
- A container on a remote Docker host (`DOCKER_HOST=tcp://...` or `ssh://...`) is only reset
|
||||
through an endpoint, since its published port is not on this machine.
|
||||
- A container on a remote Docker host (`DOCKER_HOST=tcp://other-host:...` or `ssh://...`) is only
|
||||
reset through an endpoint, since its published port is not on this machine. A daemon on this
|
||||
machine reached over TCP (`tcp://127.0.0.1:2375`) is local.
|
||||
|
||||
In each of those cases the command exits 1 without sending anything.
|
||||
When reset refuses to choose a server, the command exits 1 without sending anything.
|
||||
|
||||
### `floci config`
|
||||
|
||||
|
||||
@@ -155,7 +155,7 @@ public class ResetCommand implements Callable<Integer> {
|
||||
}
|
||||
|
||||
DockerClient.DockerHost daemon = docker.daemonHost();
|
||||
boolean remote = daemon.kind() == DockerClient.Kind.TCP;
|
||||
boolean remote = isRemote(daemon);
|
||||
if (endpointNamed && !typedContainer) {
|
||||
// A container and an endpoint were both configured: they have to be one server.
|
||||
if (sameServer(published.get(), global.endpoint, daemon)) return new Target(global.endpoint, null);
|
||||
@@ -184,28 +184,48 @@ public class ResetCommand implements Callable<Integer> {
|
||||
}
|
||||
|
||||
private static String whereItRuns(String published, DockerClient.DockerHost daemon) {
|
||||
return daemon.kind() == DockerClient.Kind.TCP
|
||||
return isRemote(daemon)
|
||||
? "port " + URI.create(published).getPort() + " of the remote Docker host " + daemon.raw()
|
||||
: published;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the daemon runs on another machine. Docker reached over TCP or SSH usually does,
|
||||
* but {@code tcp://127.0.0.1:2375} is this machine's daemon on a socket of another kind, and
|
||||
* its published ports are local like any other's.
|
||||
*/
|
||||
public static boolean isRemote(DockerClient.DockerHost daemon) {
|
||||
if (daemon.kind() != DockerClient.Kind.TCP) return false;
|
||||
try {
|
||||
String host = URI.create(daemon.raw()).getHost();
|
||||
return host == null || !thisMachine(host);
|
||||
} catch (IllegalArgumentException | NullPointerException e) {
|
||||
return true; // cannot tell: the careful answer
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean thisMachine(String host) {
|
||||
String name = host.toLowerCase(Locale.ROOT);
|
||||
return name.equals("localhost") || name.equals("127.0.0.1") || name.equals("[::1]") || name.equals("::1")
|
||||
|| name.equals("0.0.0.0") || name.equals("localhost.floci.io");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code configured} is the server a container publishes at {@code published}
|
||||
* ({@code http://localhost:<port>} as docker reports it). The port has to match. For a local
|
||||
* daemon the configured host has to mean this machine; for a remote one it has to be the
|
||||
* daemon's host, since that is where the port was published.
|
||||
* ({@code http://localhost:<port>} as docker reports it). The port has to match. For a daemon
|
||||
* on this machine the configured host has to mean this machine; for a remote one it has to be
|
||||
* the daemon's host, since that is where the port was published.
|
||||
*/
|
||||
public static boolean sameServer(String published, String configured, DockerClient.DockerHost daemon) {
|
||||
try {
|
||||
URI there = URI.create(withoutTrailingSlashes(configured));
|
||||
if (URI.create(published).getPort() != there.getPort()) return false;
|
||||
String host = there.getHost() == null ? "" : there.getHost().toLowerCase(Locale.ROOT);
|
||||
if (daemon.kind() == DockerClient.Kind.TCP) {
|
||||
String host = there.getHost() == null ? "" : there.getHost();
|
||||
if (isRemote(daemon)) {
|
||||
String daemonHost = URI.create(daemon.raw()).getHost();
|
||||
return daemonHost != null && daemonHost.equalsIgnoreCase(host);
|
||||
}
|
||||
return host.equals("localhost") || host.equals("127.0.0.1") || host.equals("[::1]")
|
||||
|| host.equals("::1") || host.equals("0.0.0.0") || host.equals("localhost.floci.io");
|
||||
return thisMachine(host);
|
||||
} catch (IllegalArgumentException | NullPointerException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -270,6 +270,34 @@ class ResetCommandTest {
|
||||
assertEquals(same, ResetCommand.sameServer("http://localhost:14566", endpoint, remote));
|
||||
}
|
||||
|
||||
/** Docker on this machine reached over TCP is local: its published ports are this machine's. */
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"tcp://127.0.0.1:2375, false",
|
||||
"tcp://localhost:2375, false",
|
||||
"tcp://[::1]:2376, false",
|
||||
"unix:///var/run/docker.sock, false",
|
||||
"tcp://remote-host:2375, true",
|
||||
"ssh://deploy@remote-host, true",
|
||||
"tcp://192.168.1.20:2375, true"})
|
||||
void onlyADaemonOnAnotherMachineIsRemote(String dockerHost, boolean remote) {
|
||||
assertEquals(remote, ResetCommand.isRemote(DockerClient.parseDockerHost(dockerHost, null, "Linux")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLocalTcpDaemonResetsItsContainerLikeASocketOne() throws Exception {
|
||||
DockerHost localTcp = DockerClient.parseDockerHost("tcp://127.0.0.1:2375", null, "Linux");
|
||||
|
||||
ResetCommand.Target plain = targetOf(dockerWith("floci", "running", "4566->4566/tcp", localTcp));
|
||||
ResetCommand.Target withProfile = targetWithProfile(dockerWith("floci", "running", "14566->4566/tcp", localTcp),
|
||||
"container: floci\nport: 14566\nendpoint: http://localhost:14566\n");
|
||||
|
||||
assertEquals("http://localhost:4566", plain.endpoint());
|
||||
assertNull(withProfile.error(), withProfile.error());
|
||||
assertEquals("http://localhost:14566", withProfile.endpoint());
|
||||
assertTrue(ResetCommand.sameServer("http://localhost:14566", "http://localhost:14566", localTcp));
|
||||
}
|
||||
|
||||
/** A profile for an instance on a remote Docker host: its endpoint and its container are one server. */
|
||||
@Test
|
||||
void aRemoteInstanceWhoseEndpointMatchesIsReset() throws Exception {
|
||||
|
||||
Reference in new issue
Block a user