Search in sources :

Example 1 with COPY

use of org.craftercms.studio.api.v2.dal.RepoOperation.Action.COPY in project studio by craftercms.

the class ConfigurationServiceImpl method validate.

@SuppressWarnings("unchecked")
protected InputStream validate(InputStream content, String filename) throws ServiceLayerException {
    // Check the filename to see if it needs to be validated
    String extension = getExtension(filename);
    if (isEmpty(extension)) {
        // without extension there is no way to know
        return content;
    }
    try {
        // Copy the contents of the stream
        byte[] bytes;
        bytes = IOUtils.toByteArray(content);
        // Perform the validation
        switch(extension.toLowerCase()) {
            case "xml":
                try {
                    DocumentHelper.parseText(new String(bytes));
                } catch (Exception e) {
                    throw new InvalidConfigurationException("Invalid XML file", e);
                }
                break;
            case "yaml":
            case "yml":
                try {
                    Yaml yaml = new Yaml(new DisableClassLoadingConstructor());
                    Map<String, Object> map = (Map<String, Object>) yaml.load(new ByteArrayInputStream(bytes));
                } catch (Exception e) {
                    throw new InvalidConfigurationException("Invalid YAML file", e);
                }
        }
        // Return a new stream
        return new ByteArrayInputStream(bytes);
    } catch (IOException e) {
        throw new ServiceLayerException("Error validating configuration", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) DisableClassLoadingConstructor(org.craftercms.commons.config.DisableClassLoadingConstructor) Map(java.util.Map) HashMap(java.util.HashMap) SAXException(org.xml.sax.SAXException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) DocumentException(org.dom4j.DocumentException) ConfigurationException(org.craftercms.studio.api.v2.exception.configuration.ConfigurationException) IOException(java.io.IOException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) InvalidConfigurationException(org.craftercms.studio.api.v2.exception.configuration.InvalidConfigurationException) Yaml(org.yaml.snakeyaml.Yaml) InvalidConfigurationException(org.craftercms.studio.api.v2.exception.configuration.InvalidConfigurationException)

Example 2 with COPY

use of org.craftercms.studio.api.v2.dal.RepoOperation.Action.COPY in project studio by craftercms.

the class StudioAuditLogProcessingTask method processAuditLogFromRepo.

private void processAuditLogFromRepo(String siteId, int batchSize) throws SiteNotFoundException {
    List<GitLog> unauditedGitlogs = contentRepository.getUnauditedCommits(siteId, batchSize);
    if (unauditedGitlogs != null) {
        SiteFeed siteFeed = siteService.getSite(siteId);
        for (GitLog gl : unauditedGitlogs) {
            if (contentRepository.commitIdExists(siteId, gl.getCommitId())) {
                String prevCommitId = gl.getCommitId() + PREVIOUS_COMMIT_SUFFIX;
                List<RepoOperation> operations = contentRepository.getOperationsFromDelta(siteId, prevCommitId, gl.getCommitId());
                for (RepoOperation repoOperation : operations) {
                    Map<String, String> activityInfo = new HashMap<String, String>();
                    String contentClass;
                    AuditLog auditLog;
                    switch(repoOperation.getAction()) {
                        case CREATE:
                        case COPY:
                            contentClass = contentService.getContentTypeClass(siteId, repoOperation.getPath());
                            if (repoOperation.getPath().endsWith(DmConstants.XML_PATTERN)) {
                                activityInfo.put(DmConstants.KEY_CONTENT_TYPE, contentClass);
                            }
                            logger.debug("Insert audit log for site: " + siteId + " path: " + repoOperation.getPath());
                            auditLog = auditServiceInternal.createAuditLogEntry();
                            auditLog.setOperation(OPERATION_CREATE);
                            auditLog.setOperationTimestamp(repoOperation.getDateTime());
                            auditLog.setSiteId(siteFeed.getId());
                            auditLog.setActorId(repoOperation.getAuthor());
                            auditLog.setActorDetails(repoOperation.getAuthor());
                            auditLog.setPrimaryTargetId(siteId + ":" + repoOperation.getPath());
                            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
                            auditLog.setPrimaryTargetValue(repoOperation.getPath());
                            auditLog.setPrimaryTargetSubtype(contentService.getContentTypeClass(siteId, repoOperation.getPath()));
                            auditLog.setOrigin(ORIGIN_GIT);
                            auditServiceInternal.insertAuditLog(auditLog);
                            break;
                        case UPDATE:
                            contentClass = contentService.getContentTypeClass(siteId, repoOperation.getPath());
                            if (repoOperation.getPath().endsWith(DmConstants.XML_PATTERN)) {
                                activityInfo.put(DmConstants.KEY_CONTENT_TYPE, contentClass);
                            }
                            logger.debug("Insert audit log for site: " + siteId + " path: " + repoOperation.getPath());
                            auditLog = auditServiceInternal.createAuditLogEntry();
                            auditLog.setOperation(OPERATION_UPDATE);
                            auditLog.setOperationTimestamp(repoOperation.getDateTime());
                            auditLog.setSiteId(siteFeed.getId());
                            auditLog.setActorId(repoOperation.getAuthor());
                            auditLog.setActorDetails(repoOperation.getAuthor());
                            auditLog.setOrigin(ORIGIN_GIT);
                            auditLog.setPrimaryTargetId(siteId + ":" + repoOperation.getPath());
                            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
                            auditLog.setPrimaryTargetValue(repoOperation.getPath());
                            auditLog.setPrimaryTargetSubtype(contentService.getContentTypeClass(siteId, repoOperation.getPath()));
                            auditServiceInternal.insertAuditLog(auditLog);
                            break;
                        case DELETE:
                            contentClass = contentService.getContentTypeClass(siteId, repoOperation.getPath());
                            if (repoOperation.getPath().endsWith(DmConstants.XML_PATTERN)) {
                                activityInfo.put(DmConstants.KEY_CONTENT_TYPE, contentClass);
                            }
                            logger.debug("Insert audit log for site: " + siteId + " path: " + repoOperation.getPath());
                            auditLog = auditServiceInternal.createAuditLogEntry();
                            auditLog.setOperation(OPERATION_DELETE);
                            auditLog.setOperationTimestamp(repoOperation.getDateTime());
                            auditLog.setSiteId(siteFeed.getId());
                            auditLog.setOrigin(ORIGIN_GIT);
                            auditLog.setActorId(repoOperation.getAuthor());
                            auditLog.setActorDetails(repoOperation.getAuthor());
                            auditLog.setPrimaryTargetId(siteId + ":" + repoOperation.getPath());
                            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
                            auditLog.setPrimaryTargetValue(repoOperation.getPath());
                            auditLog.setPrimaryTargetSubtype(contentService.getContentTypeClass(siteId, repoOperation.getPath()));
                            auditServiceInternal.insertAuditLog(auditLog);
                            break;
                        case MOVE:
                            contentClass = contentService.getContentTypeClass(siteId, repoOperation.getMoveToPath());
                            if (repoOperation.getMoveToPath().endsWith(DmConstants.XML_PATTERN)) {
                                activityInfo.put(DmConstants.KEY_CONTENT_TYPE, contentClass);
                            }
                            logger.debug("Insert audit log for site: " + siteId + " path: " + repoOperation.getMoveToPath());
                            auditLog = auditServiceInternal.createAuditLogEntry();
                            auditLog.setOperation(OPERATION_MOVE);
                            auditLog.setOperationTimestamp(repoOperation.getDateTime());
                            auditLog.setSiteId(siteFeed.getId());
                            auditLog.setActorId(repoOperation.getAuthor());
                            auditLog.setActorDetails(repoOperation.getAuthor());
                            auditLog.setOrigin(ORIGIN_GIT);
                            auditLog.setPrimaryTargetId(siteId + ":" + repoOperation.getMoveToPath());
                            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
                            auditLog.setPrimaryTargetValue(repoOperation.getMoveToPath());
                            auditLog.setPrimaryTargetSubtype(contentService.getContentTypeClass(siteId, repoOperation.getMoveToPath()));
                            auditServiceInternal.insertAuditLog(auditLog);
                            break;
                        default:
                            logger.error("Error: Unknown repo operation for site " + siteId + " operation: " + repoOperation.getAction());
                            break;
                    }
                }
            }
            contentRepository.markGitLogAudited(siteId, gl.getCommitId());
        }
    }
}
Also used : RepoOperation(org.craftercms.studio.api.v2.dal.RepoOperation) HashMap(java.util.HashMap) GitLog(org.craftercms.studio.api.v2.dal.GitLog) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog)

Example 3 with COPY

use of org.craftercms.studio.api.v2.dal.RepoOperation.Action.COPY in project studio by craftercms.

the class GitContentRepository method bootstrap.

/**
 * bootstrap the repository
 */
public void bootstrap() throws Exception {
    logger.debug("Bootstrap global repository.");
    boolean bootstrapRepo = Boolean.parseBoolean(studioConfiguration.getProperty(BOOTSTRAP_REPO));
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    boolean isCreated = false;
    HierarchicalConfiguration<ImmutableNode> registrationData = studioClusterUtils.getClusterConfiguration();
    if (bootstrapRepo && registrationData != null && !registrationData.isEmpty()) {
        String firstCommitId = getRepoFirstCommitId(StringUtils.EMPTY);
        String localAddress = studioClusterUtils.getClusterNodeLocalAddress();
        List<ClusterMember> clusterNodes = studioClusterUtils.getClusterNodes(localAddress);
        if (StringUtils.isEmpty(firstCommitId)) {
            logger.debug("Creating global repository as cluster clone");
            isCreated = studioClusterUtils.cloneGlobalRepository(clusterNodes);
        } else {
            logger.debug("Global repository exists syncing with cluster siblings");
            isCreated = true;
            Repository repo = helper.getRepository(EMPTY, GLOBAL);
            try (Git git = new Git(repo)) {
                for (ClusterMember remoteNode : clusterNodes) {
                    if (remoteNode.getState().equals(ClusterMember.State.ACTIVE)) {
                        syncFromRemote(git, remoteNode);
                    }
                }
            }
        }
    }
    if (bootstrapRepo && !isCreated && helper.createGlobalRepo()) {
        // Copy the global config defaults to the global site
        // Build a path to the bootstrap repo (the repo that ships with Studio)
        String bootstrapFolderPath = this.ctx.getRealPath(FILE_SEPARATOR + BOOTSTRAP_REPO_PATH + FILE_SEPARATOR + BOOTSTRAP_REPO_GLOBAL_PATH);
        Path source = java.nio.file.FileSystems.getDefault().getPath(bootstrapFolderPath);
        logger.info("Bootstrapping with baseline @ " + source.toFile().toString());
        // Copy the bootstrap repo to the global repo
        Path globalConfigPath = helper.buildRepoPath(GLOBAL);
        TreeCopier tc = new TreeCopier(source, globalConfigPath);
        EnumSet<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS);
        Files.walkFileTree(source, opts, MAX_VALUE, tc);
        String studioManifestLocation = this.ctx.getRealPath(STUDIO_MANIFEST_LOCATION);
        if (Files.exists(Paths.get(studioManifestLocation))) {
            FileUtils.copyFile(Paths.get(studioManifestLocation).toFile(), Paths.get(globalConfigPath.toAbsolutePath().toString(), studioConfiguration.getProperty(BLUE_PRINTS_PATH), "BLUEPRINTS.MF").toFile());
        }
        Repository globalConfigRepo = helper.getRepository(EMPTY, GLOBAL);
        try (Git git = new Git(globalConfigRepo)) {
            Status status = git.status().call();
            if (status.hasUncommittedChanges() || !status.isClean()) {
                // Commit everything
                // TODO: Consider what to do with the commitId in the future
                AddCommand addCommand = git.add().addFilepattern(GIT_COMMIT_ALL_ITEMS);
                retryingRepositoryOperationFacade.call(addCommand);
                CommitCommand commitCommand = git.commit().setMessage(helper.getCommitMessage(REPO_INITIAL_COMMIT_COMMIT_MESSAGE));
                retryingRepositoryOperationFacade.call(commitCommand);
            }
        } catch (GitAPIException err) {
            logger.error("error creating initial commit for global configuration", err);
        }
    }
    // Create global repository object
    if (!helper.buildGlobalRepo()) {
        logger.error("Failed to create global repository!");
    }
}
Also used : Path(java.nio.file.Path) Status(org.eclipse.jgit.api.Status) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) FileVisitOption(java.nio.file.FileVisitOption) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) CommitCommand(org.eclipse.jgit.api.CommitCommand) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) AddCommand(org.eclipse.jgit.api.AddCommand)

