static List setup()

in buildSrc/src/main/groovy/org/elasticsearch/hadoop/gradle/fixture/hadoop/HadoopClusterFormationTasks.groovy [66:194]


    static List<InstanceInfo> setup(Project project, HadoopClusterConfiguration clusterConfiguration) {
        String prefix = clusterConfiguration.getName()

        // The cluster wide dependencies
        Object clusterDependencies = clusterConfiguration.getDependencies()

        // Create cluster wide shared dir, just in case we ever need one
        File sharedDir = new File(project.buildDir, "fixtures/shared")

        // First task is to clean the shared directory
        Task cleanShared = project.tasks.create(
            name: "${prefix}#prepareCluster.cleanShared",
            type: Delete,
            group: 'hadoopFixture',
            dependsOn: clusterDependencies) {
                delete sharedDir
                doLast {
                    sharedDir.mkdirs()
                }
        }

        // This is the initial start dependency that the first instance will pick up.
        Object startDependency = cleanShared

        // The complete list of fixture nodes that are a part of a cluster.
        List<InstanceInfo> nodes = []

        // Create the fixtures for each service
        List<TaskPair> clusterTaskPairs = []
        for (ServiceConfiguration serviceConfiguration : clusterConfiguration.getServices()) {

            // Get the download task for this service's package and add it to the service's dependency tasks
            Configuration distributionConfiguration = getOrConfigureDistributionDownload(project, serviceConfiguration)

            // Keep track of the start tasks in this service
            List<TaskPair> serviceTaskPairs = []

            // Create fixtures for each role in the service
            for (RoleConfiguration roleConfiguration : serviceConfiguration.getRoles()) {

                // Keep track of the start tasks in this role
                List<TaskPair> roleTaskPairs = []

                // Create fixtures for each instance in the role
                for (InstanceConfiguration instanceConfiguration : roleConfiguration.getInstances()) {
                    // Every instance depends on its cluster & service & role's start dependencies.
                    // If it's the first instance, it depends on the start dependency (clean shared)
                    // If it's a later instance, it depends on the the preceding instance
                    List<Object> instanceDependencies = []
                    instanceDependencies.addAll(instanceConfiguration.getDependencies())
                    if (clusterTaskPairs.empty) {
                        instanceDependencies.add(startDependency)
                    } else {
                        instanceDependencies.add(clusterTaskPairs.last().startTask)
                    }

                    // Collect the tasks that are depend on the service being up, and are finalized by
                    // the service stopping
                    def instanceClusterTasks = instanceConfiguration.getClusterTasks()

                    // Collect the instance info
                    InstanceInfo instanceInfo = new InstanceInfo(instanceConfiguration, project, prefix, sharedDir)
                    nodes.add(instanceInfo)

                    // Create the tasks for the instance
                    TaskPair instanceTasks
                    try {
                        instanceTasks = configureNode(project, prefix, instanceDependencies, instanceInfo,
                                distributionConfiguration)
                    } catch (Exception e) {
                        throw new GradleException(
                                "Exception occurred while initializing instance [${instanceInfo.toString()}]", e)
                    }

                    // Add the task pair to the task lists
                    roleTaskPairs.add(instanceTasks)
                    serviceTaskPairs.add(instanceTasks)
                    clusterTaskPairs.add(instanceTasks)

                    // Make each cluster task that needs this instance depend on it, and also be finalized by it.
                    instanceClusterTasks.forEach { Task clusterTask ->
                        clusterTask.dependsOn(instanceTasks.startTask)
                        if (instanceTasks.stopTask != null) {
                            clusterTask.finalizedBy(instanceTasks.stopTask)
                        }
                    }

                    // Check to see if any dependencies are Fixtures, and if they are, transfer the Fixture stop tasks
                    // to the cluster task finalized-by tasks.
                    for (Object dependency : instanceConfiguration.getDependencies()) {
                        if (dependency instanceof Fixture) {
                            def depStop = ((Fixture)dependency).stopTask
                            instanceClusterTasks.forEach { Task clusterTask ->
                                clusterTask.finalizedBy(depStop)
                            }
                        }
                    }
                }
                // Make each task in the role depend on and also be finalized by each instance in the service.
                List<Task> startTasks = roleTaskPairs.collect{it.startTask}
                List<Task> stopTasks = roleTaskPairs.findAll{it.stopTask != null}.collect{it.stopTask}
                roleConfiguration.getClusterTasks().forEach { Task clusterTask ->
                    clusterTask.dependsOn(startTasks)
                    if (!stopTasks.isEmpty()) {
                        clusterTask.finalizedBy(stopTasks)
                    }
                }
            }
            // Make each task in the service depend on and also be finalized by each instance in the service.
            List<Task> startTasks = serviceTaskPairs.collect{it.startTask}
            List<Task> stopTasks = serviceTaskPairs.findAll{it.stopTask != null}.collect{it.stopTask}
            serviceConfiguration.getClusterTasks().forEach { Task clusterTask ->
                clusterTask.dependsOn(startTasks)
                if (!stopTasks.isEmpty()) {
                    clusterTask.finalizedBy(stopTasks)
                }
            }
        }
        // Make each task in the cluster depend on and also be finalized by each instance in the cluster.
        List<Task> startTasks = clusterTaskPairs.collect{it.startTask}
        List<Task> stopTasks = clusterTaskPairs.findAll{it.stopTask != null}.collect{it.stopTask}
        clusterConfiguration.getClusterTasks().forEach { Task clusterTask ->
            clusterTask.dependsOn(startTasks)
            if (!stopTasks.isEmpty()) {
                clusterTask.finalizedBy(stopTasks)
            }
        }
        return nodes
    }