Skip to content

Commit

Permalink
Merge pull request #53 from avast/ImprovedDockerComposeDetection
Browse files Browse the repository at this point in the history
Improved Docker Compose files detection
  • Loading branch information
augi committed Jan 3, 2017
2 parents 1c2abb3 + 5a5a5c4 commit 64ff6c2
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 28 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ repositories {

dependencies {
compile gradleApi()
compile 'org.yaml:snakeyaml:1.16'
compile 'org.yaml:snakeyaml:1.17'
testCompile gradleTestKit()
testCompile ('org.spockframework:spock-core:1.0-groovy-2.4') {
// Conflicting module versions: 2.4.4 vs. 2.4.1
Expand All @@ -37,7 +37,7 @@ dependencies {
}

task wrapper(type: Wrapper) {
gradleVersion = '3.1'
gradleVersion = '3.2.1'
}

task sourcesJar(type: Jar) {
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Sep 27 20:55:02 CEST 2016
#Mon Jan 02 20:24:04 CET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-bin.zip
19 changes: 11 additions & 8 deletions gradlew
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

##############################################################################
##
Expand Down Expand Up @@ -154,16 +154,19 @@ if $cygwin ; then
esac
fi

# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
# Escape application args
save ( ) {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
APP_ARGS=$(save "$@")

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi

exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
exec "$JAVACMD" "$@"
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import com.avast.gradle.dockercompose.tasks.ComposeUp
import groovy.transform.PackageScope
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.process.ExecSpec
import org.gradle.process.JavaForkOptions
import org.gradle.process.ProcessForkOptions
import org.gradle.util.VersionNumber

import java.time.Duration

Expand Down Expand Up @@ -81,6 +83,18 @@ class ComposeExtension {
res.addAll(args)
res
}

VersionNumber getDockerComposeVersion() {
def p = project
new ByteArrayOutputStream().withStream { os ->
p.exec { ExecSpec e ->
e.environment = environment
e.commandLine composeCommand('--version')
e.standardOutput = os
}
VersionNumber.parse(os.toString().trim().findAll(/(\d+\.){2}(\d+)/).head())
}
}
}

enum RemoveImages {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ComposeDown extends DefaultTask {
e.commandLine extension.composeCommand('stop')
}
if (extension.removeContainers) {
if (getDockerComposeVersion() >= VersionNumber.parse('1.6.0')) {
if (extension.getDockerComposeVersion() >= VersionNumber.parse('1.6.0')) {
String[] args = ['down']
switch (extension.removeImages) {
case RemoveImages.All:
Expand All @@ -49,15 +49,4 @@ class ComposeDown extends DefaultTask {
}
}
}

VersionNumber getDockerComposeVersion() {
new ByteArrayOutputStream().withStream { os ->
project.exec { ExecSpec e ->
e.environment = extension.environment
e.commandLine extension.composeCommand('--version')
e.standardOutput = os
}
VersionNumber.parse(os.toString().trim().findAll(/(\d+\.){2}(\d+)/).head())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.avast.gradle.dockercompose.ServiceInfo
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import org.gradle.process.ExecSpec
import org.gradle.util.VersionNumber
import org.yaml.snakeyaml.Yaml

import java.time.Instant
Expand Down Expand Up @@ -103,14 +104,49 @@ class ComposeUp extends DefaultTask {
}

Iterable<String> getServiceNames() {
String[] composeFiles = extension.useComposeFiles.empty ? ['docker-compose.yml', 'docker-compose.override.yml'] : extension.useComposeFiles
composeFiles
.findAll { project.file(it).exists() }
.collectMany { composeFile ->
if (extension.getDockerComposeVersion() >= VersionNumber.parse('1.6.0')) {
new ByteArrayOutputStream().withStream { os ->
project.exec { ExecSpec e ->
e.environment = extension.environment
e.commandLine extension.composeCommand('config', '--services')
e.standardOutput = os
}
os.toString().readLines()
}
} else {
def composeFiles = extension.useComposeFiles.empty ? getStandardComposeFiles() : getCustomComposeFiles()
composeFiles.collectMany { composeFile ->
def compose = (Map<String, Object>) (new Yaml().load(project.file(composeFile).text))
// if there is 'version: 2' on top-level then information about services is in 'services' sub-tree
'2'.equals(compose.get('version')) ? ((Map) compose.get('services')).keySet() : compose.keySet()
}.unique()

}
}

Iterable<File> getStandardComposeFiles() {
def res = []
def f = findInParentDirectories('docker-compose.yml', project.projectDir)
if (f != null) res.add(f)
f = findInParentDirectories('docker-compose.override.yml', project.projectDir)
if (f != null) res.add(f)
res
}

Iterable<File> getCustomComposeFiles() {
extension.useComposeFiles.collect {
def f = project.file(it)
if (!f.exists()) {
throw new IllegalArgumentException("Custom Docker Compose file not found: $f")
}
f
}
}

File findInParentDirectories(String filename, File directory) {
if ((directory) == null) return null
def f = new File(directory, filename)
f.exists() ? f : findInParentDirectories(filename, directory.parentFile)
}

String getContainerId(String serviceName) {
Expand Down

0 comments on commit 64ff6c2

Please sign in to comment.