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

Commit 7cadcac

Browse files
committed
#88 New command for deploying all flexrep resources from flexrep/master or flexrep/replica
1 parent 3fe7a83 commit 7cadcac

File tree

18 files changed

+709
-0
lines changed

18 files changed

+709
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.marklogic.appdeployer.command.flexrep;
2+
3+
import com.marklogic.appdeployer.AppConfig;
4+
import com.marklogic.appdeployer.command.AbstractCommand;
5+
import com.marklogic.appdeployer.command.CommandContext;
6+
import com.marklogic.appdeployer.command.SortOrderConstants;
7+
import com.marklogic.appdeployer.command.UndoableCommand;
8+
import com.marklogic.appdeployer.command.appservers.DeployOtherServersCommand;
9+
import com.marklogic.appdeployer.command.cpf.DeployCpfConfigsCommand;
10+
import com.marklogic.appdeployer.command.cpf.DeployDomainsCommand;
11+
import com.marklogic.appdeployer.command.cpf.DeployPipelinesCommand;
12+
import com.marklogic.appdeployer.impl.SimpleAppDeployer;
13+
14+
import java.io.File;
15+
16+
/**
17+
* This command is for deploying all resources associated with a flexrep config. It combines CPF, flexrep configs
18+
* and targets, and optionally an HTTP server by reusing other commands. The intent is to support a configuration
19+
* for both a master and a replica in the same project. Most of the time you won't need this, in which case you
20+
* can just use DeployConfigsCommand and DeployTargetsCommand.
21+
*/
22+
public class DeployFlexrepCommand extends AbstractCommand implements UndoableCommand {
23+
24+
private String path;
25+
26+
public DeployFlexrepCommand() {
27+
this("master");
28+
}
29+
30+
public DeployFlexrepCommand(String path) {
31+
this.path = path;
32+
setExecuteSortOrder(SortOrderConstants.DEPLOY_OTHER_SERVERS);
33+
}
34+
35+
@Override
36+
public Integer getUndoSortOrder() {
37+
return SortOrderConstants.DELETE_OTHER_SERVERS;
38+
}
39+
40+
@Override
41+
public void execute(CommandContext context) {
42+
AppConfig appConfig = context.getAppConfig();
43+
File flexrepBaseDir = getFlexrepBaseDir(appConfig);
44+
if (flexrepBaseDir != null) {
45+
SimpleAppDeployer d = new SimpleAppDeployer(context.getManageClient(), context.getAdminManager(),
46+
new DeployCpfConfigsCommand(), new DeployDomainsCommand(), new DeployPipelinesCommand(), new DeployConfigsCommand(), new DeployTargetsCommand(), new DeployOtherServersCommand());
47+
File currentBaseDir = appConfig.getConfigDir().getBaseDir();
48+
appConfig.getConfigDir().setBaseDir(flexrepBaseDir);
49+
try {
50+
d.deploy(appConfig);
51+
} finally {
52+
appConfig.getConfigDir().setBaseDir(currentBaseDir);
53+
}
54+
}
55+
}
56+
57+
58+
@Override
59+
public void undo(CommandContext context) {
60+
AppConfig appConfig = context.getAppConfig();
61+
File flexrepBaseDir = getFlexrepBaseDir(appConfig);
62+
if (flexrepBaseDir != null) {
63+
SimpleAppDeployer d = new SimpleAppDeployer(context.getManageClient(), context.getAdminManager(), new DeployOtherServersCommand());
64+
File currentBaseDir = appConfig.getConfigDir().getBaseDir();
65+
appConfig.getConfigDir().setBaseDir(flexrepBaseDir);
66+
try {
67+
d.undeploy(appConfig);
68+
} finally {
69+
appConfig.getConfigDir().setBaseDir(currentBaseDir);
70+
}
71+
}
72+
}
73+
74+
protected File getFlexrepBaseDir(AppConfig appConfig) {
75+
if (path == null) {
76+
return null;
77+
}
78+
79+
File flexrepDir = appConfig.getConfigDir().getFlexrepDir();
80+
if (flexrepDir == null || !flexrepDir.exists()) {
81+
return null;
82+
}
83+
84+
File flexrepBaseDir = new File(flexrepDir, path);
85+
if (flexrepBaseDir == null || !flexrepBaseDir.exists()) {
86+
return null;
87+
}
88+
89+
return flexrepBaseDir;
90+
}
91+
92+
public void setPath(String path) {
93+
this.path = path;
94+
}
95+
96+
}

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

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

33
import java.io.File;
44

5+
import com.marklogic.mgmt.appservers.ServerManager;
6+
import com.marklogic.mgmt.cpf.CpfConfigManager;
7+
import com.marklogic.mgmt.cpf.DomainManager;
8+
import com.marklogic.mgmt.cpf.PipelineManager;
59
import org.junit.After;
610
import org.junit.Test;
711

@@ -52,6 +56,50 @@ public void noFlexrepDir() {
5256
appDeployer.deploy(appConfig);
5357
}
5458

