|
| 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 | +} |
0 commit comments