|
@@ -20,14 +20,15 @@ package org.apache.ambari.server.controller.internal;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.HashSet;
|
|
-import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.Map.Entry;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
import java.util.regex.Pattern;
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
import org.apache.ambari.server.AmbariException;
|
|
import org.apache.ambari.server.AmbariException;
|
|
import org.apache.ambari.server.ObjectNotFoundException;
|
|
import org.apache.ambari.server.ObjectNotFoundException;
|
|
|
|
+import org.apache.ambari.server.api.resources.RepositoryResourceDefinition;
|
|
import org.apache.ambari.server.api.services.AmbariMetaInfo;
|
|
import org.apache.ambari.server.api.services.AmbariMetaInfo;
|
|
import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
|
|
import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
|
|
import org.apache.ambari.server.controller.spi.NoSuchResourceException;
|
|
import org.apache.ambari.server.controller.spi.NoSuchResourceException;
|
|
@@ -43,6 +44,8 @@ import org.apache.ambari.server.controller.utilities.PropertyHelper;
|
|
import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
|
|
import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
|
|
import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
|
|
import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
|
|
import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
|
|
import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
|
|
|
|
+import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
|
|
|
|
+import org.apache.ambari.server.orm.entities.RepositoryEntity;
|
|
import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
|
|
import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
|
|
import org.apache.ambari.server.state.OperatingSystemInfo;
|
|
import org.apache.ambari.server.state.OperatingSystemInfo;
|
|
import org.apache.ambari.server.state.StackId;
|
|
import org.apache.ambari.server.state.StackId;
|
|
@@ -50,13 +53,10 @@ import org.apache.ambari.server.state.StackInfo;
|
|
import org.apache.ambari.server.state.stack.UpgradePack;
|
|
import org.apache.ambari.server.state.stack.UpgradePack;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
|
-import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
-import com.github.fge.jackson.JsonLoader;
|
|
|
|
-import com.github.fge.jsonschema.core.report.ProcessingMessage;
|
|
|
|
-import com.github.fge.jsonschema.core.report.ProcessingReport;
|
|
|
|
-import com.github.fge.jsonschema.main.JsonSchema;
|
|
|
|
-import com.github.fge.jsonschema.main.JsonSchemaFactory;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.Gson;
|
|
|
|
+import com.google.gson.JsonArray;
|
|
|
|
+import com.google.gson.JsonElement;
|
|
|
|
+import com.google.gson.JsonParser;
|
|
import com.google.inject.Inject;
|
|
import com.google.inject.Inject;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -66,12 +66,13 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
|
|
|
|
|
|
// ----- Property ID constants ---------------------------------------------
|
|
// ----- Property ID constants ---------------------------------------------
|
|
|
|
|
|
- protected static final String REPOSITORY_VERSION_ID_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "id");
|
|
|
|
- protected static final String REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "display_name");
|
|
|
|
- protected static final String REPOSITORY_VERSION_STACK_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "stack");
|
|
|
|
- protected static final String REPOSITORY_VERSION_VERSION_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "version");
|
|
|
|
- protected static final String REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "upgrade_pack");
|
|
|
|
- protected static final String REPOSITORY_VERSION_REPOSITORIES_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "repositories");
|
|
|
|
|
|
+ public static final String REPOSITORY_VERSION_ID_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "id");
|
|
|
|
+ public static final String REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "stack_name");
|
|
|
|
+ public static final String REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "stack_version");
|
|
|
|
+ public static final String REPOSITORY_VERSION_REPOSITORY_VERSION_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "repository_version");
|
|
|
|
+ public static final String REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "display_name");
|
|
|
|
+ public static final String REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "upgrade_pack");
|
|
|
|
+ public static final String REPOSITORY_VERSION_OPERATING_SYSTEMS_PROPERTY_ID = PropertyHelper.getPropertyId("RepositoryVersions", "operating_systems");
|
|
|
|
|
|
@SuppressWarnings("serial")
|
|
@SuppressWarnings("serial")
|
|
private static Set<String> pkPropertyIds = new HashSet<String>() {
|
|
private static Set<String> pkPropertyIds = new HashSet<String>() {
|
|
@@ -84,36 +85,25 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
|
|
private static Set<String> propertyIds = new HashSet<String>() {
|
|
private static Set<String> propertyIds = new HashSet<String>() {
|
|
{
|
|
{
|
|
add(REPOSITORY_VERSION_ID_PROPERTY_ID);
|
|
add(REPOSITORY_VERSION_ID_PROPERTY_ID);
|
|
|
|
+ add(REPOSITORY_VERSION_REPOSITORY_VERSION_PROPERTY_ID);
|
|
add(REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID);
|
|
add(REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID);
|
|
- add(REPOSITORY_VERSION_STACK_PROPERTY_ID);
|
|
|
|
- add(REPOSITORY_VERSION_VERSION_PROPERTY_ID);
|
|
|
|
|
|
+ add(REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID);
|
|
|
|
+ add(REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID);
|
|
add(REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID);
|
|
add(REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID);
|
|
- add(REPOSITORY_VERSION_REPOSITORIES_PROPERTY_ID);
|
|
|
|
|
|
+ add(REPOSITORY_VERSION_OPERATING_SYSTEMS_PROPERTY_ID);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
@SuppressWarnings("serial")
|
|
@SuppressWarnings("serial")
|
|
private static Map<Type, String> keyPropertyIds = new HashMap<Type, String>() {
|
|
private static Map<Type, String> keyPropertyIds = new HashMap<Type, String>() {
|
|
{
|
|
{
|
|
|
|
+ put(Resource.Type.Stack, REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID);
|
|
|
|
+ put(Resource.Type.StackVersion, REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID);
|
|
put(Resource.Type.RepositoryVersion, REPOSITORY_VERSION_ID_PROPERTY_ID);
|
|
put(Resource.Type.RepositoryVersion, REPOSITORY_VERSION_ID_PROPERTY_ID);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
- /**
|
|
|
|
- * Json schema used for repositories validation.
|
|
|
|
- */
|
|
|
|
- private static JsonSchema repositoriesJsonSchema;
|
|
|
|
- static {
|
|
|
|
- final String schema = "{\"type\":\"array\",\"$schema\":\"http://json-schema.org/draft-04/schema#\","
|
|
|
|
- + "\"items\":{\"type\":\"object\",\"required\":[\"baseurls\",\"os\"],\"properties\":{\"baseurls\":{\"type\":\"array\",\"items\":"
|
|
|
|
- + "{\"type\":\"object\",\"required\":[\"type\",\"baseurl\",\"id\"],\"properties\":{\"type\":{\"type\":\"string\"},\"baseurl\":"
|
|
|
|
- + "{\"type\":\"string\"},\"id\":{\"type\":\"string\"}}},\"minItems\":1},\"os\":{\"type\":\"string\"}}}\r\n,\"minItems\":1}";
|
|
|
|
- try {
|
|
|
|
- repositoriesJsonSchema = JsonSchemaFactory.byDefault().getJsonSchema(JsonLoader.fromString(schema));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- LOG.error("Could not create instance of json schema for validating repositories");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ private static Gson gson = new Gson();
|
|
|
|
|
|
@Inject
|
|
@Inject
|
|
private RepositoryVersionDAO repositoryVersionDAO;
|
|
private RepositoryVersionDAO repositoryVersionDAO;
|
|
@@ -144,25 +134,19 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public Void invoke() throws AmbariException {
|
|
public Void invoke() throws AmbariException {
|
|
- final RepositoryVersionEntity entity = new RepositoryVersionEntity();
|
|
|
|
final String[] requiredProperties = {
|
|
final String[] requiredProperties = {
|
|
REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID,
|
|
REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID,
|
|
- REPOSITORY_VERSION_REPOSITORIES_PROPERTY_ID,
|
|
|
|
- REPOSITORY_VERSION_STACK_PROPERTY_ID,
|
|
|
|
- REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID,
|
|
|
|
- REPOSITORY_VERSION_VERSION_PROPERTY_ID
|
|
|
|
|
|
+ REPOSITORY_VERSION_OPERATING_SYSTEMS_PROPERTY_ID,
|
|
|
|
+ REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID,
|
|
|
|
+ REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID,
|
|
|
|
+ REPOSITORY_VERSION_REPOSITORY_VERSION_PROPERTY_ID
|
|
};
|
|
};
|
|
for (String propertyName: requiredProperties) {
|
|
for (String propertyName: requiredProperties) {
|
|
if (properties.get(propertyName) == null) {
|
|
if (properties.get(propertyName) == null) {
|
|
throw new AmbariException("Property " + propertyName + " should be provided");
|
|
throw new AmbariException("Property " + propertyName + " should be provided");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- entity.setDisplayName(properties.get(REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID).toString());
|
|
|
|
- entity.setStack(properties.get(REPOSITORY_VERSION_STACK_PROPERTY_ID).toString());
|
|
|
|
- entity.setUpgradePackage(properties.get(REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID).toString());
|
|
|
|
- entity.setVersion(properties.get(REPOSITORY_VERSION_VERSION_PROPERTY_ID).toString());
|
|
|
|
- final Object repositories = properties.get(REPOSITORY_VERSION_REPOSITORIES_PROPERTY_ID);
|
|
|
|
- entity.setRepositories(new Gson().toJson(repositories));
|
|
|
|
|
|
+ final RepositoryVersionEntity entity = toRepositoryVersionEntity(properties);
|
|
|
|
|
|
if (repositoryVersionDAO.findByDisplayName(entity.getDisplayName()) != null) {
|
|
if (repositoryVersionDAO.findByDisplayName(entity.getDisplayName()) != null) {
|
|
throw new AmbariException("Repository version with name " + entity.getDisplayName() + " already exists");
|
|
throw new AmbariException("Repository version with name " + entity.getDisplayName() + " already exists");
|
|
@@ -189,10 +173,11 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
|
|
final Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate);
|
|
final Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate);
|
|
|
|
|
|
List<RepositoryVersionEntity> requestedEntities = new ArrayList<RepositoryVersionEntity>();
|
|
List<RepositoryVersionEntity> requestedEntities = new ArrayList<RepositoryVersionEntity>();
|
|
- if (propertyMaps.isEmpty()) {
|
|
|
|
- requestedEntities = repositoryVersionDAO.findAll();
|
|
|
|
- } else {
|
|
|
|
- for (Map<String, Object> propertyMap: propertyMaps) {
|
|
|
|
|
|
+ for (Map<String, Object> propertyMap: propertyMaps) {
|
|
|
|
+ final StackId stackId = getStackInformationFromUrl(propertyMap);
|
|
|
|
+ if (propertyMaps.size() == 1 && propertyMap.get(REPOSITORY_VERSION_ID_PROPERTY_ID) == null) {
|
|
|
|
+ requestedEntities.addAll(repositoryVersionDAO.findByStack(stackId.getStackId()));
|
|
|
|
+ } else {
|
|
final Long id;
|
|
final Long id;
|
|
try {
|
|
try {
|
|
id = Long.parseLong(propertyMap.get(REPOSITORY_VERSION_ID_PROPERTY_ID).toString());
|
|
id = Long.parseLong(propertyMap.get(REPOSITORY_VERSION_ID_PROPERTY_ID).toString());
|
|
@@ -212,15 +197,13 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
|
|
final Resource resource = new ResourceImpl(Resource.Type.RepositoryVersion);
|
|
final Resource resource = new ResourceImpl(Resource.Type.RepositoryVersion);
|
|
|
|
|
|
setResourceProperty(resource, REPOSITORY_VERSION_ID_PROPERTY_ID, entity.getId(), requestedIds);
|
|
setResourceProperty(resource, REPOSITORY_VERSION_ID_PROPERTY_ID, entity.getId(), requestedIds);
|
|
|
|
+ setResourceProperty(resource, REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID, entity.getStackName(), requestedIds);
|
|
|
|
+ setResourceProperty(resource, REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID, entity.getStackVersion(), requestedIds);
|
|
setResourceProperty(resource, REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID, entity.getDisplayName(), requestedIds);
|
|
setResourceProperty(resource, REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID, entity.getDisplayName(), requestedIds);
|
|
- setResourceProperty(resource, REPOSITORY_VERSION_REPOSITORIES_PROPERTY_ID, new Gson().fromJson(entity.getRepositories(), Object.class), requestedIds);
|
|
|
|
- setResourceProperty(resource, REPOSITORY_VERSION_STACK_PROPERTY_ID, entity.getStack(), requestedIds);
|
|
|
|
setResourceProperty(resource, REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID, entity.getUpgradePackage(), requestedIds);
|
|
setResourceProperty(resource, REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID, entity.getUpgradePackage(), requestedIds);
|
|
- setResourceProperty(resource, REPOSITORY_VERSION_VERSION_PROPERTY_ID, entity.getVersion(), requestedIds);
|
|
|
|
|
|
+ setResourceProperty(resource, REPOSITORY_VERSION_REPOSITORY_VERSION_PROPERTY_ID, entity.getVersion(), requestedIds);
|
|
|
|
|
|
- if (predicate == null || predicate.evaluate(resource)) {
|
|
|
|
- resources.add(resource);
|
|
|
|
- }
|
|
|
|
|
|
+ resources.add(resource);
|
|
}
|
|
}
|
|
return resources;
|
|
return resources;
|
|
}
|
|
}
|
|
@@ -246,8 +229,7 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
|
|
throw new ObjectNotFoundException("There is no repository version with id " + id);
|
|
throw new ObjectNotFoundException("There is no repository version with id " + id);
|
|
}
|
|
}
|
|
|
|
|
|
- if (propertyMap.get(REPOSITORY_VERSION_REPOSITORIES_PROPERTY_ID) != null
|
|
|
|
- || propertyMap.get(REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID) != null) {
|
|
|
|
|
|
+ if (propertyMap.get(REPOSITORY_VERSION_OPERATING_SYSTEMS_PROPERTY_ID) != null) {
|
|
|
|
|
|
final List<ClusterVersionEntity> clusterVersionEntities =
|
|
final List<ClusterVersionEntity> clusterVersionEntities =
|
|
clusterVersionDAO.findByStackAndVersion(entity.getStack(), entity.getVersion());
|
|
clusterVersionDAO.findByStackAndVersion(entity.getStack(), entity.getVersion());
|
|
@@ -258,14 +240,11 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
|
|
firstClusterVersion.getState().name() + " on cluster " + firstClusterVersion.getClusterEntity().getClusterName());
|
|
firstClusterVersion.getState().name() + " on cluster " + firstClusterVersion.getClusterEntity().getClusterName());
|
|
}
|
|
}
|
|
|
|
|
|
- if (propertyMap.get(REPOSITORY_VERSION_REPOSITORIES_PROPERTY_ID) != null) {
|
|
|
|
- final Object repositories = propertyMap.get(REPOSITORY_VERSION_REPOSITORIES_PROPERTY_ID);
|
|
|
|
- entity.setRepositories(new Gson().toJson(repositories));
|
|
|
|
|
|
+ if (propertyMap.get(REPOSITORY_VERSION_OPERATING_SYSTEMS_PROPERTY_ID) != null) {
|
|
|
|
+ final Object repositories = propertyMap.get(REPOSITORY_VERSION_OPERATING_SYSTEMS_PROPERTY_ID);
|
|
|
|
+ entity.setOperatingSystems(gson.toJson(repositories));
|
|
}
|
|
}
|
|
|
|
|
|
- if (propertyMap.get(REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID) != null) {
|
|
|
|
- entity.setUpgradePackage(propertyMap.get(REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID).toString());
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
if (propertyMap.get(REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID) != null) {
|
|
if (propertyMap.get(REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID) != null) {
|
|
@@ -343,33 +322,14 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
|
|
throw new AmbariException("Stack " + stackFullName + " doesn't have upgrade packages");
|
|
throw new AmbariException("Stack " + stackFullName + " doesn't have upgrade packages");
|
|
}
|
|
}
|
|
|
|
|
|
- // check that given repositories node is a valid json
|
|
|
|
- ProcessingReport jsonValidationReport;
|
|
|
|
- JsonNode repositoriesJson;
|
|
|
|
- try {
|
|
|
|
- repositoriesJson = JsonLoader.fromString(repositoryVersion.getRepositories());
|
|
|
|
- jsonValidationReport = repositoriesJsonSchema.validate(repositoriesJson);
|
|
|
|
- } catch (Exception ex) {
|
|
|
|
- throw new AmbariException("Could not process repositories json");
|
|
|
|
- }
|
|
|
|
- if (!jsonValidationReport.isSuccess()) {
|
|
|
|
- final StringBuilder errors = new StringBuilder();
|
|
|
|
- final Iterator<ProcessingMessage> iterator = jsonValidationReport.iterator();
|
|
|
|
- while (iterator.hasNext()) {
|
|
|
|
- errors.append(iterator.next().toString());
|
|
|
|
- }
|
|
|
|
- throw new AmbariException("Failed to validate repositories json: " + errors.toString());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// check that repositories contain only supported operating systems
|
|
// check that repositories contain only supported operating systems
|
|
final Set<String> osSupported = new HashSet<String>();
|
|
final Set<String> osSupported = new HashSet<String>();
|
|
for (OperatingSystemInfo osInfo: ambariMetaInfo.getOperatingSystems(stackName, stackMajorVersion)) {
|
|
for (OperatingSystemInfo osInfo: ambariMetaInfo.getOperatingSystems(stackName, stackMajorVersion)) {
|
|
osSupported.add(osInfo.getOsType());
|
|
osSupported.add(osInfo.getOsType());
|
|
}
|
|
}
|
|
final Set<String> osRepositoryVersion = new HashSet<String>();
|
|
final Set<String> osRepositoryVersion = new HashSet<String>();
|
|
- final Iterator<JsonNode> repositoriesIterator = repositoriesJson.elements();
|
|
|
|
- while (repositoriesIterator.hasNext()) {
|
|
|
|
- osRepositoryVersion.add(repositoriesIterator.next().get("os").asText());
|
|
|
|
|
|
+ for (OperatingSystemEntity os: repositoryVersion.getOperatingSystems()) {
|
|
|
|
+ osRepositoryVersion.add(os.getOsType());
|
|
}
|
|
}
|
|
if (osRepositoryVersion.isEmpty()) {
|
|
if (osRepositoryVersion.isEmpty()) {
|
|
throw new AmbariException("At least one set of repositories for OS should be provided");
|
|
throw new AmbariException("At least one set of repositories for OS should be provided");
|
|
@@ -379,29 +339,118 @@ public class RepositoryVersionResourceProvider extends AbstractResourceProvider
|
|
throw new AmbariException("Operating system type " + os + " is not supported by stack " + stackFullName);
|
|
throw new AmbariException("Operating system type " + os + " is not supported by stack " + stackFullName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
|
|
- // check that upgrade pack for the stack exists
|
|
|
|
- final UpgradePack upgradePack = stackInfo.getUpgradePacks().get(repositoryVersion.getUpgradePackage());
|
|
|
|
- if (upgradePack == null) {
|
|
|
|
- throw new AmbariException("Upgrade pack " + repositoryVersion.getUpgradePackage()
|
|
|
|
- + " is not available for stack " + stackFullName);
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Transforms map of json properties to repository version entity.
|
|
|
|
+ *
|
|
|
|
+ * @param properties json map
|
|
|
|
+ * @return constructed entity
|
|
|
|
+ * @throws AmbariException if some properties are missing or json has incorrect structure
|
|
|
|
+ */
|
|
|
|
+ protected RepositoryVersionEntity toRepositoryVersionEntity(Map<String, Object> properties) throws AmbariException {
|
|
|
|
+ final RepositoryVersionEntity entity = new RepositoryVersionEntity();
|
|
|
|
+ final String stackName = properties.get(REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID).toString();
|
|
|
|
+ final String stackVersion = properties.get(REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID).toString();
|
|
|
|
+ entity.setDisplayName(properties.get(REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID).toString());
|
|
|
|
+ entity.setStack(new StackId(stackName, stackVersion).getStackId());
|
|
|
|
+ entity.setUpgradePackage(properties.get(REPOSITORY_VERSION_UPGRADE_PACK_PROPERTY_ID).toString());
|
|
|
|
+ entity.setVersion(properties.get(REPOSITORY_VERSION_REPOSITORY_VERSION_PROPERTY_ID).toString());
|
|
|
|
+ final Object repositories = properties.get(REPOSITORY_VERSION_OPERATING_SYSTEMS_PROPERTY_ID);
|
|
|
|
+ final String repositoriesJson = gson.toJson(repositories);
|
|
|
|
+ try {
|
|
|
|
+ parseRepositories(repositoriesJson);
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
+ throw new AmbariException("Json structure for operating systems is incorrect", ex);
|
|
}
|
|
}
|
|
|
|
+ entity.setOperatingSystems(repositoriesJson);
|
|
|
|
+ entity.setUpgradePackage(getUpgradePackageName(stackName, stackVersion, entity.getVersion()));
|
|
|
|
+ return entity;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
- // check that upgrade pack has <target> node
|
|
|
|
- if (StringUtils.isBlank(upgradePack.getTarget())) {
|
|
|
|
- throw new AmbariException("Upgrade pack " + repositoryVersion.getUpgradePackage()
|
|
|
|
- + " is corrupted, it should contain <target> node");
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Scans the given stack for upgrade packages which can be applied to update the cluster to given repository version.
|
|
|
|
+ *
|
|
|
|
+ * @param stackName stack name
|
|
|
|
+ * @param stackVersion stack version
|
|
|
|
+ * @param repositoryVersion target repository version
|
|
|
|
+ * @return upgrade pack name
|
|
|
|
+ * @throws AmbariException if no upgrade packs suit the requirements
|
|
|
|
+ */
|
|
|
|
+ private String getUpgradePackageName(String stackName, String stackVersion, String repositoryVersion) throws AmbariException {
|
|
|
|
+ final Map<String, UpgradePack> upgradePacks = ambariMetaInfo.getUpgradePacks(stackName, stackVersion);
|
|
|
|
+ for (Entry<String, UpgradePack> upgradePackEntry : upgradePacks.entrySet()) {
|
|
|
|
+ final UpgradePack upgradePack = upgradePackEntry.getValue();
|
|
|
|
+ final String upgradePackName = upgradePackEntry.getKey();
|
|
|
|
+ // check that upgrade pack has <target> node
|
|
|
|
+ if (StringUtils.isBlank(upgradePack.getTarget())) {
|
|
|
|
+ LOG.error("Upgrade pack " + upgradePackName + " is corrupted, it should contain <target> node");
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // check that upgrade pack can be applied to selected stack
|
|
|
|
+ // converting 2.2.*.* -> 2\.2(\.\d+)?(\.\d+)?(-\d+)?
|
|
|
|
+ String regexPattern = upgradePack.getTarget();
|
|
|
|
+ regexPattern = regexPattern.replaceAll("\\.", "\\\\."); // . -> \.
|
|
|
|
+ regexPattern = regexPattern.replaceAll("\\\\\\.\\*", "(\\\\\\.\\\\d+)?"); // \.* -> (\.\d+)?
|
|
|
|
+ regexPattern = regexPattern.concat("(-\\d+)?");
|
|
|
|
+ if (Pattern.matches(regexPattern, repositoryVersion)) {
|
|
|
|
+ return upgradePackName;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ throw new AmbariException("There were no suitable upgrade packs for stack " + stackName + " " + stackVersion);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Parses operating systems json to a list of entities. Expects json like:
|
|
|
|
+ * <pre>
|
|
|
|
+ * [
|
|
|
|
+ * {
|
|
|
|
+ * "repositories":[
|
|
|
|
+ * {
|
|
|
|
+ * "Repositories/base_url":"http://s3.amazonaws.com/dev.hortonworks.com/HDP/centos5/2.x/updates/2.2.0.0",
|
|
|
|
+ * "Repositories/repo_name":"HDP-UTILS",
|
|
|
|
+ * "Repositories/repo_id":"HDP-UTILS-1.1.0.20"
|
|
|
|
+ * },
|
|
|
|
+ * {
|
|
|
|
+ * "Repositories/base_url":"http://s3.amazonaws.com/dev.hortonworks.com/HDP/centos5/2.x/updates/2.2.0.0",
|
|
|
|
+ * "Repositories/repo_name":"HDP",
|
|
|
|
+ * "Repositories/repo_id":"HDP-2.2"
|
|
|
|
+ * }
|
|
|
|
+ * ],
|
|
|
|
+ * "OperatingSystems/os_type":"redhat5"
|
|
|
|
+ * }
|
|
|
|
+ * ]
|
|
|
|
+ * </pre>
|
|
|
|
+ * @param repositoriesJson operating systems json
|
|
|
|
+ * @return list of operating system entities
|
|
|
|
+ * @throws Exception if any kind of json parsing error happened
|
|
|
|
+ */
|
|
|
|
+ public static List<OperatingSystemEntity> parseRepositories(String repositoriesJson) throws Exception {
|
|
|
|
+ final List<OperatingSystemEntity> operatingSystems = new ArrayList<OperatingSystemEntity>();
|
|
|
|
+ final JsonArray rootJson = new JsonParser().parse(repositoriesJson).getAsJsonArray();
|
|
|
|
+ final String repositoriesSubresourceName = new RepositoryResourceDefinition().getPluralName();
|
|
|
|
+ for (JsonElement operatingSystemJson: rootJson) {
|
|
|
|
+ final OperatingSystemEntity operatingSystemEntity = new OperatingSystemEntity();
|
|
|
|
+ operatingSystemEntity.setOsType(operatingSystemJson.getAsJsonObject().get(OperatingSystemResourceProvider.OPERATING_SYSTEM_OS_TYPE_PROPERTY_ID).getAsString());
|
|
|
|
+ for (JsonElement repositoryJson: operatingSystemJson.getAsJsonObject().get(repositoriesSubresourceName).getAsJsonArray()) {
|
|
|
|
+ final RepositoryEntity repositoryEntity = new RepositoryEntity();
|
|
|
|
+ repositoryEntity.setBaseUrl(repositoryJson.getAsJsonObject().get(RepositoryResourceProvider.REPOSITORY_BASE_URL_PROPERTY_ID).getAsString());
|
|
|
|
+ repositoryEntity.setName(repositoryJson.getAsJsonObject().get(RepositoryResourceProvider.REPOSITORY_REPO_NAME_PROPERTY_ID).getAsString());
|
|
|
|
+ repositoryEntity.setRepositoryId(repositoryJson.getAsJsonObject().get(RepositoryResourceProvider.REPOSITORY_REPO_ID_PROPERTY_ID).getAsString());
|
|
|
|
+ operatingSystemEntity.getRepositories().add(repositoryEntity);
|
|
|
|
+ }
|
|
|
|
+ operatingSystems.add(operatingSystemEntity);
|
|
|
|
+ }
|
|
|
|
+ return operatingSystems;
|
|
|
|
+ }
|
|
|
|
|
|
- // check that upgrade pack can be applied to selected stack
|
|
|
|
- // converting 2.2.*.* -> 2\.2(\.\d+)?(\.\d+)?(-\d+)?
|
|
|
|
- String regexPattern = upgradePack.getTarget();
|
|
|
|
- regexPattern = regexPattern.replaceAll("\\.", "\\\\."); // . -> \.
|
|
|
|
- regexPattern = regexPattern.replaceAll("\\\\\\.\\*", "(\\\\\\.\\\\d+)?"); // \.* -> (\.\d+)?
|
|
|
|
- regexPattern = regexPattern.concat("(-\\d+)?");
|
|
|
|
- if (!Pattern.matches(regexPattern, repositoryVersion.getVersion())) {
|
|
|
|
- throw new AmbariException("Upgrade pack " + repositoryVersion.getUpgradePackage()
|
|
|
|
- + " can't be applied to stack " + stackFullName);
|
|
|
|
|
|
+ protected StackId getStackInformationFromUrl(Map<String, Object> propertyMap) {
|
|
|
|
+ if (propertyMap.containsKey(REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID) && propertyMap.containsKey(REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID)) {
|
|
|
|
+ return new StackId(propertyMap.get(REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID).toString(), propertyMap.get(REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID).toString());
|
|
}
|
|
}
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|