You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by mr...@apache.org on 2017/07/25 17:12:09 UTC

ambari git commit: AMBARI-21523: DELETE Api for Mpacks (mradhakrishnan)

Repository: ambari
Updated Branches:
  refs/heads/branch-feature-AMBARI-14714 332053bcd -> 294eddc42


AMBARI-21523: DELETE Api for Mpacks  (mradhakrishnan)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/294eddc4
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/294eddc4
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/294eddc4

Branch: refs/heads/branch-feature-AMBARI-14714
Commit: 294eddc42c959bfc55fe401b358550e6a030f305
Parents: 332053b
Author: Madhuvanthi Radhakrishnan <mr...@hortonworks.com>
Authored: Tue Jul 25 10:10:50 2017 -0700
Committer: Madhuvanthi Radhakrishnan <mr...@hortonworks.com>
Committed: Tue Jul 25 10:11:56 2017 -0700

----------------------------------------------------------------------
 .../server/api/services/AmbariMetaInfo.java     | 17 ++++++
 .../server/api/services/MpacksService.java      | 21 ++++++++
 .../controller/AmbariManagementController.java  | 11 ++++
 .../AmbariManagementControllerImpl.java         | 13 +++++
 .../internal/MpackResourceProvider.java         | 57 +++++++++++++++++++-
 .../ambari/server/mpack/MpackManager.java       | 42 ++++++++++++++-
 .../apache/ambari/server/orm/dao/MpackDAO.java  |  4 ++
 .../apache/ambari/server/orm/dao/StackDAO.java  | 25 +++++++++
 .../ambari/server/orm/entities/StackEntity.java |  1 +
 .../ambari/server/stack/StackManager.java       |  5 ++
 10 files changed, 193 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/294eddc4/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
index ecf8364..6e4b5fa 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
@@ -60,6 +60,8 @@ import org.apache.ambari.server.mpack.MpackManagerFactory;
 import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.dao.MetainfoDAO;
 import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
+import org.apache.ambari.server.orm.entities.MpackEntity;
+import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.stack.StackDirectory;
 import org.apache.ambari.server.stack.StackManager;
 import org.apache.ambari.server.stack.StackManagerFactory;
@@ -1473,4 +1475,19 @@ public class AmbariMetaInfo {
     return versionDefinitions;
   }
 
+  /***
+   * Remove Mpack from the mpackMap and stackMap which is used to power the Mpack and Stack APIs.
+   * Stack should be removed from stackMap only if it points to the mpack that is being removed.
+   * @param mpackEntity
+   * @param stackEntity
+   * @throws IOException
+   */
+  public void removeMpack(MpackEntity mpackEntity, StackEntity stackEntity) throws IOException {
+
+    boolean stackDelete = mpackManager.removeMpack(mpackEntity, stackEntity);
+
+    if(stackDelete) {
+      stackManager.removeStack(stackEntity);
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/294eddc4/ambari-server/src/main/java/org/apache/ambari/server/api/services/MpacksService.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/MpacksService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/MpacksService.java
index 4b7e025..cf71af3 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/MpacksService.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/MpacksService.java
@@ -19,6 +19,7 @@ package org.apache.ambari.server.api.services;
 
 import java.util.Collections;
 
+import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
@@ -150,6 +151,26 @@ public class MpacksService extends BaseService {
             createMpackResource(mpackId));
   }
 
+  @DELETE
+  @Path("{mpack_id}")
+  @Produces(MediaType.TEXT_PLAIN)
+  @ApiOperation(value = "Deletes a selected management pack")
+  @ApiImplicitParams({
+    @ApiImplicitParam(name = QUERY_FIELDS, value = QUERY_FILTER_DESCRIPTION, dataType = DATA_TYPE_STRING,
+                  paramType = PARAM_TYPE_QUERY, defaultValue = MpackResourceProvider.ALL_PROPERTIES),
+  })
+  @ApiResponses({
+          @ApiResponse(code = HttpStatus.SC_OK, message = MSG_SUCCESSFUL_OPERATION),
+          @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = MSG_CLUSTER_OR_HOST_NOT_FOUND),
+          @ApiResponse(code = HttpStatus.SC_UNAUTHORIZED, message = MSG_NOT_AUTHENTICATED),
+          @ApiResponse(code = HttpStatus.SC_FORBIDDEN, message = MSG_PERMISSION_DENIED),
+          @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = MSG_SERVER_ERROR),
+  })
+  public Response deleteMpack(String body, @Context HttpHeaders headers, @Context UriInfo ui, @PathParam("mpack_id") String mpackId) {
+    return handleRequest(headers, body, ui, Request.Type.DELETE,
+            createMpackResource(mpackId));
+  }
+
   /**
    * Create an mpack resource instance
    * @param mpackId

http://git-wip-us.apache.org/repos/asf/ambari/blob/294eddc4/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java
index 635949f..d792717 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java
@@ -42,6 +42,8 @@ import org.apache.ambari.server.events.AmbariEvent;
 import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 import org.apache.ambari.server.metadata.RoleCommandOrder;
 import org.apache.ambari.server.orm.entities.ExtensionLinkEntity;
+import org.apache.ambari.server.orm.entities.MpackEntity;
+import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.registry.Registry;
 import org.apache.ambari.server.scheduler.ExecutionScheduleManager;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
@@ -965,5 +967,14 @@ public interface AmbariManagementController {
    * @throws AmbariException thrown if the resource cannot be read
    */
   Registry getRegistry(Long registryId) throws AmbariException;
+
+  /***
+   * Remove Mpack from the mpackMap and stackMap which is used to power the Mpack and Stack APIs.
+   * @param mpackEntity
+   * @param stackEntity
+   * @throws IOException
+   */
+  void removeMpack(MpackEntity mpackEntity, StackEntity stackEntity) throws IOException;
+
 }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/294eddc4/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
index 72c5dbe..12e4a08 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
@@ -122,10 +122,12 @@ import org.apache.ambari.server.orm.dao.WidgetLayoutDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.ExtensionLinkEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
+import org.apache.ambari.server.orm.entities.MpackEntity;
 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.SettingEntity;
+import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.orm.entities.WidgetEntity;
 import org.apache.ambari.server.orm.entities.WidgetLayoutEntity;
 import org.apache.ambari.server.orm.entities.WidgetLayoutUserWidgetEntity;
@@ -569,6 +571,8 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
   @Override
   public MpackResponse registerMpack(MpackRequest request)
     throws IOException, AuthorizationException, ResourceAlreadyExistsException{
+
+
     MpackResponse mpackResponse = ambariMetaInfo.registerMpack(request);
     updateStacks();
     return mpackResponse;
@@ -3856,6 +3860,15 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
   }
 
   /**
+   * {@inheritDoc}
+   */
+  @Override
+  public void removeMpack(MpackEntity mpackEntity, StackEntity stackEntity) throws IOException{
+
+    ambariMetaInfo.removeMpack(mpackEntity, stackEntity);
+  }
+
+  /**
    * Get a request response for the given request ids.  Note that this method
    * fully populates a request resource including the set of task sub-resources
    * in the request response.

http://git-wip-us.apache.org/repos/asf/ambari/blob/294eddc4/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MpackResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MpackResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MpackResourceProvider.java
index e3afcde..6a3e83c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MpackResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MpackResourceProvider.java
@@ -213,8 +213,7 @@ public class MpackResourceProvider extends AbstractControllerResourceProvider {
         resource.setProperty(REGISTRY_ID, entity.getRegistryId());
         results.add(resource);
       }
-    }
-    else {
+    } else {
       // Fetch a particular mpack based on id
       Map<String, Object> propertyMap = new HashMap<>(PredicateHelper.getProperties(predicate));
       if (propertyMap.containsKey(STACK_NAME_PROPERTY_ID) && propertyMap.containsKey(STACK_VERSION_PROPERTY_ID)) {
@@ -264,5 +263,59 @@ public class MpackResourceProvider extends AbstractControllerResourceProvider {
     return results;
   }
 
+  @Override
+  protected RequestStatus deleteResourcesAuthorized(final Request request, Predicate predicate)
+          throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
+
+    final Long mpackId;
+    Map<String, Object> propertyMap = new HashMap<>(PredicateHelper.getProperties(predicate));
+    DeleteStatusMetaData deleteStatusMetaData = null;
+
+    //Allow deleting mpack only if there are no cluster services deploying using this mpack. Support deleting mpacks only if no cluster has been deployed
+    // (i.e. you should be able to delete an mpack during install wizard only).
+    //Todo : Relax the rule
+    if (getManagementController().getClusters().getClusters().size() > 0) {
+      throw new SystemException("Delete request cannot be completed since there is a cluster deployed");
+    } else {
+      if (propertyMap.containsKey(MPACK_ID)) {
+        Object objMpackId = propertyMap.get(MPACK_ID);
+        if (objMpackId != null) {
+          mpackId = Long.valueOf((String) objMpackId);
+          LOG.info("Deleting Mpack, id = " + mpackId.toString());
+
+          MpackEntity mpackEntity = mpackDAO.findById(mpackId);
+          StackEntity stackEntity = stackDAO.findByMpack(mpackId);
+          try {
+            getManagementController().removeMpack(mpackEntity, stackEntity);
+            if (mpackEntity != null) {
+              deleteStatusMetaData = modifyResources(new Command<DeleteStatusMetaData>() {
+                @Override
+                public DeleteStatusMetaData invoke() throws AmbariException {
+                  if (stackEntity != null) {
+                    stackDAO.removeByMpack(mpackId);
+                    notifyDelete(Resource.Type.Stack, predicate);
+                  }
+                  mpackDAO.removeById(mpackId);
+
+                  return new DeleteStatusMetaData();
+                }
+              });
+              notifyDelete(Resource.Type.Mpack, predicate);
+              deleteStatusMetaData.addDeletedKey(mpackId.toString());
+            } else {
+              throw new NoSuchResourceException("The requested resource doesn't exist: " + predicate);
+            }
+          } catch (IOException e) {
+            throw new SystemException("There is an issue with the Files");
+          }
+        }
+      } else {
+        throw new UnsupportedPropertyException(Resource.Type.Mpack, null);
+      }
+
+      return getRequestStatus(null, null, deleteStatusMetaData);
+    }
+  }
+
 }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/294eddc4/ambari-server/src/main/java/org/apache/ambari/server/mpack/MpackManager.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/mpack/MpackManager.java b/ambari-server/src/main/java/org/apache/ambari/server/mpack/MpackManager.java
index 0c05292..a02ea45 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/mpack/MpackManager.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/mpack/MpackManager.java
@@ -45,6 +45,7 @@ import org.apache.ambari.server.state.Packlet;
 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
 import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -364,7 +365,6 @@ public class MpackManager {
       mpackEntity.setMpackVersion(mpackVersion);
       mpackEntity.setMpackUri(mpack.getMpackUri());
       mpackEntity.setRegistryId(mpack.getRegistryId());
-
       Long mpackId = mpackDAO.create(mpackEntity);
       return mpackId;
     }
@@ -413,4 +413,44 @@ public class MpackManager {
       return mpack.getPacklets();
     return null;
   }
+
+  /***
+   * Remove the mpack and stack directories when a request comes in to delete a particular mpack.
+   * @param mpackEntity
+   * @throws IOException
+   */
+  public boolean removeMpack(MpackEntity mpackEntity, StackEntity stackEntity) throws IOException {
+
+    boolean stackDelete = false;
+    File mpackDirToDelete = new File(mpacksStaging + File.separator + mpackEntity.getMpackName() + File.separator + mpackEntity.getMpackVersion());
+    File mpackDirectory = new File(mpacksStaging + "/" + mpackEntity.getMpackName());
+    String mpackName = mpackEntity.getMpackName() + "-" + mpackEntity.getMpackVersion() + ".tar.gz";
+    Path mpackTarFile = Paths.get(mpacksStaging + File.separator + MPACK_TAR_LOCATION +File.separator + mpackName);
+
+    mpackMap.remove(mpackEntity.getMpackId());
+    FileUtils.deleteDirectory(mpackDirToDelete);
+
+    if (mpackDirectory.isDirectory()) {
+      if (mpackDirectory.list().length == 0) {
+        Files.delete(mpackDirectory.toPath());
+      }
+    }
+    if (stackEntity != null) {
+      Path stackPath = Paths.get(stackRoot + "/" + stackEntity.getStackName() + "/" + stackEntity.getStackVersion());
+      File stackDirectory = new File(stackRoot + "/" + stackEntity.getStackName());
+      if (!Files.exists(stackPath))
+        Files.delete(stackPath);
+      if (stackDirectory.isDirectory()) {
+        if (stackDirectory.list().length == 0) {
+          Files.delete(stackDirectory.toPath());
+        }
+      }
+      stackDelete = true;
+    }
+
+    if (Files.exists(mpackTarFile)){
+      Files.delete(mpackTarFile);
+    }
+    return stackDelete;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/294eddc4/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/MpackDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/MpackDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/MpackDAO.java
index 3078759..3228130 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/MpackDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/MpackDAO.java
@@ -98,5 +98,9 @@ public class MpackDAO {
     return m_daoUtils.selectList(query);
   }
 
+  @Transactional
+  public void removeById(Long mpackId) {
+    m_entityManagerProvider.get().remove(findById(mpackId));
+  }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/294eddc4/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/StackDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/StackDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/StackDAO.java
index f7c5dbe..57327c6 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/StackDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/StackDAO.java
@@ -175,4 +175,29 @@ public class StackDAO {
       entityManager.remove(stack);
     }
   }
