Example #1
0
  @Override
  public BundlePurgeResponse purge(BundlePurgeRequest request) {
    final BundlePurgeResponse response = new BundlePurgeResponse();

    try {
      final BundleResourceDeployment resourceDeployment = request.getLiveBundleResourceDeployment();
      final BundleDeployment bundleDeployment = resourceDeployment.getBundleDeployment();

      // find the resource that will purge the bundle
      InventoryManager im = getInventoryManager();
      BundleType bundleType = bundleDeployment.getBundleVersion().getBundle().getBundleType();
      ResourceType resourceType = bundleType.getResourceType();
      Set<Resource> resources = im.getResourcesWithType(resourceType);
      if (resources.isEmpty()) {
        throw new Exception("No bundle plugin supports bundle type [" + bundleType + "]");
      }
      final int bundleHandlerResourceId = resources.iterator().next().getId();
      final ResourceContainer resourceContainer = im.getResourceContainer(bundleHandlerResourceId);
      if (null == resourceContainer.getResourceContext()) {
        throw new Exception(
            "No bundle plugin resource available to handle purge for bundle type ["
                + bundleType
                + "]. Ensure the bundle plugin is deployed and its resource is imported into inventory.");
      }

      // purge the bundle utilizing the bundle facet object
      String deploymentMessage =
          "Deployment ["
              + bundleDeployment
              + "] to be purged via ["
              + resourceDeployment.getResource()
              + "]";
      auditDeployment(
          resourceDeployment, AUDIT_PURGE_STARTED, bundleDeployment.getName(), deploymentMessage);

      File absoluteDestDir = getAbsoluteDestinationDir(request.getLiveBundleResourceDeployment());

      org.rhq.core.pluginapi.bundle.BundlePurgeRequest purgeRequest =
          new org.rhq.core.pluginapi.bundle.BundlePurgeRequest();
      purgeRequest.setBundleManagerProvider(this);
      purgeRequest.setLiveResourceDeployment(resourceDeployment);
      purgeRequest.setAbsoluteDestinationDirectory(absoluteDestDir);

      // get the bundle facet object that will process the bundle and call it to start the purge
      int facetMethodTimeout =
          30 * 60
              * 1000; // 30 minutes should be enough time for the bundle plugin to purge everything
      BundleFacet bundlePluginComponent =
          getBundleFacet(bundleHandlerResourceId, facetMethodTimeout);
      BundlePurgeResult result = bundlePluginComponent.purgeBundle(purgeRequest);
      if (result.isSuccess()) {
        auditDeployment(
            resourceDeployment, AUDIT_PURGE_ENDED, bundleDeployment.getName(), deploymentMessage);
      } else {
        response.setErrorMessage(result.getErrorMessage());
        auditDeployment(
            resourceDeployment,
            AUDIT_PURGE_ENDED,
            bundleDeployment.getName(),
            null,
            Status.FAILURE,
            "Failed: " + deploymentMessage,
            result.getErrorMessage());
      }
    } catch (Throwable t) {
      log.error("Failed to purge bundle: " + request, t);
      response.setErrorMessage(t);
    }

    return response;
  }
