From b9680b8218d2345b0a2cc1f26173cf52a7170443 Mon Sep 17 00:00:00 2001 From: Stefan Neuhaus Date: Wed, 2 Jan 2019 15:20:38 +0100 Subject: [PATCH] Fix issues with killing/removing of containers when projectName is specified Extract start logic of DockerComposeTasks to parent class --- build.gradle | 2 +- .../frost/tasks/DockerComposeTask.groovy | 24 ++++++++++++++++++- .../frost/tasks/FrostComposeKillTask.groovy | 16 ++----------- .../frost/tasks/FrostComposePullTask.groovy | 7 +----- .../frost/tasks/FrostComposeUpTask.groovy | 17 +------------ 5 files changed, 28 insertions(+), 38 deletions(-) diff --git a/build.gradle b/build.gradle index c5e2bea..bb22413 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ dependencies { } group = 'org.rewedigital' -version = '0.5' +version = '0.5.1' gradlePlugin { plugins { diff --git a/src/main/groovy/org/rewedigital/frost/tasks/DockerComposeTask.groovy b/src/main/groovy/org/rewedigital/frost/tasks/DockerComposeTask.groovy index 835e3d1..9ecf9c4 100644 --- a/src/main/groovy/org/rewedigital/frost/tasks/DockerComposeTask.groovy +++ b/src/main/groovy/org/rewedigital/frost/tasks/DockerComposeTask.groovy @@ -1,11 +1,12 @@ package org.rewedigital.frost.tasks - import org.gradle.api.DefaultTask import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.TaskAction import org.rewedigital.frost.util.Util +import static org.rewedigital.frost.FrostPluginExtension.EXTENSION_NAME + abstract class DockerComposeTask extends DefaultTask { static final EXECUTABLE = "docker-compose" @@ -24,4 +25,25 @@ abstract class DockerComposeTask extends DefaultTask { @TaskAction def abstract action() + + + List dockerComposeCommand(String command) { + def envString = environment().inject([]) { result, entry -> result << "${entry.key}=${entry.value}" }.join(" ") + + def cmd = ["/bin/sh"] + cmd << "-c" + cmd << "export ${envString} && ${EXECUTABLE} -f ${getComposeFile()} -f ${getComposeOverrideFile()} ${command}" + cmd + } + + Map environment() { + def env = [ + TAG: project.extensions[EXTENSION_NAME].sutTag + ] + + if (project.extensions[EXTENSION_NAME].projectName) { + env << [COMPOSE_PROJECT_NAME: project.extensions[EXTENSION_NAME].projectName] + } + env + } } diff --git a/src/main/groovy/org/rewedigital/frost/tasks/FrostComposeKillTask.groovy b/src/main/groovy/org/rewedigital/frost/tasks/FrostComposeKillTask.groovy index c428172..8d15a4f 100644 --- a/src/main/groovy/org/rewedigital/frost/tasks/FrostComposeKillTask.groovy +++ b/src/main/groovy/org/rewedigital/frost/tasks/FrostComposeKillTask.groovy @@ -15,22 +15,10 @@ class FrostComposeKillTask extends DockerComposeTask { @Override def action() { try { - def cmd = [EXECUTABLE] - cmd << "-f" - cmd << getComposeFile() - cmd << "-f" - cmd << getComposeOverrideFile() - cmd << "kill" + def cmd = dockerComposeCommand("kill") Util.executeSynchronously(cmd, "docker_compose_kill", getReportDirectory()) - cmd = [EXECUTABLE] - cmd << "-f" - cmd << getComposeFile() - cmd << "-f" - cmd << getComposeOverrideFile() - cmd << "rm" - cmd << "-f" - cmd << "-v" + cmd = dockerComposeCommand("rm -f -v") Util.executeSynchronously(cmd, "docker_compose_rm", getReportDirectory()) } finally { getComposeOverrideFile().delete() diff --git a/src/main/groovy/org/rewedigital/frost/tasks/FrostComposePullTask.groovy b/src/main/groovy/org/rewedigital/frost/tasks/FrostComposePullTask.groovy index 5b79abc..526f5b5 100644 --- a/src/main/groovy/org/rewedigital/frost/tasks/FrostComposePullTask.groovy +++ b/src/main/groovy/org/rewedigital/frost/tasks/FrostComposePullTask.groovy @@ -4,8 +4,6 @@ import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.TaskAction import org.rewedigital.frost.util.Util -import static org.rewedigital.frost.FrostPluginExtension.EXTENSION_NAME - class FrostComposePullTask extends DockerComposeTask { @OutputDirectory @@ -16,10 +14,7 @@ class FrostComposePullTask extends DockerComposeTask { @TaskAction @Override def action() { - def cmd = ["/bin/sh"] - cmd << "-c" - cmd << "export TAG=${project.extensions[EXTENSION_NAME].sutTag} && ${EXECUTABLE} -f ${getComposeFile()} -f ${getComposeOverrideFile()} pull --ignore-pull-failures" - + def cmd = dockerComposeCommand("pull --ignore-pull-failures") Util.executeSynchronously(cmd, "docker_compose_pull", getReportDirectory()) } } diff --git a/src/main/groovy/org/rewedigital/frost/tasks/FrostComposeUpTask.groovy b/src/main/groovy/org/rewedigital/frost/tasks/FrostComposeUpTask.groovy index 74d1529..af7e8a5 100644 --- a/src/main/groovy/org/rewedigital/frost/tasks/FrostComposeUpTask.groovy +++ b/src/main/groovy/org/rewedigital/frost/tasks/FrostComposeUpTask.groovy @@ -4,8 +4,6 @@ import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.TaskAction import org.rewedigital.frost.util.Util -import static org.rewedigital.frost.FrostPluginExtension.EXTENSION_NAME - class FrostComposeUpTask extends DockerComposeTask { @OutputDirectory @@ -16,20 +14,7 @@ class FrostComposeUpTask extends DockerComposeTask { @TaskAction @Override def action() { - def env = [ - TAG: project.extensions[EXTENSION_NAME].sutTag - ] - - if (project.extensions[EXTENSION_NAME].projectName) { - env << [COMPOSE_PROJECT_NAME: project.extensions[EXTENSION_NAME].projectName] - } - - def envString = env.inject([]) { result, entry -> result << "${entry.key}=${entry.value}" }.join(" ") - - def cmd = ["/bin/sh"] - cmd << "-c" - cmd << "export ${envString} && ${EXECUTABLE} -f ${getComposeFile()} -f ${getComposeOverrideFile()} up" - + def cmd = dockerComposeCommand("up") Util.executeAsynchronously(cmd, "docker_compose_up", getReportDirectory()) } }