59+
@Test
60+
public void masterFlexrep() {
61+
appConfig.getConfigDir().setBaseDir(new File("src/test/resources/sample-app/flexrep-combined"));
62+
63+
initializeAppDeployer(new DeployContentDatabasesCommand(1), new DeployTriggersDatabaseCommand(), new DeployFlexrepCommand("master"));
64+
65+
appDeployer.deploy(appConfig);
66+
67+
final String domainName = "master-domain";
68+
final String db = appConfig.getContentDatabaseName();
69+
assertTrue(new ServerManager(manageClient).exists("master-flexrep-server"));
70+
assertTrue(new DomainManager(manageClient).exists(appConfig.getTriggersDatabaseName(), domainName));
71+
assertTrue(new PipelineManager(manageClient).exists(appConfig.getTriggersDatabaseName(), "Flexible Replication"));
72+
assertTrue(new PipelineManager(manageClient).exists(appConfig.getTriggersDatabaseName(), "Status Change Handling"));
73+
assertTrue(new CpfConfigManager(manageClient).exists(appConfig.getTriggersDatabaseName(), domainName));
74+
assertTrue(new ConfigManager(manageClient, db).exists(domainName));
75+
assertTrue(new TargetManager(manageClient, db, domainName).exists("master-domain-target"));
76+
77+
undeploySampleApp();
78+
assertFalse(new ServerManager(manageClient).exists("master-flexrep-server"));
79+
}
80+
81+
@Test
82+
public void replicaFlexrep() {
83+
appConfig.getConfigDir().setBaseDir(new File("src/test/resources/sample-app/flexrep-combined"));
84+
85+
initializeAppDeployer(new DeployContentDatabasesCommand(1), new DeployTriggersDatabaseCommand(), new DeployFlexrepCommand("replica"));
86+
87+
appDeployer.deploy(appConfig);
88+
89+
final String domainName = "replica-domain";
90+
final String db = appConfig.getContentDatabaseName();
91+
assertTrue(new ServerManager(manageClient).exists("replica-flexrep-server"));
92+
assertTrue(new DomainManager(manageClient).exists(appConfig.getTriggersDatabaseName(), domainName));
93+
assertTrue(new PipelineManager(manageClient).exists(appConfig.getTriggersDatabaseName(), "Flexible Replication"));
94+
assertTrue(new PipelineManager(manageClient).exists(appConfig.getTriggersDatabaseName(), "Status Change Handling"));
95+
assertTrue(new CpfConfigManager(manageClient).exists(appConfig.getTriggersDatabaseName(), domainName));
96+
assertTrue(new ConfigManager(manageClient, db).exists(domainName));
97+
assertTrue(new TargetManager(manageClient, db, domainName).exists("replica-domain-target"));
98+
99+
undeploySampleApp();
100+
assertFalse(new ServerManager(manageClient).exists("master-flexrep-server"));
101+
}
102+
55103
private void assertConfigAndTargetAreDeployed() {
56104
final String domainName = "sample-app-domain-1";
57105
ConfigManager configMgr = new ConfigManager(manageClient, appConfig.getContentDatabaseName());
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"database-name": "%%DATABASE%%",
3+
"triggers-database": "%%TRIGGERS_DATABASE%%"
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"database-name": "%%TRIGGERS_DATABASE%%"
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"domain-name": "master-domain",
3+
"restart-user-name": "admin",
4+
"eval-module": "Modules",
5+
"eval-root": "/",
6+
"conversion-enabled": false,
7+
"permission": [{
8+
"role-name": "app-user",
9+
"capability": "read"
10+
}]
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"domain-name": "master-domain",
3+
"description": "Test domain for sample app",
4+
"scope": "directory",
5+
"uri": "/master/",
6+
"depth": "infinity",
7+
"eval-module": "Modules",
8+
"eval-root": "/",
9+
"pipeline": ["Status Change Handling", "Flexible Replication"]
10+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<pipeline-properties xmlns="http://marklogic.com/manage/pipeline/properties">
2+
<pipeline-id>6600933789859022149</pipeline-id>
3+
<pipeline-name>Flexible Replication</pipeline-name>
4+
<pipeline-description>
5+
Attempts push replication if possible, otherwise updates document properties so that push or poll will occur later.
6+
</pipeline-description>
7+
<success-action>
8+
<module>/MarkLogic/cpf/actions/success-action.xqy</module>
9+
</success-action>
10+
<failure-action>
11+
<module>/MarkLogic/cpf/actions/failure-action.xqy</module>
12+
</failure-action>
13+
<state-transition>
14+
<annotation>
15+
Replicate a document and transition to state "replicated".
16+
</annotation>
17+
<state>http://marklogic.com/states/converted</state>
18+
<on-success>http://marklogic.com/states/replicated</on-success>
19+
<on-failure>http://marklogic.com/states/error</on-failure>
20+
<priority>9100</priority>
21+
<execute>
22+
<condition>
23+
<module>
24+
/MarkLogic/conversion/actions/conversion-source-condition.xqy
25+
</module>
26+
</condition>
27+
<action>
28+
<module>/MarkLogic/flexrep/actions/replication-action.xqy</module>
29+
</action>
30+
</execute>
31+
</state-transition>
32+
<state-transition>
33+
<annotation>
34+
Replicate a document and transition to state "replicated".
35+
</annotation>
36+
<state>http://marklogic.com/states/structured-xhtml</state>
37+
<on-success>http://marklogic.com/states/replicated</on-success>
38+
<on-failure>http://marklogic.com/states/error</on-failure>
39+
<priority>5000</priority>
40+
<default-action>
41+
<module>/MarkLogic/flexrep/actions/replication-action.xqy</module>
42+
</default-action>
43+
</state-transition>
44+
<state-transition>
45+
<annotation>
46+
Replicate a document and transition to state "replicated".
47+
</annotation>
48+
<state>http://marklogic.com/states/initial</state>
49+
<on-success>http://marklogic.com/states/replicated</on-success>
50+
<on-failure>http://marklogic.com/states/error</on-failure>
51+
<priority>5000</priority>
52+
<default-action>
53+
<module>/MarkLogic/flexrep/actions/replication-action.xqy</module>
54+
</default-action>
55+
</state-transition>
56+
<state-transition>
57+
<annotation>
58+
Replicate a document and transition to state "replicated".
59+
</annotation>
60+
<state>http://marklogic.com/states/entities/enriched</state>
61+
<on-success>http://marklogic.com/states/replicated</on-success>
62+
<on-failure>http://marklogic.com/states/error</on-failure>
63+
<priority>5000</priority>
64+
<default-action>
65+
<module>/MarkLogic/flexrep/actions/replication-action.xqy</module>
66+
</default-action>
67+
</state-transition>
68+
<state-transition>
69+
<annotation>
70+
Replicate a document and transition to state "replicated".
71+
</annotation>
72+
<state>http://marklogic.com/states/xinclude/expanded</state>
73+
<on-success>http://marklogic.com/states/replicated</on-success>
74+
<on-failure>http://marklogic.com/states/error</on-failure>
75+
<priority>5000</priority>
76+
<default-action>
77+
<module>/MarkLogic/flexrep/actions/replication-action.xqy</module>
78+
</default-action>
79+
</state-transition>
80+
<state-transition>
81+
<annotation>
82+
Replicate a document and transition to state "replicated".
83+
</annotation>
84+
<state>http://marklogic.com/states/alerted</state>
85+
<on-success>http://marklogic.com/states/replicated</on-success>
86+
<on-failure>http://marklogic.com/states/error</on-failure>
87+
<priority>5000</priority>
88+
<default-action>
89+
<module>/MarkLogic/flexrep/actions/replication-action.xqy</module>
90+
</default-action>
91+
</state-transition>
92+
<state-transition>
93+
<annotation>
94+
Replicate a document and transition to state "replicated".
95+
</annotation>
96+
<state>http://marklogic.com/states/property-updated</state>
97+
<on-success>http://marklogic.com/states/replicated</on-success>
98+
<on-failure>http://marklogic.com/states/error</on-failure>
99+
<priority>5000</priority>
100+
<default-action>
101+
<module>/MarkLogic/flexrep/actions/replication-action.xqy</module>
102+
</default-action>
103+
</state-transition>
104+
<state-transition>
105+
<annotation>
106+
Document has been updated: reprocess if need be. Note: the preconditions will avoid reprocessing of active documents, so we
107+
will avoid falling into a processing loop when the processing updates a document.
108+
</annotation>
109+
<state>http://marklogic.com/states/updated</state>
110+
<on-success>http://marklogic.com/states/initial</on-success>
111+
<on-failure>http://marklogic.com/states/error</on-failure>
112+
<priority>5000</priority>
113+
</state-transition>
114+
<status-transition>
115+
<annotation>
116+
Leave a bread crumb that the document has been deleted.
117+
</annotation>
118+
<status>deleted</status>
119+
<priority>5000</priority>
120+
<always>true</always>
121+
<default-action>
122+
<module>/MarkLogic/flexrep/actions/delete-action.xqy</module>
123+
</default-action>
124+
</status-transition>
125+
<event-transition>
126+
<annotation>
127+
Pull flexrep properties from prior version of document if needed.
128+
</annotation>
129+
<event>updated</event>
130+
<priority>5000</priority>
131+
<default-action>
132+
<module>
133+
/MarkLogic/flexrep/actions/on-event-pull-properties.xqy
134+
</module>
135+
</default-action>
136+
</event-transition>
137+
<event-transition>
138+
<annotation>
139+
Pull flexrep properties from prior version of document if needed.
140+
</annotation>
141+
<event>property-updated</event>
142+
<priority>5000</priority>
143+
<default-action>
144+
<module>
145+
/MarkLogic/flexrep/actions/on-event-pull-properties.xqy
146+
</module>
147+
</default-action>
148+
</event-transition>
149+
</pipeline-properties>

0 commit comments

Comments
 (0)