From fbc75113064da714a0c54d7618799baf7b037e1c Mon Sep 17 00:00:00 2001 From: Rob Rudin Date: Tue, 19 Jul 2016 15:31:56 -0400 Subject: [PATCH] #106 Getting started, added copyFileToString to simplify some things --- .editorconfig | 16 +++++++++ .../appdeployer/command/AbstractCommand.java | 24 +++++++++---- .../command/AbstractResourceCommand.java | 6 ++-- .../alert/DeployAlertRulesCommand.java | 2 +- .../cpf/AbstractCpfResourceCommand.java | 3 +- .../forests/DeployCustomForestsCommand.java | 34 +++++++++++++++++++ .../command/forests/DeployForestsCommand.java | 6 +++- .../DeployCertificateAuthoritiesCommand.java | 2 +- .../forests/DeployCustomForestsTest.java | 27 +++++++++++++++ .../databases/content-database.json | 3 ++ .../sample-app-content/custom-forests.json | 14 ++++++++ 11 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 .editorconfig create mode 100644 src/main/java/com/marklogic/appdeployer/command/forests/DeployCustomForestsCommand.java create mode 100644 src/test/java/com/marklogic/appdeployer/command/forests/DeployCustomForestsTest.java create mode 100644 src/test/resources/sample-app/custom-forests/databases/content-database.json create mode 100644 src/test/resources/sample-app/custom-forests/forests/sample-app-content/custom-forests.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..9427c75e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +# http://editorconfig.org +root = true + +[*] +indent_style = tab +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false + +[*.java] +indent_size = 4 diff --git a/src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java b/src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java index 07d818c4..b710992a 100644 --- a/src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java @@ -37,7 +37,7 @@ public Integer getExecuteSortOrder() { /** * Convenience method for setting the names of files to ignore when reading resources from a directory. Will * preserve any filenames already being ignored on the underlying FilenameFilter. - * + * * @param filenames */ public void setFilenamesToIgnore(String... filenames) { @@ -59,7 +59,7 @@ public void setFilenamesToIgnore(String... filenames) { /** * Simplifies reading the contents of a File into a String. - * + * * @param f * @return */ @@ -73,17 +73,29 @@ protected String copyFileToString(File f) { } } + /** + * Convenience function for reading the file into a string and replace tokens as well. Assumes this is not + * for a test-only resource. + * + * @param f + * @param context + * @return + */ + protected String copyFileToString(File f, CommandContext context) { + String str = copyFileToString(f); + return str != null ? tokenReplacer.replaceTokens(str, context.getAppConfig(), false) : str; + } + /** * Provides a basic implementation for saving a resource defined in a File, including replacing tokens. - * + * * @param mgr * @param context * @param f * @return */ protected SaveReceipt saveResource(ResourceManager mgr, CommandContext context, File f) { - String payload = copyFileToString(f); - payload = tokenReplacer.replaceTokens(payload, context.getAppConfig(), false); + String payload = copyFileToString(f, context); SaveReceipt receipt = mgr.save(payload); if (storeResourceIdsAsCustomTokens) { storeTokenForResourceId(receipt, context); @@ -95,7 +107,7 @@ protected SaveReceipt saveResource(ResourceManager mgr, CommandContext context, * Any resource that may be referenced by its ID by another resource will most likely need its ID stored as a custom * token so that it can be referenced by the other resource. To enable this, the subclass should set * storeResourceIdAsCustomToken to true. - * + * * @param receipt * @param context */ diff --git a/src/main/java/com/marklogic/appdeployer/command/AbstractResourceCommand.java b/src/main/java/com/marklogic/appdeployer/command/AbstractResourceCommand.java index 059d8f74..6f99020d 100644 --- a/src/main/java/com/marklogic/appdeployer/command/AbstractResourceCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/AbstractResourceCommand.java @@ -41,7 +41,7 @@ public void execute(CommandContext context) { /** * Subclasses can override this to add functionality after a resource has been saved. - * + * * @param mgr * @param context * @param resourceFile @@ -77,13 +77,13 @@ public void undo(CommandContext context) { * delete the resource. This has been necessary when deleting two app servers in a row - for some reason, the 2nd * delete will intermittently fail with a connection reset error, but the app server is in fact deleted * successfully. - * + * * @param mgr * @param context * @param f */ protected void deleteResource(final ResourceManager mgr, CommandContext context, File f) { - final String payload = tokenReplacer.replaceTokens(copyFileToString(f), context.getAppConfig(), false); + final String payload = copyFileToString(f, context); try { if (restartAfterDelete) { context.getAdminManager().invokeActionRequiringRestart(new ActionRequiringRestart() { diff --git a/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertRulesCommand.java b/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertRulesCommand.java index 6a3d9821..8d425b9e 100644 --- a/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertRulesCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertRulesCommand.java @@ -44,7 +44,7 @@ protected void deployRulesInDirectory(File dir, CommandContext context) { * parse its contents. */ for (File f : listFilesInDirectory(dir)) { - String payload = copyFileToString(f); + String payload = copyFileToString(f, context); String actionName = payloadParser.getPayloadFieldValue(payload, "action-name"); AlertRuleManager mgr = new AlertRuleManager(context.getManageClient(), dbName, configUri, actionName); saveResource(mgr, context, f); diff --git a/src/main/java/com/marklogic/appdeployer/command/cpf/AbstractCpfResourceCommand.java b/src/main/java/com/marklogic/appdeployer/command/cpf/AbstractCpfResourceCommand.java index 988fdfb4..ebb9545f 100644 --- a/src/main/java/com/marklogic/appdeployer/command/cpf/AbstractCpfResourceCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/cpf/AbstractCpfResourceCommand.java @@ -20,8 +20,7 @@ public void execute(CommandContext context) { if (dir.exists()) { AbstractCpfResourceManager mgr = getResourceManager(context); for (File f : listFilesInDirectory(dir)) { - String payload = copyFileToString(f); - payload = tokenReplacer.replaceTokens(payload, config, false); + String payload = copyFileToString(f, context); mgr.save(config.getTriggersDatabaseName(), payload); } } diff --git a/src/main/java/com/marklogic/appdeployer/command/forests/DeployCustomForestsCommand.java b/src/main/java/com/marklogic/appdeployer/command/forests/DeployCustomForestsCommand.java new file mode 100644 index 00000000..3b2fc8b4 --- /dev/null +++ b/src/main/java/com/marklogic/appdeployer/command/forests/DeployCustomForestsCommand.java @@ -0,0 +1,34 @@ +package com.marklogic.appdeployer.command.forests; + +import com.marklogic.appdeployer.command.AbstractCommand; +import com.marklogic.appdeployer.command.CommandContext; +import com.marklogic.appdeployer.command.SortOrderConstants; + +import java.io.File; + +/** + * For each directory in the ml-config/forests directory, TODO. + */ +public class DeployCustomForestsCommand extends AbstractCommand { + + public DeployCustomForestsCommand() { + setExecuteSortOrder(SortOrderConstants.DEPLOY_FORESTS); + } + + @Override + public void execute(CommandContext context) { + File dir = new File(context.getAppConfig().getConfigDir().getBaseDir(), "forests"); + for (File f : dir.listFiles()) { + if (f.isDirectory()) { + processDirectory(f, context); + } + } + } + + protected void processDirectory(File dir, CommandContext context) { + for (File f : listFilesInDirectory(dir)) { + logger.info("Processing: " + f.getAbsolutePath()); + } + + } +} diff --git a/src/main/java/com/marklogic/appdeployer/command/forests/DeployForestsCommand.java b/src/main/java/com/marklogic/appdeployer/command/forests/DeployForestsCommand.java index 0c706b39..543c264c 100644 --- a/src/main/java/com/marklogic/appdeployer/command/forests/DeployForestsCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/forests/DeployForestsCommand.java @@ -15,6 +15,10 @@ import com.marklogic.mgmt.hosts.HostManager; /** + * This command is for a simple use case where all the forests created for a database have the same structure, + * but possibly exist on different forests. For more precise control over how forests are created, please see + * DeployCustomForestsCommand. + * * Doesn't yet support deleting forests - currently assumed that this will be done by deleting a database. */ public class DeployForestsCommand extends AbstractCommand { @@ -148,4 +152,4 @@ public boolean isCreateForestsOnEachHost() { public void setCreateForestsOnEachHost(boolean createForestsOnEachHost) { this.createForestsOnEachHost = createForestsOnEachHost; } -} \ No newline at end of file +} diff --git a/src/main/java/com/marklogic/appdeployer/command/security/DeployCertificateAuthoritiesCommand.java b/src/main/java/com/marklogic/appdeployer/command/security/DeployCertificateAuthoritiesCommand.java index e8029dcc..4e270a3c 100644 --- a/src/main/java/com/marklogic/appdeployer/command/security/DeployCertificateAuthoritiesCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/security/DeployCertificateAuthoritiesCommand.java @@ -25,7 +25,7 @@ public void execute(CommandContext context) { if (logger.isInfoEnabled()) { logger.info("Creating certificate authority from file: " + f.getAbsolutePath()); } - String payload = copyFileToString(f); + String payload = copyFileToString(f, context); ResponseEntity response = mgr.create(payload); if (logger.isInfoEnabled()) { logger.info("Created certificate authority, location: " + response.getHeaders().getLocation()); diff --git a/src/test/java/com/marklogic/appdeployer/command/forests/DeployCustomForestsTest.java b/src/test/java/com/marklogic/appdeployer/command/forests/DeployCustomForestsTest.java new file mode 100644 index 00000000..6a5c175e --- /dev/null +++ b/src/test/java/com/marklogic/appdeployer/command/forests/DeployCustomForestsTest.java @@ -0,0 +1,27 @@ +package com.marklogic.appdeployer.command.forests; + +import com.marklogic.appdeployer.AbstractAppDeployerTest; +import com.marklogic.appdeployer.ConfigDir; +import com.marklogic.appdeployer.command.databases.DeployContentDatabasesCommand; +import org.junit.After; +import org.junit.Test; + +import java.io.File; + +/** + * Created by rrudin on 7/19/2016. + */ +public class DeployCustomForestsTest extends AbstractAppDeployerTest { + + @After + public void tearDown() { + //undeploySampleApp(); + } + + @Test + public void test() { + appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/sample-app/custom-forests"))); + initializeAppDeployer(new DeployContentDatabasesCommand(1), new DeployCustomForestsCommand()); + deploySampleApp(); + } +} diff --git a/src/test/resources/sample-app/custom-forests/databases/content-database.json b/src/test/resources/sample-app/custom-forests/databases/content-database.json new file mode 100644 index 00000000..49cc15ce --- /dev/null +++ b/src/test/resources/sample-app/custom-forests/databases/content-database.json @@ -0,0 +1,3 @@ +{ + "database-name": "%%DATABASE%%" +} \ No newline at end of file diff --git a/src/test/resources/sample-app/custom-forests/forests/sample-app-content/custom-forests.json b/src/test/resources/sample-app/custom-forests/forests/sample-app-content/custom-forests.json new file mode 100644 index 00000000..395a271d --- /dev/null +++ b/src/test/resources/sample-app/custom-forests/forests/sample-app-content/custom-forests.json @@ -0,0 +1,14 @@ +[ + { + "forest-name": "sample-app-content-custom-1", + "enabled": true, + "host": "%%HOST%%", + "database": "%%DATABASE%%" + }, + { + "forest-name": "sample-app-content-custom-2", + "enabled": true, + "host": "%%HOST%%", + "database": "%%DATABASE%%" + } +]