Search in sources :

Example 16 with DeploymentException

use of org.craftercms.studio.api.v1.service.deployment.DeploymentException in project studio by craftercms.

the class DmPublishServiceImpl method bulkGoLive.

@Override
@ValidateParams
public void bulkGoLive(@ValidateStringParam(name = "site") String site, @ValidateStringParam String environment, @ValidateSecurePathParam(name = "path") String path, String comment) throws ServiceLayerException {
    logger.info("Starting Bulk Publish to '" + environment + "' for path " + path + " site " + site);
    String queryPath = path;
    if (queryPath.startsWith(FILE_SEPARATOR + DmConstants.INDEX_FILE)) {
        queryPath = queryPath.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, "");
    }
    logger.debug("Get change set for subtree for site: " + site + " root path: " + queryPath);
    List<String> childrenPaths = new ArrayList<String>();
    childrenPaths = objectStateService.getChangeSetForSubtree(site, queryPath);
    logger.debug("Collected " + childrenPaths.size() + " content items for site " + site + " and root path " + queryPath);
    Set<String> processedPaths = new HashSet<String>();
    ZonedDateTime launchDate = ZonedDateTime.now(ZoneOffset.UTC);
    for (String childPath : childrenPaths) {
        String childHash = DigestUtils.md2Hex(childPath);
        logger.debug("Processing dependencies for site " + site + " path " + childPath);
        if (processedPaths.add(childHash)) {
            List<String> pathsToPublish = new ArrayList<String>();
            List<String> candidatesToPublish = new ArrayList<String>();
            pathsToPublish.add(childPath);
            candidatesToPublish.addAll(objectMetadataManager.getSameCommitItems(site, childPath));
            candidatesToPublish.addAll(dependencyService.getPublishingDependencies(site, childPath));
            for (String pathToAdd : candidatesToPublish) {
                String hash = DigestUtils.md2Hex(pathToAdd);
                if (processedPaths.add(hash)) {
                    pathsToPublish.add(pathToAdd);
                }
            }
            String aprover = securityService.getCurrentUser();
            if (StringUtils.isEmpty(comment)) {
                comment = "Bulk Publish invoked by " + aprover;
            }
            logger.info("Deploying package of " + pathsToPublish.size() + " items to '" + environment + "' for site" + site + " path " + childPath);
            try {
                deploymentService.deploy(site, environment, pathsToPublish, launchDate, aprover, comment, true);
            } catch (DeploymentException e) {
                logger.error("Error while running Bulk Publish operation", e);
            } finally {
                logger.debug("Finished processing deployment package for path " + childPath + " site " + site);
            }
        }
    }
    logger.info("Finished Bulk Publish to '" + environment + "' for path " + path + " site " + site);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) ArrayList(java.util.ArrayList) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) HashSet(java.util.HashSet) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 17 with DeploymentException

use of org.craftercms.studio.api.v1.service.deployment.DeploymentException in project studio by craftercms.

the class PublishingManagerImpl method markItemsReady.

@RetryingOperation
@Override
@ValidateParams
public void markItemsReady(@ValidateStringParam(name = "site") String site, @ValidateStringParam(name = "environment") String environment, List<PublishRequest> copyToEnvironmentItems) throws DeploymentException {
    for (PublishRequest item : copyToEnvironmentItems) {
        item.setState(READY_FOR_LIVE);
        publishRequestMapper.updateItemDeploymentState(item);
    }
}
Also used : PublishRequest(org.craftercms.studio.api.v1.dal.PublishRequest) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 18 with DeploymentException

use of org.craftercms.studio.api.v1.service.deployment.DeploymentException in project studio by craftercms.

the class PublishingManagerImpl method markItemsProcessing.

@RetryingOperation
@Override
@ValidateParams
public void markItemsProcessing(@ValidateStringParam(name = "site") String site, @ValidateStringParam(name = "environment") String environment, List<PublishRequest> itemsToDeploy) throws DeploymentException {
    for (PublishRequest item : itemsToDeploy) {
        item.setState(PublishRequest.State.PROCESSING);
        publishRequestMapper.updateItemDeploymentState(item);
    }
}
Also used : PublishRequest(org.craftercms.studio.api.v1.dal.PublishRequest) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 19 with DeploymentException

