Search in sources :

Example 1 with SiteCreationException

use of org.craftercms.studio.api.v1.exception.SiteCreationException in project studio by craftercms.

the class SiteServiceImpl method createSiteWithRemoteOption.

@Override
@ValidateParams
public void createSiteWithRemoteOption(@ValidateStringParam(name = "siteId", maxLength = 50, whitelistedPatterns = "[a-z0-9\\-]*") String siteId, @ValidateStringParam(name = "sandboxBranch") String sandboxBranch, @ValidateNoTagsParam(name = "description") String description, String blueprintName, @ValidateStringParam(name = "remoteName") String remoteName, @ValidateStringParam(name = "remoteUrl") String remoteUrl, String remoteBranch, boolean singleBranch, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey, @ValidateStringParam(name = "createOption") String createOption, Map<String, String> params, boolean createAsOrphan) throws ServiceLayerException, InvalidRemoteRepositoryException, InvalidRemoteRepositoryCredentialsException, RemoteRepositoryNotFoundException, RemoteRepositoryNotBareException, InvalidRemoteUrlException {
    if (exists(siteId)) {
        throw new SiteAlreadyExistsException();
    }
    logger.debug("Validate site entitlements");
    try {
        entitlementValidator.validateEntitlement(EntitlementType.SITE, 1);
    } catch (EntitlementException e) {
        throw new SiteCreationException("Unable to complete request due to entitlement limits. Please contact your " + "system administrator.", e);
    }
    switch(createOption) {
        case REMOTE_REPOSITORY_CREATE_OPTION_CLONE:
            logger.info("Clone from remote repository create option selected");
            createSiteCloneRemote(siteId, sandboxBranch, description, remoteName, remoteUrl, remoteBranch, singleBranch, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey, params, createAsOrphan);
            break;
        case REMOTE_REPOSITORY_CREATE_OPTION_PUSH:
            logger.info("Push to remote repository create option selected");
            createSitePushToRemote(siteId, sandboxBranch, description, blueprintName, remoteName, remoteUrl, remoteBranch, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey, params, createAsOrphan);
            break;
        default:
            logger.error("Invalid create option for create site using remote repository: " + createOption + "\nAvailable options: [" + REMOTE_REPOSITORY_CREATE_OPTION_CLONE + ", " + REMOTE_REPOSITORY_CREATE_OPTION_PUSH + "]");
            break;
    }
}
Also used : EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) SiteAlreadyExistsException(org.craftercms.studio.api.v1.exception.SiteAlreadyExistsException) SiteCreationException(org.craftercms.studio.api.v1.exception.SiteCreationException) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 2 with SiteCreationException

use of org.craftercms.studio.api.v1.exception.SiteCreationException in project studio by craftercms.

the class SiteServiceImpl method createSiteCloneRemote.