+
+  /**
+   * Removes the specified stack based on mpackid.
+   *
+   * @param mpackId
+   *
+   */
+  @Transactional
+  public void removeByMpack(Long mpackId) {
+    entityManagerProvider.get().remove(findByMpack(mpackId));
+  }
+
+  /**
+   * Gets the stack that matches the specified mpackid.
+   *
+   * @return the stack matching the specified mpackid or {@code null}
+   *         if none.
+   */
+  public StackEntity findByMpack(Long mpackId) {
+    TypedQuery<StackEntity> query = entityManagerProvider.get().createNamedQuery(
+            "StackEntity.findByMpack", StackEntity.class);
+    query.setParameter("currentMpackId", mpackId);
+
+    return daoUtils.selectOne(query);
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/294eddc4/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/StackEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/StackEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/StackEntity.java
index 0cb8628..bee53b6 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/StackEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/StackEntity.java
@@ -40,6 +40,7 @@ import javax.persistence.UniqueConstraint;
 @TableGenerator(name = "stack_id_generator", table = "ambari_sequences", pkColumnName = "sequence_name", valueColumnName = "sequence_value", pkColumnValue = "stack_id_seq", initialValue = 0)
 @NamedQueries({
     @NamedQuery(name = "StackEntity.findAll", query = "SELECT stack FROM StackEntity stack"),
+    @NamedQuery(name = "StackEntity.findByMpack", query = "SELECT stack FROM StackEntity stack where stack.currentMpackId = :currentMpackId"),
     @NamedQuery(name = "StackEntity.findByNameAndVersion", query = "SELECT stack FROM StackEntity stack WHERE stack.stackName = :stackName AND stack.stackVersion = :stackVersion",
                 hints = {
                   @QueryHint(name = "eclipselink.query-results-cache", value = "true"),

http://git-wip-us.apache.org/repos/asf/ambari/blob/294eddc4/ambari-server/src/main/java/org/apache/ambari/server/stack/StackManager.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/stack/StackManager.java b/ambari-server/src/main/java/org/apache/ambari/server/stack/StackManager.java
index 9615a75..b498fcb 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/stack/StackManager.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/stack/StackManager.java
@@ -678,4 +678,9 @@ public class StackManager {
     }
     return extensionModules;
   }
+
+  public void removeStack(StackEntity stackEntity) {
+    String stackKey = stackEntity.getStackName() + StackManager.PATH_DELIMITER +  stackEntity.getStackVersion();
+    stackMap.remove(stackKey);
+  }
 }