From 210335eed24d464a7a97082620c554cbfad86aff Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Sat, 4 May 2019 16:19:33 -0400 Subject: [PATCH 1/2] Add tasks to build Docker build context artifacts This commit adds some tasks to generate dedicated Docker build context artifacts. --- distribution/docker/build.gradle | 75 +++++++++---------- .../docker/docker-build-context/build.gradle | 3 + .../oss-docker-build-context/build.gradle | 3 + settings.gradle | 2 + 4 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 distribution/docker/docker-build-context/build.gradle create mode 100644 distribution/docker/oss-docker-build-context/build.gradle diff --git a/distribution/docker/build.gradle b/distribution/docker/build.gradle index 56ad936bdfe5d..35a10054a1c56 100644 --- a/distribution/docker/build.gradle +++ b/distribution/docker/build.gradle @@ -17,34 +17,17 @@ dependencies { ossDockerSource project(path: ":distribution:archives:oss-linux-tar") } -ext.expansions = { oss -> +ext.expansions = { oss, 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 [ 'elasticsearch' : elasticsearch, 'license' : oss ? 'Apache-2.0' : 'Elastic License', - 'source_elasticsearch': local() ? "COPY $elasticsearch /opt/" : "RUN cd /opt && curl --retry 8 -s -L -O https://artifacts.elastic.co/downloads/elasticsearch/${elasticsearch} && cd -", + '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 ] } -/* - * We need to be able to render a Dockerfile that references the official artifacts on https://artifacts.elastic.co. For this, we use a - * substitution in the Dockerfile template where we can either replace source_elasticsearch with a COPY from the Docker build context, or - * a RUN curl command to retrieve the artifact from https://artifacts.elastic.co. The system property build.docker.source, which can be - * either "local" (default) or "remote" controls which version of the Dockerfile is produced. - */ -private static boolean local() { - final String buildDockerSource = System.getProperty("build.docker.source") - if (buildDockerSource == null || "local".equals(buildDockerSource)) { - return true - } else if ("remote".equals(buildDockerSource)) { - return false - } else { - throw new IllegalArgumentException("expected build.docker.source to be [local] or [remote] but was [" + buildDockerSource + "]") - } -} - private static String files(final boolean oss) { return "build/${ oss ? 'oss-' : ''}docker" } @@ -53,10 +36,8 @@ private static String taskName(final String prefix, final boolean oss, final Str return "${prefix}${oss ? 'Oss' : ''}${suffix}" } -void addCopyDockerContextTask(final boolean oss) { - task(taskName("copy", oss, "DockerContext"), type: Sync) { - into files(oss) - +CopySpec dockerBuildContext(final boolean oss, final boolean local) { + copySpec { into('bin') { from 'src/docker/bin' } @@ -65,27 +46,26 @@ void addCopyDockerContextTask(final boolean oss) { from 'src/docker/config' } - if (local()) { - if (oss) { - from configurations.ossDockerSource - } else { - from configurations.dockerSource - } - - from configurations.dockerPlugins + from('src/docker/Dockerfile') { + MavenFilteringHack.filter(it, expansions(oss, local)) } } } -void addCopyDockerfileTask(final boolean oss) { - task(taskName("copy", oss, "Dockerfile"), type: Copy) { - dependsOn taskName("copy", oss, "DockerContext") - inputs.properties(expansions(oss)) // ensure task is run when ext.expansions is changed +void addCopyDockerContextTask(final boolean oss) { + task(taskName("copy", oss, "DockerContext"), type: Sync) { + inputs.properties(expansions(oss, true)) into files(oss) - from('src/docker/Dockerfile') { - MavenFilteringHack.filter(it, expansions(oss)) + with dockerBuildContext(oss, true) + + if (oss) { + from configurations.ossDockerSource + } else { + from configurations.dockerSource } + + from configurations.dockerPlugins } } @@ -104,7 +84,6 @@ check.dependsOn postProcessFixture void addBuildDockerImage(final boolean oss) { final Task buildDockerImageTask = task(taskName("build", oss, "DockerImage"), type: LoggedExec) { dependsOn taskName("copy", oss, "DockerContext") - dependsOn taskName("copy", oss, "Dockerfile") List tags if (oss) { tags = [ @@ -130,9 +109,27 @@ void addBuildDockerImage(final boolean oss) { BuildPlugin.requireDocker(buildDockerImageTask) } +Closure commonDockerBuildContextConfig(final boolean oss) { + return { + extension = 'tar.gz' + compression = Compression.GZIP + archiveClassifier = "docker-build-context" + destinationDir = file("${oss ? "oss-" : ""}docker-build-context/build/distributions") + archiveBaseName = "elasticsearch${oss ? "-oss" : ""}" + with dockerBuildContext(oss, false) + } +} + +task buildDockerBuildContext(type: Tar) { + configure(commonDockerBuildContextConfig(false)) +} + +task buildOssDockerBuildContext(type: Tar) { + configure(commonDockerBuildContextConfig(true)) +} + for (final boolean oss : [false, true]) { addCopyDockerContextTask(oss) - addCopyDockerfileTask(oss) addBuildDockerImage(oss) } diff --git a/distribution/docker/docker-build-context/build.gradle b/distribution/docker/docker-build-context/build.gradle new file mode 100644 index 0000000000000..e424c4b95dce1 --- /dev/null +++ b/distribution/docker/docker-build-context/build.gradle @@ -0,0 +1,3 @@ +apply plugin: 'base' + +assemble.dependsOn ":distribution:docker:buildDockerBuildContext" diff --git a/distribution/docker/oss-docker-build-context/build.gradle b/distribution/docker/oss-docker-build-context/build.gradle new file mode 100644 index 0000000000000..064dad80e9713 --- /dev/null +++ b/distribution/docker/oss-docker-build-context/build.gradle @@ -0,0 +1,3 @@ +apply plugin: 'base' + +assemble.dependsOn ":distribution:docker:buildOssDockerBuildContext" diff --git a/settings.gradle b/settings.gradle index 76fab9a657648..4e6c59116e9ab 100644 --- a/settings.gradle +++ b/settings.gradle @@ -29,6 +29,8 @@ List projects = [ 'distribution:archives:oss-no-jdk-linux-tar', 'distribution:archives:no-jdk-linux-tar', 'distribution:docker', + 'distribution:docker:oss-docker-build-context', + 'distribution:docker:docker-build-context', 'distribution:packages:oss-deb', 'distribution:packages:deb', 'distribution:packages:oss-no-jdk-deb', From 79fc5522fe563ff3c2422b66c18b40b05d07711e Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 6 May 2019 17:59:02 -0400 Subject: [PATCH 2/2] Iteration --- distribution/docker/build.gradle | 45 ++++++------------- .../docker/docker-build-context/build.gradle | 10 ++++- .../oss-docker-build-context/build.gradle | 10 ++++- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/distribution/docker/build.gradle b/distribution/docker/build.gradle index 35a10054a1c56..ab55c737783e5 100644 --- a/distribution/docker/build.gradle +++ b/distribution/docker/build.gradle @@ -36,18 +36,20 @@ private static String taskName(final String prefix, final boolean oss, final Str return "${prefix}${oss ? 'Oss' : ''}${suffix}" } -CopySpec dockerBuildContext(final boolean oss, final boolean local) { - copySpec { - into('bin') { - from 'src/docker/bin' - } - - into('config') { - from 'src/docker/config' - } - - from('src/docker/Dockerfile') { - MavenFilteringHack.filter(it, expansions(oss, local)) +project.ext { + dockerBuildContext = { boolean oss, boolean local -> + copySpec { + into('bin') { + from project.projectDir.toPath().resolve("src/docker/bin") + } + + into('config') { + from project.projectDir.toPath().resolve("src/docker/config") + } + + from(project.projectDir.toPath().resolve("src/docker/Dockerfile")) { + MavenFilteringHack.filter(it, expansions(oss, local)) + } } } } @@ -109,25 +111,6 @@ void addBuildDockerImage(final boolean oss) { BuildPlugin.requireDocker(buildDockerImageTask) } -Closure commonDockerBuildContextConfig(final boolean oss) { - return { - extension = 'tar.gz' - compression = Compression.GZIP - archiveClassifier = "docker-build-context" - destinationDir = file("${oss ? "oss-" : ""}docker-build-context/build/distributions") - archiveBaseName = "elasticsearch${oss ? "-oss" : ""}" - with dockerBuildContext(oss, false) - } -} - -task buildDockerBuildContext(type: Tar) { - configure(commonDockerBuildContextConfig(false)) -} - -task buildOssDockerBuildContext(type: Tar) { - configure(commonDockerBuildContextConfig(true)) -} - for (final boolean oss : [false, true]) { addCopyDockerContextTask(oss) addBuildDockerImage(oss) diff --git a/distribution/docker/docker-build-context/build.gradle b/distribution/docker/docker-build-context/build.gradle index e424c4b95dce1..254407093ce82 100644 --- a/distribution/docker/docker-build-context/build.gradle +++ b/distribution/docker/docker-build-context/build.gradle @@ -1,3 +1,11 @@ apply plugin: 'base' -assemble.dependsOn ":distribution:docker:buildDockerBuildContext" +task buildDockerBuildContext(type: Tar) { + extension = 'tar.gz' + compression = Compression.GZIP + archiveClassifier = "docker-build-context" + archiveBaseName = "elasticsearch" + with dockerBuildContext(false, false) +} + +assemble.dependsOn buildDockerBuildContext diff --git a/distribution/docker/oss-docker-build-context/build.gradle b/distribution/docker/oss-docker-build-context/build.gradle index 064dad80e9713..248b260daa94a 100644 --- a/distribution/docker/oss-docker-build-context/build.gradle +++ b/distribution/docker/oss-docker-build-context/build.gradle @@ -1,3 +1,11 @@ apply plugin: 'base' -assemble.dependsOn ":distribution:docker:buildOssDockerBuildContext" +task buildOssDockerBuildContext(type: Tar) { + extension = 'tar.gz' + compression = Compression.GZIP + archiveClassifier = "docker-build-context" + archiveBaseName = "elasticsearch-oss" + with dockerBuildContext(true, false) +} + +assemble.dependsOn buildOssDockerBuildContext