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

Commit 6b9d815

Browse files
committed
#304 Can now specify server names to undeploy
And the 4 OOTB ML servers are not undeployed by default now
1 parent c1a425f commit 6b9d815

File tree

6 files changed

+193
-32
lines changed

6 files changed

+193
-32
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ public class AppConfig {
222222

223223
private boolean updateMimetypeWhenPropertiesAreEqual = false;
224224

225+
private String[] serversToNotUndeploy;
226+
225227
private Map<String, Object> additionalProperties = new HashMap<>();
226228

227229
public AppConfig() {
@@ -1256,4 +1258,12 @@ public X509TrustManager getAppServicesTrustManager() {
12561258
public void setAppServicesTrustManager(X509TrustManager appServicesTrustManager) {
12571259
this.appServicesTrustManager = appServicesTrustManager;
12581260
}
1261+
1262+
public String[] getServersToNotUndeploy() {
1263+
return serversToNotUndeploy;
1264+
}
1265+
1266+
public void setServersToNotUndeploy(String... serversToNotUndeploy) {
1267+
this.serversToNotUndeploy = serversToNotUndeploy;
1268+
}
12591269
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,12 @@ public void initialize() {
635635
logger.info("Update mimetype when properties are equal (defaults to false to avoid unnecessary ML restarts): " + prop);
636636
config.setUpdateMimetypeWhenPropertiesAreEqual(Boolean.parseBoolean(prop));
637637
});
638+
639+
propertyConsumerMap.put("mlServersToNotUndeploy", (config, prop) -> {
640+
String[] values = prop.split(",");
641+
logger.info("Servers that will not be undeployed: " + Arrays.asList(values));
642+
config.setServersToNotUndeploy(values);
643+
});
638644
}
639645

640646
@Override

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,19 @@ protected void processUndoOnResourceDir(CommandContext context, File resourceDir
130130
* @param f
131131
*/
132132
protected void deleteResource(ResourceManager mgr, CommandContext context, File f) {
133-
final String payload = copyFileToString(f, context);
133+
String payload = copyFileToString(f, context);
134134
final ResourceManager resourceManager = adjustResourceManagerForPayload(mgr, context, payload);
135+
136+
final String finalPayload = adjustPayloadBeforeDeletingResource(resourceManager, context, f, payload);
137+
if (finalPayload == null) {
138+
return;
139+
}
140+
135141
try {
136142
if (restartAfterDelete) {
137-
context.getAdminManager().invokeActionRequiringRestart(new ActionRequiringRestart() {
138-
@Override
139-
public boolean execute() {
140-
return resourceManager.delete(payload).isDeleted();
141-
}
142-
});
143+
context.getAdminManager().invokeActionRequiringRestart(() -> resourceManager.delete(finalPayload).isDeleted());
143144
} else {
144-
resourceManager.delete(payload);
145+
resourceManager.delete(finalPayload);
145146
}
146147
} catch (RuntimeException e) {
147148
if (catchExceptionOnDeleteFailure) {
@@ -155,6 +156,20 @@ public boolean execute() {
155156
}
156157
}
157158

159+
/**
160+
* A subclass can override this to e.g. determine that, based on the payload, the resource should not be undeployed,
161+
* in which case null should be returned.
162+
*
163+
* @param mgr
164+
* @param context
165+
* @param f
166+
* @param payload
167+
* @return
168+
*/
169+
protected String adjustPayloadBeforeDeletingResource(ResourceManager mgr, CommandContext context, File f, String payload) {
170+
return payload;
171+
}
172+
158173
public void setDeleteResourcesOnUndo(boolean deleteResourceOnUndo) {
159174
this.deleteResourcesOnUndo = deleteResourceOnUndo;
160175
}

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

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,57 @@
55
import com.marklogic.appdeployer.command.ResourceFilenameFilter;
66
import com.marklogic.appdeployer.command.SortOrderConstants;
77
import com.marklogic.mgmt.PayloadParser;
8-
import com.marklogic.mgmt.SaveReceipt;
98
import com.marklogic.mgmt.resource.ResourceManager;
109
import com.marklogic.mgmt.resource.appservers.ServerManager;
11-
import com.marklogic.mgmt.util.ObjectMapperFactory;
1210

1311
import java.io.File;
12+
import java.util.Arrays;
13+
import java.util.HashSet;
14+
import java.util.Set;
1415

1516
/**
1617
* "Other" = non-REST-API servers. This will process every JSON/XML file that's not named "rest-api-server.*" in the
1718
* servers directory.
1819
*/
1920
public class DeployOtherServersCommand extends AbstractResourceCommand {
2021

21-
public DeployOtherServersCommand() {
22-
setExecuteSortOrder(SortOrderConstants.DEPLOY_OTHER_SERVERS);
23-
setUndoSortOrder(SortOrderConstants.DELETE_OTHER_SERVERS);
24-
setRestartAfterDelete(true);
25-
setCatchExceptionOnDeleteFailure(true);
26-
setResourceFilenameFilter(new ResourceFilenameFilter("rest-api-server.xml", "rest-api-server.json"));
27-
}
28-
29-
@Override
30-
protected File[] getResourceDirs(CommandContext context) {
31-
return findResourceDirs(context.getAppConfig(), configDir -> configDir.getServersDir());
32-
}
33-
34-
@Override
35-
protected ResourceManager getResourceManager(CommandContext context) {
36-
return new ServerManager(context.getManageClient(), context.getAppConfig().getGroupName());
37-
}
38-
39-
@Override
40-
public Integer getUndoSortOrder() {
41-
return 0;
42-
}
22+
/**
23+
* Defines the server names that, by default, this command will never undeploy.
24+
*/
25+
private Set<String> defaultServersToNotUndeploy = new HashSet<>();
26+
27+
public DeployOtherServersCommand() {
28+
setExecuteSortOrder(SortOrderConstants.DEPLOY_OTHER_SERVERS);
29+
setUndoSortOrder(SortOrderConstants.DELETE_OTHER_SERVERS);
30+
setRestartAfterDelete(true);
31+
setCatchExceptionOnDeleteFailure(true);
32+
setResourceFilenameFilter(new ResourceFilenameFilter("rest-api-server.xml", "rest-api-server.json"));
33+
34+
initializeDefaultServersToNotUndeploy();
35+
}
36+
37+
protected void initializeDefaultServersToNotUndeploy() {
38+
defaultServersToNotUndeploy = new HashSet<>();
39+
defaultServersToNotUndeploy.add("Admin");
40+
defaultServersToNotUndeploy.add("App-Services");
41+
defaultServersToNotUndeploy.add("HealthCheck");
42+
defaultServersToNotUndeploy.add("Manage");
43+
}
44+
45+
@Override
46+
protected File[] getResourceDirs(CommandContext context) {
47+
return findResourceDirs(context.getAppConfig(), configDir -> configDir.getServersDir());
48+
}
49+
50+
@Override
51+
protected ResourceManager getResourceManager(CommandContext context) {
52+
return new ServerManager(context.getManageClient(), context.getAppConfig().getGroupName());
53+
}
54+
55+
@Override
56+
public Integer getUndoSortOrder() {
57+
return 0;
58+
}
4359

4460
/**
4561
* If the payload has a group-name that differs from the group name in the AppConfig, then this returns a new
@@ -58,4 +74,37 @@ protected ResourceManager adjustResourceManagerForPayload(ResourceManager mgr, C
5874
}
5975
return mgr;
6076
}
77+
78+
@Override
79+
protected String adjustPayloadBeforeDeletingResource(ResourceManager mgr, CommandContext context, File f, String payload) {
80+
String serverName = new PayloadParser().getPayloadFieldValue(payload, "server-name", false);
81+
if (serverName != null) {
82+
if (!shouldUndeployServer(serverName, context)) {
83+
logger.info(format("Not undeploying server %s because it's in the list of server names to not undeploy", serverName));
84+
return null;
85+
}
86+
}
87+
88+
return super.adjustPayloadBeforeDeletingResource(mgr, context, f, payload);
89+
}
90+
91+
public boolean shouldUndeployServer(String serverName, CommandContext context) {
92+
if (defaultServersToNotUndeploy != null && defaultServersToNotUndeploy.contains(serverName)) {
93+
return false;
94+
}
95+
96+
String[] names = null;
97+
if (context != null && context.getAppConfig() != null) {
98+
names = context.getAppConfig().getServersToNotUndeploy();
99+
}
100+
return names != null ? !Arrays.asList(names).contains(serverName) : true;
101+
}
102+
103+
public Set<String> getDefaultServersToNotUndeploy() {
104+
return defaultServersToNotUndeploy;
105+
}
106+
107+
public void setDefaultServersToNotUndeploy(Set<String> defaultServersToNotUndeploy) {
108+
this.defaultServersToNotUndeploy = defaultServersToNotUndeploy;
109+
}
61110
}

src/test/java/com/marklogic/appdeployer/DefaultAppConfigFactoryTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ public void allProperties() {
199199

200200
p.setProperty("mlUpdateMimetypeWhenPropertiesAreEqual", "true");
201201

202+
p.setProperty("mlServersToNotUndeploy", "server1,server2");
203+
202204
sut = new DefaultAppConfigFactory(new SimplePropertySource(p));
203205
AppConfig config = sut.newAppConfig();
204206

@@ -348,6 +350,10 @@ public void allProperties() {
348350
assertEquals("other-group", map.get("host2"));
349351

350352
assertTrue(config.isUpdateMimetypeWhenPropertiesAreEqual());
353+
354+
assertEquals(2, config.getServersToNotUndeploy().length);
355+
assertEquals("server1", config.getServersToNotUndeploy()[0]);
356+
assertEquals("server2", config.getServersToNotUndeploy()[1]);
351357
}
352358

353359
/**
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.marklogic.appdeployer.command.servers;
2+
3+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.appdeployer.AppConfig;
5+
import com.marklogic.appdeployer.ConfigDir;
6+
import com.marklogic.appdeployer.command.CommandContext;
7+
import com.marklogic.appdeployer.command.appservers.DeployOtherServersCommand;
8+
import com.marklogic.mgmt.resource.appservers.ServerManager;
9+
import org.junit.Test;
10+
11+
import java.io.File;
12+
import java.util.HashSet;
13+
import java.util.Set;
14+
15+
public class DontUndeploySpecificServersTest extends AbstractAppDeployerTest {
16+
17+
@Test
18+
public void test() {
19+
appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/sample-app/other-servers")));
20+
21+
ServerManager mgr = new ServerManager(manageClient);
22+
23+
DeployOtherServersCommand c = new DeployOtherServersCommand();
24+
initializeAppDeployer(c);
25+
26+
appConfig.getCustomTokens().put("%%ODBC_PORT%%", "8048");
27+
appConfig.getCustomTokens().put("%%XDBC_PORT%%", "8049");
28+
deploySampleApp();
29+
30+
try {
31+
assertTrue(mgr.exists("sample-app-xdbc"));
32+
assertTrue(mgr.exists("sample-app-odbc"));
33+
34+
appConfig.setServersToNotUndeploy("sample-app-xdbc", "sample-app-odbc");
35+
undeploySampleApp();
36+
assertTrue(mgr.exists("sample-app-xdbc"));
37+
assertTrue(mgr.exists("sample-app-odbc"));
38+
} finally {
39+
appConfig.setServersToNotUndeploy(null);
40+
undeploySampleApp();
41+
assertFalse(mgr.exists("sample-app-xdbc"));
42+
assertFalse(mgr.exists("sample-app-odbc"));
43+
}
44+
}
45+
46+
@Test
47+
public void unitTestShouldUndeployServer() {
48+
DeployOtherServersCommand c = new DeployOtherServersCommand();
49+
50+
AppConfig appConfig = new AppConfig();
51+
CommandContext context = new CommandContext(appConfig, null, null);
52+
53+
// These should all be not-undeployed by default
54+
assertFalse(c.shouldUndeployServer("Admin", null));
55+
assertFalse(c.shouldUndeployServer("App-Services", null));
56+
assertFalse(c.shouldUndeployServer("HealthCheck", null));
57+
assertFalse(c.shouldUndeployServer("Manage", null));
58+
59+
appConfig.setServersToNotUndeploy("server1", "server2");
60+
assertFalse(c.shouldUndeployServer("server1", context));
61+
assertFalse(c.shouldUndeployServer("server2", context));
62+
assertTrue(c.shouldUndeployServer("server3", context));
63+
64+
Set<String> customDefaultServersNotToUndeploy = new HashSet<>();
65+
customDefaultServersNotToUndeploy.add("Admin");
66+
customDefaultServersNotToUndeploy.add("TestServer");
67+
c.setDefaultServersToNotUndeploy(customDefaultServersNotToUndeploy);
68+
69+
assertFalse(c.shouldUndeployServer("Admin", null));
70+
assertFalse(c.shouldUndeployServer("TestServer", null));
71+
assertTrue(c.shouldUndeployServer("App-Services", null));
72+
assertTrue(c.shouldUndeployServer("HealthCheck", null));
73+
assertTrue(c.shouldUndeployServer("Manage", null));
74+
}
75+
}

0 commit comments

Comments
 (0)