Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve container name parsing #419

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import com.google.common.collect.ImmutableList;
import com.palantir.docker.compose.configuration.ShutdownStrategy;
import com.palantir.docker.compose.logging.DoNothingLogCollector;
import org.hamcrest.MatcherAssert;
Expand All @@ -36,13 +37,13 @@ public void shut_down_multiple_containers_immediately() throws Exception {
.shutdownStrategy(ShutdownStrategy.AGGRESSIVE)
.build();

MatcherAssert.assertThat(docker.dockerCompose().ps(), Matchers.is(TestContainerNames.of()));
MatcherAssert.assertThat(docker.dockerCompose().ps(), Matchers.is(ImmutableList.of()));

docker.before();
assertThat(docker.dockerCompose().ps().size(), is(2));
docker.after();

assertThat(docker.dockerCompose().ps(), is(TestContainerNames.of()));
assertThat(docker.dockerCompose().ps(), is(ImmutableList.of()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.palantir.docker.compose.configuration.ShutdownStrategy;
import com.palantir.docker.compose.logging.DoNothingLogCollector;
Expand All @@ -39,13 +40,13 @@ public class AggressiveShutdownWithNetworkCleanupStrategyIntegrationTest {

@Test
public void shut_down_multiple_containers_immediately() throws Exception {
assertThat(docker.dockerCompose().ps(), is(TestContainerNames.of()));
assertThat(docker.dockerCompose().ps(), is(ImmutableList.of()));

docker.before();
assertThat(docker.dockerCompose().ps().size(), is(2));
docker.after();

assertThat(docker.dockerCompose().ps(), is(TestContainerNames.of()));
assertThat(docker.dockerCompose().ps(), is(ImmutableList.of()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public ShutdownStrategy shutdownStrategy() {
@Value.Default
public com.palantir.docker.compose.execution.DockerCompose dockerCompose() {
com.palantir.docker.compose.execution.DockerCompose
dockerCompose = new DefaultDockerCompose(dockerComposeExecutable(), machine());
dockerCompose = new DefaultDockerCompose(dockerComposeExecutable(), dockerExecutable(), machine());
return new RetryingDockerCompose(retryAttempts(), dockerCompose);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

package com.palantir.docker.compose.connection;

import static java.util.stream.Collectors.joining;

import com.google.common.base.Splitter;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.immutables.value.Value;

@Value.Immutable
public abstract class ContainerName {

// Docker default container names have the format of:
// <project>_<service>_<index> or <project>_<service>_<index>_<slug>
// Regex without escape characters: ^(?<project>[^_]+)_(?<service>[^_]+)_(?<index>[^_]+)(_(?<slug>[^_]+))?$
private static final Pattern DEFAULT_CONTAINER_NAME_PATTERN =
Pattern.compile("^(?<project>[^_]+)_(?<service>[^_]+)_(?<index>[^_]+)(_(?<slug>[^_]+))?$");

public abstract String rawName();

public abstract String semanticName();
Expand All @@ -23,39 +27,20 @@ public String toString() {
return semanticName();
}

public static ContainerName fromPsLine(String psLine) {
List<String> lineComponents = Splitter.on(" ").splitToList(psLine);
String rawName = lineComponents.get(0);

if (probablyCustomName(rawName)) {
return ImmutableContainerName.builder()
.rawName(rawName)
.semanticName(rawName)
.build();
}

String semanticName = withoutDirectory(withoutScaleNumber(rawName));
public static ContainerName fromName(String name) {
return ImmutableContainerName.builder()
.rawName(rawName)
.semanticName(semanticName)
.rawName(name)
.semanticName(parseSemanticName(name))
.build();
}

private static boolean probablyCustomName(String rawName) {
return !(rawName.split("_").length >= 3);
}
private static String parseSemanticName(String name) {
Matcher matcher = DEFAULT_CONTAINER_NAME_PATTERN.matcher(name);

private static String withoutDirectory(String rawName) {
return Arrays.stream(rawName.split("_"))
.skip(1)
.collect(joining("_"));
}
if (matcher.matches()) {
return matcher.group("service");
}

public static String withoutScaleNumber(String rawName) {
String[] components = rawName.split("_");
return Arrays.stream(components)
.limit(components.length - 1)
.collect(joining("_"));
return name;
}

}

This file was deleted.