From c2875169c110883a736f39059ef3ff8b08269543 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Thu, 28 Sep 2023 15:54:02 +0200 Subject: [PATCH] seednode: Create dynamically slim docker image --- build-logic/docker-image-builder/build.gradle | 17 +++++++++ .../image_builder/CreateDockerfileTask.kt | 37 +++++++++++++++++++ .../docker/image_builder/DockerBuildTask.kt | 31 ++++++++++++++++ .../image_builder/DockerImageBuilderPlugin.kt | 28 ++++++++++++++ .../src/main/resources/Dockerfile | 6 +++ build-logic/settings.gradle | 1 + seednode/build.gradle | 1 + 7 files changed, 121 insertions(+) create mode 100644 build-logic/docker-image-builder/build.gradle create mode 100644 build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/CreateDockerfileTask.kt create mode 100644 build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/DockerBuildTask.kt create mode 100644 build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/DockerImageBuilderPlugin.kt create mode 100644 build-logic/docker-image-builder/src/main/resources/Dockerfile diff --git a/build-logic/docker-image-builder/build.gradle b/build-logic/docker-image-builder/build.gradle new file mode 100644 index 0000000000..217492b4cc --- /dev/null +++ b/build-logic/docker-image-builder/build.gradle @@ -0,0 +1,17 @@ +plugins { + id 'org.jetbrains.kotlin.jvm' version '1.7.10' + id 'org.gradle.kotlin.kotlin-dsl' version '2.4.1' +} + +repositories { + mavenCentral() +} + +gradlePlugin { + plugins { + simplePlugin { + id = 'bisq.gradle.docker.image_builder.DockerImageBuilderPlugin' + implementationClass = 'bisq.gradle.docker.image_builder.DockerImageBuilderPlugin' + } + } +} diff --git a/build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/CreateDockerfileTask.kt b/build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/CreateDockerfileTask.kt new file mode 100644 index 0000000000..3ade1ff74a --- /dev/null +++ b/build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/CreateDockerfileTask.kt @@ -0,0 +1,37 @@ +package bisq.gradle.docker.image_builder + +import org.gradle.api.DefaultTask +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.TaskAction + +abstract class CreateDockerfileTask : DefaultTask() { + + @get:Input + abstract val archiveFileName: Property + + @get:OutputFile + abstract val outputFile: RegularFileProperty + + @TaskAction + fun create() { + val dockerFileContent: String = readDockerFileTemplate() + .replace("'{{ ARCHIVE_PATH }}'", archiveFileName.get()) + + outputFile.asFile.get() + .writeText(dockerFileContent) + } + + private fun readDockerFileTemplate(): String { + this.javaClass.getResourceAsStream("/Dockerfile") + .use { inputStream -> + inputStream!!.bufferedReader() + .use { bufferedReader -> + return bufferedReader.readText() + } + } + } + +} diff --git a/build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/DockerBuildTask.kt b/build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/DockerBuildTask.kt new file mode 100644 index 0000000000..549faa2a8c --- /dev/null +++ b/build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/DockerBuildTask.kt @@ -0,0 +1,31 @@ +package bisq.gradle.docker.image_builder + +import org.gradle.api.DefaultTask +import org.gradle.api.provider.Property +import org.gradle.api.tasks.InputDirectory +import org.gradle.api.tasks.TaskAction +import java.io.File +import java.util.concurrent.TimeUnit + +abstract class DockerBuildTask : DefaultTask() { + + @get:InputDirectory + abstract val dockerDirectory: Property + + @TaskAction + fun build() { + val processBuilder = ProcessBuilder( + "docker", "build", + "--tag", "bisq/seednode:latest", + dockerDirectory.get().absolutePath + ) + + processBuilder.redirectErrorStream(true) + val process = processBuilder.start() + + val isSuccess = process.waitFor(2, TimeUnit.MINUTES) && process.exitValue() == 0 + if (!isSuccess) { + throw IllegalStateException("Couldn't build docker image.") + } + } +} diff --git a/build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/DockerImageBuilderPlugin.kt b/build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/DockerImageBuilderPlugin.kt new file mode 100644 index 0000000000..5c29b01fc2 --- /dev/null +++ b/build-logic/docker-image-builder/src/main/java/bisq/gradle/docker/image_builder/DockerImageBuilderPlugin.kt @@ -0,0 +1,28 @@ +package bisq.gradle.docker.image_builder + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.tasks.Copy +import org.gradle.api.tasks.TaskProvider +import org.gradle.api.tasks.bundling.Tar +import org.gradle.kotlin.dsl.register + +class DockerImageBuilderPlugin : Plugin { + override fun apply(project: Project) { + val distTarTask: TaskProvider = project.tasks.named("distTar", Tar::class.java) + + val copyTask = project.tasks.register("copyDistTar") { + from(distTarTask.flatMap { it.archiveFile }) + into(project.layout.buildDirectory.dir("docker")) + } + + project.tasks.register("generateDockerfile") { + archiveFileName.set(distTarTask.flatMap { it.archiveFileName }) + outputFile.set(project.layout.buildDirectory.file("docker/Dockerfile")) + } + + project.tasks.register("dockerImage") { + dockerDirectory.set(copyTask.map { it.destinationDir }) + } + } +} diff --git a/build-logic/docker-image-builder/src/main/resources/Dockerfile b/build-logic/docker-image-builder/src/main/resources/Dockerfile new file mode 100644 index 0000000000..7676ac84e4 --- /dev/null +++ b/build-logic/docker-image-builder/src/main/resources/Dockerfile @@ -0,0 +1,6 @@ +FROM azul/zulu-openjdk:11.0.20-11.66.15 + +COPY '{{ ARCHIVE_PATH }}' / +RUN tar xvf /seednode.tar + +ENTRYPOINT ["bash", "/seednode/bin/seednode"] diff --git a/build-logic/settings.gradle b/build-logic/settings.gradle index f4fa9e7a54..089b13337e 100644 --- a/build-logic/settings.gradle +++ b/build-logic/settings.gradle @@ -8,6 +8,7 @@ dependencyResolutionManagement { include 'app-start-plugin' include 'commons' +include 'docker-image-builder' include 'gradle-tasks' include 'packaging' include 'regtest' diff --git a/seednode/build.gradle b/seednode/build.gradle index 638ae668a6..1c5517593b 100644 --- a/seednode/build.gradle +++ b/seednode/build.gradle @@ -1,6 +1,7 @@ plugins { id 'bisq.application' id 'bisq.gradle.app_start_plugin.AppStartPlugin' + id 'bisq.gradle.docker.image_builder.DockerImageBuilderPlugin' } distTar.enabled = true