Search in sources :

Example 26 with SiteFeed

use of org.craftercms.studio.api.v1.dal.SiteFeed 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)

Example 27 with SiteFeed

use of org.craftercms.studio.api.v1.dal.SiteFeed in project studio by craftercms.

the class SiteServiceImpl method updateLastCommitId.

@Override
@ValidateParams
public void updateLastCommitId(@ValidateStringParam(name = "site") String site, @ValidateStringParam(name = "commitId") String commitId) {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("siteId", site);
    params.put("lastCommitId", commitId);
    retryingOperationFacade.updateSiteLastCommitId(params);
    try {
        ClusterMember clusterMember = clusterDao.getMemberByLocalAddress(studioClusterUtils.getClusterNodeLocalAddress());
        if (Objects.nonNull(clusterMember)) {
            SiteFeed siteFeed = getSite(site);
            retryingOperationFacade.updateClusterNodeLastCommitId(clusterMember.getId(), siteFeed.getId(), commitId);
        }
    } catch (SiteNotFoundException e) {
        logger.error("Site not found " + site);
    }
}
Also used : ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) HashMap(java.util.HashMap) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 28 with SiteFeed

use of org.craftercms.studio.api.v1.dal.SiteFeed in project studio by craftercms.

the class SiteServiceImpl method insertDeleteSiteAuditLog.

private void insertDeleteSiteAuditLog(String siteId, String siteName) throws SiteNotFoundException {
    SiteFeed siteFeed = getSite(studioConfiguration.getProperty(CONFIGURATION_GLOBAL_SYSTEM_SITE));
    String user = securityService.getCurrentUser();
    AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
    auditLog.setOperation(OPERATION_DELETE);
    auditLog.setSiteId(siteFeed.getId());
    auditLog.setActorId(user);
    auditLog.setPrimaryTargetId(siteId);
    auditLog.setPrimaryTargetType(TARGET_TYPE_SITE);
    auditLog.setPrimaryTargetValue(siteName);
    auditServiceInternal.insertAuditLog(auditLog);
}
Also used : SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog)

Example 29 with SiteFeed

use of org.craftercms.studio.api.v1.dal.SiteFeed in project studio by craftercms.

the class SiteServiceImpl method writeConfiguration.

@Override
@ValidateParams
public boolean writeConfiguration(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path, InputStream content) throws ServiceLayerException {
    // Write site configuration
    String operation = OPERATION_UPDATE;
    if (!contentRepository.contentExists(site, path)) {
        operation = OPERATION_CREATE;
    }
    String commitId = contentRepository.writeContent(site, path, content);
    contentRepository.reloadRepository(site);
    PreviewEventContext context = new PreviewEventContext();
    context.setSite(site);
    eventService.publish(EVENT_PREVIEW_SYNC, context);
    String user = securityService.getCurrentUser();
    Map<String, String> extraInfo = new HashMap<String, String>();
    if (StringUtils.startsWith(path, contentTypeService.getConfigPath())) {
        extraInfo.put(DmConstants.KEY_CONTENT_TYPE, StudioConstants.CONTENT_TYPE_CONTENT_TYPE);
    } else {
        extraInfo.put(DmConstants.KEY_CONTENT_TYPE, StudioConstants.CONTENT_TYPE_CONFIGURATION);
    }
    SiteFeed siteFeed = getSite(site);
    AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
    auditLog.setOperation(operation);
    auditLog.setSiteId(siteFeed.getId());
    auditLog.setActorId(user);
    auditLog.setPrimaryTargetId(site + ":" + path);
    auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
    auditLog.setPrimaryTargetValue(path);
    auditServiceInternal.insertAuditLog(auditLog);
    objectStateService.transition(site, path, TransitionEvent.SAVE);
    if (!objectMetadataManager.metadataExist(site, path)) {
        objectMetadataManager.insertNewObjectMetadata(site, path);
    }
    Map<String, Object> properties = new HashMap<>();
    properties.put(ItemMetadata.PROP_NAME, FilenameUtils.getName(path));
    properties.put(ItemMetadata.PROP_MODIFIED, ZonedDateTime.now(ZoneOffset.UTC));
    properties.put(ItemMetadata.PROP_MODIFIER, user);
    objectMetadataManager.setObjectMetadata(site, path, properties);
    if (commitId != null) {
        objectMetadataManager.updateCommitId(site, path, commitId);
        contentRepositoryV2.insertGitLog(site, commitId, 1);
    }
    boolean toRet = StringUtils.isEmpty(commitId);
    return toRet;
}
Also used : HashMap(java.util.HashMap) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) PreviewEventContext(org.craftercms.studio.api.v1.ebus.PreviewEventContext) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 30 with SiteFeed

use of org.craftercms.studio.api.v1.dal.SiteFeed in project studio by craftercms.

the class SiteServiceImpl method insertAddRemoteAuditLog.

private void insertAddRemoteAuditLog(String siteId, String remoteName) throws SiteNotFoundException {
    SiteFeed siteFeed = getSite(siteId);
    String user = securityService.getCurrentUser();
    AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
    auditLog.setOperation(OPERATION_ADD_REMOTE);
    auditLog.setSiteId(siteFeed.getId());
    auditLog.setActorId(user);
    auditLog.setPrimaryTargetId(remoteName);
    auditLog.setPrimaryTargetType(TARGET_TYPE_REMOTE_REPOSITORY);
    auditLog.setPrimaryTargetValue(remoteName);
    auditServiceInternal.insertAuditLog(auditLog);
}
Also used : SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog)

Aggregations

SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)58 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)39 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)21 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)18 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)15 AuditLogParameter (org.craftercms.studio.api.v2.dal.AuditLogParameter)12 Group (org.craftercms.studio.api.v2.dal.Group)12 HasPermission (org.craftercms.commons.security.permissions.annotations.HasPermission)11 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)11 User (org.craftercms.studio.api.v2.dal.User)10 IOException (java.io.IOException)9 ClusterMember (org.craftercms.studio.api.v2.dal.ClusterMember)9 CryptoException (org.craftercms.commons.crypto.CryptoException)8 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)8 EntitlementException (org.craftercms.commons.entitlements.exception.EntitlementException)6 ZonedDateTime (java.time.ZonedDateTime)5 AuthenticationSystemException (org.craftercms.studio.api.v1.exception.security.AuthenticationSystemException)5 UserAlreadyExistsException (org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException)5 SiteService (org.craftercms.studio.api.v1.service.site.SiteService)5