@SuppressWarnings("deprecation")
private void createSiteCloneRemote(String siteId, String sandboxBranch, String description, String remoteName, String remoteUrl, String remoteBranch, boolean singleBranch, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey, Map<String, String> params, boolean createAsOrphan) throws ServiceLayerException, InvalidRemoteRepositoryException, InvalidRemoteRepositoryCredentialsException, RemoteRepositoryNotFoundException, InvalidRemoteUrlException {
    boolean success = true;
    // We must fail site creation if any of the site creations steps fail and rollback
    // For example: Create site => create Deployer Target (fail) = fail
    // and rollback the whole thing.
    // What we need to do for site creation and the order of execution:
    // 1) git repo, 2) deployer target, 3) database, 4) kick deployer
    String siteUuid = UUID.randomUUID().toString();
    try {
        // create site by cloning remote git repo
        logger.info("Creating site " + siteId + " by cloning remote repository " + remoteName + " (" + remoteUrl + ")");
        success = contentRepositoryV2.createSiteCloneRemote(siteId, sandboxBranch, remoteName, remoteUrl, remoteBranch, singleBranch, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey, params, createAsOrphan);
    } catch (InvalidRemoteRepositoryException | InvalidRemoteRepositoryCredentialsException | RemoteRepositoryNotFoundException | InvalidRemoteUrlException | ServiceLayerException e) {
        contentRepository.deleteSite(siteId);
        logger.error("Error while creating site: " + siteId + " ID: " + siteId + " as clone from " + "remote repository: " + remoteName + " (" + remoteUrl + "). Rolling back.", e);
        throw e;
    }
    if (!success) {
        contentRepository.removeRemoteRepositoriesForSite(siteId);
        contentRepository.deleteSite(siteId);
        throw new ServiceLayerException("Failed to create site: " + siteId + " ID: " + siteId + " as clone from " + "remote repository: " + remoteName + " (" + remoteUrl + ")");
    }
    // try to get the search engine from the blueprint descriptor file
    String searchEngine = studioConfiguration.getProperty(StudioConfiguration.PREVIEW_SEARCH_ENGINE);
    PluginDescriptor descriptor = sitesServiceInternal.getSiteBlueprintDescriptor(siteId);
    if (Objects.nonNull(descriptor) && Objects.nonNull(descriptor.getPlugin())) {
        searchEngine = descriptor.getPlugin().getSearchEngine();
        logger.info("Using search engine {0} from plugin descriptor", searchEngine);
    } else if (Objects.nonNull(descriptor) && Objects.nonNull(descriptor.getBlueprint())) {
        searchEngine = descriptor.getBlueprint().getSearchEngine();
        logger.info("Using search engine {0} from blueprint descriptor", searchEngine);
    } else {
        logger.info("Missing descriptor, using default search engine {0}", searchEngine);
    }
    if (success) {
        // Create the site in the preview deployer
        try {
            logger.info("Creating Deployer targets for site " + siteId);
            deployer.createTargets(siteId, searchEngine);
        } catch (Exception e) {
            logger.error("Error while creating site: " + siteId + " ID: " + siteId + " as clone from" + " remote repository: " + remoteName + " (" + remoteUrl + "). Rolling back...", e);
            contentRepositoryV2.removeRemote(siteId, remoteName);
            boolean deleted = contentRepository.deleteSite(siteId);
            if (!deleted) {
                logger.error("Error while rolling back site: " + siteId);
            }
            throw new DeployerTargetException("Error while creating site: " + siteId + " ID: " + siteId + " as clone from remote repository: " + remoteName + " (" + remoteUrl + "). The required Deployer targets couldn't " + "be created", e);
        }
    }
    if (success) {
        ZonedDateTime now = ZonedDateTime.now();
        String creator = securityService.getCurrentUser();
        try {
            logger.debug("Adding site UUID.");
            addSiteUuidFile(siteId, siteUuid);
            // insert database records
            logger.info("Adding site record to database for site " + siteId);
            SiteFeed siteFeed = new SiteFeed();
            siteFeed.setName(siteId);
            siteFeed.setSiteId(siteId);
            siteFeed.setSiteUuid(siteUuid);
            siteFeed.setDescription(description);
            siteFeed.setPublishingStatus(READY);
            siteFeed.setPublishingStatusMessage(studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_DEFAULT));
            siteFeed.setSandboxBranch(sandboxBranch);
            siteFeed.setSearchEngine(searchEngine);
            siteFeedMapper.createSite(siteFeed);
            upgradeManager.upgradeSite(siteId);
            // Add default groups
            logger.info("Adding default groups for site " + siteId);
            addDefaultGroupsForNewSite(siteId);
            String lastCommitId = contentRepositoryV2.getRepoLastCommitId(siteId);
            String firstCommitId = contentRepositoryV2.getRepoFirstCommitId(siteId);
            long startGetChangeSetCreatedFilesMark = logger.isDebugEnabled() ? System.currentTimeMillis() : 0;
            Map<String, String> createdFiles = contentRepositoryV2.getChangeSetPathsFromDelta(siteId, null, lastCommitId);
            if (logger.isDebugEnabled()) {
                logger.debug("Get change set created files finished in " + (System.currentTimeMillis() - startGetChangeSetCreatedFilesMark) + " milliseconds");
            }
            insertCreateSiteAuditLog(siteId, siteId, createdFiles);
            contentRepositoryV2.insertGitLog(siteId, firstCommitId, 1, 1);
            processCreatedFiles(siteId, createdFiles, creator, now, lastCommitId);
            updateLastCommitId(siteId, lastCommitId);
            updateLastVerifiedGitlogCommitId(siteId, lastCommitId);
            updateLastSyncedGitlogCommitId(siteId, firstCommitId);
            logger.info("Loading configuration for site " + siteId);
            reloadSiteConfiguration(siteId);
        } catch (Exception e) {
            success = false;
            logger.error("Error while creating site: " + siteId + " ID: " + siteId + " as clone from " + "remote repository: " + remoteName + " (" + remoteUrl + "). Rolling back.", e);
            deleteSite(siteId);
            throw new SiteCreationException("Error while creating site: " + siteId + " ID: " + siteId + " as clone from remote repository: " + remoteName + " (" + remoteUrl + "). Rolling back.");
        }
    }
    if (success) {
        // Now that everything is created, we can sync the preview deployer with the new content
        logger.info("Sync all site content to preview for " + siteId);
        try {
            deploymentService.syncAllContentToPreview(siteId, true);
        } catch (ServiceLayerException e) {
            logger.error("Error while syncing site: " + siteId + " ID: " + siteId + " to preview. Site was " + "successfully created otherwise. Ignoring.", e);
            throw new SiteCreationException("Error while syncing site: " + siteId + " ID: " + siteId + " to preview. Site was successfully created, but it won't be preview-able until the " + "Preview Deployer is reachable.");
        }
        setSiteState(siteId, STATE_CREATED);
    } else {
        throw new SiteCreationException("Error while creating site: " + siteId + " ID: " + siteId + ".");
    }
    logger.info("Finished creating site " + siteId);
}
Also used : InvalidRemoteRepositoryCredentialsException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException) DeployerTargetException(org.craftercms.studio.api.v1.exception.DeployerTargetException) InvalidRemoteRepositoryException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) SiteConfigNotFoundException(org.craftercms.studio.api.v1.service.site.SiteConfigNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) SiteCreationException(org.craftercms.studio.api.v1.exception.SiteCreationException) MissingPluginParameterException(org.craftercms.studio.api.v2.exception.MissingPluginParameterException) IOException(java.io.IOException) RemoteRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) DeployerTargetException(org.craftercms.studio.api.v1.exception.DeployerTargetException) RestServiceException(org.craftercms.commons.rest.RestServiceException) SiteAlreadyExistsException(org.craftercms.studio.api.v1.exception.SiteAlreadyExistsException) GroupAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.GroupAlreadyExistsException) DocumentException(org.dom4j.DocumentException) EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) BlueprintNotFoundException(org.craftercms.studio.api.v1.exception.BlueprintNotFoundException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) CryptoException(org.craftercms.commons.crypto.CryptoException) RemoteRepositoryNotBareException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotBareException) InvalidRemoteRepositoryCredentialsException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException) InvalidRemoteRepositoryException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException) PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor) ZonedDateTime(java.time.ZonedDateTime) RemoteRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) SiteCreationException(org.craftercms.studio.api.v1.exception.SiteCreationException)

