Skip to content

Commit 6a11882

Browse files
rahulvudutalaSanjeevani19
authored andcommitted
DHFPROD-10454: Appservice Basepath Configurations
1 parent fe4f447 commit 6a11882

File tree

3 files changed

+164
-18
lines changed

3 files changed

+164
-18
lines changed
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
# Cloud Environment Properties
2-
mlCloudApiKey=admin:admin
3-
4-
# Basepath Configuration
5-
mlStagingBasePath=/data-hub/staging
6-
mlFinalBasePath=/data-hub/final
7-
mlJobBasePath=/data-hub/jobs
8-
mlManageBasePath=/local/manage
9-
mlAppServicesBasePath=/local/app-services
10-
mlAdminBasePath=/local/admin
11-
12-
# AppServer SSL configuration
13-
mlAdminSimpleSsl=true
14-
mlManageSimpleSsl=true
15-
mlAppServicesSimpleSsl=true
16-
mlAuthentication=cloud
17-
mlSslHostnameVerifier=ANY
1+
# Cloud Environment Properties
2+
mlHost=
3+
mlCloudApiKey=
4+
5+
# Basepath Configuration
6+
mlAdminBasePath=/ml/env1/mldb/0/admin/
7+
mlManageBasePath=/ml/env1/mldb/0/manage/
8+
mlAppServicesBasePath=/ml/env1/mldb/app-services/
9+
mlStagingBasePath=/ml/env1/mldb/staging/
10+
mlFinalBasePath=/ml/env1/mldb/final/
11+
mlJobBasePath=/ml/env1/mldb/jobs/
12+
13+
# AppServer SSL configuration
14+
mlAdminSimpleSsl=true
15+
mlManageSimpleSsl=true
16+
mlAppServicesSimpleSsl=true
17+
mlAuthentication=cloud
18+
mlSslHostnameVerifier=ANY
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.marklogic.hub.deploy.commands;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.node.ObjectNode;
6+
import com.marklogic.appdeployer.command.AbstractCommand;
7+
import com.marklogic.appdeployer.command.CommandContext;
8+
import com.marklogic.appdeployer.command.SortOrderConstants;
9+
import com.marklogic.client.DatabaseClient;
10+
import com.marklogic.hub.HubConfig;
11+
import com.marklogic.hub.impl.HubConfigImpl;
12+
import org.apache.commons.io.IOUtils;
13+
import org.apache.commons.lang3.StringUtils;
14+
import org.apache.http.HttpEntity;
15+
import org.apache.http.HttpResponse;
16+
import org.apache.http.NameValuePair;
17+
import org.apache.http.client.entity.UrlEncodedFormEntity;
18+
import org.apache.http.client.methods.HttpPost;
19+
import org.apache.http.entity.StringEntity;
20+
import org.apache.http.impl.client.CloseableHttpClient;
21+
import org.apache.http.impl.client.HttpClientBuilder;
22+
import org.apache.http.message.BasicNameValuePair;
23+
24+
import java.io.IOException;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
28+
/**
29+
* ConfigureAppServerBasePaths will configure the staging/final/jobs app-server basepaths in ML cloud environment
30+
* The basepaths are updated when mlAuthentication is set to cloud
31+
*/
32+
33+
public class ConfigureAppServerBasePaths extends AbstractCommand {
34+
35+
private final HubConfigImpl hubConfig;
36+
private ObjectMapper mapper = new ObjectMapper();
37+
38+
public ConfigureAppServerBasePaths(HubConfig hubConfig) {
39+
this.hubConfig = (HubConfigImpl) hubConfig;
40+
setExecuteSortOrder(SortOrderConstants.DEPLOY_OTHER_SERVERS + 1);
41+
}
42+
43+
@Override
44+
public void execute(CommandContext context) {
45+
if(StringUtils.equals("cloud", hubConfig.getMlAuthentication())) {
46+
updateAppServersBasePaths();
47+
waitForGateWayToRestart();
48+
}
49+
}
50+
51+
private void updateAppServersBasePaths() {
52+
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
53+
try (CloseableHttpClient httpClient = clientBuilder.build()) {
54+
String url = "https://" + hubConfig.getHost() + "/api/service/dataHubEndpoints";
55+
HttpPost postRequest = new HttpPost(url);
56+
StringEntity entity = new StringEntity(getBastPathConfig());
57+
postRequest.setHeader("Content-Type", "application/json");
58+
postRequest.setHeader("Authorization", "bearer " + getAccessToken());
59+
postRequest.setEntity(entity);
60+
httpClient.execute(postRequest);
61+
} catch (IOException e) {
62+
throw new RuntimeException(e);
63+
}
64+
}
65+
66+
private String getAccessToken() {
67+
String tokenRequestURL = "https://".concat(hubConfig.getHost()).concat("/token");
68+
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
69+
try (CloseableHttpClient httpClient = clientBuilder.build()) {
70+
{
71+
List<NameValuePair> postParams = new ArrayList<>();
72+
postParams.add(new BasicNameValuePair("grant_type", "apikey"));
73+
postParams.add(new BasicNameValuePair("key", hubConfig.getCloudApiKey()));
74+
75+
HttpPost postRequest = new HttpPost(tokenRequestURL);
76+
postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
77+
postRequest.setEntity(new UrlEncodedFormEntity(postParams));
78+
79+
HttpResponse response = httpClient.execute(postRequest);
80+
HttpEntity entity = response.getEntity();
81+
byte[] returnedData = IOUtils.toByteArray(entity.getContent());
82+
83+
JsonNode tokenMap = mapper.readTree(returnedData);
84+
logger.info("Access Token Map: " + tokenMap);
85+
return tokenMap.get("access_token").asText();
86+
}
87+
} catch (IOException e) {
88+
throw new RuntimeException(e);
89+
}
90+
}
91+
92+
private String getBastPathConfig() {
93+
ObjectNode baseConfig = mapper.createObjectNode();
94+
ObjectNode appServers = mapper.createObjectNode();
95+
ObjectNode jobServer = mapper.createObjectNode();
96+
ObjectNode stagingServer = mapper.createObjectNode();
97+
ObjectNode finalServer = mapper.createObjectNode();
98+
99+
String[] stagingBasePathArray = hubConfig.getStagingBasePath().split("/");
100+
String stagingBasePath = stagingBasePathArray[stagingBasePathArray.length - 1];
101+
102+
String[] finalBasePathArray = hubConfig.getFinalBasePath().split("/");
103+
String finalBasePath = finalBasePathArray[finalBasePathArray.length - 1];
104+
105+
String[] jobBasePathArray = hubConfig.getJobBasePath().split("/");
106+
String jobBasePath = jobBasePathArray[jobBasePathArray.length - 1];
107+
108+
baseConfig.put("adminPath", hubConfig.getAdminConfig().getBasePath());
109+
baseConfig.put("appServers", appServers);
110+
111+
appServers.put("staging", stagingServer);
112+
appServers.put("final", finalServer);
113+
appServers.put("jobs", jobServer);
114+
115+
stagingServer.put("name", hubConfig.getStagingDbName());
116+
stagingServer.put("path", stagingBasePath.concat("/"));
117+
118+
finalServer.put("name", hubConfig.getFinalDbName());
119+
finalServer.put("path", finalBasePath.concat("/"));
120+
121+
jobServer.put("name", hubConfig.getJobDbName());
122+
jobServer.put("path", jobBasePath.concat("/"));
123+
124+
return baseConfig.toString();
125+
}
126+
127+
private void waitForGateWayToRestart() {
128+
DatabaseClient databaseClient = hubConfig.newStagingClient();
129+
int maxTimeToWaitInMs = 90000;
130+
int maxRetries = 15;
131+
int sleepTime = maxTimeToWaitInMs/maxRetries;
132+
while(!databaseClient.checkConnection().isConnected() && maxRetries > 0) {
133+
logger.info("Checking gateway status: " + databaseClient.checkConnection().isConnected());
134+
try {
135+
Thread.sleep(sleepTime);
136+
} catch (InterruptedException e) {
137+
Thread.currentThread().interrupt();
138+
}
139+
maxRetries --;
140+
}
141+
logger.info("Checking gateway status: " + databaseClient.checkConnection().isConnected());
142+
}
143+
}

