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

Commit

Permalink
#106 Getting started, added copyFileToString to simplify some things
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrudin committed Jul 19, 2016
1 parent dfb31d7 commit fbc7511
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 14 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -59,7 +59,7 @@ public void setFilenamesToIgnore(String... filenames) {

/**
* Simplifies reading the contents of a File into a String.
*
*
* @param f
* @return
*/
Expand All @@ -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);
Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -148,4 +152,4 @@ public boolean isCreateForestsOnEachHost() {
public void setCreateForestsOnEachHost(boolean createForestsOnEachHost) {
this.createForestsOnEachHost = createForestsOnEachHost;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> response = mgr.create(payload);
if (logger.isInfoEnabled()) {
logger.info("Created certificate authority, location: " + response.getHeaders().getLocation());
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"database-name": "%%DATABASE%%"
}
Original file line number Diff line number Diff line change
@@ -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%%"
}
]

0 comments on commit fbc7511

Please sign in to comment.