|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.gradle.clusterformation; |
| 20 | + |
| 21 | +import groovy.lang.Closure; |
| 22 | +import org.elasticsearch.GradleServicesAdapter; |
| 23 | +import org.gradle.api.NamedDomainObjectContainer; |
| 24 | +import org.gradle.api.Plugin; |
| 25 | +import org.gradle.api.Project; |
| 26 | +import org.gradle.api.Task; |
| 27 | +import org.gradle.api.execution.TaskActionListener; |
| 28 | +import org.gradle.api.execution.TaskExecutionListener; |
| 29 | +import org.gradle.api.logging.Logger; |
| 30 | +import org.gradle.api.logging.Logging; |
| 31 | +import org.gradle.api.plugins.ExtraPropertiesExtension; |
| 32 | +import org.gradle.api.tasks.TaskState; |
| 33 | + |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +public class ClusterformationPlugin implements Plugin<Project> { |
| 41 | + |
| 42 | + public static final String LIST_TASK_NAME = "listElasticSearchClusters"; |
| 43 | + public static final String EXTENSION_NAME = "elasticSearchClusters"; |
| 44 | + |
| 45 | + private final Logger logger = Logging.getLogger(ClusterformationPlugin.class); |
| 46 | + |
| 47 | + @Override |
| 48 | + public void apply(Project project) { |
| 49 | + NamedDomainObjectContainer<? extends ElasticsearchConfiguration> container = project.container( |
| 50 | + ElasticsearchNode.class, |
| 51 | + (name) -> new ElasticsearchNode(name, GradleServicesAdapter.getInstance(project)) |
| 52 | + ); |
| 53 | + project.getExtensions().add(EXTENSION_NAME, container); |
| 54 | + |
| 55 | + Task listTask = project.getTasks().create(LIST_TASK_NAME); |
| 56 | + listTask.setGroup("ES cluster formation"); |
| 57 | + listTask.setDescription("Lists all ES clusters configured for this project"); |
| 58 | + listTask.doLast((Task task) -> |
| 59 | + container.forEach((ElasticsearchConfiguration cluster) -> |
| 60 | + logger.lifecycle(" * {}: {}", cluster.getName(), cluster.getDistribution()) |
| 61 | + ) |
| 62 | + ); |
| 63 | + |
| 64 | + Map<Task, List<ElasticsearchConfiguration>> taskToCluster = new HashMap<>(); |
| 65 | + |
| 66 | + // register an extension for all current and future tasks, so that any task can declare that it wants to use a |
| 67 | + // specific cluster. |
| 68 | + project.getTasks().all((Task task) -> |
| 69 | + task.getExtensions().findByType(ExtraPropertiesExtension.class) |
| 70 | + .set( |
| 71 | + "useCluster", |
| 72 | + new Closure<Void>(this, this) { |
| 73 | + public void doCall(ElasticsearchConfiguration conf) { |
| 74 | + taskToCluster.computeIfAbsent(task, k -> new ArrayList<>()).add(conf); |
| 75 | + } |
| 76 | + }) |
| 77 | + ); |
| 78 | + |
| 79 | + project.getGradle().getTaskGraph().whenReady(taskExecutionGraph -> |
| 80 | + taskExecutionGraph.getAllTasks() |
| 81 | + .forEach(task -> |
| 82 | + taskToCluster.getOrDefault(task, Collections.emptyList()).forEach(ElasticsearchConfiguration::claim) |
| 83 | + ) |
| 84 | + ); |
| 85 | + project.getGradle().addListener( |
| 86 | + new TaskActionListener() { |
| 87 | + @Override |
| 88 | + public void beforeActions(Task task) { |
| 89 | + // we only start the cluster before the actions, so we'll not start it if the task is up-to-date |
| 90 | + taskToCluster.getOrDefault(task, new ArrayList<>()).forEach(ElasticsearchConfiguration::start); |
| 91 | + } |
| 92 | + @Override |
| 93 | + public void afterActions(Task task) {} |
| 94 | + } |
| 95 | + ); |
| 96 | + project.getGradle().addListener( |
| 97 | + new TaskExecutionListener() { |
| 98 | + @Override |
| 99 | + public void afterExecute(Task task, TaskState state) { |
| 100 | + // always un-claim the cluster, even if _this_ task is up-to-date, as others might not have been and caused the |
| 101 | + // cluster to start. |
| 102 | + taskToCluster.getOrDefault(task, new ArrayList<>()).forEach(ElasticsearchConfiguration::unClaimAndStop); |
| 103 | + } |
| 104 | + @Override |
| 105 | + public void beforeExecute(Task task) {} |
| 106 | + } |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments