Skip to content

Commit

Permalink
Merge pull request #140 from palantir/feature/separate_projects
Browse files Browse the repository at this point in the history
Feature/separate projects
  • Loading branch information
hpryce authored Jan 20, 2017
2 parents 858449a + 68b3760 commit d35070e
Show file tree
Hide file tree
Showing 128 changed files with 128 additions and 334 deletions.
67 changes: 24 additions & 43 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,59 +23,40 @@ repositories {
jcenter()
}

apply from: "gradle/idea.gradle"
apply plugin: 'java'

group = 'com.palantir.docker.compose'
version "git describe --tags --dirty".execute().text.trim()

apply plugin: 'com.palantir.baseline-config'
apply plugin: 'com.palantir.baseline-checkstyle'
apply plugin: 'com.palantir.baseline-eclipse'
apply plugin: 'com.palantir.baseline-findbugs'
apply plugin: 'com.palantir.baseline-idea'
apply from: "${rootDir}/gradle/publish.gradle"
apply from: "${rootDir}/gradle/idea.gradle"

apply plugin: 'org.inferred.processors'
apply plugin: 'com.palantir.configuration-resolver'

sourceCompatibility = 1.8
targetCompatibility = 1.8

configurations.all {
exclude module: 'slf4j-log4j12'
resolutionStrategy.force(
"org.slf4j:slf4j-api:$slf4jVersion",
"org.slf4j:slf4j-log4j12:$slf4jVersion",
)
}
subprojects {
apply from: "${rootDir}/gradle/idea.gradle"
apply plugin: 'java'

dependencies {
compile "org.slf4j:slf4j-api:$slf4jVersion"
compile "org.apache.commons:commons-lang3:3.0"
compile "commons-io:commons-io:2.4"
compile "com.google.guava:guava:$guavaVersion"
compile "joda-time:joda-time:$jodaVersion"
compile "com.github.zafarkhaja:java-semver:$javaSemverVersion"
apply plugin: 'com.palantir.baseline-checkstyle'
apply plugin: 'com.palantir.baseline-eclipse'
apply plugin: 'com.palantir.baseline-findbugs'
apply plugin: 'com.palantir.baseline-idea'
apply from: "${rootDir}/gradle/publish.gradle"

compile "junit:junit:$junitVersion"
compile 'com.jayway.awaitility:awaitility:1.6.5'
apply plugin: 'org.inferred.processors'
apply plugin: 'com.palantir.configuration-resolver'

processor 'org.immutables:value:2.1.14'
sourceCompatibility = 1.8
targetCompatibility = 1.8

testCompile "org.slf4j:slf4j-log4j12:$slf4jVersion"
testCompile "org.apache.logging.log4j:log4j-core:$log4jVersion"
testCompile "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion"

testCompile "org.hamcrest:hamcrest-all:$hamcrestVersion"
testCompile "org.mockito:mockito-core:$mockitoVersion"
testCompile "com.github.tomakehurst:wiremock:2.0.6-beta"
testCompile "com.google.code.findbugs:jsr305:3.0.0"
testCompile "com.github.stefanbirkner:system-rules:1.16.1"
}
repositories {
mavenCentral()
jcenter()
}

// Instead of copying files out of `$buildDir/test-results/` on CircleCI, we can
// just write them into the right place straight away.
if (System.env.CIRCLE_TEST_REPORTS) {
test.reports.junitXml.destination = file(System.env.CIRCLE_TEST_REPORTS)
configurations.all {
exclude module: 'slf4j-log4j12'
resolutionStrategy.force(
"org.slf4j:slf4j-api:$slf4jVersion",
"org.slf4j:slf4j-log4j12:$slf4jVersion",
)
}
}
1 change: 1 addition & 0 deletions docker-compose-rule-core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
29 changes: 29 additions & 0 deletions docker-compose-rule-core/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
dependencies {
compile "org.slf4j:slf4j-api:$slf4jVersion"
compile "org.apache.commons:commons-lang3:3.0"
compile "commons-io:commons-io:2.4"
compile "com.google.guava:guava:$guavaVersion"
compile "joda-time:joda-time:$jodaVersion"
compile "com.github.zafarkhaja:java-semver:$javaSemverVersion"

compile 'com.jayway.awaitility:awaitility:1.6.5'

processor 'org.immutables:value:2.1.14'

testCompile "org.slf4j:slf4j-log4j12:$slf4jVersion"
testCompile "org.apache.logging.log4j:log4j-core:$log4jVersion"
testCompile "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion"

testCompile "junit:junit:$junitVersion"
testCompile "org.hamcrest:hamcrest-all:$hamcrestVersion"
testCompile "org.mockito:mockito-core:$mockitoVersion"
testCompile "com.github.tomakehurst:wiremock:2.0.6-beta"
testCompile "com.google.code.findbugs:jsr305:3.0.0"
testCompile "com.github.stefanbirkner:system-rules:1.16.1"
}

// Instead of copying files out of `$buildDir/test-results/` on CircleCI, we can
// just write them into the right place straight away.
if (System.env.CIRCLE_TEST_REPORTS) {
test.reports.junitXml.destination = file(System.env.CIRCLE_TEST_REPORTS)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

package com.palantir.docker.compose.configuration;

import com.palantir.docker.compose.DockerComposeRule;
import com.palantir.docker.compose.execution.AggressiveShutdownStrategy;
import com.palantir.docker.compose.execution.AggressiveShutdownWithNetworkCleanupStrategy;
import com.palantir.docker.compose.execution.Docker;
import com.palantir.docker.compose.execution.DockerCompose;
import com.palantir.docker.compose.execution.GracefulShutdownStrategy;
import com.palantir.docker.compose.execution.SkipShutdownStrategy;
import java.io.IOException;
Expand All @@ -22,6 +23,6 @@ public interface ShutdownStrategy {
ShutdownStrategy SKIP = new SkipShutdownStrategy();
ShutdownStrategy AGGRESSIVE_WITH_NETWORK_CLEANUP = new AggressiveShutdownWithNetworkCleanupStrategy();

void shutdown(DockerComposeRule rule) throws IOException, InterruptedException;
void shutdown(DockerCompose dockerCompose, Docker docker) throws IOException, InterruptedException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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

import com.palantir.docker.compose.DockerComposeRule;
import com.palantir.docker.compose.configuration.ShutdownStrategy;
import com.palantir.docker.compose.connection.ContainerName;
import java.io.IOException;
Expand All @@ -23,38 +22,38 @@ public class AggressiveShutdownStrategy implements ShutdownStrategy {
private static final Logger log = LoggerFactory.getLogger(AggressiveShutdownStrategy.class);

@Override
public void shutdown(DockerComposeRule rule) throws IOException, InterruptedException {
List<ContainerName> runningContainers = rule.dockerCompose().ps();
public void shutdown(DockerCompose dockerCompose, Docker docker) throws IOException, InterruptedException {
List<ContainerName> runningContainers = dockerCompose.ps();

log.info("Shutting down {}", runningContainers.stream().map(ContainerName::semanticName).collect(toList()));
if (removeContainersCatchingErrors(rule, runningContainers)) {
if (removeContainersCatchingErrors(docker, runningContainers)) {
return;
}

log.debug("First shutdown attempted failed due to btrfs volume error... retrying");
if (removeContainersCatchingErrors(rule, runningContainers)) {
if (removeContainersCatchingErrors(docker, runningContainers)) {
return;
}

log.warn("Couldn't shut down containers due to btrfs volume error, "
+ "see https://circleci.com/docs/docker-btrfs-error/ for more info.");
}

private boolean removeContainersCatchingErrors(DockerComposeRule rule, List<ContainerName> runningContainers) throws IOException, InterruptedException {
private boolean removeContainersCatchingErrors(Docker docker, List<ContainerName> runningContainers) throws IOException, InterruptedException {
try {
removeContainers(rule, runningContainers);
removeContainers(docker, runningContainers);
return true;
} catch (DockerExecutionException exception) {
return false;
}
}

private void removeContainers(DockerComposeRule rule, List<ContainerName> running) throws IOException, InterruptedException {
private void removeContainers(Docker docker, List<ContainerName> running) throws IOException, InterruptedException {
List<String> rawContainerNames = running.stream()
.map(ContainerName::rawName)
.collect(toList());

rule.docker().rm(rawContainerNames);
docker.rm(rawContainerNames);
log.debug("Finished shutdown");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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

import com.palantir.docker.compose.DockerComposeRule;
import com.palantir.docker.compose.configuration.ShutdownStrategy;
import com.palantir.docker.compose.connection.ContainerName;
import java.io.IOException;
Expand All @@ -22,32 +21,32 @@ public class AggressiveShutdownWithNetworkCleanupStrategy implements ShutdownStr
private static final Logger log = LoggerFactory.getLogger(AggressiveShutdownWithNetworkCleanupStrategy.class);

@Override
public void shutdown(DockerComposeRule rule) throws IOException, InterruptedException {
List<ContainerName> runningContainers = rule.dockerCompose().ps();
public void shutdown(DockerCompose dockerCompose, Docker docker) throws IOException, InterruptedException {
List<ContainerName> runningContainers = dockerCompose.ps();

log.info("Shutting down {}", runningContainers.stream().map(ContainerName::semanticName).collect(toList()));
removeContainersCatchingErrors(rule, runningContainers);
removeNetworks(rule);
removeContainersCatchingErrors(docker, runningContainers);
removeNetworks(dockerCompose);
}

private void removeContainersCatchingErrors(DockerComposeRule rule, List<ContainerName> runningContainers) throws IOException, InterruptedException {
private void removeContainersCatchingErrors(Docker docker, List<ContainerName> runningContainers) throws IOException, InterruptedException {
try {
removeContainers(rule, runningContainers);
removeContainers(docker, runningContainers);
} catch (DockerExecutionException exception) {
log.error("Error while trying to remove containers: {}", exception.getMessage());
}
}

private void removeContainers(DockerComposeRule rule, List<ContainerName> running) throws IOException, InterruptedException {
private void removeContainers(Docker docker, List<ContainerName> running) throws IOException, InterruptedException {
List<String> rawContainerNames = running.stream()
.map(ContainerName::rawName)
.collect(toList());

rule.docker().rm(rawContainerNames);
docker.rm(rawContainerNames);
log.debug("Finished shutdown");
}

private void removeNetworks(DockerComposeRule rule) throws IOException, InterruptedException {
rule.dockerCompose().down();
private void removeNetworks(DockerCompose dockerCompose) throws IOException, InterruptedException {
dockerCompose.down();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package com.palantir.docker.compose.execution;

import com.palantir.docker.compose.DockerComposeRule;
import com.palantir.docker.compose.configuration.ShutdownStrategy;
import java.io.IOException;
import org.slf4j.Logger;
Expand All @@ -19,11 +18,11 @@ public class GracefulShutdownStrategy implements ShutdownStrategy {
private static final Logger log = LoggerFactory.getLogger(GracefulShutdownStrategy.class);

@Override
public void shutdown(DockerComposeRule rule) throws IOException, InterruptedException {
public void shutdown(DockerCompose dockerCompose, Docker docker) throws IOException, InterruptedException {
log.debug("Killing docker-compose cluster");
rule.dockerCompose().down();
rule.dockerCompose().kill();
rule.dockerCompose().rm();
dockerCompose.down();
dockerCompose.kill();
dockerCompose.rm();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package com.palantir.docker.compose.execution;

import com.palantir.docker.compose.DockerComposeRule;
import com.palantir.docker.compose.configuration.ShutdownStrategy;
import java.io.IOException;
import org.slf4j.Logger;
Expand All @@ -15,7 +14,7 @@ public class SkipShutdownStrategy implements ShutdownStrategy {
private static final Logger log = LoggerFactory.getLogger(SkipShutdownStrategy.class);

@Override
public void shutdown(DockerComposeRule rule) throws IOException, InterruptedException {
public void shutdown(DockerCompose dockerCompose, Docker docker) throws IOException, InterruptedException {
log.warn("\n"
+ "******************************************************************************************\n"
+ "* docker-compose-rule has been configured to skip docker-compose shutdown: *\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.palantir.docker.compose.configuration.ShutdownStrategy;
import com.palantir.docker.compose.execution.Docker;
import com.palantir.docker.compose.execution.DockerCompose;
import com.palantir.docker.compose.execution.DockerExecutionException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand All @@ -25,27 +23,21 @@ public class AggressiveShutdownStrategyTest {
@Rule
public final ExpectedException exception = ExpectedException.none();

private final DockerComposeRule rule = mock(DockerComposeRule.class);
private final DockerCompose mockDockerCompose = mock(DockerCompose.class);
private final Docker mockDocker = mock(Docker.class);

private static final String btrfs_message = "'docker rm -f test-1.container.name test-2.container.name' "
+ "returned exit code 1\nThe output was:\nFailed to remove container (test-1.container.name): "
+ "Error response from daemon: Driver btrfs failed to remove root filesystem ";

@Before
public void before() {
when(rule.dockerCompose()).thenReturn(mock(DockerCompose.class));
when(rule.docker()).thenReturn(mockDocker);
}

@Test
public void first_btrfs_error_should_be_caught_silently_and_retried() throws Exception {
doThrow(new DockerExecutionException(btrfs_message))
.doNothing()
.when(mockDocker)
.rm(anyListOf(String.class));

ShutdownStrategy.AGGRESSIVE.shutdown(rule);
ShutdownStrategy.AGGRESSIVE.shutdown(mockDockerCompose, mockDocker);

verify(mockDocker, times(2)).rm(anyListOf(String.class));
}
Expand All @@ -57,7 +49,7 @@ public void after_two_btrfs_failures_we_should_just_log_and_continue() throws Ex
.when(mockDocker)
.rm(anyListOf(String.class));

ShutdownStrategy.AGGRESSIVE.shutdown(rule);
ShutdownStrategy.AGGRESSIVE.shutdown(mockDockerCompose, mockDocker);

verify(mockDocker, times(2)).rm(anyListOf(String.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.palantir.docker.compose.configuration.ShutdownStrategy;
import com.palantir.docker.compose.execution.Docker;
import com.palantir.docker.compose.execution.DockerCompose;
import com.palantir.docker.compose.execution.DockerExecutionException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand All @@ -25,25 +23,18 @@ public class AggressiveShutdownWithNetworkCleanupStrategyTest {
@Rule
public final ExpectedException exception = ExpectedException.none();

private final DockerComposeRule rule = mock(DockerComposeRule.class);
private final DockerCompose mockDockerCompose = mock(DockerCompose.class);
private final Docker mockDocker = mock(Docker.class);

private static final String error_msg = "Random DockerExecutionException message";

@Before
public void before() {
when(rule.dockerCompose()).thenReturn(mockDockerCompose);
when(rule.docker()).thenReturn(mockDocker);
}

@Test
public void docker_compose_down_should_be_called_despite_docker_rm_throwing_exception() throws Exception {
doThrow(new DockerExecutionException(error_msg))
.when(mockDocker)
.rm(anyListOf(String.class));

ShutdownStrategy.AGGRESSIVE_WITH_NETWORK_CLEANUP.shutdown(rule);
ShutdownStrategy.AGGRESSIVE_WITH_NETWORK_CLEANUP.shutdown(mockDockerCompose, mockDocker);

verify(mockDockerCompose, times(1)).down();
}
Expand Down
Loading

0 comments on commit d35070e

Please sign in to comment.