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

Commit 45a027b

Browse files
author
Rob Rudin
committed
#156 #157 #158 #159 #160 Can now export databases, servers, forests, users, roles, and privileges
1 parent 2903562 commit 45a027b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1209
-71
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ dependencies {
3636
testCompile ('com.marklogic:ml-junit:2.6.0') {
3737
exclude module: "ml-javaclient-util"
3838
}
39+
40+
testCompile('commons-io:commons-io:2.5')
3941
}
4042

4143
// This ensures that Gradle includes in the published jar any non-java files under src/main/java

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
group=com.marklogic
22
javadocsDir=../gh-pages-marklogic-java/javadocs
3-
version=2.6.0
3+
version=159

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@ public File getRestApiFile() {
4949
}
5050

5151
public File getRestApiServerFile() {
52-
return new File(new File(baseDir, "servers"), "rest-api-server.json");
52+
return new File(getServersDir(), "rest-api-server.json");
5353
}
5454

5555
public File getSecurityDir() {
5656
return new File(baseDir, "security");
5757
}
5858

59+
public File getServersDir() { return new File(baseDir, "servers"); }
60+
61+
public File getForestsDir() {
62+
return new File(baseDir, "forests");
63+
}
64+
5965
public File getCpfDir() {
6066
return new File(baseDir, "cpf");
6167
}
@@ -74,6 +80,10 @@ public File getFlexrepDir() {
7480

7581
public File getTemporalDir() { return new File(baseDir, "temporal"); }
7682

83+
public File getTasksDir() {
84+
return new File(baseDir, "tasks");
85+
}
86+
7787
public void setDatabasesPath(String databasesPath) {
7888
this.databasesPath = databasesPath;
7989
}

src/main/java/com/marklogic/appdeployer/command/appservers/DeployOtherServersCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public DeployOtherServersCommand() {
2525

2626
@Override
2727
protected File[] getResourceDirs(CommandContext context) {
28-
return new File[] { new File(context.getAppConfig().getConfigDir().getBaseDir(), "servers") };
28+
return new File[] { context.getAppConfig().getConfigDir().getServersDir() };
2929
}
3030

3131
@Override

src/main/java/com/marklogic/appdeployer/command/databases/DeployDatabaseCommand.java

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.marklogic.mgmt.PayloadParser;
1010
import com.marklogic.mgmt.SaveReceipt;
1111
import com.marklogic.mgmt.databases.DatabaseManager;
12-
import com.marklogic.mgmt.forests.ForestManager;
1312

1413
import java.io.File;
1514
import java.util.Map;
@@ -37,14 +36,23 @@ public class DeployDatabaseCommand extends AbstractCommand implements UndoableCo
3736
*/
3837
private String databaseName;
3938

39+
/**
40+
* If true, this will look for a ./forests/(name of database) directory that defines custom forests for this
41+
* database. If such a directory exists, this command will not create any forests for the database. If the
42+
* directory does not exist, then this command will create forests for the database as defined by forestsPerHost.
43+
*/
44+
private boolean checkForCustomForests = true;
45+
4046
/**
4147
* Optional name of the file in the forests directory that will be used to create each forest. If not provided, a
42-
* "vanilla" forest is created on each host with a name based on the databaseName attribute.
48+
* "vanilla" forest is created on each host with a name based on the databaseName attribute. This is ignored if
49+
* custom forests are found in ./forests/(name of forest).
4350
*/
4451
private String forestFilename;
4552

4653
/**
47-
* Number of forests to create per host for this database.
54+
* Number of forests to create per host for this database. This is ignored if custom forests are found in
55+
* ./forests/(name of forest).
4856
*/
4957
private int forestsPerHost = 1;
5058

@@ -81,7 +89,13 @@ public void execute(CommandContext context) {
8189
if (payload != null) {
8290
DatabaseManager dbMgr = new DatabaseManager(context.getManageClient());
8391
SaveReceipt receipt = dbMgr.save(payload);
84-
buildDeployForestsCommand(payload, receipt, context).execute(context);
92+
if (shouldCreateForests(context, payload)) {
93+
buildDeployForestsCommand(payload, receipt, context).execute(context);
94+
} else {
95+
if (logger.isInfoEnabled()) {
96+
logger.info("Found custom forests for database, so not creating default forests");
97+
}
98+
}
8599
}
86100
}
87101

@@ -146,6 +160,40 @@ protected String getPayload(CommandContext context) {
146160
}
147161
}
148162

163+
/**
164+
* This is where we check to see if a custom forests directory exists at ./forests/(database name). The database
165+
* name is extracted from the payload via a PayloadParser. This check can be disabled by setting
166+
* checkForCustomForests to false.
167+
*
168+
* @param context
169+
* @param payload
170+
* @return
171+
*/
172+
protected boolean shouldCreateForests(CommandContext context, String payload) {
173+
if (isCheckForCustomForests()) {
174+
PayloadParser parser = new PayloadParser();
175+
String dbName = parser.getPayloadFieldValue(payload, "database-name");
176+
return !customForestsExist(context, dbName);
177+
}
178+
return true;
179+
}
180+
181+
/**
182+
* @param context
183+
* @param dbName
184+
* @return true if any file exists in ./forests/(name of database)
185+
*/
186+
protected boolean customForestsExist(CommandContext context, String dbName) {
187+
File dir = context.getAppConfig().getConfigDir().getForestsDir();
188+
if (dir.exists()) {
189+
File dbDir = new File(dir, dbName);
190+
if (dbDir.exists()) {
191+
return dbDir.listFiles().length > 0;
192+
}
193+
}
194+
return false;
195+
}
196+
149197
/**
150198
* Allows for how an instance of DeployForestsCommand is built to be overridden by a subclass.
151199
*
@@ -248,4 +296,12 @@ public boolean isCreateForestsOnEachHost() {
248296
public void setCreateForestsOnEachHost(boolean createForestsOnEachHost) {
249297
this.createForestsOnEachHost = createForestsOnEachHost;
250298
}
299+
300+
public boolean isCheckForCustomForests() {
301+
return checkForCustomForests;
302+
}
303+
304+
public void setCheckForCustomForests(boolean checkForCustomForests) {
305+
this.checkForCustomForests = checkForCustomForests;
306+
}
251307
}

src/main/java/com/marklogic/appdeployer/command/databases/DeployOtherDatabasesCommand.java

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,19 @@
2828
*/
2929
public class DeployOtherDatabasesCommand extends AbstractUndoableCommand {
3030

31+
// Each of these is copied to the instances of DeployDatabaseCommand that are created
32+
private int forestsPerHost = 1;
33+
private boolean checkForCustomForests = true;
34+
private String forestFilename;
35+
private boolean createForestsOnEachHost = true;
36+
3137
public DeployOtherDatabasesCommand() {
32-
setExecuteSortOrder(SortOrderConstants.DEPLOY_OTHER_DATABASES);
33-
setUndoSortOrder(SortOrderConstants.DELETE_OTHER_DATABASES);
38+
this(1);
39+
}
40+
41+
public DeployOtherDatabasesCommand(int forestsPerHost) {
42+
setExecuteSortOrder(SortOrderConstants.DEPLOY_OTHER_DATABASES);
43+
setUndoSortOrder(SortOrderConstants.DELETE_OTHER_DATABASES);
3444
}
3545

3646
@Override
@@ -69,14 +79,22 @@ protected List<DeployDatabaseCommand> buildDatabaseCommands(CommandContext conte
6979
if (logger.isDebugEnabled()) {
7080
logger.debug("Will process other database in file: " + f.getName());
7181
}
72-
DeployDatabaseCommand c = new DeployDatabaseCommand();
73-
c.setDatabaseFilename(f.getName());
74-
dbCommands.add(c);
82+
dbCommands.add(buildDeployDatabaseCommand(f));
7583
}
7684
}
7785
return dbCommands;
7886
}
7987

