From 41d96c5b8a56a99ea68855b58d234b62f5df8b0e Mon Sep 17 00:00:00 2001 From: Hudson Mendes Date: Fri, 25 Nov 2016 15:44:05 +0100 Subject: [PATCH 1/2] Using the gradle plugin framework, we now identify the OS (windows or not) and use that to predict if we use /bin/sh or cmd. Has been tested on a windows+eclipse machine and works --- .gitignore | 4 ++++ build.gradle | 2 +- .../gradle/docker/DockerUtils.groovy | 16 +++++++++---- .../gradle/docker/DockerUtilsTest.groovy | 24 +++++++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 src/test/groovy/io/advantageous/gradle/docker/DockerUtilsTest.groovy diff --git a/.gitignore b/.gitignore index 0b09647..14ad7f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,9 @@ .gradle .idea +.settings +.project +.classpath rbss-devops-plugin.iml build/ rbss-platform-deploy-gradle-plugin.iml +/bin/ diff --git a/build.gradle b/build.gradle index 1cbd87d..1620449 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { } group 'io.advantageous.gradle' -version '0.1.7' +version '0.1.8' apply plugin: 'maven' apply plugin: 'signing' diff --git a/src/main/groovy/io/advantageous/gradle/docker/DockerUtils.groovy b/src/main/groovy/io/advantageous/gradle/docker/DockerUtils.groovy index c291a89..55add43 100644 --- a/src/main/groovy/io/advantageous/gradle/docker/DockerUtils.groovy +++ b/src/main/groovy/io/advantageous/gradle/docker/DockerUtils.groovy @@ -1,5 +1,7 @@ package io.advantageous.gradle.docker +import org.apache.tools.ant.taskdefs.condition.Os + class DockerUtils { static Map getDockerEnv() { @@ -14,7 +16,7 @@ class DockerUtils { .findAll { it.startsWith('export') } .collect { it.replace('export', '').replace('"', '').trim() } .collect { it.split('=') } - .collectEntries { [it[0], it[1]] } + .collectEntries { [it[0], it[1]]} } } } @@ -40,12 +42,18 @@ class DockerUtils { runCommand("docker", "build", "-t", dockerCoordinates(namespace, projectName, projectVersion), "build/") } - static runCommand(String command) { - + static prepareArgs(String command, boolean runningOnWindows) { + if (!runningOnWindows) + return ["/bin/sh", "-c", "$command"] + else + return ["cmd", "/c", "$command"] + } + static runCommand(String command) { println("Running command $command") - String[] args = ["/bin/sh", "-c", "$command"] + def runningOnWindows = Os.isFamily(Os.FAMILY_WINDOWS); + String[] args = prepareArgs(command, runningOnWindows); def stringBuilder = new StringBuilder() def processBuilder = new ProcessBuilder() diff --git a/src/test/groovy/io/advantageous/gradle/docker/DockerUtilsTest.groovy b/src/test/groovy/io/advantageous/gradle/docker/DockerUtilsTest.groovy new file mode 100644 index 0000000..22ba4c5 --- /dev/null +++ b/src/test/groovy/io/advantageous/gradle/docker/DockerUtilsTest.groovy @@ -0,0 +1,24 @@ +package io.advantageous.gradle.docker + +import org.junit.Test +import org.testng.Assert + +class DockerUtilsTest { + final SAMPLE = "docker run -d -p 2375:2375 --volume=/var/run/docker.sock:/var/run/docker.sock --env='FOO=bar' --name=docker-http sequenceiq/socat" + + @Test + void prepareArgs_windows() throws Exception { + def actualWin = DockerUtils.prepareArgs(SAMPLE, true) + Assert.assertEquals("cmd", actualWin.get(0)) + Assert.assertEquals("/c", actualWin.get(1)) + Assert.assertEquals(SAMPLE, actualWin.get(2)) + } + + @Test + void prepareArgs_linux() throws Exception { + def actualLinux = DockerUtils.prepareArgs(SAMPLE, false) + Assert.assertEquals("/bin/sh", actualLinux.get(0)) + Assert.assertEquals("-c", actualLinux.get(1)) + Assert.assertEquals(SAMPLE, actualLinux.get(2)) + } +} From 4fea58f4efd922dfae74ee1047451ff912074cbe Mon Sep 17 00:00:00 2001 From: Hudson Mendes Date: Fri, 25 Nov 2016 17:38:11 +0100 Subject: [PATCH 2/2] Using delimiters for Environment variables does not work in cmd /c, therefore we are now conditionally deciding the delimiter --- .../advantageous/gradle/docker/DockerContainer.groovy | 9 +++++---- .../gradle/docker/DockerContainerTest.groovy | 10 +++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/groovy/io/advantageous/gradle/docker/DockerContainer.groovy b/src/main/groovy/io/advantageous/gradle/docker/DockerContainer.groovy index 28212f6..d1720ff 100644 --- a/src/main/groovy/io/advantageous/gradle/docker/DockerContainer.groovy +++ b/src/main/groovy/io/advantageous/gradle/docker/DockerContainer.groovy @@ -1,5 +1,7 @@ package io.advantageous.gradle.docker +import org.apache.tools.ant.taskdefs.condition.Os + class DockerContainer { private final String name @@ -105,14 +107,14 @@ class DockerContainer { } if (env.size() > 0) { + def delimiter = Os.isFamily(Os.FAMILY_WINDOWS) ? "" : "'"; env.entrySet().stream().forEach { entry -> builder.append(" --env=") - .append("'") + .append(delimiter) .append(entry.key.toString().toUpperCase()) .append('=') .append(entry.value) - .append("'") - + .append(delimiter) } } @@ -136,5 +138,4 @@ class DockerContainer { builder.toString() } - } diff --git a/src/test/groovy/io/advantageous/gradle/docker/DockerContainerTest.groovy b/src/test/groovy/io/advantageous/gradle/docker/DockerContainerTest.groovy index 8f9ef64..e9a5160 100644 --- a/src/test/groovy/io/advantageous/gradle/docker/DockerContainerTest.groovy +++ b/src/test/groovy/io/advantageous/gradle/docker/DockerContainerTest.groovy @@ -1,12 +1,13 @@ package io.advantageous.gradle.docker +import org.apache.tools.ant.taskdefs.condition.Os import org.junit.Test import org.testng.Assert class DockerContainerTest { @Test - void testRunCommand() throws Exception { + void testRunCommand_nonWin() throws Exception { def socat = new DockerContainer("socat") .containerName("docker-http") @@ -15,7 +16,10 @@ class DockerContainerTest { .image("sequenceiq/socat").env("foo":"bar") .runCommand() - Assert.assertEquals( - "docker run -d -p 2375:2375 --volume=/var/run/docker.sock:/var/run/docker.sock --env='FOO=bar' --name=docker-http sequenceiq/socat", socat) + def expected = Os.isFamily(Os.FAMILY_WINDOWS) ? + "docker run -d -p 2375:2375 --volume=/var/run/docker.sock:/var/run/docker.sock --env=FOO=bar --name=docker-http sequenceiq/socat": + "docker run -d -p 2375:2375 --volume=/var/run/docker.sock:/var/run/docker.sock --env='FOO=bar' --name=docker-http sequenceiq/socat" ; + + Assert.assertEquals(expected, socat) } }