From 9d02f3cca39f9f7926ade868598977f7bad5a961 Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Wed, 19 Aug 2020 17:31:41 +0200 Subject: [PATCH] Features, features... --- .../gradle/environment/docker/Container.kt | 32 ++++++++++++++++--- .../environment/docker/ContainerManager.kt | 17 ++++++++-- .../gradle/environment/docker/Docker.kt | 7 +++- .../docker/container/HostFileManager.kt | 20 +++++++++--- .../gradle/environment/hosts/Host.kt | 2 ++ .../gradle/environment/hosts/HostOptions.kt | 2 ++ 6 files changed, 69 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/Container.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/Container.kt index 33614bc..8fca8b0 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/Container.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/Container.kt @@ -182,12 +182,36 @@ class Container(val docker: Docker, val name: String) { this.exitCodes = exitCode?.run { listOf(this) } ?: listOf() } - fun ensureDir(vararg paths: String) = paths.forEach { path -> - execShell("Ensuring directory at path '$path'", "mkdir -p $path") + fun ensureFile(vararg paths: String) = ensureFile(paths.asIterable()) + + fun ensureFile(paths: Iterable) { + ensureDir(paths.map { it.substringBeforeLast("/") }) + + val command = "touch ${paths.joinToString(" ")}" + when (paths.count()) { + 1 -> execShell("Ensuring file '${paths.first()}'", command) + else -> execShell("Ensuring files (${paths.count()})", command) + } } - fun cleanDir(vararg paths: String) = paths.forEach { path -> - execShell("Cleaning directory contents at path '$path'", "rm -fr $path/*") + fun ensureDir(vararg paths: String) = ensureDir(paths.toList()) + + fun ensureDir(paths: Iterable) { + val command = "mkdir -p ${paths.joinToString(" ")}" + when (paths.count()) { + 1 -> execShell("Ensuring directory '${paths.first()}'", command) + else -> execShell("Ensuring directories (${paths.count()})", command) + } + } + + fun cleanDir(vararg paths: String) = cleanDir(paths.asIterable()) + + fun cleanDir(paths: Iterable) { + val command = "rm -fr ${paths.joinToString(" ") { "$it/*" }}" + when (paths.count()) { + 1 -> execShell("Cleaning contents of directory at path '${paths.first()}'", command) + else -> execShell("Cleaning contents of directories (${paths.count()})", command) + } } private fun exec(spec: ExecSpec): DockerResult { diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/ContainerManager.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/ContainerManager.kt index 604daa8..ec5e1cf 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/ContainerManager.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/ContainerManager.kt @@ -6,6 +6,11 @@ class ContainerManager(private val docker: Docker) { val defined = mutableListOf() + val dependent = common.obj.boolean { + convention(true) + common.prop.boolean("environment.docker.container.dependent")?.let { set(it) } + } + /** * Define container. */ @@ -51,14 +56,22 @@ class ContainerManager(private val docker: Docker) { fun up() { common.progress { message = "Configuring container(s): ${defined.names}" - common.parallel.each(defined) { it.up() } + if (dependent.get()) { + defined.forEach { it.up() } + } else { + common.parallel.each(defined) { it.up() } + } } } fun reload() { common.progress { message = "Reloading container(s): ${defined.names}" - common.parallel.each(defined) { it.reload() } + if (dependent.get()) { + defined.forEach { it.reload() } + } else { + common.parallel.each(defined) { it.reload() } + } } } } 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 46c959e..0e772a6 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt @@ -43,6 +43,8 @@ class Docker(val environment: EnvironmentExtension) { val composeTemplateFile = common.obj.relativeFile(environment.sourceDir, "docker-compose.yml.peb") + val composeProperties = common.obj.map { convention(mapOf()) } + // Shorthands useful to be used in template: 'docker-compose.yml.peb' val configPath get() = runtime.determinePath(environment.sourceDir.get().asFile) @@ -68,7 +70,10 @@ class Docker(val environment: EnvironmentExtension) { targetFile.takeIf { it.exists() }?.delete() templateFile.copyTo(targetFile) - common.prop.expand(targetFile, mapOf("docker" to this)) + common.prop.expand(targetFile, composeProperties.get() + mapOf( + "docker" to this, + "project" to common.project + )) } fun up() { diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/container/HostFileManager.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/container/HostFileManager.kt index 5a0e178..0740da5 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/container/HostFileManager.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/container/HostFileManager.kt @@ -37,6 +37,16 @@ class HostFileManager(val container: Container) { return files } + fun ensureFile(vararg paths: String, content: String = "") = paths.forEach { path -> + ensureDir(path.substringBeforeLast("/")) + file(path).apply { + if (!exists()) { + logger.info("Ensuring file '$this' for container '${container.name}'") + writeText(content) + } + } + } + fun ensureDir() { rootDir.get().asFile.apply { logger.info("Ensuring root directory '$this' for container '${container.name}'") @@ -44,10 +54,12 @@ class HostFileManager(val container: Container) { } } - fun ensureDir(vararg paths: String) = paths.forEach { path -> - file(path).apply { - logger.info("Ensuring directory '$this' for container '${container.name}'") - mkdirs() + fun ensureDir(vararg paths: String) { + paths.forEach { path -> + file(path).apply { + logger.info("Ensuring directory '$this' for container '${container.name}'") + mkdirs() + } } } diff --git a/src/main/kotlin/com/cognifide/gradle/environment/hosts/Host.kt b/src/main/kotlin/com/cognifide/gradle/environment/hosts/Host.kt index bd9602e..3946645 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/hosts/Host.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/hosts/Host.kt @@ -29,4 +29,6 @@ class Host(val url: String) { } fun tag(vararg ids: String) = tag(ids.asIterable()) + + override fun toString(): String = "Host(url='$url', ip='$ip', tags=$tags)" } diff --git a/src/main/kotlin/com/cognifide/gradle/environment/hosts/HostOptions.kt b/src/main/kotlin/com/cognifide/gradle/environment/hosts/HostOptions.kt index 1f43db5..38a82e7 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/hosts/HostOptions.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/hosts/HostOptions.kt @@ -35,6 +35,8 @@ class HostOptions(environment: EnvironmentExtension) : Serializable { operator fun String.invoke(options: Host.() -> Unit = {}) = define(this, options) + operator fun String.invoke(vararg tags: String) = define(this) { tag(tags.asIterable()) } + fun define(url: String, options: Host.() -> Unit = {}) { defined.add(common.obj.provider { Host(url).apply { ip = ipDefault.get(); options() } }) }