com.jelastic.api.system.service.utils.NodeUtils Maven / Gradle / Ivy
The newest version!
/*Server class MD5: 1f3b1ccc5076223bec0f21fe26612301*/
package com.jelastic.api.system.service.utils;
import com.jelastic.api.common.Constants;
import com.jelastic.api.core.utils.StringUtils;
import com.jelastic.api.data.po.DockerLink;
import com.jelastic.api.data.po.DockerLinkType;
import com.jelastic.api.data.po.DockerMetadata;
import com.jelastic.api.system.containermanager.types.ServiceAdapterConstants;
import com.jelastic.api.system.data.IpInfo;
import com.jelastic.api.system.po.HardwareNodeRole;
import com.jelastic.api.system.statistic.persistence.CurrentNodeStatistic;
import com.jelastic.api.utils.JavaNetUtils;
import com.jelastic.api.utils.Random;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.stream.Collectors;
import com.jelastic.api.system.persistence.*;
import java.util.*;
/**
* @name Jelastic API Client
* @version 8.11.2
* @copyright Jelastic, Inc.
*/
public class NodeUtils {
public static final String ENV_TYPE_NODE = "node";
public static final String NODE_GROUP_TEST = "node_group_test";
public static final String NODE_GROUP_COMPUTE = "cp";
public static final String ENV_TYPE_UNKNOWN = "unknown";
public static final String ENV_TYPE_RAW = "raw";
public static final String DEFAULT_BL_TYPE = "nginx";
public static final String NODE_TYPE_DOCKER = "docker";
public static final String NODE_MISSION_COMPUTE = "cp";
public static final String NODE_MISSION_SQL_DB = "sqldb";
public static final String NODE_MISSION_NOSQL_DB = "nosqldb";
public static final String NODE_MISSION_BALANCER = "bl";
public static final String NODE_MISSION_BUILD = "build";
public static final String NODE_MISSION_VDS = "vds";
public static final String NODE_MISSION_VPS = "vps";
public static final String NODE_MISSION_CACHE = "cache";
public static final String NODE_MISSION_DOCKER = "docker";
public static final String NODE_MISSION_STORAGE = "storage";
public static final String NODE_MISSION_EXTRA = "extra";
public static final String ENABLED = "enabled";
public static final String[] NODE_MISSIONS = { NODE_MISSION_COMPUTE, NODE_MISSION_SQL_DB, NODE_MISSION_NOSQL_DB, NODE_MISSION_BALANCER, NODE_MISSION_BUILD, NODE_MISSION_VDS, NODE_MISSION_VPS, NODE_MISSION_CACHE, NODE_MISSION_DOCKER, NODE_MISSION_EXTRA };
public static final String HTTPS_PROTOCOL_SEGMENT = "https://";
public static final String HTTP_PROTOCOL_SEGMENT = "http://";
public static final String NODE_DEFAULT_SUBDOMAIN = "node";
public static final String NODE_VDS_OLD_SUBDOMAIN = "vds";
public static final String NODE_RESOLVER_SUBDOMAIN = "rslv";
public static final String NODE_MAINTENANCE_SUBDOMAIN = "maintenance";
private static final String POM_FILE = "pom.xml";
public static final String[] WINDOWS_NODE_TYPES = { "iis8", "mssql", "mssqlweb", "mssqlstd", "windows2008", "windows2012" };
public static final Set NODE_TYPES_NO_FTP_SUPPORT;
public static final String CROSS_DEFAULT_SUBDOMAIN = "cross";
static {
NODE_TYPES_NO_FTP_SUPPORT = new HashSet<>();
NODE_TYPES_NO_FTP_SUPPORT.add("ubuntu-vps");
NODE_TYPES_NO_FTP_SUPPORT.add("debian-vps");
NODE_TYPES_NO_FTP_SUPPORT.add("vzlinux-vps");
}
private static final String NODE_URL_ENV_NAME_PLACEHOLDER = "{env-name}";
private static final String NODE_URL_PROTOCOL_PLACEHOLDER = "{protocol}";
public static final String PLACEHOLDER = "placeholder";
public static final String URL = "url";
public static final int MAX_HOSTNAME_LENGTH = 63;
public static final int MAX_SOFT_NODE_PROPERTIES_SUB_DOMAIN_LENGTH = (NODE_DEFAULT_SUBDOMAIN + EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_HYPHEN).length();
public static final int UNIX_MAX_HOSTNAME_LENGTH = 128;
public static final int VM_MAX_HOSTNAME_LENGTH = 40;
public static boolean isWinType(String nodeType) {
String foundNodeType = Arrays.stream(WINDOWS_NODE_TYPES).filter(x -> x.equals(nodeType)).findFirst().orElse(null);
return foundNodeType != null;
}
public static boolean equalsNodeType(SoftwareNode softwareNode, String nodeType) {
if (softwareNode.getNodeTypeAlias() != null && softwareNode.getNodeTypeAlias().equalsIgnoreCase(nodeType)) {
return true;
}
return softwareNode.getProperties().getNodeType().equalsIgnoreCase(nodeType);
}
public static boolean equalsNodeGroup(SoftwareNode softwareNode, String nodeGroup) {
return nodeGroup.equalsIgnoreCase(softwareNode.getNodeGroup());
}
public static boolean equalsHardwareNodeRole(HardwareNode hardwareNode, HardwareNodeRole hardwareNodeRole) {
Set hardwareNodeClusters = hardwareNode.getHardwareNodeClusters();
for (HardwareNodePcsCluster hardwareNodeCluster : hardwareNodeClusters) {
if ((HardwareNodeRole.CL == hardwareNodeRole && hardwareNodeCluster.isClient()) || (HardwareNodeRole.CS == hardwareNodeRole && hardwareNodeCluster.isChunk()) || (HardwareNodeRole.MDS == hardwareNodeRole && hardwareNodeCluster.isMDS())) {
return true;
}
}
return false;
}
public static boolean equalsNodeMission(SoftwareNode softwareNode, String nodeMission) {
return StringUtils.equalsIgnoreCase(softwareNode.getProperties().getNodeMission(), nodeMission);
}
public static boolean equalsNodeGroupAndMission(SoftwareNode softwareNode, String nodeGroupAndMission) {
return equalsNodeGroup(softwareNode, nodeGroupAndMission) && equalsNodeMission(softwareNode, nodeGroupAndMission);
}
public static boolean equalsAnyNodeMission(SoftwareNode softwareNode, String... nodeMissions) {
return equalsAnyNodeMission(softwareNode.getProperties().getNodeMission(), nodeMissions);
}
public static boolean equalsAnyNodeMission(String nodeMission, String... matchNodeMissions) {
for (String matchNodeMission : matchNodeMissions) {
if (nodeMission.equalsIgnoreCase(matchNodeMission)) {
return true;
}
}
return false;
}
public static boolean equalsAnyNodeGroup(SoftwareNode softwareNode, String... nodeGroups) {
for (String nodeGroup : nodeGroups) {
if (softwareNode.getNodeGroup().equalsIgnoreCase(nodeGroup)) {
return true;
}
}
return false;
}
public static Set selectByType(Set softwareNodes, TemplateType templateType) {
Set result = new HashSet();
for (SoftwareNode softwareNode : softwareNodes) {
if (softwareNode.getProperties().getType() == templateType) {
result.add(softwareNode);
}
}
return result;
}
public static boolean equalsType(SoftwareNode softwareNode, TemplateType templateType) {
if (softwareNode.getProperties().getType() == templateType) {
return true;
}
return false;
}
public static String createEnvUrl(AppNodes env) {
return (env.getSslState() ? HTTPS_PROTOCOL_SEGMENT : HTTP_PROTOCOL_SEGMENT) + env.getFullDomain().toLowerCase() + "/";
}
public static String createNodeUrl(String domain, String zone, SoftwareNode softwareNode, boolean isExtIpPresent, boolean sslState) {
String url = getProtocol(softwareNode, isExtIpPresent, sslState);
if (softwareNode.getProperties().getAdminUrlTemplate() != null) {
String adminUrlTemplate = softwareNode.getProperties().getAdminUrlTemplate();
url = parseAdminUrlPlaceholders(domain, zone, softwareNode, url, adminUrlTemplate);
} else {
url += makeNodeHost(softwareNode, domain, zone);
url += !StringUtils.isBlank(softwareNode.getProperties().getAdminConsolePort()) ? ":" + softwareNode.getProperties().getAdminConsolePort() : "";
url += !StringUtils.isBlank(softwareNode.getProperties().getDomainSuffix()) ? "/" + softwareNode.getProperties().getDomainSuffix() : "";
}
return url;
}
private static String getProtocol(SoftwareNode softwareNode, boolean isExtIpPresent, boolean sslState) {
String url = softwareNode.getProperties().getDomainPrefix() != null ? softwareNode.getProperties().getDomainPrefix() : HTTP_PROTOCOL_SEGMENT;
if (equalsAnyNodeMission(softwareNode, NODE_MISSION_SQL_DB, NODE_MISSION_NOSQL_DB)) {
url = isExtIpPresent ? HTTP_PROTOCOL_SEGMENT : HTTPS_PROTOCOL_SEGMENT;
} else if (sslState) {
url = NodeUtils.HTTPS_PROTOCOL_SEGMENT;
}
return url;
}
private static String parseAdminUrlPlaceholders(String domain, String zone, SoftwareNode softwareNode, String url, String adminUrlTemplate) {
adminUrlTemplate = adminUrlTemplate.replace(NODE_URL_PROTOCOL_PLACEHOLDER, url);
return adminUrlTemplate.replace(NODE_URL_ENV_NAME_PLACEHOLDER, makeNodeHost(softwareNode, domain, zone));
}
public static Map createNodeUrls(String domain, String zone, SoftwareNode softwareNode, boolean isExtIpPresent, boolean sslState) {
Map urls = new HashMap<>();
String adminUrlsTemplate = softwareNode.getProperties().getAdminUrlsTemplate();
if (!isJSONArray(adminUrlsTemplate)) {
return urls;
}
addUrlsToMap(urls, getJSONArray(adminUrlsTemplate), zone, domain, softwareNode, isExtIpPresent, sslState);
return urls;
}
private static void addUrlsToMap(Map urls, JSONArray array, String zone, String domain, SoftwareNode softwareNode, boolean isExtIpPresent, boolean sslState) {
for (int i = 0; i < array.length(); i++) {
addUrlToMap(urls, array, zone, domain, softwareNode, isExtIpPresent, sslState, i);
}
}
private static void addUrlToMap(Map urls, JSONArray array, String zone, String domain, SoftwareNode softwareNode, boolean isExtIpPresent, boolean sslState, int i) {
JSONObject element = array.optJSONObject(i);
String key = element.optString(PLACEHOLDER);
String url = getProtocol(softwareNode, isExtIpPresent, sslState);
String value = parseAdminUrlPlaceholders(domain, zone, softwareNode, url, element.optString(URL));
if (key.isEmpty() || value.isEmpty()) {
return;
}
urls.put(key, value);
}
public static boolean isJSONArray(String adminUrlsTemplate) {
if (adminUrlsTemplate == null) {
return false;
}
try {
new JSONArray(adminUrlsTemplate);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static JSONArray getJSONArray(String adminUrlsTemplate) {
try {
return new JSONArray(adminUrlsTemplate);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
public static String getSubdomainCheckRegexp(String subdomain) {
if (subdomain.equals("das")) {
return "^(?i)" + subdomain + EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_HYPHEN + "(.+)?";
}
if (subdomain.equals(NODE_RESOLVER_SUBDOMAIN)) {
return "^(?i)" + subdomain + "\\d*$";
}
if (subdomain.equals(NODE_MAINTENANCE_SUBDOMAIN)) {
return "^(?i)" + subdomain + "$";
}
return "^(?i)" + subdomain + "\\d*" + EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_HYPHEN + "(.+)?";
}
public static String makeNodeHost(SoftwareNode softwareNode, String domain, String zone, String subdomain) {
if (subdomain == null) {
subdomain = softwareNode.getProperties().getSubdomain();
}
if (StringUtils.isBlank(subdomain)) {
subdomain = NodeUtils.NODE_DEFAULT_SUBDOMAIN;
}
if (NodeUtils.equalsAnyNodeMission(softwareNode, NodeUtils.NODE_MISSION_VDS, NodeUtils.NODE_MISSION_SQL_DB, NodeUtils.NODE_MISSION_NOSQL_DB, NodeUtils.NODE_MISSION_CACHE, NodeUtils.NODE_MISSION_DOCKER) || StringUtils.isBlank(softwareNode.getProperties().getSubdomain())) {
subdomain += softwareNode.getId();
}
if (!subdomain.isEmpty()) {
subdomain += EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_HYPHEN;
}
return subdomain + domain.toLowerCase() + "." + zone;
}
public static String makeNodeHost(SoftwareNode softwareNode, String domain, String zone) {
return makeNodeHost(softwareNode, domain, zone, null);
}
public static String makeAdminNodeUrl(SoftwareNode softwareNode, String environmentDomain, String zone, boolean sslState, boolean isExtIpPresent) {
return makeAdminNodeUrl(softwareNode.getProperties().getNodeMission(), softwareNode.getId(), environmentDomain, zone, sslState, isExtIpPresent);
}
public static String makeAdminNodeUrl(String nodeMission, int softwareNodeId, String environmentDomain, String zone, boolean sslState, boolean isExtIpPresent) {
String url = HTTP_PROTOCOL_SEGMENT;
if (equalsAnyNodeMission(nodeMission, NODE_MISSION_SQL_DB, NODE_MISSION_NOSQL_DB)) {
url = isExtIpPresent ? HTTP_PROTOCOL_SEGMENT : HTTPS_PROTOCOL_SEGMENT;
} else if (sslState) {
url = NodeUtils.HTTPS_PROTOCOL_SEGMENT;
}
return makeNodeUrlString(url, softwareNodeId, environmentDomain, zone);
}
public static String makeDefaultNodeHost(SoftwareNode softwareNode, String environmentDomain, String zone) {
return makeDefaultNodeHost(softwareNode, environmentDomain, zone, EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_HYPHEN);
}
public static String makeDefaultNodeHostWithDot(SoftwareNode softwareNode, String environmentDomain, String zone) {
return makeDefaultNodeHost(softwareNode, environmentDomain, zone, EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_DOT);
}
public static String makeDefaultNodeHost(SoftwareNode softwareNode, String envFullDomain) {
return makeDefaultNodeHostByFullDomain(softwareNode, envFullDomain, EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_HYPHEN);
}
public static String makeDefaultNodeHostWithDot(SoftwareNode softwareNode, String envFullDomain) {
return makeDefaultNodeHostByFullDomain(softwareNode, envFullDomain, EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_DOT);
}
public static String makeDefaultNodeHost(SoftwareNode softwareNode, String environmentDomain, String zone, String separator) {
return makeDefaultNodeHost(softwareNode, environmentDomain, zone, separator, false);
}
public static String makeNodeGroupHost(SoftwareNode softwareNode, String environmentDomain, String zone) {
return softwareNode.getNodeGroup() + Constants.DOT_SEPARATOR + NodeUtils.makeEnvHost(environmentDomain, zone);
}
public static String makeNodeGroupCrossServiceHost(SoftwareNode softwareNode, String environmentDomain, String zone) {
return CROSS_DEFAULT_SUBDOMAIN + Constants.DOT_SEPARATOR + makeNodeGroupHost(softwareNode, environmentDomain, zone);
}
public static Set makeNodeGroupCrossServiceHosts(SoftwareNode softwareNode, String environmentDomain, Set zones) {
HashSet hosts = new HashSet<>();
for (String zone : zones) {
hosts.add(makeNodeGroupCrossServiceHost(softwareNode, environmentDomain, zone));
}
return hosts;
}
public static String makeDefaultNodeHost(SoftwareNode softwareNode, String environmentDomain, String zone, String separator, boolean shortFormat) {
String shortDomain = NODE_DEFAULT_SUBDOMAIN + softwareNode.getId();
return shortFormat ? shortDomain : (shortDomain + separator + environmentDomain.toLowerCase() + EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_DOT + zone);
}
public static String makeDefaultNodeHostByFullDomain(SoftwareNode softwareNode, String envFullDomain, String separator) {
return NODE_DEFAULT_SUBDOMAIN + softwareNode.getId() + separator + envFullDomain;
}
public static String makeEnvHost(String environmentDomain, String zone) {
return makeEnvHost(environmentDomain, zone, false);
}
public static String makeEnvHost(String environmentDomain, String zone, boolean convertToPunycode) {
String host = environmentDomain.toLowerCase() + Constants.DOT_SEPARATOR + zone;
return convertToPunycode ? JavaNetUtils.convertToPunycode(host) : host;
}
public static Set makeAllEnvHosts(AppNodes env) {
return makeEnvHosts(env, false, false, true);
}
public static Set makeAllEnvHosts(AppNodes env, boolean inShort) {
return makeEnvHosts(env, false, inShort, true);
}
public static Set makeEnvHosts(AppNodes env) {
return makeEnvHosts(env, false, false, false);
}
public static Set makeEnvHosts(AppNodes env, boolean inShort) {
return makeEnvHosts(env, false, inShort, false);
}
public static Set makeEnvHosts(AppNodes env, boolean convertToPunycode, boolean inShort, boolean forAllDomains) {
Set domainSet = forAllDomains ? env.getAllDomains() : env.getDomains();
if (inShort) {
return domainSet.stream().map(d -> convertToPunycode ? JavaNetUtils.convertToPunycode(d.getDomain()) : d.getDomain()).collect(Collectors.toSet());
}
Set envs = new LinkedHashSet<>();
for (String zone : env.getZones()) {
Set domainsZone = domainSet.stream().map(d -> makeEnvHost(d.getDomain(), zone, convertToPunycode)).collect(Collectors.toSet());
envs.addAll(domainsZone);
}
return envs;
}
public static String makeOldNodeHostNullable(SoftwareNode softwareNode, String domain, String zone) {
String subdomain = StringUtils.trimToEmpty(softwareNode.getProperties().getSubdomain());
if (subdomain.isEmpty()) {
return null;
}
if (NodeUtils.equalsNodeMission(softwareNode, NodeUtils.NODE_MISSION_COMPUTE) && NodeUtils.isClusterEnabled(softwareNode) && !softwareNode.isMaster()) {
return null;
}
return subdomain + EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_HYPHEN + domain.toLowerCase() + "." + zone;
}
public static String makeNodeHostNullable(SoftwareNode softwareNode, String domain, String zone) {
String subdomain = StringUtils.trimToEmpty(softwareNode.getProperties().getSubdomain());
if (subdomain.isEmpty()) {
return null;
}
return subdomain + softwareNode.getId() + EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_HYPHEN + domain.toLowerCase() + "." + zone;
}
public static String makeClusterNodeHostNullable(SoftwareNode softwareNode, String domain, String zone) {
String subdomain = StringUtils.trimToEmpty(softwareNode.getProperties().getSubdomain());
if (subdomain.isEmpty()) {
return null;
}
return subdomain + EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_HYPHEN + domain.toLowerCase() + "." + zone;
}
public static boolean isContextValid(String context, SoftwareNode softwareNode) {
String validatorRegex = softwareNode.getProperties().getContextValidatorRegex();
return context.matches(validatorRegex == null ? SoftNodeProperties.DEFAULT_CONTEXT_VALIDATOR : validatorRegex);
}
public static String normalizeContext(String context) {
if (StringUtils.isBlank(context)) {
return Constants.CONTEXT_ROOT;
}
if (context.startsWith("/") || context.startsWith("\\")) {
context = context.substring(1);
}
return context.equalsIgnoreCase(Constants.CONTEXT_ROOT) ? Constants.CONTEXT_ROOT : context;
}
public static Set selectSoftwareNodesByType(Set softwareNodes, TemplateType templateType) {
Set selectedSoftwareNodes = new HashSet();
for (SoftwareNode softwareNode : softwareNodes) {
if (NodeUtils.equalsType(softwareNode, templateType) && !softwareNode.isDeleted()) {
selectedSoftwareNodes.add(softwareNode);
}
}
return selectedSoftwareNodes;
}
public static Set selectSoftwareNodes(Set softwareNodes, String... nodeMissions) {
Set selectedSoftwareNodes = new HashSet();
for (SoftwareNode softwareNode : softwareNodes) {
if (NodeUtils.equalsAnyNodeMission(softwareNode, nodeMissions) && !softwareNode.isDeleted()) {
selectedSoftwareNodes.add(softwareNode);
}
}
return selectedSoftwareNodes;
}
public static Set selectSoftwareNodes(Set softwareNodes, String nodeMission, boolean isMaster) {
Set selectedSoftwareNodes = new HashSet();
for (SoftwareNode softwareNode : softwareNodes) {
if (softwareNode.isMaster() == isMaster && NodeUtils.equalsNodeMission(softwareNode, nodeMission) && !softwareNode.isDeleted()) {
selectedSoftwareNodes.add(softwareNode);
}
}
return selectedSoftwareNodes;
}
public static SoftwareNode findSoftwareNode(AppNodes env, int nodeId) {
return findSoftwareNode(env.getNodes(), nodeId);
}
public static SoftwareNode findSoftwareNode(Collection softwareNodes, int nodeId) {
for (SoftwareNode softwareNode : softwareNodes) {
if (softwareNode.getId() == nodeId) {
return softwareNode;
}
}
return null;
}
public static Set selectEntryPoints(AppNodes env) {
return selectEntryPoints(env.getNodes());
}
public static Set selectEntryPoints(Set softwareNodes) {
Set balancers = selectSoftwareNodesByNodeGroup(softwareNodes, NODE_MISSION_BALANCER);
if (!balancers.isEmpty()) {
return balancers;
}
return selectSoftwareNodesByNodeGroup(softwareNodes, NODE_MISSION_COMPUTE);
}
public static boolean isEntryPoint(AppNodes env, SoftwareNode softNode) {
if (NodeUtils.equalsNodeGroup(softNode, NODE_MISSION_BALANCER)) {
return true;
}
Set entryPoints = selectEntryPoints(env);
return entryPoints.contains(softNode);
}
public static Set selectSoftwareNodesByNodeType(Set softwareNodes, String nodeType) {
Set selectedSoftwareNodes = new HashSet();
for (SoftwareNode softwareNode : softwareNodes) {
if (NodeUtils.equalsNodeType(softwareNode, nodeType)) {
selectedSoftwareNodes.add(softwareNode);
}
}
return selectedSoftwareNodes;
}
public static Set selectSoftwareNodesByNodeGroup(Set softwareNodes, String nodeGroup) {
Set selectedSoftwareNodes = new HashSet<>();
for (SoftwareNode softwareNode : softwareNodes) {
if (NodeUtils.equalsNodeGroup(softwareNode, nodeGroup)) {
selectedSoftwareNodes.add(softwareNode);
}
}
return selectedSoftwareNodes;
}
public static Set selectSoftwareNodesByNodeGroup(Set softwareNodes, SoftNodeGroup nodeGroup) {
Set selectedSoftwareNodes = new HashSet<>();
for (SoftwareNode softwareNode : softwareNodes) {
if (softwareNode.getSoftNodeGroup().getName().equals(nodeGroup.getName())) {
selectedSoftwareNodes.add(softwareNode);
}
}
return selectedSoftwareNodes;
}
public static Set selectSoftwareNodesByNodeGroupAndNodeMission(Set softwareNodes, String nodeGroupAndMission) {
Set selectedSoftwareNodes = new HashSet<>();
for (SoftwareNode softwareNode : softwareNodes) {
if (NodeUtils.equalsNodeGroup(softwareNode, nodeGroupAndMission) && NodeUtils.equalsNodeMission(softwareNode, nodeGroupAndMission)) {
selectedSoftwareNodes.add(softwareNode);
}
}
return selectedSoftwareNodes;
}
public static Set selectSoftwareNodesByNodeGroupAndNodeMission(Set softwareNodes, String nodeGroup, String nodeMission) {
Set selectedSoftwareNodes = new HashSet<>();
for (SoftwareNode softwareNode : softwareNodes) {
if (NodeUtils.equalsNodeGroup(softwareNode, nodeGroup) && NodeUtils.equalsNodeMission(softwareNode, nodeMission)) {
selectedSoftwareNodes.add(softwareNode);
}
}
return selectedSoftwareNodes;
}
public static SoftwareNode selectSoftwareNodesByNodeGroup(Set softwareNodes, String nodeGroup, Boolean isMaster) {
for (SoftwareNode softwareNode : softwareNodes) {
if (NodeUtils.equalsNodeGroup(softwareNode, nodeGroup) && (isMaster == null || softwareNode.isMaster() == isMaster)) {
return softwareNode;
}
}
return null;
}
public static SoftwareNode selectMasterSoftwareNode(Set softwareNodes) {
for (SoftwareNode softwareNode : softwareNodes) {
if (softwareNode.isMaster()) {
return softwareNode;
}
}
return null;
}
public static Set selectSoftwareNodes(Set softwareNodes, String nodeType, String nodeGroup, int nodeId) {
Set selectedSoftwareNodes = new HashSet();
if (nodeId > 0) {
SoftwareNode softwareNode = findSoftwareNode(softwareNodes, nodeId);
if (softwareNode != null) {
selectedSoftwareNodes.add(softwareNode);
}
} else if (StringUtils.isNotBlank(nodeGroup)) {
selectedSoftwareNodes.addAll(NodeUtils.selectSoftwareNodesByNodeGroup(softwareNodes, nodeGroup));
} else if (StringUtils.isNotBlank(nodeType)) {
selectedSoftwareNodes.addAll(NodeUtils.selectSoftwareNodesByNodeType(softwareNodes, nodeType));
}
return selectedSoftwareNodes;
}
public static String findNodeMission(Set softwareNodes, String nodeType) {
for (SoftwareNode softwareNode : softwareNodes) {
if (softwareNode.getProperties().getNodeType().equalsIgnoreCase(nodeType)) {
return softwareNode.getProperties().getNodeMission();
}
}
return null;
}
public static String getNodeName(SoftwareNode softNode) {
if (softNode == null) {
return "";
}
String nodeName = softNode.getProperties().getName();
if (NodeUtils.equalsNodeMission(softNode, NodeUtils.NODE_MISSION_DOCKER) && softNode.getDockerMetadata() != null) {
DockerMetadata dockerMetadata = (DockerMetadata) softNode.getDockerMetadata();
nodeName = dockerMetadata.getFullName();
}
return nodeName;
}
public static String getNodeVersion(SoftwareNode softNode) {
if (softNode == null) {
return "";
}
String nodeVersion = softNode.getProperties().getVersion();
String customItem = softNode.getRawCustomItem();
if (NodeUtils.equalsNodeMission(softNode, NodeUtils.NODE_MISSION_DOCKER) && softNode.getDockerMetadata() != null) {
DockerMetadata dockerMetadata = (DockerMetadata) softNode.getDockerMetadata();
nodeVersion = dockerMetadata.getTag();
} else if (customItem != null) {
try {
JSONObject jsonObject = new JSONObject(customItem);
if (jsonObject.has(CurrentNodeStatistic.NODE_VERSION)) {
String customItemVersion = jsonObject.getString(CurrentNodeStatistic.NODE_VERSION);
if (customItemVersion != null) {
nodeVersion = customItemVersion;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return nodeVersion;
}
public static String makeContainerName(int ctid, String nodeType, String envShortDomain) {
return makeContainerName(ctid, nodeType, envShortDomain, UNIX_MAX_HOSTNAME_LENGTH);
}
public static String makeContainerName(int ctid, String nodeType, String envShortDomain, int maxLength) {
String firstPart = nodeType + ServiceAdapterConstants.CONTAINER__DOT_DELIMITER + ctid + ServiceAdapterConstants.CONTAINER__DOT_DELIMITER;
String name = firstPart + envShortDomain.toLowerCase();
if (name.length() > maxLength) {
String randomStr = Random.getSecurePswd(3, 3, 3);
int envNameLength = maxLength - (firstPart.length() + 10);
name = firstPart + envShortDomain.toLowerCase().substring(0, envNameLength) + Constants.HYPHEN_SEPARATOR + randomStr;
}
return name;
}
private static String makeContainerHostnameInner(OsNode osNode, String hostname, String regionDomain, int maxNodeNameLength) {
SoftwareNode node = osNode.getSoftNode();
if (node != null) {
String correctHostName = makeHostNameCorrectLength(JavaNetUtils.convertToPunycode(hostname), maxNodeNameLength);
return NodeUtils.makeDefaultNodeHost(node, correctHostName, regionDomain);
}
return null;
}
public static String makeContainerHostname(OsNode osNode, String envShortDomain, String zone, int maxNodeNameLength) {
return makeContainerHostnameInner(osNode, envShortDomain, zone, maxNodeNameLength);
}
public static Set selectSoftwareNodeIds(Collection softwareNodes) {
Set softNodeIds = new HashSet();
for (SoftwareNode softwareNode : softwareNodes) {
softNodeIds.add(softwareNode.getId());
}
return softNodeIds;
}
public static boolean isSupportedDockerFunctionality(SoftwareNode softwareNode) {
return NodeUtils.equalsNodeMission(softwareNode, NodeUtils.NODE_MISSION_DOCKER) || softwareNode.getProperties().getType() == TemplateType.DOCKERIZED;
}
public static void sortSoftwareNodesByIsMaster(List softwareNodes) {
softwareNodes.sort((o1, o2) -> {
if (o1.isMaster() == o2.isMaster()) {
return 0;
}
if (o2.isMaster()) {
return 1;
}
return -1;
});
}
public static void sortSoftwareNodesByIsMasterAndId(List softwareNodes) {
softwareNodes.sort((o1, o2) -> {
if (o1.isMaster() == o2.isMaster()) {
return Integer.compare(o1.getId(), o2.getId());
}
if (o2.isMaster()) {
return 1;
}
return -1;
});
}
public static void sortSoftwareNodesByIsMasterDesc(List softwareNodes) {
softwareNodes.sort((o1, o2) -> {
if (o1.isMaster() == o2.isMaster()) {
return 0;
}
if (o1.isMaster()) {
return 1;
}
return -1;
});
}
public static boolean useDockerCreationFlow(TemplateType templateType, String nodeMission) {
boolean isDockerizedTemplate = templateType == TemplateType.DOCKERIZED;
boolean isDockerByMission = NodeUtils.NODE_MISSION_DOCKER.equals(nodeMission) || templateType == TemplateType.DOCKER;
return isDockerizedTemplate || isDockerByMission;
}
public static JSONObject getEnvVarsJsonObject(SoftwareNode softwareNode, OsNode osNode, String envDomain) throws JSONException {
JSONObject envVarsJsonObject = new JSONObject();
envVarsJsonObject.put(Constants.MASTER_ID_ENV_VAR, softwareNode.getId() + "");
envVarsJsonObject.put(Constants.MASTER_HOST_ENV_VAR, NodeUtils.makeDefaultNodeHost(softwareNode, envDomain, null, EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_HYPHEN, true));
envVarsJsonObject.put(Constants.MASTER_IP_ENV_VAR, osNode.getLocalAddressIfPossible());
return envVarsJsonObject;
}
public static String normalizeWorkingDirectory(String workDir) {
if (StringUtils.isNotBlank(workDir)) {
if (workDir.startsWith("/")) {
workDir = workDir.replaceFirst("/", "");
}
if (!workDir.endsWith("/")) {
if (workDir.endsWith(POM_FILE)) {
workDir = workDir.replaceAll(POM_FILE, "");
} else {
workDir = workDir + "/";
}
}
}
return workDir;
}
public static boolean isValidWorkingDirectory(String workDir) {
if (!workDir.isEmpty()) {
return workDir.matches(SoftNodeProperties.DEFAULT_WORK_DIR_VALIDATOR);
}
return true;
}
public static boolean useStorageDiskLimit(SoftwareNode softwareNode) {
return useStorageDiskLimit(softwareNode.getProperties().getNodeMission(), softwareNode.getNodeGroup());
}
public static boolean useStorageDiskLimit(String nodeMission, String nodeGroup) {
return NODE_MISSION_STORAGE.equalsIgnoreCase(nodeMission) || (NODE_MISSION_DOCKER.equalsIgnoreCase(nodeMission) && NODE_MISSION_STORAGE.equals(nodeGroup));
}
public static List selectIpsInfo(OsNode osNode, boolean external, boolean internal) {
return selectIpsInfo(osNode, external, internal, false);
}
public static List selectIpsInfo(OsNode osNode, boolean external, boolean internal, boolean crossService) {
List ips = new ArrayList();
if (internal && osNode.getIpAddress() != null) {
ips.add(new IpInfo(osNode.getIpAddress().getIpAddress(), IpInfo.IpAddressUseType.INTERNAL));
}
if (external) {
for (ExtIpAddress extIpAddress : osNode.getAllExtIpAddresses()) {
ips.add(new IpInfo(extIpAddress.getIpAddress(), IpInfo.IpAddressUseType.EXTERNAL, extIpAddress.getType()));
}
}
if (crossService) {
for (CrossNetworkIpAddress crossNetworkIpAddress : osNode.getCrossNetworkIpAddresses()) {
ips.add(new IpInfo(crossNetworkIpAddress.getIpAddress(), IpInfo.IpAddressUseType.CROSS_SERVICE));
}
}
return ips;
}
public static Map> getEntryPointsPrivatePublicIPs(List osNodes) {
Map> ipsMap = new HashMap<>();
for (OsNode osNode : osNodes) {
String intIp = null;
Set extIps = new HashSet<>();
List ipInfos = selectIpsInfo(osNode, true, true);
for (IpInfo ipInfo : ipInfos) {
if (ipInfo.isInternal()) {
intIp = ipInfo.getIp();
} else {
extIps.add(ipInfo.getIp());
}
}
if (intIp != null) {
ipsMap.put(intIp, extIps);
}
}
return ipsMap;
}
public static boolean isClusterSupport(SoftwareNode softwareNode) {
if (softwareNode == null || softwareNode.getProperties() == null) {
return false;
}
boolean isClusterSupport = softwareNode.getProperties().isClusterSupport();
if (!isClusterSupport) {
return false;
}
SoftNodeGroup softNodeGroup = softwareNode.getSoftNodeGroup();
if (softNodeGroup == null) {
return isClusterSupport;
}
SoftNodeGroupProperty clusterSupportProperty = softNodeGroup.getPropertyByName(SoftNodeGroup.CLUSTER_SUPPORT_PROPERTY);
if (clusterSupportProperty != null && (clusterSupportProperty.getType() == SoftNodeGroupPropertyType.BOOLEAN || clusterSupportProperty.getType() == SoftNodeGroupPropertyType.STRING) && StringUtils.isNotBlank(clusterSupportProperty.getValue()) && String.valueOf(Boolean.FALSE).equalsIgnoreCase(clusterSupportProperty.getValue())) {
return false;
}
return isClusterSupport;
}
public static boolean isClusterEnabled(SoftwareNode softwareNode) {
boolean isClusterSupport = isClusterSupport(softwareNode);
if (!isClusterSupport) {
return false;
}
SoftNodeGroup softNodeGroup = softwareNode.getSoftNodeGroup();
if (softNodeGroup == null) {
return isClusterSupport;
}
SoftNodeGroupProperty clusterProperty = softNodeGroup.getPropertyByName(SoftNodeGroup.CLUSTER_PROPERTY);
if (clusterProperty == null || clusterProperty.getType() != SoftNodeGroupPropertyType.JSON_OBJECT || StringUtils.isBlank(clusterProperty.getValue())) {
return isClusterSupport;
}
try {
JSONObject clusterJSON = new JSONObject(clusterProperty.getValue());
if (!clusterJSON.isNull(ENABLED)) {
return clusterJSON.getBoolean(ENABLED);
}
} catch (JSONException e) {
e.printStackTrace();
}
return isClusterSupport;
}
public static boolean skipAddNodeEmail(SoftwareNode softwareNode) {
SoftNodeGroup softNodeGroup = softwareNode.getSoftNodeGroup();
if (softNodeGroup == null) {
return false;
}
SoftNodeGroupProperty skipEmailsProperty = softNodeGroup.getPropertyByName(SoftNodeGroup.SKIP_NODE_EMAILS_PROPERTY);
if (skipEmailsProperty == null || !(skipEmailsProperty.getType() == SoftNodeGroupPropertyType.BOOLEAN || skipEmailsProperty.getType() == SoftNodeGroupPropertyType.STRING) || StringUtils.isBlank(skipEmailsProperty.getValue())) {
return false;
}
return Boolean.valueOf(skipEmailsProperty.getValue());
}
public static String getAdminUrl(SoftwareNode softwareNode, String defaultUrl) {
SoftNodeGroup softNodeGroup = softwareNode.getSoftNodeGroup();
if (softNodeGroup == null) {
return defaultUrl;
}
SoftNodeGroupProperty adminUrlProperty = softNodeGroup.getPropertyByName(SoftNodeGroup.ADMIN_URL_PROPERTY);
if (adminUrlProperty == null || adminUrlProperty.getValue() == null) {
return defaultUrl;
}
return StringUtils.stripToEmpty(adminUrlProperty.getValue());
}
public static boolean isRedeploySupport(SoftwareNode softwareNode) {
if (!(NodeUtils.equalsNodeMission(softwareNode, NodeUtils.NODE_MISSION_DOCKER) || softwareNode.getProperties().getType() == TemplateType.DOCKERIZED)) {
return false;
}
SoftNodeGroup softNodeGroup = softwareNode.getSoftNodeGroup();
if (softNodeGroup == null) {
return true;
}
SoftNodeGroupProperty redeploySupportProperty = softNodeGroup.getPropertyByName(SoftNodeGroup.REDEPLOY_SUPPORT_PROPERTY);
if (redeploySupportProperty != null && (redeploySupportProperty.getType() == SoftNodeGroupPropertyType.BOOLEAN || redeploySupportProperty.getType() == SoftNodeGroupPropertyType.STRING) && StringUtils.isNotBlank(redeploySupportProperty.getValue()) && String.valueOf(Boolean.FALSE).equalsIgnoreCase(redeploySupportProperty.getValue())) {
return false;
}
return true;
}
public static boolean isDeploySupport(SoftwareNode softwareNode) {
SoftNodeGroup softNodeGroup = softwareNode.getSoftNodeGroup();
if (softNodeGroup == null) {
return true;
}
SoftNodeGroupProperty deploySupportProperty = softNodeGroup.getPropertyByName(SoftNodeGroup.DEPLOY_SUPPORT_PROPERTY);
if (deploySupportProperty != null && (deploySupportProperty.getType() == SoftNodeGroupPropertyType.BOOLEAN || deploySupportProperty.getType() == SoftNodeGroupPropertyType.STRING) && StringUtils.isNotBlank(deploySupportProperty.getValue()) && String.valueOf(Boolean.FALSE).equalsIgnoreCase(deploySupportProperty.getValue())) {
return false;
}
return true;
}
private static String getDefaultNodeGroupDomain(SoftNodeGroup nodeGroup, boolean inShort, String envFullDomain) {
if (nodeGroup == null) {
return null;
}
String nodeGroupLowerCase = nodeGroup.getName().toLowerCase();
return inShort ? nodeGroupLowerCase : NodeUtils.makeEnvHost(nodeGroupLowerCase, envFullDomain);
}
private static Set getNodeGroupDomains(SoftNodeGroup nodeGroup, boolean inShort, boolean includeDefault, String envFullDomain, boolean primaryEnvDomain) {
if (nodeGroup == null) {
return Collections.emptySet();
}
LinkedHashSet nodeGroupDomains = new LinkedHashSet<>();
if (includeDefault) {
addIgnoreNull(nodeGroupDomains, getDefaultNodeGroupDomain(nodeGroup, inShort, envFullDomain));
}
nodeGroupDomains.addAll(getDomains(nodeGroup.getSubdomains(), inShort, envFullDomain, primaryEnvDomain));
return nodeGroupDomains;
}
public static Set getNodeGroupDomains(SoftNodeGroup nodeGroup, boolean inShort, boolean includeDefault, String primaryEnvDomain, Set envDomains) {
if (nodeGroup == null) {
return Collections.emptySet();
}
LinkedHashSet nodeGroupDomains = new LinkedHashSet<>();
if (includeDefault) {
addIgnoreNull(nodeGroupDomains, getDefaultNodeGroupDomain(nodeGroup, inShort, primaryEnvDomain));
}
for (String envDomain : envDomains) {
boolean isPrimaryEnvDomain = StringUtils.equals(envDomain, primaryEnvDomain);
nodeGroupDomains.addAll(getNodeGroupDomains(nodeGroup, inShort, includeDefault, envDomain, isPrimaryEnvDomain));
}
return nodeGroupDomains;
}
public static String getDefaultShortNodeDomain(SoftwareNode softwareNode) {
return NodeUtils.NODE_DEFAULT_SUBDOMAIN + softwareNode.getId();
}
private static Set getDefaultNodeDomains(SoftwareNode softwareNode, boolean inShort, String envFullDomain) {
LinkedHashSet nodeDomains = new LinkedHashSet<>();
String defaultDomain = NodeUtils.NODE_DEFAULT_SUBDOMAIN + softwareNode.getId();
if (inShort) {
nodeDomains.add(defaultDomain);
} else {
nodeDomains.add(NodeUtils.makeDefaultNodeHost(softwareNode, envFullDomain));
nodeDomains.add(NodeUtils.makeDefaultNodeHostWithDot(softwareNode, envFullDomain));
}
return nodeDomains;
}
public static Set getNodeDomains(SoftwareNode softwareNode, boolean inShort, boolean includeDefault, String envFullDomain, boolean primaryEnvDomain) {
LinkedHashSet nodeDomains = new LinkedHashSet<>();
if (includeDefault) {
nodeDomains.addAll(getDefaultNodeDomains(softwareNode, inShort, envFullDomain));
}
nodeDomains.addAll(getDomains(softwareNode.getSubdomains(), inShort, envFullDomain, primaryEnvDomain));
return nodeDomains;
}
private static Set getDomains(Set subdomains, boolean inShort, String envFullDomain, boolean primaryEnvDomain) {
if (subdomains.isEmpty()) {
return Collections.emptySet();
}
Set domains = new HashSet<>();
for (Subdomain subdomain : subdomains) {
if (inShort) {
domains.add(subdomain.getDomain());
continue;
}
if (subdomain.isTopLevel()) {
if (primaryEnvDomain) {
domains.add(makeHost(subdomain, envFullDomain));
}
} else {
domains.add(makeHost(subdomain, envFullDomain));
}
}
return domains;
}
public static Set getNodeDomains(SoftwareNode softwareNode, boolean inShort, boolean includeDefault, String primaryEnvDomain, Set envDomains) {
LinkedHashSet nodeDomains = new LinkedHashSet<>();
if (includeDefault) {
nodeDomains.addAll(getDefaultNodeDomains(softwareNode, inShort, primaryEnvDomain));
}
for (String envDomain : envDomains) {
boolean isPrimaryEnvDomain = StringUtils.equals(envDomain, primaryEnvDomain);
nodeDomains.addAll(getNodeDomains(softwareNode, inShort, includeDefault, envDomain, isPrimaryEnvDomain));
}
return nodeDomains;
}
public static String getDefaultSubdomainCheckRegexp(String subdomain) {
return "^(?i)" + subdomain + "\\d*$";
}
public static Set getDockerLinkHosts(SoftwareNode softwareNode, String primaryEnvDomain, boolean convertToPunycode) {
if (NodeUtils.isSupportedDockerFunctionality(softwareNode)) {
Set linkHosts = new HashSet<>();
DockerMetadata dockerMetadata = (DockerMetadata) softwareNode.getDockerMetadata();
if (dockerMetadata != null) {
for (DockerLink masterLink : dockerMetadata.getLinks()) {
if (masterLink.getType() == DockerLinkType.OUT && masterLink.getGroupAlias() != null) {
String host = masterLink.getGroupAlias() + Constants.DOT_SEPARATOR + primaryEnvDomain;
if (convertToPunycode) {
linkHosts.add(JavaNetUtils.convertToPunycode(host));
} else {
linkHosts.add(host);
}
}
}
}
return linkHosts;
}
return Collections.emptySet();
}
private static String makeHost(Subdomain subdomain, String envHost, boolean convertToPunycode) {
String delimiter = Constants.DOT_SEPARATOR;
if (subdomain.isTopLevel()) {
delimiter = Constants.HYPHEN_SEPARATOR;
}
String host = subdomain.getDomain() + delimiter + envHost;
return convertToPunycode ? JavaNetUtils.convertToPunycode(host) : host;
}
public static String makeHost(Subdomain subdomain, String envHost) {
return makeHost(subdomain, envHost, false);
}
public static String makeHostNameCorrectLength(String hostname, int maxLength) {
if (hostname.length() >= maxLength) {
return hostname.substring(0, maxLength);
}
return hostname;
}
public static boolean isTagSuitable(TemplateTag templateTag, String nodeTypeAlias) {
if (templateTag == null || StringUtils.isBlank(templateTag.getNodeTypeAlias()) || StringUtils.isBlank(nodeTypeAlias)) {
return false;
}
String[] aliases = templateTag.getNodeTypeAlias().split(Constants.SEMICOLON_SEPARATOR);
for (String alias : aliases) {
if (StringUtils.equals(alias, nodeTypeAlias)) {
return true;
}
}
return false;
}
private static boolean addIgnoreNull(Collection collection, Object object) {
return (object == null ? false : collection.add(object));
}
private static String makeNodeUrlString(String url, int softNodeId, String domain, String zone) {
return url + NODE_DEFAULT_SUBDOMAIN + softNodeId + EnvironmentConstants.INTERNAL_SUB_DOMAIN_SEPARATOR_HYPHEN + domain.toLowerCase() + "." + zone;
}
public static String getNodeType(SoftwareNode softwareNode) {
if (StringUtils.isBlank(softwareNode.getNodeTypeAlias())) {
return softwareNode.getProperties().getNodeType();
} else {
return softwareNode.getNodeTypeAlias();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy