|
1 | 1 | package com.marklogic.appdeployer.command.forests; |
2 | 2 |
|
3 | 3 | import java.io.File; |
| 4 | +import java.util.ArrayList; |
4 | 5 | import java.util.List; |
5 | 6 |
|
| 7 | +import com.marklogic.appdeployer.command.Command; |
| 8 | +import com.marklogic.mgmt.databases.DatabaseManager; |
6 | 9 | import org.springframework.util.StringUtils; |
7 | 10 |
|
8 | 11 | import com.marklogic.appdeployer.AppConfig; |
@@ -68,23 +71,33 @@ public void execute(CommandContext context) { |
68 | 71 | protected void createForests(String originalPayload, CommandContext context) { |
69 | 72 | ForestManager mgr = new ForestManager(context.getManageClient()); |
70 | 73 | AppConfig appConfig = context.getAppConfig(); |
| 74 | + |
| 75 | + // Find out which hosts to create forests on |
71 | 76 | List<String> hostNames = new HostManager(context.getManageClient()).getHostNames(); |
72 | | - int size = hostNames.size(); |
73 | 77 | if (!createForestsOnEachHost) { |
74 | | - logger.info(format("Only creating forests on the first host: " + hostNames.get(0))); |
75 | | - size = 1; |
| 78 | + String first = hostNames.get(0); |
| 79 | + logger.info(format("Only creating forests on the first host: " + first)); |
| 80 | + hostNames = new ArrayList<>(); |
| 81 | + hostNames.add(first); |
76 | 82 | } |
77 | | - for (int i = 0; i < size; i++) { |
78 | | - String hostName = hostNames.get(i); |
79 | | - logger.info(format("Creating forests on host %s", hostName)); |
80 | | - int startNumber = (i * forestsPerHost) + 1; |
81 | | - int endNumber = (i + 1) * forestsPerHost; |
82 | | - for (int j = startNumber; j <= endNumber; j++) { |
83 | | - String payload = tokenReplacer.replaceTokens(originalPayload, appConfig, false); |
84 | | - payload = payload.replace("%%FOREST_HOST%%", hostName); |
85 | | - payload = payload.replace("%%FOREST_NAME%%", getForestName(appConfig, j)); |
86 | | - payload = payload.replace("%%FOREST_DATABASE%%", getForestDatabaseName(appConfig)); |
87 | | - mgr.save(payload); |
| 83 | + |
| 84 | + // Find out how many forests exist already |
| 85 | + int countOfExistingForests = new DatabaseManager(context.getManageClient()).getPrimaryForestIds(getForestDatabaseName(appConfig)).size(); |
| 86 | + int desiredNumberOfForests = hostNames.size() * forestsPerHost; |
| 87 | + |
| 88 | + // Loop over the number of forests to create, starting with count + 1, and iterating over the hosts |
| 89 | + for (int i = countOfExistingForests + 1; i <= desiredNumberOfForests;) { |
| 90 | + for (String hostName : hostNames) { |
| 91 | + if (i <= desiredNumberOfForests) { |
| 92 | + String payload = tokenReplacer.replaceTokens(originalPayload, appConfig, false); |
| 93 | + payload = payload.replace("%%FOREST_HOST%%", hostName); |
| 94 | + String forestName = getForestName(appConfig, i); |
| 95 | + payload = payload.replace("%%FOREST_NAME%%", forestName); |
| 96 | + payload = payload.replace("%%FOREST_DATABASE%%", getForestDatabaseName(appConfig)); |
| 97 | + logger.info(format("Creating forest %s on host %s", forestName, hostName)); |
| 98 | + mgr.save(payload); |
| 99 | + } |
| 100 | + i++; |
88 | 101 | } |
89 | 102 | } |
90 | 103 | } |
|
0 commit comments