88+
protected DeployDatabaseCommand buildDeployDatabaseCommand(File file) {
89+
DeployDatabaseCommand c = new DeployDatabaseCommand();
90+
c.setForestsPerHost(getForestsPerHost());
91+
c.setDatabaseFilename(file.getName());
92+
c.setCheckForCustomForests(isCheckForCustomForests());
93+
c.setForestFilename(getForestFilename());
94+
c.setCreateForestsOnEachHost(isCreateForestsOnEachHost());
95+
return c;
96+
}
97+
8098
/**
8199
* Adds to the list of resource filenames to ignore. Some may already have been set via the superclass method.
82100
*
@@ -91,4 +109,36 @@ protected void ignoreKnownDatabaseFilenames(CommandContext context) {
91109
ignore.add(DeployTriggersDatabaseCommand.DATABASE_FILENAME);
92110
setFilenamesToIgnore(ignore.toArray(new String[]{}));
93111
}
112+
113+
public int getForestsPerHost() {
114+
return forestsPerHost;
115+
}
116+
117+
public void setForestsPerHost(int forestsPerHost) {
118+
this.forestsPerHost = forestsPerHost;
119+
}
120+
121+
public boolean isCheckForCustomForests() {
122+
return checkForCustomForests;
123+
}
124+
125+
public void setCheckForCustomForests(boolean checkForCustomForests) {
126+
this.checkForCustomForests = checkForCustomForests;
127+
}
128+
129+
public String getForestFilename() {
130+
return forestFilename;
131+
}
132+
133+
public void setForestFilename(String forestFilename) {
134+
this.forestFilename = forestFilename;
135+
}
136+
137+
public boolean isCreateForestsOnEachHost() {
138+
return createForestsOnEachHost;
139+
}
140+
141+
public void setCreateForestsOnEachHost(boolean createForestsOnEachHost) {
142+
this.createForestsOnEachHost = createForestsOnEachHost;
143+
}
94144
}

src/main/java/com/marklogic/appdeployer/command/forests/DeployCustomForestsCommand.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.marklogic.appdeployer.command.AbstractCommand;
44
import com.marklogic.appdeployer.command.CommandContext;
55
import com.marklogic.appdeployer.command.SortOrderConstants;
6+
import com.marklogic.mgmt.PayloadParser;
67
import com.marklogic.mgmt.forests.ForestManager;
78

89
import java.io.File;
@@ -20,6 +21,7 @@
2021
public class DeployCustomForestsCommand extends AbstractCommand {
2122

2223
private String customForestsPath = "forests";
24+
private PayloadParser payloadParser;
2325

2426
public DeployCustomForestsCommand() {
2527
setExecuteSortOrder(SortOrderConstants.DEPLOY_FORESTS);
@@ -29,6 +31,7 @@ public DeployCustomForestsCommand() {
2931
public void execute(CommandContext context) {
3032
File dir = new File(context.getAppConfig().getConfigDir().getBaseDir(), customForestsPath);
3133
if (dir != null && dir.exists()) {
34+
payloadParser = new PayloadParser();
3235
for (File f : dir.listFiles()) {
3336
if (f.isDirectory()) {
3437
processDirectory(f, context);
@@ -37,6 +40,12 @@ public void execute(CommandContext context) {
3740
}
3841
}
3942

43+
/**
44+
* Supports JSON files with a single payload or an array of payloads, or an XML file with a single payload.
45+
*
46+
* @param dir
47+
* @param context
48+
*/
4049
protected void processDirectory(File dir, CommandContext context) {
4150
if (logger.isInfoEnabled()) {
4251
logger.info("Processing custom forest files in directory: " + dir.getAbsolutePath());
@@ -47,7 +56,11 @@ protected void processDirectory(File dir, CommandContext context) {
4756
logger.info("Processing forests in file: " + f.getAbsolutePath());
4857
}
4958
String payload = copyFileToString(f, context);
50-
mgr.saveJsonForests(payload);
59+
if (payloadParser.isJsonPayload(payload)) {
60+
mgr.saveJsonForests(payload);
61+
} else {
62+
mgr.save(payload);
63+
}
5164
}
5265
}
5366

src/main/java/com/marklogic/appdeployer/command/forests/DeployForestsCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public DeployForestsCommand() {
4646
public void execute(CommandContext context) {
4747
String payload = null;
4848
if (forestFilename != null) {
49-
File dir = new File(context.getAppConfig().getConfigDir().getBaseDir(), "forests");
49+
File dir = context.getAppConfig().getConfigDir().getForestsDir();
5050
if (dir.exists()) {
5151
File f = new File(dir, forestFilename);
5252
if (f.exists()) {

src/main/java/com/marklogic/appdeployer/command/tasks/DeployScheduledTasksCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public DeployScheduledTasksCommand() {
2020

2121
@Override
2222
protected File[] getResourceDirs(CommandContext context) {
23-
return new File[] { new File(context.getAppConfig().getConfigDir().getBaseDir(), "tasks") };
23+
return new File[] { context.getAppConfig().getConfigDir().getTasksDir() };
2424
}
2525

2626
@Override
@@ -88,4 +88,4 @@ class IsDirectoryFilter implements FileFilter {
8888
public boolean accept(File pathname) {
8989
return pathname != null && pathname.isDirectory();
9090
}
91-
}
91+
}

0 commit comments

Comments
 (0)