From a6b1813599e95c870a20da25c328daeeadeab16a Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Mon, 13 Sep 2021 10:38:23 +0200 Subject: [PATCH 1/3] Docker load & more --- .../cognifide/gradle/environment/docker/Docker.kt | 15 +++++++++++++++ .../gradle/environment/docker/stack/Compose.kt | 14 ++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt index 059f6a1..090ae13 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt @@ -6,6 +6,7 @@ import com.cognifide.gradle.environment.EnvironmentExtension import kotlinx.coroutines.* import org.apache.commons.io.output.TeeOutputStream import org.gradle.process.internal.streams.SafeStreams +import java.io.File import java.io.FileOutputStream class Docker(val environment: EnvironmentExtension) { @@ -267,4 +268,18 @@ class Docker(val environment: EnvironmentExtension) { logger.lifecycle("Stopped Docker daemon \"$operation\"") } + + @Suppress("TooGenericExceptionCaught") + fun load(file: File): String { + val output = DockerProcess().execString { + withArgs("load", "--input", file.absolutePath) + } + return output.lineSequence().firstOrNull { it.startsWith("Loaded image:") } + ?.substringAfter(":")?.trim() + ?: throw DockerException("Cannot determine loaded Docker image name from output:\n$output\n") + } + + fun load(composePropertyName: String, file: File) { + composeProperties.putAll(common.project.provider { mapOf(composePropertyName to load(file)) }) + } } diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/stack/Compose.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/stack/Compose.kt index e999316..71c2e1d 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/stack/Compose.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/stack/Compose.kt @@ -60,6 +60,11 @@ class Compose(environment: EnvironmentExtension) : Stack(environment) { var deployRetry = common.retry { afterSecond(common.prop.long("docker.compose.deployRetry") ?: 30) } + val deployArgs = common.obj.strings { + convention(listOf("--remove-orphans")) + common.prop.list("docker.compose.deployArgs")?.let { set(it) } + } + override fun deploy() { init() @@ -68,7 +73,7 @@ class Compose(environment: EnvironmentExtension) : Stack(environment) { try { process().exec { - withArgs("-p", internalName.get(), "-f", composeFilePath, "up", "-d") + withArgs("-p", internalName.get(), "-f", composeFilePath, "up", "-d", *deployArgs.get().toTypedArray()) } } catch (e: DockerException) { throw StackException("Failed to deploy Docker Compose stack '${internalName.get()}'!", e) @@ -88,13 +93,18 @@ class Compose(environment: EnvironmentExtension) : Stack(environment) { var undeployRetry = common.retry { afterSecond(common.prop.long("docker.compose.undeployRetry") ?: 30) } + val undeployArgs = common.obj.strings { + convention(listOf("--remove-orphans")) + common.prop.list("docker.compose.undeployArgs")?.let { set(it) } + } + override fun undeploy() { init() common.progressIndicator { message = "Stopping stack '${internalName.get()}'" - val args = arrayOf("-p", internalName.get(), "-f", composeFilePath, "down") + val args = arrayOf("-p", internalName.get(), "-f", composeFilePath, "down", *undeployArgs.get().toTypedArray()) try { process().exec { withArgs(*args) } } catch (e: DockerException) { From 1987de45794e2109d1d98b8d10ba93e4648cecb5 Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Wed, 15 Sep 2021 09:29:49 +0200 Subject: [PATCH 2/3] Docker load improvement + better compose troubleshooting --- .../gradle/environment/docker/Docker.kt | 7 ++++-- .../environment/docker/stack/Compose.kt | 24 +++++++++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt index 090ae13..196dd1a 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt @@ -5,6 +5,7 @@ import com.cognifide.gradle.common.utils.using import com.cognifide.gradle.environment.EnvironmentExtension import kotlinx.coroutines.* import org.apache.commons.io.output.TeeOutputStream +import org.gradle.api.provider.Provider import org.gradle.process.internal.streams.SafeStreams import java.io.File import java.io.FileOutputStream @@ -279,7 +280,9 @@ class Docker(val environment: EnvironmentExtension) { ?: throw DockerException("Cannot determine loaded Docker image name from output:\n$output\n") } - fun load(composePropertyName: String, file: File) { - composeProperties.putAll(common.project.provider { mapOf(composePropertyName to load(file)) }) + fun load(composePropertyName: String, fileProvider: () -> File) = load(composePropertyName, common.project.provider { fileProvider() }) + + fun load(composePropertyName: String, fileProvider: Provider) { + composeProperties.putAll(common.project.provider { mapOf(composePropertyName to load(fileProvider.get()))}) } } diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/stack/Compose.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/stack/Compose.kt index 71c2e1d..a4d40fe 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/stack/Compose.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/stack/Compose.kt @@ -38,6 +38,7 @@ class Compose(environment: EnvironmentExtension) : Stack(environment) { val processBuilder = common.obj.typed { convention(common.obj.provider { ComposeProcessBuilder.detect() }) + finalizeValueOnRead() common.prop.string("docker.compose.processBuilder")?.let { set(ComposeProcessBuilder.of(it)) } } @@ -128,18 +129,31 @@ class Compose(environment: EnvironmentExtension) : Stack(environment) { override fun troubleshoot(): List = mutableListOf().apply { add("Consider troubleshooting:") + val process = process() val psArgs = arrayOf("-p", internalName.get(), "ps") + val logsArgs = arrayOf("-p", internalName.get(), "logs") + try { - val out = try { + add("* restarting Docker") + + val psOut = try { process().execString { withArgs(*psArgs) } } catch (e: Exception) { throw StackException("Cannot list processes in Docker Compose stack named '${internalName.get()}'!", e) } - add("* restarting Docker") - add("* using output of command: 'docker ${psArgs.joinToString(" ")}':\n") - add(out) + add("* using output of command: '${(process.commandLine + psArgs).joinToString(" ")}':\n") + add(psOut + "\n") + + val logsOut = try { + process().execString { withArgs(*logsArgs) } + } catch (e: Exception) { + throw StackException("Cannot print logs from Docker Compose stack named '${internalName.get()}'!", e) + } + add("* using output of command: '${(process.commandLine + logsArgs).joinToString(" ")}':\n") + add(logsOut + "\n") } catch (e: Exception) { - add("* using command: 'docker ${psArgs.joinToString(" ")}'") + add("* using command: '${(process.commandLine + psArgs).joinToString(" ")}'") + add("* using command: '${(process.commandLine + logsArgs).joinToString(" ")}'") add("* restarting Docker") } } From 60318c5f5b6c2d5c190abfc2ce8d80985d473ed0 Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Wed, 15 Sep 2021 11:30:36 +0200 Subject: [PATCH 3/3] Detekt fix --- .../kotlin/com/cognifide/gradle/environment/docker/Docker.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt index 196dd1a..1def316 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt @@ -283,6 +283,6 @@ class Docker(val environment: EnvironmentExtension) { fun load(composePropertyName: String, fileProvider: () -> File) = load(composePropertyName, common.project.provider { fileProvider() }) fun load(composePropertyName: String, fileProvider: Provider) { - composeProperties.putAll(common.project.provider { mapOf(composePropertyName to load(fileProvider.get()))}) + composeProperties.putAll(common.project.provider { mapOf(composePropertyName to load(fileProvider.get())) }) } }