Example 3 with SiteCreationException

use of org.craftercms.studio.api.v1.exception.SiteCreationException in project studio by craftercms.

the class SiteServiceImpl method createSitePushToRemote.

private void createSitePushToRemote(String siteId, String sandboxBranch, String description, String blueprintId, String remoteName, String remoteUrl, String remoteBranch, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey, Map<String, String> params, boolean createAsOrphan) throws ServiceLayerException {
    if (exists(siteId)) {
        throw new SiteAlreadyExistsException();
    }
    logger.debug("Get blueprint descriptor for " + blueprintId);
    PluginDescriptor descriptor = sitesServiceInternal.getBlueprintDescriptor(blueprintId);
    if (Objects.isNull(descriptor)) {
        throw new BlueprintNotFoundException();
    }
    logger.debug("Validate blueprint parameters");
    sitesServiceInternal.validateBlueprintParameters(descriptor, params);
    String blueprintLocation = sitesServiceInternal.getBlueprintLocation(blueprintId);
    String searchEngine = descriptor.getPlugin().getSearchEngine();
    boolean success = true;
    // We must fail site creation if any of the site creations steps fail and rollback
    // For example: Create site => create Deployer Target (fail) = fail
    // and rollback the whole thing.
    // What we need to do for site creation and the order of execution:
    // 1) deployer target, 2) git repo, 3) database, 4) kick deployer
    String siteUuid = UUID.randomUUID().toString();
    logger.info("Starting site creation process for site " + siteId + " from " + blueprintId + " blueprint.");
    // Create the site in the preview deployer
    try {
        logger.info("Creating Deployer targets for site " + siteId);
        deployer.createTargets(siteId, searchEngine);
    } catch (RestServiceException e) {
        String msg = "Error while creating site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId + ". The required Deployer targets couldn't be created";
        logger.error(msg, e);
        throw new DeployerTargetException(msg, e);
    }
    if (success) {
        try {
            logger.info("Creating site " + siteId + " from blueprint " + blueprintId);
            success = createSiteFromBlueprintGit(blueprintLocation, siteId, siteId, sandboxBranch, description, params);
            addSiteUuidFile(siteId, siteUuid);
            // insert database records
            logger.info("Adding site record to database for site " + siteId);
            SiteFeed siteFeed = new SiteFeed();
            siteFeed.setName(siteId);
            siteFeed.setSiteId(siteId);
            siteFeed.setSiteUuid(siteUuid);
            siteFeed.setDescription(description);
            siteFeed.setPublishingStatus(READY);
            siteFeed.setPublishingStatusMessage(studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_DEFAULT));
            siteFeed.setSandboxBranch(sandboxBranch);
            siteFeed.setSearchEngine(searchEngine);
            siteFeedMapper.createSite(siteFeed);
            logger.info("Upgrading site");
            upgradeManager.upgradeSite(siteId);
        } catch (Exception e) {
            success = false;
            logger.error("Error while creating site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId + ". Rolling back...", e);
            contentRepository.deleteSite(siteId);
            try {
                deployer.deleteTargets(siteId);
            } catch (Exception ex) {
                logger.error("Error while rolling back/deleting site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId + ". This means the site's Deployer " + "targets are still present, but the site was not successfully created", e);
            }
            throw new SiteCreationException("Error while creating site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId, e);
        }
        if (success) {
            ZonedDateTime now = ZonedDateTime.now();
            String creator = securityService.getCurrentUser();
            try {
                logger.info("Pushing site " + siteId + " to remote repository " + remoteName + " (" + remoteUrl + ")");
                contentRepository.addRemote(siteId, remoteName, remoteUrl, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey);
                contentRepository.createSitePushToRemote(siteId, remoteName, remoteUrl, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey, createAsOrphan);
            } catch (RemoteRepositoryNotFoundException | InvalidRemoteRepositoryException | InvalidRemoteRepositoryCredentialsException | RemoteRepositoryNotBareException | InvalidRemoteUrlException | ServiceLayerException e) {
                logger.error("Error while pushing site: " + siteId + " ID: " + siteId + " to remote repository " + remoteName + " (" + remoteUrl + ")", e);
                contentRepositoryV2.removeRemote(siteId, remoteName);
            }
            try {
                // Add default groups
                logger.info("Adding default groups for site " + siteId);
                addDefaultGroupsForNewSite(siteId);
                logger.debug("Adding audit logs.");
                String lastCommitId = contentRepositoryV2.getRepoLastCommitId(siteId);
                Map<String, String> createdFiles = contentRepositoryV2.getChangeSetPathsFromDelta(siteId, null, lastCommitId);
                insertCreateSiteAuditLog(siteId, siteId, createdFiles);
                insertAddRemoteAuditLog(siteId, remoteName);
                processCreatedFiles(siteId, createdFiles, creator, now, lastCommitId);
                contentRepositoryV2.insertGitLog(siteId, lastCommitId, 1, 1);
                updateLastCommitId(siteId, lastCommitId);
                updateLastVerifiedGitlogCommitId(siteId, lastCommitId);
                updateLastSyncedGitlogCommitId(siteId, lastCommitId);
                logger.info("Loading configuration for site " + siteId);
                reloadSiteConfiguration(siteId);
            } catch (Exception e) {
                success = false;
                logger.error("Error while creating site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId + ". Rolling back.", e);
                deleteSite(siteId);
                throw new SiteCreationException("Error while creating site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId + ". Rolling back.");
            }
        }
    }
    if (success) {
        // Now that everything is created, we can sync the preview deployer with the new content
        logger.info("Sync all content to preview for site " + siteId);
        try {
            deploymentService.syncAllContentToPreview(siteId, true);
        } catch (ServiceLayerException e) {
            logger.error("Error while syncing site: " + siteId + " ID: " + siteId + " to preview. Site was " + "successfully created otherwise. Ignoring.", e);
            throw new SiteCreationException("Error while syncing site: " + siteId + " ID: " + siteId + " to preview. Site was successfully created, but it won't be preview-able until the " + "Preview Deployer is reachable.");
        }
        setSiteState(siteId, STATE_CREATED);
    } else {
        throw new SiteCreationException("Error while creating site: " + siteId + " ID: " + siteId + ".");
    }
    logger.info("Finished creating site " + siteId);
}
Also used : BlueprintNotFoundException(org.craftercms.studio.api.v1.exception.BlueprintNotFoundException) InvalidRemoteRepositoryCredentialsException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException) DeployerTargetException(org.craftercms.studio.api.v1.exception.DeployerTargetException) SiteAlreadyExistsException(org.craftercms.studio.api.v1.exception.SiteAlreadyExistsException) InvalidRemoteRepositoryException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) SiteConfigNotFoundException(org.craftercms.studio.api.v1.service.site.SiteConfigNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) SiteCreationException(org.craftercms.studio.api.v1.exception.SiteCreationException) MissingPluginParameterException(org.craftercms.studio.api.v2.exception.MissingPluginParameterException) IOException(java.io.IOException) RemoteRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) DeployerTargetException(org.craftercms.studio.api.v1.exception.DeployerTargetException) RestServiceException(org.craftercms.commons.rest.RestServiceException) SiteAlreadyExistsException(org.craftercms.studio.api.v1.exception.SiteAlreadyExistsException) GroupAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.GroupAlreadyExistsException) DocumentException(org.dom4j.DocumentException) EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) BlueprintNotFoundException(org.craftercms.studio.api.v1.exception.BlueprintNotFoundException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) CryptoException(org.craftercms.commons.crypto.CryptoException) RemoteRepositoryNotBareException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotBareException) InvalidRemoteRepositoryCredentialsException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException) InvalidRemoteRepositoryException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException) RestServiceException(org.craftercms.commons.rest.RestServiceException) PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor) ZonedDateTime(java.time.ZonedDateTime) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) RemoteRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException) SiteCreationException(org.craftercms.studio.api.v1.exception.SiteCreationException) RemoteRepositoryNotBareException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotBareException)