marklogic-data-hub/src/main/java/com/marklogic/hub/impl/DataHubImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.marklogic.hub.MarkLogicVersion;
6363
import com.marklogic.hub.dataservices.ArtifactService;
6464
import com.marklogic.hub.deploy.commands.CheckSecurityConfiguration;
65+
import com.marklogic.hub.deploy.commands.ConfigureAppServerBasePaths;
6566
import com.marklogic.hub.deploy.commands.CreateGranularPrivilegesCommand;
6667
import com.marklogic.hub.deploy.commands.DeployDatabaseFieldCommand;
6768
import com.marklogic.hub.deploy.commands.DeployHubTriggersCommand;
@@ -694,7 +695,7 @@ private void updateDatabaseCommandList(Map<String, List<Command>> commandMap) {
694695
commandMap.put("mlDatabaseCommands", dbCommands);
695696
}
696697

697-
private static void updateServerCommandList(Map<String, List<Command>> commandMap) {
698+
private void updateServerCommandList(Map<String, List<Command>> commandMap) {
698699
final String key = "mlServerCommands";
699700
List<Command> newCommands = new ArrayList<>();
700701
for (Command c : commandMap.get(key)) {
@@ -707,6 +708,7 @@ private static void updateServerCommandList(Map<String, List<Command>> commandMa
707708
}
708709
newCommands.add(c);
709710
}
711+
newCommands.add(new ConfigureAppServerBasePaths(hubConfig));
710712
commandMap.put(key, newCommands);
711713
}
712714

0 commit comments

Comments
 (0)