Skip to content

Commit 21eea3b

Browse files
committed
[Backport] Consolidate docker availability logic (elastic#52656)
1 parent 8563bec commit 21eea3b

File tree

8 files changed

+90
-35
lines changed

8 files changed

+90
-35
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import static org.elasticsearch.gradle.tool.Boilerplate.maybeConfigure
3434

3535
plugins {
3636
id 'lifecycle-base'
37+
id 'elasticsearch.docker-support'
3738
id 'elasticsearch.global-build-info'
3839
id "com.diffplug.gradle.spotless" version "3.24.2" apply false
3940
}

buildSrc/src/main/java/org/elasticsearch/gradle/DistributionDownloadPlugin.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
import org.elasticsearch.gradle.ElasticsearchDistribution.Flavor;
2323
import org.elasticsearch.gradle.ElasticsearchDistribution.Platform;
2424
import org.elasticsearch.gradle.ElasticsearchDistribution.Type;
25+
import org.elasticsearch.gradle.docker.DockerSupportPlugin;
26+
import org.elasticsearch.gradle.docker.DockerSupportService;
2527
import org.elasticsearch.gradle.info.BuildParams;
2628
import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin;
29+
import org.elasticsearch.gradle.tool.Boilerplate;
2730
import org.gradle.api.GradleException;
2831
import org.gradle.api.NamedDomainObjectContainer;
2932
import org.gradle.api.Plugin;
@@ -38,6 +41,7 @@
3841
import org.gradle.api.file.FileTree;
3942
import org.gradle.api.file.RelativePath;
4043
import org.gradle.api.plugins.ExtraPropertiesExtension;
44+
import org.gradle.api.provider.Provider;
4145
import org.gradle.api.tasks.Sync;
4246
import org.gradle.api.tasks.TaskProvider;
4347
import org.gradle.authentication.http.HttpHeaderAuthentication;
@@ -72,11 +76,17 @@ public class DistributionDownloadPlugin implements Plugin<Project> {
7276
public void apply(Project project) {
7377
// this is needed for isInternal
7478
project.getRootProject().getPluginManager().apply(GlobalBuildInfoPlugin.class);
79+
project.getRootProject().getPluginManager().apply(DockerSupportPlugin.class);
80+
81+
Provider<DockerSupportService> dockerSupport = Boilerplate.getBuildService(
82+
project.getGradle().getSharedServices(),
83+
DockerSupportPlugin.DOCKER_SUPPORT_SERVICE_NAME
84+
);
7585

7686
distributionsContainer = project.container(ElasticsearchDistribution.class, name -> {
7787
Configuration fileConfiguration = project.getConfigurations().create("es_distro_file_" + name);
7888
Configuration extractedConfiguration = project.getConfigurations().create("es_distro_extracted_" + name);
79-
return new ElasticsearchDistribution(name, project.getObjects(), fileConfiguration, extractedConfiguration);
89+
return new ElasticsearchDistribution(name, project.getObjects(), dockerSupport, fileConfiguration, extractedConfiguration);
8090
});
8191
project.getExtensions().add(CONTAINER_NAME, distributionsContainer);
8292

buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchDistribution.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919

2020
package org.elasticsearch.gradle;
2121

22+
import org.elasticsearch.gradle.docker.DockerSupportService;
2223
import org.gradle.api.Buildable;
2324
import org.gradle.api.artifacts.Configuration;
2425
import org.gradle.api.model.ObjectFactory;
2526
import org.gradle.api.provider.Property;
27+
import org.gradle.api.provider.Provider;
2628
import org.gradle.api.tasks.TaskDependency;
2729

2830
import java.io.File;
31+
import java.util.Collections;
2932
import java.util.Iterator;
3033
import java.util.Locale;
3134

@@ -110,6 +113,7 @@ public String toString() {
110113
}
111114

112115
private final String name;
116+
private final Provider<DockerSupportService> dockerSupport;
113117
// pkg private so plugin can configure
114118
final Configuration configuration;
115119
private final Extracted extracted;
@@ -119,21 +123,25 @@ public String toString() {
119123
private final Property<Platform> platform;
120124
private final Property<Flavor> flavor;
121125
private final Property<Boolean> bundledJdk;
126+
private final Property<Boolean> failIfUnavailable;
122127

123128
ElasticsearchDistribution(
124129
String name,
125130
ObjectFactory objectFactory,
131+
Provider<DockerSupportService> dockerSupport,
126132
Configuration fileConfiguration,
127133
Configuration extractedConfiguration
128134
) {
129135
this.name = name;
136+
this.dockerSupport = dockerSupport;
130137
this.configuration = fileConfiguration;
131138
this.version = objectFactory.property(String.class).convention(VersionProperties.getElasticsearch());
132139
this.type = objectFactory.property(Type.class);
133140
this.type.convention(Type.ARCHIVE);
134141
this.platform = objectFactory.property(Platform.class);
135142
this.flavor = objectFactory.property(Flavor.class);
136143
this.bundledJdk = objectFactory.property(Boolean.class);
144+
this.failIfUnavailable = objectFactory.property(Boolean.class).convention(true);
137145
this.extracted = new Extracted(extractedConfiguration);
138146
}
139147

@@ -182,6 +190,14 @@ public void setBundledJdk(Boolean bundledJdk) {
182190
this.bundledJdk.set(bundledJdk);
183191
}
184192

193+
public boolean getFailIfUnavailable() {
194+
return this.failIfUnavailable.get();
195+
}
196+
197+
public void setFailIfUnavailable(boolean failIfUnavailable) {
198+
this.failIfUnavailable.set(failIfUnavailable);
199+
}
200+
185201
@Override
186202
public String toString() {
187203
return configuration.getSingleFile().toString();
@@ -203,6 +219,13 @@ public Extracted getExtracted() {
203219

204220
@Override
205221
public TaskDependency getBuildDependencies() {
222+
// For non-required Docker distributions, skip building the distribution is Docker is unavailable
223+
if (getType() == Type.DOCKER
224+
&& getFailIfUnavailable() == false
225+
&& dockerSupport.get().getDockerAvailability().isAvailable == false) {
226+
return task -> Collections.emptySet();
227+
}
228+
206229
return configuration.getBuildDependencies();
207230
}
208231

@@ -222,7 +245,7 @@ void finalizeValues() {
222245
if (getType() == Type.INTEG_TEST_ZIP) {
223246
if (platform.getOrNull() != null) {
224247
throw new IllegalArgumentException(
225-
"platform not allowed for elasticsearch distribution [" + name + "] of type [integ_test_zip]"
248+
"platform cannot be set on elasticsearch distribution [" + name + "] of type [integ_test_zip]"
226249
);
227250
}
228251
if (flavor.getOrNull() != null) {
@@ -232,12 +255,18 @@ void finalizeValues() {
232255
}
233256
if (bundledJdk.getOrNull() != null) {
234257
throw new IllegalArgumentException(
235-
"bundledJdk not allowed for elasticsearch distribution [" + name + "] of type [integ_test_zip]"
258+
"bundledJdk cannot be set on elasticsearch distribution [" + name + "] of type [integ_test_zip]"
236259
);
237260
}
238261
return;
239262
}
240263

264+
if (getType() != Type.DOCKER && failIfUnavailable.get() == false) {
265+
throw new IllegalArgumentException(
266+
"failIfUnavailable cannot be 'false' on elasticsearch distribution [" + name + "] of type [" + getType() + "]"
267+
);
268+
}
269+
241270
if (getType() == Type.ARCHIVE) {
242271
// defaults for archive, set here instead of via convention so integ-test-zip can verify they are not set
243272
if (platform.isPresent() == false) {
@@ -246,7 +275,12 @@ void finalizeValues() {
246275
} else { // rpm, deb or docker
247276
if (platform.isPresent()) {
248277
throw new IllegalArgumentException(
249-
"platform not allowed for elasticsearch distribution [" + name + "] of type [" + getType() + "]"
278+
"platform cannot be set on elasticsearch distribution [" + name + "] of type [" + getType() + "]"
279+
);
280+
}
281+
if (getType() == Type.DOCKER && bundledJdk.isPresent()) {
282+
throw new IllegalArgumentException(
283+
"bundledJdk cannot be set on elasticsearch distribution [" + name + "] of type [docker]"
250284
);
251285
}
252286
}

buildSrc/src/main/java/org/elasticsearch/gradle/testclusters/TestClustersPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
import java.io.File;
4141

42+
import static org.elasticsearch.gradle.tool.Boilerplate.noop;
43+
4244
public class TestClustersPlugin implements Plugin<Project> {
4345

4446
public static final String EXTENSION_NAME = "testClusters";
@@ -72,7 +74,7 @@ public void apply(Project project) {
7274
createListClustersTask(project, container);
7375

7476
// register cluster registry as a global build service
75-
project.getGradle().getSharedServices().registerIfAbsent(REGISTRY_SERVICE_NAME, TestClustersRegistry.class, spec -> {});
77+
project.getGradle().getSharedServices().registerIfAbsent(REGISTRY_SERVICE_NAME, TestClustersRegistry.class, noop());
7678

7779
// register throttle so we only run at most max-workers/2 nodes concurrently
7880
project.getGradle()

buildSrc/src/main/java/org/elasticsearch/gradle/tool/Boilerplate.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838

3939
public abstract class Boilerplate {
4040

41+
public static <T> Action<T> noop() {
42+
return t -> {};
43+
}
44+
4145
public static SourceSetContainer getJavaSourceSets(Project project) {
4246
return project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
4347
}

distribution/docker/build.gradle

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import org.elasticsearch.gradle.BuildPlugin
1+
import org.elasticsearch.gradle.ElasticsearchDistribution.Flavor
22
import org.elasticsearch.gradle.LoggedExec
33
import org.elasticsearch.gradle.VersionProperties
44
import org.elasticsearch.gradle.info.BuildParams
55
import org.elasticsearch.gradle.testfixtures.TestFixturesPlugin
66

77
apply plugin: 'elasticsearch.standalone-rest-test'
88
apply plugin: 'elasticsearch.test.fixtures'
9+
apply plugin: 'elasticsearch.distribution-download'
910

1011
testFixtures.useFixture()
1112

@@ -105,10 +106,19 @@ task copyKeystore(type: Sync) {
105106
}
106107
}
107108

108-
preProcessFixture {
109-
if (TestFixturesPlugin.dockerComposeSupported()) {
110-
dependsOn assemble
109+
elasticsearch_distributions {
110+
Flavor.values().each { distroFlavor ->
111+
"docker_$distroFlavor" {
112+
flavor = distroFlavor
113+
type = 'docker'
114+
version = VersionProperties.getElasticsearch()
115+
failIfUnavailable = false // This ensures we don't attempt to build images if docker is unavailable
116+
}
111117
}
118+
}
119+
120+
preProcessFixture {
121+
dependsOn elasticsearch_distributions.docker_default, elasticsearch_distributions.docker_oss
112122
dependsOn copyKeystore
113123
doLast {
114124
// tests expect to have an empty repo
@@ -140,16 +150,13 @@ task integTest(type: Test) {
140150
outputs.doNotCacheIf('Build cache is disabled for Docker tests') { true }
141151
maxParallelForks = '1'
142152
include '**/*IT.class'
143-
// don't add the tasks to build the docker images if we have no way of testing them
144-
if (TestFixturesPlugin.dockerComposeSupported()) {
145-
dependsOn assemble
146-
}
147153
}
148154

149155
check.dependsOn integTest
150156

151157
void addBuildDockerImage(final boolean oss) {
152158
final Task buildDockerImageTask = task(taskName("build", oss, "DockerImage"), type: LoggedExec) {
159+
ext.requiresDocker = true // mark this task as requiring docker to execute
153160
inputs.files(tasks.named(taskName("copy", oss, "DockerContext")))
154161
List<String> tags
155162
if (oss) {
@@ -179,7 +186,6 @@ void addBuildDockerImage(final boolean oss) {
179186
}
180187
}
181188
assemble.dependsOn(buildDockerImageTask)
182-
BuildPlugin.requireDocker(buildDockerImageTask)
183189
}
184190

185191
for (final boolean oss : [false, true]) {

plugins/repository-hdfs/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ for (String fixtureName : ['hdfsFixture', 'haHdfsFixture', 'secureHdfsFixture',
9797
executable = "${BuildParams.runtimeJavaHome}/bin/java"
9898
env 'CLASSPATH', "${-> project.configurations.hdfsFixture.asPath}"
9999
maxWaitInSeconds 60
100-
onlyIf { project(':test:fixtures:krb5kdc-fixture').buildFixture.enabled && BuildParams.inFipsJvm == false }
100+
onlyIf { BuildParams.inFipsJvm == false }
101101
waitCondition = { fixture, ant ->
102102
// the hdfs.MiniHDFS fixture writes the ports file when
103103
// it's ready, so we can just wait for the file to exist

qa/remote-clusters/build.gradle

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
import org.elasticsearch.gradle.VersionProperties
1920
import org.elasticsearch.gradle.testfixtures.TestFixturesPlugin
2021

2122
apply plugin: 'elasticsearch.standalone-rest-test'
2223
apply plugin: 'elasticsearch.test.fixtures'
24+
apply plugin: 'elasticsearch.distribution-download'
2325

2426
testFixtures.useFixture()
2527

@@ -42,15 +44,17 @@ task copyKeystore(type: Sync) {
4244
}
4345
}
4446

45-
preProcessFixture {
46-
if (TestFixturesPlugin.dockerComposeSupported()) {
47-
if ('default'.equalsIgnoreCase(System.getProperty('tests.distribution', 'default'))) {
48-
dependsOn ":distribution:docker:buildDockerImage"
49-
} else {
50-
dependsOn ":distribution:docker:buildOssDockerImage"
51-
}
47+
elasticsearch_distributions {
48+
docker {
49+
type = 'docker'
50+
flavor = System.getProperty('tests.distribution', 'default')
51+
version = VersionProperties.getElasticsearch()
52+
failIfUnavailable = false // This ensures we skip this testing if Docker is unavailable
5253
}
53-
dependsOn copyKeystore
54+
}
55+
56+
preProcessFixture {
57+
dependsOn copyKeystore, elasticsearch_distributions.docker
5458
doLast {
5559
// tests expect to have an empty repo
5660
project.delete(
@@ -68,14 +72,12 @@ preProcessFixture {
6872
}
6973
}
7074

71-
if (TestFixturesPlugin.dockerComposeSupported()) {
72-
dockerCompose {
73-
tcpPortsToIgnoreWhenWaiting = [9600, 9601]
74-
if ('default'.equalsIgnoreCase(System.getProperty('tests.distribution', 'default'))) {
75-
useComposeFiles = ['docker-compose.yml']
76-
} else {
77-
useComposeFiles = ['docker-compose-oss.yml']
78-
}
75+
dockerCompose {
76+
tcpPortsToIgnoreWhenWaiting = [9600, 9601]
77+
if ('default'.equalsIgnoreCase(System.getProperty('tests.distribution', 'default'))) {
78+
useComposeFiles = ['docker-compose.yml']
79+
} else {
80+
useComposeFiles = ['docker-compose-oss.yml']
7981
}
8082
}
8183

@@ -100,10 +102,6 @@ task integTest(type: Test) {
100102
outputs.doNotCacheIf('Build cache is disabled for Docker tests') { true }
101103
maxParallelForks = '1'
102104
include '**/*IT.class'
103-
// don't add the tasks to build the docker images if we have no way of testing them
104-
if (TestFixturesPlugin.dockerComposeSupported()) {
105-
dependsOn ":distribution:docker:buildDockerImage"
106-
}
107105
}
108106

109107
check.dependsOn integTest

0 commit comments

Comments
 (0)