Skip to content

Commit

Permalink
Adds ability to use local Docker compose binary instead of container.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Aug 10, 2016
1 parent 3961611 commit 539128c
Showing 1 changed file with 100 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.dockerjava.api.model.Container;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.util.concurrent.Uninterruptibles;
import org.junit.runner.Description;
import org.rnorth.ducttape.ratelimits.RateLimiter;
Expand All @@ -17,9 +18,12 @@
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.startupcheck.IndefiniteWaitOneShotStartupCheckStrategy;
import org.testcontainers.utility.Base58;
import org.testcontainers.utility.DockerLoggerFactory;
import org.testcontainers.utility.ResourceReaper;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -44,6 +48,7 @@ public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>> e
private Set<String> spawnedContainerIds;
private Map<String, Integer> scalingPreferences = new HashMap<>();
private DockerClient dockerClient;
private boolean local;

/**
* Properties that should be passed through to all Compose and ambassador containers (not
Expand Down Expand Up @@ -98,7 +103,13 @@ private void createServices() {
}

private DockerCompose getDockerCompose(String cmd) {
return new DockerCompose(composeFile, identifier)
final DockerCompose dockerCompose;
if (local) {
dockerCompose = new LocalDockerCompose(composeFile, identifier);
} else {
dockerCompose = new ContainerisedDockerCompose(composeFile, identifier);
}
return dockerCompose
.withCommand(cmd)
.withEnv(env);
}
Expand Down Expand Up @@ -267,13 +278,31 @@ public SELF withEnv(Map<String, String> env) {
return self();
}

/**
* Use a local Docker compose binary instead of a container.
*
* @return this instance, for chaining
*/
public SELF withLocalCompose() {
this.local = true;
return self();
}

private SELF self() {
return (SELF) this;
}
}

class DockerCompose extends GenericContainer<DockerCompose> {
public DockerCompose(File composeFile, String identifier) {
interface DockerCompose {
DockerCompose withCommand(String cmd);

DockerCompose withEnv(Map<String, String> env);

void start();
}

class ContainerisedDockerCompose extends GenericContainer<ContainerisedDockerCompose> implements DockerCompose {
public ContainerisedDockerCompose(File composeFile, String identifier) {

super("docker/compose:1.8.0");
addEnv("COMPOSE_PROJECT_NAME", identifier);
Expand Down Expand Up @@ -305,4 +334,71 @@ public void start() {
}
logger().info("Docker compose has finished running");
}
}
}

class LocalDockerCompose implements DockerCompose {
private final File composeFile;
private final String identifier;
private String cmd = "";
private Map<String, String> env = new HashMap<>();

public LocalDockerCompose(File composeFile, String identifier) {
this.composeFile = composeFile;
this.identifier = identifier;
}

@Override
public DockerCompose withCommand(String cmd) {
this.cmd = cmd;
return this;
}

@Override
public DockerCompose withEnv(Map<String, String> env) {
this.env = env;
return this;
}

@Override
public void start() {
try {
final ProcessBuilder pb = new ProcessBuilder(Splitter.onPattern(" ")
.omitEmptyStrings()
.splitToList("docker-compose " + cmd));

pb.redirectErrorStream(true);

final Map<String, String> environment = pb.environment();
environment.putAll(env);
environment.put("COMPOSE_PROJECT_NAME", identifier);

final File pwd = composeFile.getAbsoluteFile().getParentFile().getAbsoluteFile();
environment.put("COMPOSE_FILE", new File(pwd, composeFile.getAbsoluteFile().getName()).getAbsolutePath());

logger().info("Local Docker compose is running command: {}", cmd);
pb.directory(pwd);
final Process p = pb.start();

// wait for the compose process to stop, which should only happen after it has spawned all the service containers
p.waitFor(120, TimeUnit.SECONDS);

try (final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
for (String line; (line = reader.readLine()) != null; ) {
logger().info(line);
}
}

logger().info("Docker compose has finished running");

} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* @return a logger
*/
private Logger logger() {
return DockerLoggerFactory.getLogger("docker-compose");
}
}

0 comments on commit 539128c

Please sign in to comment.