Example #2
0
  @Override
  public BundleScheduleResponse schedule(final BundleScheduleRequest request) {
    final BundleScheduleResponse response = new BundleScheduleResponse();

    try {
      final BundleResourceDeployment resourceDeployment = request.getBundleResourceDeployment();
      final BundleDeployment bundleDeployment = resourceDeployment.getBundleDeployment();

      // find the resource that will handle the bundle processing
      InventoryManager im = getInventoryManager();
      BundleType bundleType = bundleDeployment.getBundleVersion().getBundle().getBundleType();
      ResourceType resourceType = bundleType.getResourceType();
      Set<Resource> resources = im.getResourcesWithType(resourceType);
      if (resources.isEmpty()) {
        throw new Exception("No bundle plugin supports bundle type [" + bundleType + "]");
      }
      final int bundleHandlerResourceId = resources.iterator().next().getId();
      final ResourceContainer resourceContainer = im.getResourceContainer(bundleHandlerResourceId);
      if (null == resourceContainer.getResourceContext()) {
        throw new Exception(
            "No bundle plugin resource available to handle deployment for bundle type ["
                + bundleType
                + "]. Ensure the bundle plugin is deployed and its resource is imported into inventory.");
      }

      auditDeployment(
          resourceDeployment,
          AUDIT_DEPLOYMENT_SCHEDULED,
          bundleDeployment.getName(),
          "Scheduled deployment time: " + request.getRequestedDeployTimeAsString());

      Runnable deployerRunnable =
          new Runnable() {
            public void run() {
              try {
                // pull down the bundle files that the plugin will need in order to process the
                // bundle
                File pluginTmpDir = resourceContainer.getResourceContext().getTemporaryDirectory();
                File bundleFilesDir =
                    new File(
                        pluginTmpDir,
                        "bundle-versions/" + bundleDeployment.getBundleVersion().getId());
                bundleFilesDir.mkdirs();

                // clean up any old downloads we may have retrieved before. This helps clean out
                // our temp directory so we don't unnecessarily fill up our file system with
                // obsolete files
                removeOldDownloadedBundleFiles(bundleFilesDir);

                // now download the bundle files we need for the current deployment
                Map<PackageVersion, File> downloadedFiles =
                    downloadBundleFiles(resourceDeployment, bundleFilesDir);

                // deploy the bundle utilizing the bundle facet object
                String deploymentMessage =
                    "Deployment ["
                        + bundleDeployment
                        + "] to ["
                        + resourceDeployment.getResource()
                        + "]";
                auditDeployment(
                    resourceDeployment,
                    AUDIT_DEPLOYMENT_STARTED,
                    bundleDeployment.getName(),
                    deploymentMessage);

                File absoluteDestDir =
                    getAbsoluteDestinationDir(request.getBundleResourceDeployment());

                BundleDeployRequest deployRequest = new BundleDeployRequest();
                deployRequest.setBundleManagerProvider(BundleManager.this);
                deployRequest.setResourceDeployment(resourceDeployment);
                deployRequest.setBundleFilesLocation(bundleFilesDir);
                deployRequest.setPackageVersionFiles(downloadedFiles);
                deployRequest.setCleanDeployment(request.isCleanDeployment());
                deployRequest.setRevert(request.isRevert());
                deployRequest.setAbsoluteDestinationDirectory(absoluteDestDir);

                // get the bundle facet object that will process the bundle and call it to start the
                // deployment
                int facetMethodTimeout =
                    4 * 60 * 60 * 1000; // 4 hours is given to the bundle plugin to do its thing
                BundleFacet bundlePluginComponent =
                    getBundleFacet(bundleHandlerResourceId, facetMethodTimeout);
                BundleDeployResult result = bundlePluginComponent.deployBundle(deployRequest);
                if (result.isSuccess()) {
                  completeDeployment(
                      resourceDeployment, BundleDeploymentStatus.SUCCESS, deploymentMessage);
                } else {
                  completeDeployment(
                      resourceDeployment, BundleDeploymentStatus.FAILURE, result.getErrorMessage());
                }
              } catch (InterruptedException ie) {
                log.error("Failed to complete bundle deployment due to interrupt", ie);
                completeDeployment(
                    resourceDeployment, BundleDeploymentStatus.FAILURE, "Deployment interrupted");
              } catch (Throwable t) {
                log.error("Failed to complete bundle deployment", t);
                completeDeployment(
                    resourceDeployment,
                    BundleDeploymentStatus.FAILURE,
                    "Deployment failed: " + ThrowableUtil.getAllMessages(t));
              }
            }
          };

      this.deployerThreadPool.execute(deployerRunnable);
    } catch (Throwable t) {
      log.error("Failed to schedule bundle request: " + request, t);
      response.setErrorMessage(t);
    }

    return response;
  }