Example 4 with SiteCreationException

use of org.craftercms.studio.api.v1.exception.SiteCreationException in project studio by craftercms.

the class SiteServiceImpl method createSiteFromBlueprint.

@Override
@ValidateParams
public void createSiteFromBlueprint(@ValidateStringParam(name = "blueprintId") String blueprintId, @ValidateNoTagsParam(name = "siteName") String siteName, @ValidateStringParam(name = "siteId", maxLength = 50, whitelistedPatterns = "[a-z0-9\\-]*") String siteId, @ValidateStringParam(name = "sandboxBranch") String sandboxBranch, @ValidateNoTagsParam(name = "desc") String desc, Map<String, String> params, boolean createAsOrphan) throws SiteAlreadyExistsException, SiteCreationException, DeployerTargetException, BlueprintNotFoundException, MissingPluginParameterException {
    if (exists(siteId)) {
        throw new SiteAlreadyExistsException();
    }
    logger.debug("Get blueprint descriptor for: " + blueprintId);
    PluginDescriptor descriptor = sitesServiceInternal.getBlueprintDescriptor(blueprintId);
    if (Objects.isNull(descriptor)) {
        throw new BlueprintNotFoundException();
    }
    logger.debug("Validating blueprint parameters");
    sitesServiceInternal.validateBlueprintParameters(descriptor, params);
    String blueprintLocation = sitesServiceInternal.getBlueprintLocation(blueprintId);
    String searchEngine = descriptor.getPlugin().getSearchEngine();
    logger.debug("Validate site entitlements");
    try {
        entitlementValidator.validateEntitlement(EntitlementType.SITE, 1);
    } catch (EntitlementException e) {
        throw new SiteCreationException("Unable to complete request due to entitlement limits. Please contact " + "your system administrator.", e);
    }
    logger.info("Starting site creation process for site " + siteName + " from " + blueprintId + " blueprint.");
    boolean success = true;
    // We must fail site creation if any of the site creations steps fail and rollback
    // For example: Create site => create Deployer Target (fail) = fail
    // and rollback the whole thing.
    // What we need to do for site creation and the order of execution:
    // 1) deployer target, 2) git repo, 3) database, 4) kick deployer
    String siteUuid = UUID.randomUUID().toString();
    // Create the site in the preview deployer
    logger.info("Creating deployer targets.");
    try {
        deployer.createTargets(siteId, searchEngine);
    } catch (Exception e) {
        success = false;
        String msg = "Error while creating site: " + siteName + " ID: " + siteId + " from blueprint: " + blueprintId + ". The required Deployer targets couldn't be created";
        logger.error(msg, e);
        throw new DeployerTargetException(msg, e);
    }
    if (success) {
        try {
            logger.info("Copying site content from blueprint.");
            success = createSiteFromBlueprintGit(blueprintLocation, siteName, siteId, sandboxBranch, desc, params);
            ZonedDateTime now = ZonedDateTime.now();
            logger.debug("Adding site UUID.");
            addSiteUuidFile(siteId, siteUuid);
            logger.info("Adding site record to database for site " + siteId);
            // insert database records
            SiteFeed siteFeed = new SiteFeed();
            siteFeed.setName(siteName);
            siteFeed.setSiteId(siteId);
            siteFeed.setSiteUuid(siteUuid);
            siteFeed.setDescription(desc);
            siteFeed.setPublishingStatus(READY);
            siteFeed.setPublishingStatusMessage(studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_DEFAULT));
            siteFeed.setSandboxBranch(sandboxBranch);
            siteFeed.setSearchEngine(searchEngine);
            siteFeedMapper.createSite(siteFeed);
            String localeAddress = studioClusterUtils.getClusterNodeLocalAddress();
            ClusterMember cm = clusterDao.getMemberByLocalAddress(localeAddress);
            if (Objects.nonNull(cm)) {
                SiteFeed s = getSite(siteId);
                clusterDao.insertClusterSiteSyncRepo(cm.getId(), s.getId(), null, null, null);
            }
            logger.info("Upgrading site.");
            upgradeManager.upgradeSite(siteId);
            // Add default groups
            logger.info("Adding default groups");
            addDefaultGroupsForNewSite(siteId);
            String lastCommitId = contentRepositoryV2.getRepoLastCommitId(siteId);
            String creator = securityService.getCurrentUser();
            long startGetChangeSetCreatedFilesMark = logger.isDebugEnabled() ? System.currentTimeMillis() : 0;
            Map<String, String> createdFiles = contentRepositoryV2.getChangeSetPathsFromDelta(siteId, null, lastCommitId);
            if (logger.isDebugEnabled()) {
                logger.debug("Get change set created files finished in " + (System.currentTimeMillis() - startGetChangeSetCreatedFilesMark) + " milliseconds");
            }
            logger.info("Adding audit log");
            insertCreateSiteAuditLog(siteId, siteName, createdFiles);
            processCreatedFiles(siteId, createdFiles, creator, now, lastCommitId);
            contentRepositoryV2.insertGitLog(siteId, lastCommitId, 1, 1);
            updateLastCommitId(siteId, lastCommitId);
            updateLastVerifiedGitlogCommitId(siteId, lastCommitId);
            updateLastSyncedGitlogCommitId(siteId, lastCommitId);
            logger.info("Reload site configuration");
            reloadSiteConfiguration(siteId);
        } catch (Exception e) {
            success = false;
            logger.error("Error while creating site: " + siteName + " ID: " + siteId + " from blueprint: " + blueprintId + ". Rolling back.", e);
            deleteSite(siteId);
            throw new SiteCreationException("Error while creating site: " + siteName + " ID: " + siteId + " from blueprint: " + blueprintId + ". Rolling back.");
        }
    }
    if (success) {
        logger.info("Syncing all content to preview.");
        // Now that everything is created, we can sync the preview deployer with the new content
        try {
            deploymentService.syncAllContentToPreview(siteId, true);
        } catch (ServiceLayerException e) {
            logger.error("Error while syncing site: " + siteName + " ID: " + siteId + " to preview. Site was " + "successfully created otherwise. Ignoring.", e);
            throw new SiteCreationException("Error while syncing site: " + siteName + " ID: " + siteId + " to preview. Site was successfully created, but it won't be preview-able until the Preview " + "Deployer is reachable.");
        }
        setSiteState(siteId, STATE_CREATED);
    } else {
        throw new SiteCreationException("Error while creating site: " + siteName + " ID: " + siteId + ".");
    }
    logger.info("Finished creating site " + siteId);
}
Also used : BlueprintNotFoundException(org.craftercms.studio.api.v1.exception.BlueprintNotFoundException) DeployerTargetException(org.craftercms.studio.api.v1.exception.DeployerTargetException) SiteAlreadyExistsException(org.craftercms.studio.api.v1.exception.SiteAlreadyExistsException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) SiteConfigNotFoundException(org.craftercms.studio.api.v1.service.site.SiteConfigNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) SiteCreationException(org.craftercms.studio.api.v1.exception.SiteCreationException) MissingPluginParameterException(org.craftercms.studio.api.v2.exception.MissingPluginParameterException) IOException(java.io.IOException) RemoteRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) DeployerTargetException(org.craftercms.studio.api.v1.exception.DeployerTargetException) RestServiceException(org.craftercms.commons.rest.RestServiceException) SiteAlreadyExistsException(org.craftercms.studio.api.v1.exception.SiteAlreadyExistsException) GroupAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.GroupAlreadyExistsException) DocumentException(org.dom4j.DocumentException) EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) BlueprintNotFoundException(org.craftercms.studio.api.v1.exception.BlueprintNotFoundException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) CryptoException(org.craftercms.commons.crypto.CryptoException) RemoteRepositoryNotBareException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotBareException) InvalidRemoteRepositoryCredentialsException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException) InvalidRemoteRepositoryException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException) PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor) EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) ZonedDateTime(java.time.ZonedDateTime) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) SiteCreationException(org.craftercms.studio.api.v1.exception.SiteCreationException) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Aggregations

EntitlementException (org.craftercms.commons.entitlements.exception.EntitlementException)4 SiteAlreadyExistsException (org.craftercms.studio.api.v1.exception.SiteAlreadyExistsException)4 SiteCreationException (org.craftercms.studio.api.v1.exception.SiteCreationException)4 IOException (java.io.IOException)3 ZonedDateTime (java.time.ZonedDateTime)3 CryptoException (org.craftercms.commons.crypto.CryptoException)3 PluginDescriptor (org.craftercms.commons.plugin.model.PluginDescriptor)3 RestServiceException (org.craftercms.commons.rest.RestServiceException)3 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)3 BlueprintNotFoundException (org.craftercms.studio.api.v1.exception.BlueprintNotFoundException)3 DeployerTargetException (org.craftercms.studio.api.v1.exception.DeployerTargetException)3 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)3 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)3 InvalidRemoteRepositoryCredentialsException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException)3 InvalidRemoteRepositoryException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException)3 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)3 RemoteRepositoryNotBareException (org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotBareException)3 RemoteRepositoryNotFoundException (org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException)3 GroupAlreadyExistsException (org.craftercms.studio.api.v1.exception.security.GroupAlreadyExistsException)3 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)3