Skip to content

Commit

Permalink
Enable projects to modify the image name used by the DockerProxyExten…
Browse files Browse the repository at this point in the history
…sion (#552)

Projects using DockerProxyExtension can now override the docker image name used by the proxy.
  • Loading branch information
mswintermeyer authored Dec 15, 2022
1 parent f1c17cf commit d16aea4
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 10 deletions.
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-552.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: feature
feature:
description: Projects using DockerProxyExtension can now override the docker image
name used by the proxy.
links:
- https://github.com/palantir/docker-proxy-rule/pull/552
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.palantir.docker.compose.configuration.ProjectName;
import com.palantir.docker.compose.execution.DockerExecutable;
import java.io.IOException;
import java.util.Optional;
import java.util.function.Function;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
Expand Down Expand Up @@ -55,6 +56,20 @@ public static DockerProxyExtension fromProjectName(ProjectName projectName, Clas
docker -> new ProjectBasedDockerContainerInfo(docker, projectName), classToLogFor);
}

/**
* Creates a {@link DockerProxyExtension} using a {@link ProjectBasedDockerContainerInfo}.
*
* @param projectName The docker-compose-rule ProjectName to use to find the containers
* @param imageNameOverride The docker image name to use instead of the default
* @param classToLogFor The class using {@link DockerProxyExtension}
*/
public static DockerProxyExtension fromProjectName(
ProjectName projectName, Class<?> classToLogFor, String imageNameOverride) {
return new DockerProxyExtension(
docker -> new ProjectBasedDockerContainerInfo(docker, projectName, Optional.of(imageNameOverride)),
classToLogFor);
}

/**
* Creates a {@link DockerProxyExtension} using a {@link NetworkBasedDockerContainerInfo}.
*
Expand All @@ -66,6 +81,20 @@ public static DockerProxyExtension fromNetworkName(String networkName, Class<?>
docker -> new NetworkBasedDockerContainerInfo(docker, networkName), classToLogFor);
}

/**
* Creates a {@link DockerProxyExtension} using a {@link NetworkBasedDockerContainerInfo}.
*
* @param networkName The network name to use to find the containers
* @param imageNameOverride The docker image name to use instead of the default
* @param classToLogFor The class using {@link DockerProxyExtension}
*/
public static DockerProxyExtension fromNetworkName(
String networkName, Class<?> classToLogFor, String imageNameOverride) {
return new DockerProxyExtension(
docker -> new NetworkBasedDockerContainerInfo(docker, networkName, Optional.of(imageNameOverride)),
classToLogFor);
}

@Override
public void beforeAll(ExtensionContext _context) throws IOException, InterruptedException {
before();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public Optional<String> getHostForIp(String ip) {
public String getNetworkName() {
return delegate.getNetworkName();
}

@Override
public Optional<String> getImageNameOverride() {
return delegate.getImageNameOverride();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ public interface DockerContainerInfo {
* @return The network for the proxy to connect to
*/
String getNetworkName();

/**
* Returns an override for the image name to use for the docker container,
* otherwise `vimagick/dante:latest` will get used.
*/
Optional<String> getImageNameOverride();
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ abstract class DockerProxyManager<SelfT extends DockerComposeManager.BuilderExte
.build());
String logDirectory = DockerProxyManager.class.getSimpleName() + "-" + classToLogFor.getSimpleName();
this.dockerContainerInfo = new CachingDockerContainerInfo(builtDockerContainerInfo);
this.dockerComposeRule = builderSupplier.customize(
builder -> builder.file(getDockerComposeFile(this.dockerContainerInfo.getNetworkName())
.getPath())
.waitingForService("proxy", Container::areAllPortsOpen)
.saveLogsTo(LogDirectory.circleAwareLogDirectory(logDirectory)));
this.dockerComposeRule = builderSupplier.customize(builder -> builder.file(getDockerComposeFile(
this.dockerContainerInfo.getNetworkName(),
this.dockerContainerInfo.getImageNameOverride().orElse("vimagick/dante:latest"))
.getPath())
.waitingForService("proxy", Container::areAllPortsOpen)
.saveLogsTo(LogDirectory.circleAwareLogDirectory(logDirectory)));
}

public interface Customizer<T> {
Expand Down Expand Up @@ -98,12 +99,15 @@ public void after() {
dockerComposeRule.after();
}

private static File getDockerComposeFile(String networkName) {
private static File getDockerComposeFile(String networkName, String imageName) {
try {
File proxyFile = File.createTempFile("proxy", ".yml");
String proxyConfig =
Resources.toString(Resources.getResource("docker-compose.proxy.yml"), StandardCharsets.UTF_8);
Files.write(proxyConfig.replace("{{NETWORK_NAME}}", networkName), proxyFile, StandardCharsets.UTF_8);
Files.write(
proxyConfig.replace("{{NETWORK_NAME}}", networkName).replace("{{IMAGE_NAME}}", imageName),
proxyFile,
StandardCharsets.UTF_8);
return proxyFile;
} catch (IOException e) {
throw Throwables.propagate(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@
public final class NetworkBasedDockerContainerInfo implements DockerContainerInfo {
private final DockerExecutable docker;
private final String networkName;
private final Optional<String> imageNameOverride;

public NetworkBasedDockerContainerInfo(DockerExecutable docker, String networkName) {
public NetworkBasedDockerContainerInfo(
DockerExecutable docker, String networkName, Optional<String> imageNameOverride) {
this.docker = docker;
this.networkName = networkName;
this.imageNameOverride = imageNameOverride;
}

public NetworkBasedDockerContainerInfo(DockerExecutable docker, String networkName) {
this(docker, networkName, Optional.empty());
}

@Override
Expand Down Expand Up @@ -56,4 +63,9 @@ public Optional<String> getHostForIp(String ip) {
public String getNetworkName() {
return networkName;
}

@Override
public Optional<String> getImageNameOverride() {
return imageNameOverride;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@
public final class ProjectBasedDockerContainerInfo implements DockerContainerInfo {
private final DockerExecutable docker;
private final ProjectName projectName;
private final Optional<String> imageNameOverride;

public ProjectBasedDockerContainerInfo(DockerExecutable docker, ProjectName projectName) {
public ProjectBasedDockerContainerInfo(
DockerExecutable docker, ProjectName projectName, Optional<String> imageNameOverride) {
this.docker = docker;
this.projectName = projectName;
this.imageNameOverride = imageNameOverride;
}

public ProjectBasedDockerContainerInfo(DockerExecutable docker, ProjectName projectName) {
this(docker, projectName, Optional.empty());
}

@Override
Expand Down Expand Up @@ -57,4 +64,9 @@ public Optional<String> getHostForIp(String ip) {
public String getNetworkName() {
return projectName.asString() + "_default";
}

@Override
public Optional<String> getImageNameOverride() {
return imageNameOverride;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '2'

services:
proxy:
image: vimagick/dante:latest
image: {{IMAGE_NAME}}
ports:
- "1080"
command: bash -c '(sed -i.bak "s/username //" /etc/dante/sockd.conf && sockd -f /etc/dante/sockd.conf -p /run/sockd.pid -N 10) || (sed -i.bak "s/username //" /etc/sockd.conf && sockd -f /etc/sockd.conf -p /tmp/sockd.pid -N 10)'
Expand Down

0 comments on commit d16aea4

Please sign in to comment.