Example 4 with COPY

use of org.craftercms.studio.api.v2.dal.RepoOperation.Action.COPY in project studio by craftercms.

the class SiteServiceImpl method processRepoOperations.

private boolean processRepoOperations(String siteId, List<RepoOperation> repoOperations, Path file) throws IOException {
    boolean toReturn = true;
    long startProcessRepoOperationMark = logger.isDebugEnabled() ? System.currentTimeMillis() : 0;
    for (RepoOperation repoOperation : repoOperations) {
        switch(repoOperation.getAction()) {
            case CREATE:
            case COPY:
                Files.write(file, upsertItemStateRow(siteId, repoOperation.getPath()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, "\n\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, upsertItemMetadataRow(siteId, repoOperation.getPath(), repoOperation.getAuthor(), repoOperation.getDateTime(), repoOperation.getCommitId()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, "\n\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                logger.debug("Extract dependencies for site: " + siteId + " path: " + repoOperation.getPath());
                addDependenciesScriptSnippets(siteId, repoOperation.getPath(), null, file);
                break;
            case UPDATE:
                Files.write(file, transitionSaveItemStateRow(siteId, repoOperation.getPath()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, "\n\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, updateItemMetadataRow(siteId, repoOperation.getPath(), repoOperation.getAuthor(), repoOperation.getDateTime(), repoOperation.getCommitId()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, "\n\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                logger.debug("Extract dependencies for site: " + siteId + " path: " + repoOperation.getPath());
                addDependenciesScriptSnippets(siteId, repoOperation.getPath(), null, file);
                break;
            case DELETE:
                Files.write(file, deleteItemStateRow(siteId, repoOperation.getPath()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, "\n\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, deleteItemMetadataRow(siteId, repoOperation.getPath()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, "\n\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, deleteDependencyRows(siteId, repoOperation.getPath()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, "\n\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                break;
            case MOVE:
                Files.write(file, moveItemStateRow(siteId, repoOperation.getPath(), repoOperation.getMoveToPath()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, "\n\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, transitionSaveItemStateRow(siteId, repoOperation.getMoveToPath()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, "\n\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, moveItemMetadataRow(siteId, repoOperation.getPath(), repoOperation.getMoveToPath()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, "\n\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, updateItemMetadataRow(siteId, repoOperation.getMoveToPath(), repoOperation.getAuthor(), repoOperation.getDateTime(), repoOperation.getCommitId()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                Files.write(file, "\n\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                addDependenciesScriptSnippets(siteId, repoOperation.getMoveToPath(), repoOperation.getPath(), file);
                break;
            default:
                logger.error("Error: Unknown repo operation for site " + siteId + " operation: " + repoOperation.getAction());
                toReturn = false;
                break;
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Process Repo operations finished in " + (System.currentTimeMillis() - startProcessRepoOperationMark) + " milliseconds");
    }
    return toReturn;
}
Also used : RepoOperation(org.craftercms.studio.api.v2.dal.RepoOperation)

Example 5 with COPY

use of org.craftercms.studio.api.v2.dal.RepoOperation.Action.COPY in project studio by craftercms.

the class DeploymentServiceImpl method createCommitItems.

private List<PublishRequest> createCommitItems(String site, String environment, List<String> commitIds, ZonedDateTime scheduledDate, String approver, String comment) {
    List<PublishRequest> newItems = new ArrayList<PublishRequest>(commitIds.size());
    String packageId = UUID.randomUUID().toString();
    logger.debug("Get repository operations for each commit id and create publish request items");
    for (String commitId : commitIds) {
        logger.debug("Get repository operations for commit " + commitId);
        List<RepoOperation> operations = contentRepositoryV2.getOperationsFromDelta(site, commitId + PREVIOUS_COMMIT_SUFFIX, commitId);
        for (RepoOperation op : operations) {
            logger.debug("Creating publish request item: ");
            PublishRequest item = new PublishRequest();
            item.setId(++CTED_AUTOINCREMENT);
            item.setSite(site);
            item.setEnvironment(environment);
            item.setScheduledDate(scheduledDate);
            item.setState(PublishRequest.State.READY_FOR_LIVE);
            item.setCommitId(commitId);
            item.setUser(approver);
            item.setPackageId(packageId);
            item.setSubmissionComment(comment);
            switch(op.getAction()) {
                case CREATE:
                case COPY:
                    item.setPath(op.getPath());
                    item.setAction(PublishRequest.Action.NEW);
                    item.setContentTypeClass(contentService.getContentTypeClass(site, op.getPath()));
                    break;
                case UPDATE:
                    item.setPath(op.getPath());
                    item.setAction(PublishRequest.Action.UPDATE);
                    item.setContentTypeClass(contentService.getContentTypeClass(site, op.getPath()));
                    break;
                case DELETE:
                    item.setPath(op.getPath());
                    item.setAction(PublishRequest.Action.DELETE);
                    item.setContentTypeClass(contentService.getContentTypeClass(site, op.getPath()));
                    break;
                case MOVE:
                    item.setPath(op.getMoveToPath());
                    item.setOldPath(op.getPath());
                    item.setAction(PublishRequest.Action.MOVE);
                    item.setContentTypeClass(contentService.getContentTypeClass(site, op.getPath()));
                    break;
                default:
                    logger.error("Error: Unknown repo operation for site " + site + " operation: " + op.getAction());
                    continue;
            }
            logger.debug("\tPath: " + item.getPath() + " operation: " + item.getAction());
            newItems.add(item);
        }
    }
    logger.debug("Created " + newItems.size() + " publish request items for queue");
    return newItems;
}
Also used : RepoOperation(org.craftercms.studio.api.v2.dal.RepoOperation) ArrayList(java.util.ArrayList) FastArrayList(org.apache.commons.collections.FastArrayList) PublishRequest(org.craftercms.studio.api.v1.dal.PublishRequest)

Aggregations

RepoOperation (org.craftercms.studio.api.v2.dal.RepoOperation)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)2 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)2 Repository (org.eclipse.jgit.lib.Repository)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 FileVisitOption (java.nio.file.FileVisitOption)1 Path (java.nio.file.Path)1 ZonedDateTime (java.time.ZonedDateTime)1 Map (java.util.Map)1 FastArrayList (org.apache.commons.collections.FastArrayList)1 ImmutableNode (org.apache.commons.configuration2.tree.ImmutableNode)1 DisableClassLoadingConstructor (org.craftercms.commons.config.DisableClassLoadingConstructor)1 CryptoException (org.craftercms.commons.crypto.CryptoException)1 PublishRequest (org.craftercms.studio.api.v1.dal.PublishRequest)1 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)1 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)1 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)1