seednode: Create dynamically slim docker image

This commit is contained in:
Alva Swanson 2023-09-28 15:54:02 +02:00
parent ed7587bc22
commit c2875169c1
No known key found for this signature in database
GPG Key ID: 004760E77F753090
7 changed files with 121 additions and 0 deletions

View File

@ -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'
}
}
}

View File

@ -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<String>
@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()
}
}
}
}

View File

@ -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<File>
@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.")
}
}
}

View File

@ -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<Project> {
override fun apply(project: Project) {
val distTarTask: TaskProvider<Tar> = project.tasks.named("distTar", Tar::class.java)
val copyTask = project.tasks.register<Copy>("copyDistTar") {
from(distTarTask.flatMap { it.archiveFile })
into(project.layout.buildDirectory.dir("docker"))
}
project.tasks.register<CreateDockerfileTask>("generateDockerfile") {
archiveFileName.set(distTarTask.flatMap { it.archiveFileName })
outputFile.set(project.layout.buildDirectory.file("docker/Dockerfile"))
}
project.tasks.register<DockerBuildTask>("dockerImage") {
dockerDirectory.set(copyTask.map { it.destinationDir })
}
}
}

View File

@ -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"]

View File

@ -8,6 +8,7 @@ dependencyResolutionManagement {
include 'app-start-plugin' include 'app-start-plugin'
include 'commons' include 'commons'
include 'docker-image-builder'
include 'gradle-tasks' include 'gradle-tasks'
include 'packaging' include 'packaging'
include 'regtest' include 'regtest'

View File

@ -1,6 +1,7 @@
plugins { plugins {
id 'bisq.application' id 'bisq.application'
id 'bisq.gradle.app_start_plugin.AppStartPlugin' id 'bisq.gradle.app_start_plugin.AppStartPlugin'
id 'bisq.gradle.docker.image_builder.DockerImageBuilderPlugin'
} }
distTar.enabled = true distTar.enabled = true