Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 333cb0e

Browse files
authored
Merge pull request #289 from miguelrgonzalez/dev
Support flexrep pull configurations #120
2 parents cad9145 + a18424d commit 333cb0e

File tree

10 files changed

+158
-2
lines changed

10 files changed

+158
-2
lines changed

src/main/java/com/marklogic/appdeployer/ConfigDir.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ public File getFlexrepConfigsDir() {
164164
return new File(getFlexrepDir(), "configs");
165165
}
166166

167+
public File getFlexrepPullsDir() {
168+
return new File(getFlexrepDir(), "pulls");
169+
}
170+
167171
public File getGroupsDir() {
168172
return new File(baseDir, "groups");
169173
}

src/main/java/com/marklogic/appdeployer/command/SortOrderConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public abstract class SortOrderConstants {
6666

6767
public static Integer DEPLOY_FLEXREP_CONFIGS = 1000;
6868
public static Integer DEPLOY_FLEXREP_TARGETS = 1010;
69+
public static Integer DEPLOY_FLEXREP_PULLS = 1020;
6970

7071
public static Integer DEPLOY_SQL_VIEWS = 1100;
7172

src/main/java/com/marklogic/appdeployer/command/flexrep/DeployConfigsCommand.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.marklogic.appdeployer.command.AbstractResourceCommand;
66
import com.marklogic.appdeployer.command.CommandContext;
77
import com.marklogic.appdeployer.command.SortOrderConstants;
8+
import com.marklogic.mgmt.SaveReceipt;
89
import com.marklogic.mgmt.resource.ResourceManager;
910
import com.marklogic.mgmt.resource.flexrep.ConfigManager;
1011

@@ -23,6 +24,8 @@ public DeployConfigsCommand() {
2324
setExecuteSortOrder(SortOrderConstants.DEPLOY_FLEXREP_CONFIGS);
2425
// Flexrep config is stored in a database, so we don't need to delete it as the database will be deleted
2526
setDeleteResourcesOnUndo(false);
27+
// We need to refer domains by id at the FlexRep Pulls payload
28+
setStoreResourceIdsAsCustomTokens(true);
2629
}
2730

2831
@Override
@@ -41,6 +44,17 @@ protected void deployConfigs(CommandContext context, ConfigDir configDir, String
4144
processExecuteOnResourceDir(context, configDir.getFlexrepConfigsDir());
4245
}
4346

47+
@Override
48+
protected void storeTokenForResourceId(SaveReceipt receipt, CommandContext context) {
49+
String targetId = currentConfigManager.getDomainID(receipt.getResourceId());
50+
String key = "%%flexrep-domains-id-" + receipt.getResourceId() + "%%";
51+
if (logger.isInfoEnabled()) {
52+
logger.info(format("Storing token with key '%s' and value '%s'", key, targetId));
53+
}
54+
55+
context.getAppConfig().getCustomTokens().put(key, targetId);
56+
}
57+
4458
/**
4559
* Not used as we override execute in this command.
4660
*
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.marklogic.appdeployer.command.flexrep;
2+
3+
import com.marklogic.appdeployer.AppConfig;
4+
import com.marklogic.appdeployer.ConfigDir;
5+
import com.marklogic.appdeployer.command.AbstractResourceCommand;
6+
import com.marklogic.appdeployer.command.CommandContext;
7+
import com.marklogic.appdeployer.command.SortOrderConstants;
8+
import com.marklogic.mgmt.resource.ResourceManager;
9+
import com.marklogic.mgmt.resource.flexrep.PullsManager;
10+
11+
import java.io.File;
12+
13+
public class DeployPullsCommand extends AbstractResourceCommand {
14+
15+
ResourceManager pullsManager;
16+
17+
public DeployPullsCommand() {
18+
setExecuteSortOrder(SortOrderConstants.DEPLOY_FLEXREP_PULLS);
19+
}
20+
21+
@Override
22+
protected File[] getResourceDirs(CommandContext context) {
23+
return findResourceDirs(context, configDir -> configDir.getFlexrepPullsDir());
24+
}
25+
26+
@Override
27+
protected ResourceManager getResourceManager(CommandContext context) {
28+
return pullsManager;
29+
}
30+
31+
@Override
32+
public void execute(CommandContext context) {
33+
AppConfig appConfig = context.getAppConfig();
34+
for (ConfigDir configDir : appConfig.getConfigDirs()) {
35+
deployFlexRepPulls(context, configDir, appConfig.getContentDatabaseName());
36+
for (File dir : configDir.getDatabaseResourceDirectories()) {
37+
deployFlexRepPulls(context, new ConfigDir(dir), dir.getName());
38+
}
39+
}
40+
}
41+
42+
protected void deployFlexRepPulls(CommandContext context, ConfigDir configDir, String databaseIdOrName) {
43+
pullsManager = new PullsManager(context.getManageClient(), databaseIdOrName);
44+
processExecuteOnResourceDir(context, configDir.getFlexrepPullsDir());
45+
}
46+
47+
48+
}

src/main/java/com/marklogic/appdeployer/command/flexrep/DeployTargetsCommand.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
import com.marklogic.appdeployer.command.AbstractCommand;
88
import com.marklogic.appdeployer.command.CommandContext;
99
import com.marklogic.appdeployer.command.SortOrderConstants;
10+
import com.marklogic.mgmt.ManageClient;
11+
import com.marklogic.mgmt.SaveReceipt;
1012
import com.marklogic.mgmt.resource.flexrep.TargetManager;
1113

1214
import java.io.File;
15+
import java.net.URI;
1316

1417
/**
1518
* The directory structure for this is a bit different from most command. Since targets belong to a certain flexrep
@@ -20,9 +23,11 @@
2023
public class DeployTargetsCommand extends AbstractCommand {
2124

2225
private String targetDirectorySuffix = "-targets";
26+
private TargetManager mgr;
2327

2428
public DeployTargetsCommand() {
2529
setExecuteSortOrder(SortOrderConstants.DEPLOY_FLEXREP_TARGETS);
30+
setStoreResourceIdsAsCustomTokens(true);
2631
}
2732

2833
@Override
@@ -57,12 +62,23 @@ protected void deployTargetsInDirectory(File dir, CommandContext context, String
5762
dir.getAbsolutePath()));
5863
}
5964

60-
TargetManager mgr = new TargetManager(context.getManageClient(), databaseIdOrName, configName);
65+
this.mgr = new TargetManager(context.getManageClient(), databaseIdOrName, configName);
6166
for (File f : listFilesInDirectory(dir)) {
62-
saveResource(mgr, context, f);
67+
SaveReceipt receipt = saveResource(mgr, context, f);
6368
}
6469
}
6570

71+
@Override
72+
protected void storeTokenForResourceId(SaveReceipt receipt, CommandContext context) {
73+
String targetId = mgr.getTargetId(receipt.getResourceId());
74+
String key = "%%flexrep-targets-id-" + receipt.getResourceId() + "%%";
75+
if (logger.isInfoEnabled()) {
76+
logger.info(format("Storing token with key '%s' and value '%s'", key, targetId));
77+
}
78+
79+
context.getAppConfig().getCustomTokens().put(key, targetId);
80+
}
81+
6682
protected String extractConfigNameFromDirectory(File dir) {
6783
String name = dir.getName();
6884
return name.substring(0, name.length() - targetDirectorySuffix.length());

src/main/java/com/marklogic/mgmt/resource/flexrep/ConfigManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.marklogic.mgmt.resource.AbstractResourceManager;
44
import com.marklogic.mgmt.ManageClient;
5+
import com.marklogic.rest.util.Fragment;
56

67
public class ConfigManager extends AbstractResourceManager {
78

@@ -47,4 +48,9 @@ public void enableAllFlexrepTargets() {
4748
}
4849
}
4950

51+
public String getDomainID(String configName) {
52+
Fragment f = getManageClient().getXml(getResourcesPath() +"/" + configName);
53+
return f.getElementValue("/node()/db:id");
54+
}
55+
5056
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.marklogic.mgmt.resource.flexrep;
2+
3+
import com.marklogic.mgmt.ManageClient;
4+
import com.marklogic.mgmt.resource.AbstractResourceManager;
5+
6+
public class PullsManager extends AbstractResourceManager {
7+
private String databaseIdOrName;
8+
9+
public PullsManager(ManageClient client, String databaseIdOrName) {
10+
super(client);
11+
this.databaseIdOrName = databaseIdOrName;
12+
}
13+
14+
@Override
15+
protected String getIdFieldName() {
16+
return "pull-name";
17+
}
18+
19+
@Override
20+
public String getResourcesPath() {
21+
return format("/manage/v2/databases/%s/flexrep/pulls", databaseIdOrName);
22+
}
23+
}

src/main/java/com/marklogic/mgmt/resource/flexrep/TargetManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.marklogic.mgmt.resource.AbstractResourceManager;
44
import com.marklogic.mgmt.ManageClient;
5+
import com.marklogic.rest.util.Fragment;
56

67
public class TargetManager extends AbstractResourceManager {
78

@@ -38,4 +39,9 @@ public void disableTarget(String targetIdOrName) {
3839
public void enableTarget(String targetIdOrName) {
3940
getManageClient().putJson(getPropertiesPath(targetIdOrName), "{\"enabled\":true}");
4041
}
42+
43+
public String getTargetId(String targetName) {
44+
Fragment f = getManageClient().getXml(getResourcesPath() +"/" + targetName);
45+
return f.getElementValue("/node()/db:id");
46+
}
4147
}

src/test/java/com/marklogic/appdeployer/command/flexrep/DeployFlexrepTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.marklogic.mgmt.resource.cpf.CpfConfigManager;
88
import com.marklogic.mgmt.resource.cpf.DomainManager;
99
import com.marklogic.mgmt.resource.cpf.PipelineManager;
10+
import com.marklogic.mgmt.resource.flexrep.PullsManager;
1011
import org.junit.After;
1112
import org.junit.Test;
1213

@@ -105,6 +106,34 @@ public void replicaFlexrep() {
105106
assertFalse(new ServerManager(manageClient).exists("master-flexrep-server"));
106107
}
107108

109+
@Test
110+
public void flexPull() {
111+
112+
appConfig.getFirstConfigDir().setBaseDir(new File("src/test/resources/sample-app/flexrep-config"));
113+
114+
initializeAppDeployer(new DeployContentDatabasesCommand(1), new DeployTriggersDatabaseCommand(),
115+
new DeployCpfConfigsCommand(), new DeployDomainsCommand(), new DeployPipelinesCommand(),
116+
new DeployConfigsCommand(), new DeployTargetsCommand(), new DeployOtherDatabasesCommand(), new DeployPullsCommand());
117+
118+
appDeployer.deploy(appConfig);
119+
assertConfigAndTargetAreDeployed();
120+
121+
final String pullName = "docs2go";
122+
PullsManager pullsMangager = new PullsManager(manageClient, "other-sample-app-content");
123+
assertTrue(pullsMangager.exists(pullName));
124+
125+
// Run deploy again to make sure nothing blows up
126+
appDeployer.deploy(appConfig);
127+
128+
ConfigManager mgr = new ConfigManager(manageClient, appConfig.getContentDatabaseName());
129+
mgr.deleteAllConfigs();
130+
assertTrue("All of the configs should have been deleted, including their targets", mgr.getAsXml()
131+
.getListItemIdRefs().isEmpty());
132+
133+
undeploySampleApp();
134+
assertFalse(new ServerManager(manageClient).exists("master-flexrep-server"));
135+
}
136+
108137
private void assertConfigAndTargetAreDeployed() {
109138
final String domainName = "sample-app-domain-1";
110139
ConfigManager configMgr = new ConfigManager(manageClient, appConfig.getContentDatabaseName());
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"pull-name": "docs2go",
3+
"domain-id": "%%flexrep-domains-id-sample-app-domain-1%%",
4+
"target-id": "%%flexrep-targets-id-sample-app-domain-1-target%%",
5+
"enabled": true,
6+
"url": [
7+
"http://localhost:8076"
8+
]
9+
}

0 commit comments

Comments
 (0)