use of org.craftercms.studio.api.v1.service.deployment.DeploymentException in project studio by craftercms.

the class PublishingManagerImpl method markItemsCompleted.

@Override
@ValidateParams
public void markItemsCompleted(@ValidateStringParam(name = "site") String site, @ValidateStringParam(name = "environment") String environment, List<PublishRequest> processedItems) throws DeploymentException {
    ZonedDateTime completed = ZonedDateTime.now();
    for (PublishRequest item : processedItems) {
        item.setState(PublishRequest.State.COMPLETED);
        item.setCompletedDate(completed);
        retryingOperationFacade.markPublishRequestCompleted(item);
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) PublishRequest(org.craftercms.studio.api.v1.dal.PublishRequest) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 20 with DeploymentException

use of org.craftercms.studio.api.v1.service.deployment.DeploymentException in project studio by craftercms.

the class PublishingManagerImpl method processItem.

@Override
public DeploymentItemTO processItem(PublishRequest item) throws DeploymentException, SiteNotFoundException {
    if (item == null) {
        throw new DeploymentException("Cannot process item, item is null.");
    }
    DeploymentItemTO deploymentItem = new DeploymentItemTO();
    deploymentItem.setSite(item.getSite());
    deploymentItem.setPath(item.getPath());
    deploymentItem.setCommitId(item.getCommitId());
    deploymentItem.setPackageId(item.getPackageId());
    String site = item.getSite();
    String path = item.getPath();
    String oldPath = item.getOldPath();
    String environment = item.getEnvironment();
    String action = item.getAction();
    String user = item.getUser();
    String liveEnvironment = LIVE_ENVIRONMENT;
    if (servicesConfig.isStagingEnvironmentEnabled(site)) {
        liveEnvironment = servicesConfig.getLiveEnvironment(site);
    }
    boolean isLive = false;
    if (StringUtils.isNotEmpty(liveEnvironment)) {
        if (liveEnvironment.equals(environment)) {
            isLive = true;
        }
    } else if (StringUtils.equalsIgnoreCase(LIVE_ENVIRONMENT, item.getEnvironment()) || StringUtils.equalsIgnoreCase(PRODUCTION_ENVIRONMENT, environment)) {
        isLive = true;
    }
    if (StringUtils.equals(action, PublishRequest.Action.DELETE)) {
        if (oldPath != null && oldPath.length() > 0) {
            contentService.deleteContent(site, oldPath, user);
            boolean hasRenamedChildren = false;
            if (oldPath.endsWith(FILE_SEPARATOR + DmConstants.INDEX_FILE)) {
                if (contentService.contentExists(site, oldPath.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""))) {
                    // TODO: SJ: This bypasses the Content Service, fix
                    RepositoryItem[] children = contentRepository.getContentChildren(site, oldPath.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""));
                    if (children.length > 1) {
                        hasRenamedChildren = true;
                    }
                }
                if (!hasRenamedChildren) {
                    deleteFolder(site, oldPath.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""), user);
                }
            }
            deploymentItem.setMove(true);
            deploymentItem.setOldPath(oldPath);
            objectMetadataManager.clearRenamed(site, path);
        }
        boolean haschildren = false;
        if (item.getPath().endsWith(FILE_SEPARATOR + DmConstants.INDEX_FILE)) {
            if (contentService.contentExists(site, path.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""))) {
                // TODO: SJ: This bypasses the Content Service, fix
                RepositoryItem[] children = contentRepository.getContentChildren(site, path.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""));
                if (children.length > 1) {
                    haschildren = true;
                }
            }
        }
        if (contentService.contentExists(site, path)) {
            contentService.deleteContent(site, path, user);
            if (!haschildren) {
                deleteFolder(site, path.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""), user);
            }
        }
        deploymentItem.setDelete(true);
    } else {
        if (StringUtils.equals(action, PublishRequest.Action.MOVE)) {
            deploymentItem.setMove(true);
            deploymentItem.setOldPath(oldPath);
            if (oldPath != null && oldPath.length() > 0) {
                if (isLive) {
                    objectMetadataManager.clearRenamed(site, path);
                }
            }
        }
        ItemMetadata itemMetadata = objectMetadataManager.getProperties(site, path);
        if (itemMetadata == null) {
            if (contentService.contentExists(site, path)) {
                LOGGER.warn("Content item: '" + site + "':'" + path + "' doesn't exists in " + "the database, but does exist in git. This may cause problems " + "in the environment: '" + environment + "'");
            } else {
                LOGGER.warn("Content item: '" + site + "':'" + path + "' cannot be published. " + "Content does not exist in git nor in the database. Skipping...");
                return null;
            }
        } else {
            ContentItemTO contentItem = contentService.getContentItem(site, path);
            if (isLive) {
                // should consider what should be done if this does not work.
                // Currently the method will bail and the item is stuck in processing.
                LOGGER.debug("Environment is live, transition item to LIVE state {0}:{1}", site, path);
                // check if commit id from workflow and from object state match
                if (Objects.isNull(itemMetadata.getCommitId()) || itemMetadata.getCommitId().equals(item.getCommitId())) {
                    objectStateService.transition(site, contentItem, TransitionEvent.DEPLOYMENT);
                }
            } else {
                objectStateService.transition(site, contentItem, TransitionEvent.SAVE);
            }
            itemMetadata.setSubmittedBy(StringUtils.EMPTY);
            itemMetadata.setSendEmail(0);
            itemMetadata.setSubmittedForDeletion(0);
            itemMetadata.setSubmissionComment(StringUtils.EMPTY);
            itemMetadata.setSubmittedToEnvironment(StringUtils.EMPTY);
            itemMetadata.setLaunchDate(null);
            objectMetadataManager.updateObjectMetadata(itemMetadata);
        }
        String blacklistConfig = studioConfiguration.getProperty(CONFIGURATION_PUBLISHING_BLACKLIST_REGEX);
        if (StringUtils.isNotEmpty(blacklistConfig) && ContentUtils.matchesPatterns(item.getPath(), Arrays.asList(StringUtils.split(blacklistConfig, ",")))) {
            LOGGER.debug("File " + item.getPath() + " of the site " + site + " will not be published because it " + "matches the configured publishing blacklist regex patterns.");
            markItemsCompleted(site, item.getEnvironment(), Arrays.asList(item));
            deploymentItem = null;
        }
    }
    return deploymentItem;
}
Also used : RepositoryItem(org.craftercms.studio.api.v1.repository.RepositoryItem) ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) DeploymentItemTO(org.craftercms.studio.api.v1.to.DeploymentItemTO) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) ItemMetadata(org.craftercms.studio.api.v1.dal.ItemMetadata)

Aggregations

DeploymentException (org.craftercms.studio.api.v1.service.deployment.DeploymentException)12 DeploymentItemTO (org.craftercms.studio.api.v1.to.DeploymentItemTO)10 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)9 ArrayList (java.util.ArrayList)8 PublishRequest (org.craftercms.studio.api.v1.dal.PublishRequest)8 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)6 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)6 ZonedDateTime (java.time.ZonedDateTime)5 HashSet (java.util.HashSet)5 CryptoException (org.craftercms.commons.crypto.CryptoException)4 IOException (java.io.IOException)3 RetryingOperation (org.craftercms.studio.api.v2.annotation.RetryingOperation)3 Test (org.testng.annotations.Test)3 SimpleDateFormat (java.text.SimpleDateFormat)2 ItemMetadata (org.craftercms.studio.api.v1.dal.ItemMetadata)2 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)2 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)2 InvalidRemoteRepositoryCredentialsException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException)2 InvalidRemoteRepositoryException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException)2 RemoteRepositoryNotFoundException (org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException)2