OpenSearch/distribution/docker/build.gradle

235 lines
7.6 KiB
Groovy

import org.elasticsearch.gradle.BuildPlugin
import org.elasticsearch.gradle.LoggedExec
import org.elasticsearch.gradle.VersionProperties
import org.elasticsearch.gradle.info.BuildParams
import org.elasticsearch.gradle.testfixtures.TestFixturesPlugin
apply plugin: 'elasticsearch.standalone-rest-test'
apply plugin: 'elasticsearch.test.fixtures'
testFixtures.useFixture()
configurations {
dockerPlugins
dockerSource
ossDockerSource
restSpec
}
dependencies {
dockerSource project(path: ":distribution:archives:linux-tar")
ossDockerSource project(path: ":distribution:archives:oss-linux-tar")
restSpec project(':rest-api-spec')
}
ext.expansions = { oss, ubi, local ->
final String classifier = 'linux-x86_64'
final String elasticsearch = oss ? "elasticsearch-oss-${VersionProperties.elasticsearch}-${classifier}.tar.gz" : "elasticsearch-${VersionProperties.elasticsearch}-${classifier}.tar.gz"
return [
'base_image' : ubi ? 'registry.access.redhat.com/ubi7/ubi-minimal:7.7' : 'centos:7',
'build_date' : BuildParams.buildDate,
'elasticsearch' : elasticsearch,
'git_revision' : BuildParams.gitRevision,
'license' : oss ? 'Apache-2.0' : 'Elastic-License',
'package_manager' : ubi ? 'microdnf' : 'yum',
'source_elasticsearch': local ? "COPY $elasticsearch /opt/" : "RUN cd /opt && curl --retry 8 -s -L -O https://artifacts.elastic.co/downloads/elasticsearch/${elasticsearch} && cd -",
'version' : VersionProperties.elasticsearch
]
}
private static String buildPath(final boolean oss, final boolean ubi) {
return "build/${oss ? 'oss-' : ''}${ubi ? 'ubi-' : ''}docker"
}
private static String taskName(final String prefix, final boolean oss, final boolean ubi, final String suffix) {
return "${prefix}${oss ? 'Oss' : ''}${ubi ? 'Ubi' : ''}${suffix}"
}
project.ext {
dockerBuildContext = { boolean oss, boolean ubi, boolean local ->
copySpec {
into('bin') {
from project.projectDir.toPath().resolve("src/docker/bin")
}
into('config') {
/*
* Oss and default distribution can have different configuration, therefore we want to allow overriding the default configuration
* by creating config files in oss or default build-context sub-modules.
*/
from project.projectDir.toPath().resolve("src/docker/config")
if (oss) {
from project.projectDir.toPath().resolve("src/docker/config/oss")
}
}
from(project.projectDir.toPath().resolve("src/docker/Dockerfile")) {
expand(expansions(oss, ubi, local))
}
}
}
}
void addCopyDockerContextTask(final boolean oss, final boolean ubi) {
task(taskName("copy", oss, ubi, "DockerContext"), type: Sync) {
expansions(oss, ubi, true).findAll { it.key != 'build_date' }.each { k, v ->
inputs.property(k, { v.toString() })
}
into buildPath(oss, ubi)
with dockerBuildContext(oss, ubi, true)
if (oss) {
from configurations.ossDockerSource
} else {
from configurations.dockerSource
}
from configurations.dockerPlugins
}
}
def createAndSetWritable(Object... locations) {
locations.each { location ->
File file = file(location)
file.mkdirs()
file.setWritable(true, false)
}
}
task copyKeystore(type: Sync) {
from project(':x-pack:plugin:core')
.file('src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks')
into "${buildDir}/certs"
doLast {
file("${buildDir}/certs").setReadable(true, false)
file("${buildDir}/certs/testnode.jks").setReadable(true, false)
}
}
preProcessFixture {
if (TestFixturesPlugin.dockerComposeSupported()) {
dependsOn assemble
}
dependsOn copyKeystore
doLast {
// tests expect to have an empty repo
project.delete(
"${buildDir}/repo",
"${buildDir}/oss-repo"
)
createAndSetWritable(
"${buildDir}/repo",
"${buildDir}/oss-repo",
"${buildDir}/logs/default-1",
"${buildDir}/logs/default-2",
"${buildDir}/logs/oss-1",
"${buildDir}/logs/oss-2"
)
}
}
processTestResources {
from({ zipTree(configurations.restSpec.singleFile) }) {
include 'rest-api-spec/api/**'
}
from project(':x-pack:plugin:core')
.file('src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks')
dependsOn configurations.restSpec
}
task integTest(type: Test) {
maxParallelForks = '1'
include '**/*IT.class'
// don't add the tasks to build the docker images if we have no way of testing them
if (TestFixturesPlugin.dockerComposeSupported()) {
dependsOn assemble
}
}
check.dependsOn integTest
void addBuildDockerImage(final boolean oss, final boolean ubi) {
final Task buildDockerImageTask = task(taskName("build", oss, ubi, "DockerImage"), type: LoggedExec) {
dependsOn taskName("copy", oss, ubi, "DockerContext")
List<String> tags
if (oss) {
tags = [
"docker.elastic.co/elasticsearch/elasticsearch-oss${ubi ? '-ubi7' : ''}:${VersionProperties.elasticsearch}",
"elasticsearch-oss${ubi ? '-ubi7' : ''}:test"
]
} else {
tags = [
"elasticsearch${ubi ? '-ubi7' : ''}:${VersionProperties.elasticsearch}",
"docker.elastic.co/elasticsearch/elasticsearch${ubi ? '-ubi7' : ''}:${VersionProperties.elasticsearch}",
"docker.elastic.co/elasticsearch/elasticsearch${ubi ? '-ubi7' : ''}-full:${VersionProperties.elasticsearch}",
"elasticsearch${ubi ? '-ubi7' : ''}:test",
]
}
executable 'docker'
final List<String> dockerArgs = ['build', buildPath(oss, ubi), '--pull', '--no-cache']
for (final String tag : tags) {
dockerArgs.add('--tag')
dockerArgs.add(tag)
}
args dockerArgs.toArray()
File markerFile = file("build/markers/${it.name}.marker")
outputs.file(markerFile)
doLast {
markerFile.setText('', 'UTF-8')
}
}
assemble.dependsOn(buildDockerImageTask)
BuildPlugin.requireDocker(buildDockerImageTask)
}
for (final boolean oss : [false, true]) {
for (final boolean ubi : [false, true]) {
addCopyDockerContextTask(oss, ubi)
addBuildDockerImage(oss, ubi)
}
}
// We build the images used in compose locally, but the pull command insists on using a repository
// thus we must disable it to prevent it from doing so.
// Everything will still be pulled since we will build the local images on a pull
if (tasks.findByName("composePull")) {
tasks.composePull.enabled = false
}
/*
* The export subprojects write out the generated Docker images to disk, so
* that they can be easily reloaded, for example into a VM.
*/
subprojects { Project subProject ->
if (subProject.name.contains('docker-export')) {
apply plugin: 'distribution'
final boolean oss = subProject.name.contains('oss-')
final boolean ubi = subProject.name.contains('ubi-')
def exportTaskName = taskName("export", oss, ubi, "DockerImage")
def buildTaskName = taskName("build", oss, ubi, "DockerImage")
def tarFile = "${parent.projectDir}/build/elasticsearch${oss ? '-oss' : ''}${ubi ? '-ubi7' : ''}_test.${VersionProperties.elasticsearch}.docker.tar"
final Task exportDockerImageTask = task(exportTaskName, type: LoggedExec) {
executable 'docker'
outputs.file(tarFile)
args "save",
"-o",
tarFile,
"elasticsearch${oss ? '-oss' : ''}${ubi ? '-ubi7' : ''}:test"
}
exportDockerImageTask.dependsOn(parent.tasks.getByName(buildTaskName))
artifacts.add('default', file(tarFile)) {
type 'tar'
name "elasticsearch${oss ? '-oss' : ''}${ubi ? '-ubi7' : ''}"
builtBy exportTaskName
}
assemble.dependsOn exportTaskName
}
}