|
| 1 | +package com.marklogic.gradle.task.forests |
| 2 | + |
| 3 | +import com.marklogic.appdeployer.AppConfig |
| 4 | +import com.marklogic.appdeployer.command.databases.DeployContentDatabasesCommand |
| 5 | +import com.marklogic.appdeployer.command.databases.DeployDatabaseCommand |
| 6 | +import com.marklogic.appdeployer.command.databases.DeploySchemasDatabaseCommand |
| 7 | +import com.marklogic.appdeployer.command.databases.DeployTriggersDatabaseCommand |
| 8 | +import com.marklogic.appdeployer.impl.SimpleAppDeployer |
| 9 | +import com.marklogic.gradle.task.MarkLogicTask |
| 10 | +import com.marklogic.mgmt.api.forest.Forest |
| 11 | +import org.gradle.api.tasks.TaskAction |
| 12 | + |
| 13 | +class PrintForestPlanTask extends MarkLogicTask { |
| 14 | + |
| 15 | + @TaskAction |
| 16 | + void printForestPlan() { |
| 17 | + if (!project.hasProperty("database")) { |
| 18 | + println "Please specify a database via the 'database' property" |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + String database = project.property("database") |
| 23 | + |
| 24 | + /** |
| 25 | + * We unfortunately have to do a little work here to determine what command object to use based on the database |
| 26 | + * name. That's to account for the "special" stuff that's done for the content database (mlContentForestsPerHost) |
| 27 | + * and for the schema/triggers databases (those commands default to only having forests on one host). |
| 28 | + */ |
| 29 | + AppConfig appConfig = getAppConfig() |
| 30 | + SimpleAppDeployer appDeployer = getAppDeployer() |
| 31 | + DeployDatabaseCommand command |
| 32 | + if (database.equals(appConfig.getContentDatabaseName()) || database.equals(appConfig.getTestContentDatabaseName())) { |
| 33 | + command = appDeployer.getCommandOfType(DeployContentDatabasesCommand.class) |
| 34 | + } else if (database.equals(appConfig.getSchemasDatabaseName())) { |
| 35 | + command = appDeployer.getCommandOfType(DeploySchemasDatabaseCommand.class) |
| 36 | + } else if (database.equals(appConfig.getTriggersDatabaseName())) { |
| 37 | + command = appDeployer.getCommandOfType(DeployTriggersDatabaseCommand.class) |
| 38 | + } else { |
| 39 | + command = new DeployDatabaseCommand() |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Now that we have a command, use it to build its command for deploying forests, and then use that to build |
| 44 | + * the list of forests that will be created. |
| 45 | + */ |
| 46 | + List<Forest> forests = command.buildDeployForestsCommand(database, getCommandContext()).buildForests(getCommandContext(), true) |
| 47 | + |
| 48 | + |
| 49 | + if (forests.isEmpty()) { |
| 50 | + println "\nNo primary forests will be created the next time the database '" + database + "' is deployed; this is likely because it already has all of the primary desired forests based on the configuration settings." |
| 51 | + println "\nIf replicas have been configured for the database - e.g. via mlDatabaseNamesAndReplicaCounts - and these do not exist yet, " + |
| 52 | + "then replicas will be created the next time either the mlDeploy task or mlConfigureForestReplicas task is run." |
| 53 | + } else { |
| 54 | + for (Forest f : forests) { |
| 55 | + println f.getJson() |
| 56 | + } |
| 57 | + println "\nThe " + forests.size() + " forests (and replicas if applicable) that will be created the next time the database '" + database + "' is deployed (e.g. via the mlDeploy task) are listed above." |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments