Skip to content

Commit

Permalink
Merge pull request #22 from palantir/feature/prefer-using-the-environ…
Browse files Browse the repository at this point in the history
…ment-variable-for-docker-compose-location

Feature/prefer using the environment variable for docker compose location
  • Loading branch information
joelea committed Mar 4, 2016
2 parents f662c75 + 618d596 commit dfc3493
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,9 @@ To record the logs from your containers specify a location:
}

This will automatically record logs for all containers in real time to the specified directory. Collection will stop when the containers terminate.

Using a custom version of docker-compose
---------------

docker-compose-rule tries to use the docker-compose binary located at `/usr/local/bin/docker-compose`. This can be overriden by setting `DOCKER_COMPOSE_LOCATION` to be the path to a valid file.

Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@

import com.google.common.collect.ImmutableList;
import com.palantir.docker.compose.configuration.DockerComposeFiles;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static java.util.Arrays.asList;


public class DockerComposeExecutable {
private static final Logger log = LoggerFactory.getLogger(DockerComposeExecutable.class);

private static final List<String> dockerComposeLocations = asList(
"/usr/local/bin/docker-compose",
System.getenv("DOCKER_COMPOSE_LOCATION"));
private static final DockerComposeLocations DOCKER_COMPOSE_LOCATIONS = new DockerComposeLocations(
System.getenv("DOCKER_COMPOSE_LOCATION"),
"/usr/local/bin/docker-compose"
);

private final DockerComposeFiles dockerComposeFiles;
private final DockerConfiguration dockerConfiguration;
Expand All @@ -56,12 +56,13 @@ public Process execute(String... commands) throws IOException {
}

private static String findDockerComposePath() {
return dockerComposeLocations.stream()
.filter(StringUtils::isNotBlank)
.filter(path -> new File(path).exists())
.findAny()
String pathToUse = DOCKER_COMPOSE_LOCATIONS.preferredLocation()
.orElseThrow(() -> new IllegalStateException(
"Could not find docker-compose, looked in: " + dockerComposeLocations));
"Could not find docker-compose, looked in: " + DOCKER_COMPOSE_LOCATIONS));

log.debug("Using docker-compose found at " + pathToUse);

return pathToUse;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.docker.compose.execution;

import java.io.File;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import static java.util.Arrays.asList;

public class DockerComposeLocations {
private static final Predicate<String> IS_NOT_NULL = path -> path != null;
private static final Predicate<String> FILE_EXISTS = path -> new File(path).exists();

private final List<String> possiblePaths;

public DockerComposeLocations(String... possiblePaths) {
this.possiblePaths = asList(possiblePaths);
}

public Optional<String> preferredLocation() {

return possiblePaths.stream()
.filter(IS_NOT_NULL)
.filter(FILE_EXISTS)
.findFirst();
}

@Override
public String toString() {
return "DockerComposeLocations{" +
"possiblePaths=" + possiblePaths +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.docker.compose.execution;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;

import static java.util.Optional.empty;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

public class DockerComposeLocationsShould {
private static final String badLocation = "file/that/does/not/exist";
private static final String otherBadLocation = "another/file/that/does/not/exist";

@Rule public TemporaryFolder folder = new TemporaryFolder();

String goodLocation;

@Before
public void setup() throws IOException {
goodLocation = folder.newFile("docker-compose.yml").getAbsolutePath();
}

@Test public void
provide_the_first_docker_compose_location_if_it_exists() throws IOException {
DockerComposeLocations dockerComposeLocations = new DockerComposeLocations(
badLocation,
goodLocation,
otherBadLocation);

assertThat(dockerComposeLocations.preferredLocation().get(),
is(goodLocation));
}

@Test public void
skip_paths_from_environment_variables_that_are_unset() {
DockerComposeLocations dockerComposeLocations = new DockerComposeLocations(
System.getenv("AN_UNSET_DOCKER_COMPOSE_PATH"),
goodLocation);

assertThat(dockerComposeLocations.preferredLocation().get(),
is(goodLocation));
}

@Test public void
have_no_preferred_path_when_all_possible_paths_are_all_invalid() {
DockerComposeLocations dockerComposeLocations = new DockerComposeLocations(
badLocation);

assertThat(dockerComposeLocations.preferredLocation(),
is(empty()));
}
}

0 comments on commit dfc3493

Please sign in to comment.