Search in sources :

Example 1 with ClusterSiteRecord

use of org.craftercms.studio.api.v2.dal.ClusterSiteRecord in project studio by craftercms.

the class StudioClusterPublishedRepoSyncTask method updateContent.

protected void updateContent(long sId, String siteId, List<ClusterMember> clusterNodes, List<ClusterSiteRecord> clusterSiteRecords) throws IOException, CryptoException, ServiceLayerException {
    logger.debug("Update published repo for site " + siteId);
    Path siteSandboxPath = buildRepoPath(siteId).resolve(GIT_ROOT);
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(siteSandboxPath.toFile()).readEnvironment().findGitDir().build();
    String gitLockKey = SITE_PUBLISHED_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
    logger.debug("Git Lock Key: " + gitLockKey);
    try (Git git = new Git(repo)) {
        Set<String> environments = getAllPublishingEnvironments(siteId);
        logger.debug("Update published repo from all active cluster members");
        if (generalLockService.tryLock(gitLockKey)) {
            try {
                for (ClusterMember remoteNode : clusterNodes) {
                    ClusterSiteRecord csr = clusterDao.getClusterSiteRecord(remoteNode.getId(), sId);
                    if (Objects.nonNull(csr) && csr.getPublishedRepoCreated() > 0) {
                        try {
                            logger.debug("Fetch from cluster member " + remoteNode.getLocalAddress());
                            final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
                            FetchCommand fetch = git.fetch().setRemote(remoteNode.getGitRemoteName());
                            fetch = studioClusterUtils.configureAuthenticationForCommand(remoteNode, fetch, tempKey);
                            fetch.call();
                            Files.delete(tempKey);
                        } catch (GitAPIException e) {
                            logger.error("Error while fetching published repo for site " + siteId + " from remote " + remoteNode.getGitRemoteName());
                            logger.error(e.getMessage());
                        }
                    }
                }
                for (String branch : environments) {
                    for (ClusterMember remoteNode : clusterNodes) {
                        ClusterSiteRecord csr = clusterDao.getClusterSiteRecord(remoteNode.getId(), sId);
                        if (Objects.nonNull(csr) && csr.getPublishedRepoCreated() > 0) {
                            try {
                                updatePublishedBranch(siteId, git, remoteNode, branch);
                            } catch (GitAPIException e) {
                                logger.error("Error while updating published repo for site " + siteId + " from remote " + remoteNode.getGitRemoteName() + " environment " + branch);
                                logger.error(e.getMessage());
                            }
                        }
                    }
                }
            } finally {
                generalLockService.unlock(gitLockKey);
            }
        } else {
            logger.debug("Failed to get lock " + gitLockKey);
        }
    }
}
Also used : Path(java.nio.file.Path) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) Git(org.eclipse.jgit.api.Git) FetchCommand(org.eclipse.jgit.api.FetchCommand) ClusterSiteRecord(org.craftercms.studio.api.v2.dal.ClusterSiteRecord) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Example 2 with ClusterSiteRecord

use of org.craftercms.studio.api.v2.dal.ClusterSiteRecord in project studio by craftercms.

the class StudioClusterPublishedRepoSyncTask method executeInternal.

@Override
protected void executeInternal(String siteId) {
    // Log start time
    long startTime = System.currentTimeMillis();
    logger.debug("Worker starts syncing cluster node published for site " + siteId);
    try {
        HierarchicalConfiguration<ImmutableNode> registrationData = studioClusterUtils.getClusterConfiguration();
        if (registrationData != null && !registrationData.isEmpty()) {
            String localAddress = studioClusterUtils.getClusterNodeLocalAddress();
            ClusterMember localNode = clusterDao.getMemberByLocalAddress(localAddress);
            List<ClusterMember> clusterNodes = studioClusterUtils.getClusterNodes(localAddress);
            SiteFeed siteFeed = siteService.getSite(siteId);
            List<ClusterSiteRecord> clusterSiteRecords = clusterDao.getSiteStateAcrossCluster(siteId);
            Optional<ClusterSiteRecord> localNodeRecord = clusterSiteRecords.stream().filter(x -> x.getClusterNodeId() == localNode.getId() && StringUtils.equals(x.getState(), STATE_CREATED)).findFirst();
            if (!localNodeRecord.isPresent()) {
                return;
            }
            long nodesCreated = clusterSiteRecords.stream().filter(x -> StringUtils.equals(x.getState(), STATE_CREATED)).count();
            if (nodesCreated < 1) {
                return;
            }
            // Check if site exists
            logger.debug("Check if site " + siteId + " exists in local repository");
            boolean success = true;
            int publishedReposCreated = clusterSiteRecords.stream().mapToInt(ClusterSiteRecord::getPublishedRepoCreated).sum();
            if (publishedReposCreated > 0 || siteFeed.getPublishedRepoCreated() > 0) {
                boolean siteCheck = checkIfSiteRepoExists(siteId);
                if (!siteCheck) {
                    // Site doesn't exist locally, create it
                    success = createSite(localNode.getId(), siteFeed.getId(), siteId, siteFeed.getSandboxBranch());
                } else {
                    clusterDao.setPublishedRepoCreated(localNode.getId(), siteFeed.getId());
                }
            } else {
                success = false;
            }
            if (success) {
                try {
                    // Add the remote repositories to the local repository to sync from if not added already
                    logger.debug("Add remotes for site " + siteId);
                    addRemotes(siteId, clusterNodes);
                } catch (InvalidRemoteUrlException | ServiceLayerException | CryptoException e) {
                    logger.error("Error while adding remotes on cluster node for site " + siteId);
                }
                try {
                    // Sync with remote and update the local cache with the last commit ID to speed things up
                    logger.debug("Update content for site " + siteId);
                    updateContent(siteFeed.getId(), siteId, clusterNodes, clusterSiteRecords);
                } catch (IOException | CryptoException | ServiceLayerException e) {
                    logger.error("Error while updating content for site " + siteId + " on cluster node.", e);
                }
            }
        }
    } catch (SiteNotFoundException e) {
        logger.error("Error while executing Cluster Node Sync Published for site " + siteId, e);
    }
    // Compute execution duration and log it
    long duration = System.currentTimeMillis() - startTime;
    logger.debug("Worker finished syncing cluster node for site " + siteId);
    logger.debug("Worker performed cluster node sync for site " + siteId + " in " + duration + "ms");
    logger.debug("Finished Cluster Node Sync task for site " + siteId);
}
Also used : RetryingRepositoryOperationFacade(org.craftercms.studio.api.v2.repository.RetryingRepositoryOperationFacade) PullCommand(org.eclipse.jgit.api.PullCommand) PUBLISHED_PATH(org.craftercms.studio.api.v2.utils.StudioConfiguration.PUBLISHED_PATH) UserServiceInternal(org.craftercms.studio.api.v2.service.security.internal.UserServiceInternal) TextEncryptor(org.craftercms.commons.crypto.TextEncryptor) URISyntaxException(java.net.URISyntaxException) StringUtils(org.apache.commons.lang3.StringUtils) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) Config(org.eclipse.jgit.lib.Config) SITE_PUBLISHED_REPOSITORY_GIT_LOCK(org.craftercms.studio.api.v1.constant.StudioConstants.SITE_PUBLISHED_REPOSITORY_GIT_LOCK) ClusterSiteRecord(org.craftercms.studio.api.v2.dal.ClusterSiteRecord) Map(java.util.Map) URIish(org.eclipse.jgit.transport.URIish) ClusterDAO(org.craftercms.studio.api.v2.dal.ClusterDAO) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) Path(java.nio.file.Path) STATE_CREATED(org.craftercms.studio.api.v1.dal.SiteFeed.STATE_CREATED) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) Set(java.util.Set) SiteService(org.craftercms.studio.api.v1.service.site.SiteService) Constants(org.eclipse.jgit.lib.Constants) UUID(java.util.UUID) CONFIG_PARAMETER_URL(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CONFIG_PARAMETER_URL) Objects(java.util.Objects) CONFIG_SECTION_REMOTE(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CONFIG_SECTION_REMOTE) List(java.util.List) ServicesConfig(org.craftercms.studio.api.v1.service.configuration.ServicesConfig) StudioConfiguration(org.craftercms.studio.api.v2.utils.StudioConfiguration) PATTERN_SITE(org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE) Ref(org.eclipse.jgit.lib.Ref) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) Optional(java.util.Optional) CLUSTER_NODE_REMOTE_NAME_PREFIX(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CLUSTER_NODE_REMOTE_NAME_PREFIX) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) Logger(org.craftercms.studio.api.v1.log.Logger) HashMap(java.util.HashMap) FetchCommand(org.eclipse.jgit.api.FetchCommand) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand) HashSet(java.util.HashSet) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) LoggerFactory(org.craftercms.studio.api.v1.log.LoggerFactory) Files(java.nio.file.Files) StudioClusterUtils(org.craftercms.studio.impl.v2.service.cluster.StudioClusterUtils) IOException(java.io.IOException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) CryptoException(org.craftercms.commons.crypto.CryptoException) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) GIT_ROOT(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.GIT_ROOT) SecurityService(org.craftercms.studio.api.v1.service.security.SecurityService) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) Paths(java.nio.file.Paths) GeneralLockService(org.craftercms.studio.api.v1.service.GeneralLockService) Git(org.eclipse.jgit.api.Git) Repository(org.eclipse.jgit.lib.Repository) PUBLISHED(org.craftercms.studio.api.v1.constant.GitRepositories.PUBLISHED) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) ClusterSiteRecord(org.craftercms.studio.api.v2.dal.ClusterSiteRecord) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) CryptoException(org.craftercms.commons.crypto.CryptoException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException)

Example 3 with ClusterSiteRecord

use of org.craftercms.studio.api.v2.dal.ClusterSiteRecord in project studio by craftercms.

the class StudioClusterSandboxRepoSyncTask method executeInternal.

@Override
protected void executeInternal(String siteId) {
    // Log start time
    long startTime = System.currentTimeMillis();
    logger.debug("Worker starts syncing cluster node sandbox for site " + siteId);
    try {
        HierarchicalConfiguration<ImmutableNode> registrationData = studioClusterUtils.getClusterConfiguration();
        if (registrationData != null && !registrationData.isEmpty()) {
            String localAddress = studioClusterUtils.getClusterNodeLocalAddress();
            ClusterMember localNode = clusterDao.getMemberByLocalAddress(localAddress);
            List<ClusterMember> clusterNodes = studioClusterUtils.getClusterNodes(localAddress);
            SiteFeed siteFeed = siteService.getSite(siteId);
            List<ClusterSiteRecord> clusterSiteRecords = clusterDao.getSiteStateAcrossCluster(siteId);
            long nodesCreated = clusterSiteRecords.stream().filter(x -> StringUtils.equals(x.getState(), STATE_CREATED)).count();
            if (nodesCreated < 1 && !StringUtils.equals(siteFeed.getState(), STATE_CREATED)) {
                return;
            }
            // Check if site exists
            logger.debug("Check if site " + siteId + " exists in local repository");
            boolean success = true;
            boolean siteCheck = checkIfSiteRepoExists(siteId);
            if (!siteCheck) {
                // Site doesn't exist locally, create it
                success = createSite(localNode.getId(), siteFeed.getId(), siteId, siteFeed.getSiteUuid(), siteFeed.getSearchEngine(), clusterNodes, clusterSiteRecords);
            }
            if (success && clusterDao.existsClusterSiteSyncRepo(localNode.getId(), siteFeed.getId()) < 1) {
                String commitId = contentRepository.getRepoLastCommitId(siteId);
                clusterDao.insertClusterSiteSyncRepo(localNode.getId(), siteFeed.getId(), commitId, commitId, siteFeed.getLastSyncedGitlogCommitId());
                clusterDao.setSiteState(localNode.getId(), siteFeed.getId(), STATE_CREATED);
                addSiteUuidFile(siteId, siteFeed.getSiteUuid());
            }
            if (success) {
                syncRemoteRepositories(siteId, localAddress);
                // Check if the site needs to be synced
                boolean syncRequired = isSyncRequired(siteId, siteFeed.getLastCommitId());
                if (syncRequired) {
                    try {
                        // Add the remote repositories to the local repository to sync from if not added already
                        logger.debug("Add remotes for site " + siteId);
                        addRemotes(siteId, clusterNodes);
                    } catch (InvalidRemoteUrlException | ServiceLayerException e) {
                        logger.error("Error while adding remotes on cluster node for site " + siteId);
                    }
                    try {
                        // Sync with remote and update the local cache with the last commit ID to speed things up
                        logger.debug("Update content for site " + siteId);
                        updateContent(localNode.getId(), siteFeed.getId(), siteId, siteFeed.getSandboxBranch(), clusterNodes);
                    } catch (IOException | CryptoException | ServiceLayerException e) {
                        logger.error("Error while updating content for site " + siteId + " on cluster node.", e);
                    }
                }
            }
        }
    } catch (SiteNotFoundException | IOException e) {
        logger.error("Error while executing Cluster Node Sync Sandbox for site " + siteId, e);
    }
    // Compute execution duration and log it
    long duration = System.currentTimeMillis() - startTime;
    logger.debug("Worker finished syncing cluster node for site " + siteId);
    logger.debug("Worker performed cluster node sync for site " + siteId + " in " + duration + "ms");
    logger.debug("Finished Cluster Node Sync task for site " + siteId);
}
Also used : PullCommand(org.eclipse.jgit.api.PullCommand) URISyntaxException(java.net.URISyntaxException) SITES_REPOS_PATH(org.craftercms.studio.api.v2.utils.StudioConfiguration.SITES_REPOS_PATH) StringUtils(org.apache.commons.lang3.StringUtils) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) Config(org.eclipse.jgit.lib.Config) ClusterSiteRecord(org.craftercms.studio.api.v2.dal.ClusterSiteRecord) Map(java.util.Map) URIish(org.eclipse.jgit.transport.URIish) ClusterDAO(org.craftercms.studio.api.v2.dal.ClusterDAO) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) Path(java.nio.file.Path) STATE_CREATED(org.craftercms.studio.api.v1.dal.SiteFeed.STATE_CREATED) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) Set(java.util.Set) SiteService(org.craftercms.studio.api.v1.service.site.SiteService) UUID(java.util.UUID) SITE_SANDBOX_REPOSITORY_GIT_LOCK(org.craftercms.studio.api.v1.constant.StudioConstants.SITE_SANDBOX_REPOSITORY_GIT_LOCK) CONFIG_PARAMETER_URL(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CONFIG_PARAMETER_URL) Objects(java.util.Objects) CONFIG_SECTION_REMOTE(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CONFIG_SECTION_REMOTE) List(java.util.List) StudioConfiguration(org.craftercms.studio.api.v2.utils.StudioConfiguration) PATTERN_SITE(org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) Optional(java.util.Optional) CLUSTER_NODE_REMOTE_NAME_PREFIX(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CLUSTER_NODE_REMOTE_NAME_PREFIX) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) SANDBOX_PATH(org.craftercms.studio.api.v2.utils.StudioConfiguration.SANDBOX_PATH) CloneCommand(org.eclipse.jgit.api.CloneCommand) Logger(org.craftercms.studio.api.v1.log.Logger) HashMap(java.util.HashMap) DeploymentService(org.craftercms.studio.api.v1.service.deployment.DeploymentService) CollectionUtils(org.apache.commons.collections4.CollectionUtils) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand) EventService(org.craftercms.studio.api.v1.service.event.EventService) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) EVENT_PREVIEW_SYNC(org.craftercms.studio.api.v1.ebus.EBusConstants.EVENT_PREVIEW_SYNC) LoggerFactory(org.craftercms.studio.api.v1.log.LoggerFactory) PreviewEventContext(org.craftercms.studio.api.v1.ebus.PreviewEventContext) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) StudioConstants(org.craftercms.studio.api.v1.constant.StudioConstants) Files(java.nio.file.Files) StudioClusterUtils(org.craftercms.studio.impl.v2.service.cluster.StudioClusterUtils) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) CryptoException(org.craftercms.commons.crypto.CryptoException) File(java.io.File) Deployer(org.craftercms.studio.api.v2.deployment.Deployer) TransportException(org.eclipse.jgit.api.errors.TransportException) GIT_ROOT(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.GIT_ROOT) SANDBOX(org.craftercms.studio.api.v1.constant.GitRepositories.SANDBOX) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) Paths(java.nio.file.Paths) GeneralLockService(org.craftercms.studio.api.v1.service.GeneralLockService) REPO_BASE_PATH(org.craftercms.studio.api.v2.utils.StudioConfiguration.REPO_BASE_PATH) Git(org.eclipse.jgit.api.Git) Repository(org.eclipse.jgit.lib.Repository) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) ClusterSiteRecord(org.craftercms.studio.api.v2.dal.ClusterSiteRecord) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) CryptoException(org.craftercms.commons.crypto.CryptoException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException)

Example 4 with ClusterSiteRecord

use of org.craftercms.studio.api.v2.dal.ClusterSiteRecord in project studio by craftercms.

the class StudioClusterSandboxRepoSyncTask method updateContent.

protected void updateContent(long localNodeId, long sId, String siteId, String sandboxBranchName, List<ClusterMember> clusterNodes) throws IOException, CryptoException, ServiceLayerException {
    logger.debug("Update sandbox for site " + siteId);
    Path siteSandboxPath = buildRepoPath(siteId).resolve(GIT_ROOT);
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(siteSandboxPath.toFile()).readEnvironment().findGitDir().build();
    Map<String, String> remoteLastSyncCommits = remotesMap.get(siteId);
    if (remoteLastSyncCommits == null || remoteLastSyncCommits.isEmpty()) {
        remoteLastSyncCommits = new HashMap<String, String>();
        remotesMap.put(siteId, remoteLastSyncCommits);
    }
    try (Git git = new Git(repo)) {
        logger.debug("Update content from each active cluster memeber");
        for (ClusterMember remoteNode : clusterNodes) {
            ClusterSiteRecord csr = clusterDao.getClusterSiteRecord(remoteNode.getId(), sId);
            if (Objects.nonNull(csr) && StringUtils.equals(csr.getState(), STATE_CREATED)) {
                updateBranch(siteId, git, remoteNode, sandboxBranchName);
            }
        }
        String updatedCommitId = contentRepository.getRepoLastCommitId(siteId);
        clusterDao.updateNodeLastCommitId(localNodeId, sId, updatedCommitId);
        PreviewEventContext context = new PreviewEventContext();
        context.setSite(siteId);
        eventService.publish(EVENT_PREVIEW_SYNC, context);
    } catch (GitAPIException e) {
        logger.error("Error while syncing cluster node content for site " + siteId);
    }
}
Also used : Path(java.nio.file.Path) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) Git(org.eclipse.jgit.api.Git) ClusterSiteRecord(org.craftercms.studio.api.v2.dal.ClusterSiteRecord) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) PreviewEventContext(org.craftercms.studio.api.v1.ebus.PreviewEventContext)

Example 5 with ClusterSiteRecord

use of org.craftercms.studio.api.v2.dal.ClusterSiteRecord in project studio by craftercms.

the class StudioClusterSandboxRepoSyncTask method createSiteFromRemote.

protected boolean createSiteFromRemote(String siteId, long localNodeId, List<ClusterSiteRecord> clusterSiteRecords) throws CryptoException, ServiceLayerException {
    // Clone from the first node in the cluster (it doesn't matter which one to clone from, so pick the first)
    // we will eventually to catch up to the latest
    boolean cloned = false;
    int idx = 0;
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
    if (generalLockService.tryLock(gitLockKey)) {
        try {
            Optional<ClusterSiteRecord> csr = clusterSiteRecords.stream().filter(x -> StringUtils.equals(x.getState(), STATE_CREATED) && !(x.getClusterNodeId() == localNodeId)).findFirst();
            if (csr.isPresent()) {
                ClusterMember remoteNode = clusterDao.getMemberById(csr.get().getClusterNodeId());
                logger.debug("Cloning " + SANDBOX + " repository for site " + siteId + " from " + remoteNode.getLocalAddress());
                // prepare a new folder for the cloned repository
                Path siteSandboxPath = buildRepoPath(siteId);
                File localPath = siteSandboxPath.toFile();
                // then clone
                logger.debug("Cloning from " + remoteNode.getGitUrl() + " to " + localPath);
                CloneCommand cloneCommand = Git.cloneRepository();
                Git cloneResult = null;
                try {
                    if (localPath.exists()) {
                        FileUtils.forceDelete(localPath);
                    }
                    final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
                    logger.debug("Add user credentials if provided");
                    studioClusterUtils.configureAuthenticationForCommand(remoteNode, cloneCommand, tempKey);
                    String cloneUrl = remoteNode.getGitUrl().replace("{siteId}", siteId);
                    cloneUrl = cloneUrl + "/" + studioConfiguration.getProperty(SANDBOX_PATH);
                    logger.debug("Executing clone command");
                    Git gitClone = cloneResult = cloneCommand.setURI(cloneUrl).setRemote(remoteNode.getGitRemoteName()).setDirectory(localPath).setCloneAllBranches(true).call();
                    Files.deleteIfExists(tempKey);
                    cloned = validateRepository(gitClone.getRepository());
                } catch (InvalidRemoteException e) {
                    logger.error("Invalid remote repository: " + remoteNode.getGitRemoteName() + " (" + remoteNode.getGitUrl() + ")", e);
                } catch (TransportException e) {
                    if (StringUtils.endsWithIgnoreCase(e.getMessage(), "not authorized")) {
                        logger.error("Bad credentials or read only repository: " + remoteNode.getGitRemoteName() + " (" + remoteNode.getGitUrl() + ")", e);
                    } else {
                        logger.error("Remote repository not found: " + remoteNode.getGitRemoteName() + " (" + remoteNode.getGitUrl() + ")", e);
                    }
                } catch (GitAPIException | IOException e) {
                    logger.error("Error while creating repository for site with path" + siteSandboxPath, e);
                } finally {
                    if (cloneResult != null) {
                        cloneResult.close();
                    }
                }
            }
        } finally {
            generalLockService.unlock(gitLockKey);
        }
    } else {
        logger.debug("Failed to get lock " + gitLockKey);
    }
    return cloned;
}
Also used : PullCommand(org.eclipse.jgit.api.PullCommand) URISyntaxException(java.net.URISyntaxException) SITES_REPOS_PATH(org.craftercms.studio.api.v2.utils.StudioConfiguration.SITES_REPOS_PATH) StringUtils(org.apache.commons.lang3.StringUtils) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) Config(org.eclipse.jgit.lib.Config) ClusterSiteRecord(org.craftercms.studio.api.v2.dal.ClusterSiteRecord) Map(java.util.Map) URIish(org.eclipse.jgit.transport.URIish) ClusterDAO(org.craftercms.studio.api.v2.dal.ClusterDAO) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) Path(java.nio.file.Path) STATE_CREATED(org.craftercms.studio.api.v1.dal.SiteFeed.STATE_CREATED) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) Set(java.util.Set) SiteService(org.craftercms.studio.api.v1.service.site.SiteService) UUID(java.util.UUID) SITE_SANDBOX_REPOSITORY_GIT_LOCK(org.craftercms.studio.api.v1.constant.StudioConstants.SITE_SANDBOX_REPOSITORY_GIT_LOCK) CONFIG_PARAMETER_URL(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CONFIG_PARAMETER_URL) Objects(java.util.Objects) CONFIG_SECTION_REMOTE(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CONFIG_SECTION_REMOTE) List(java.util.List) StudioConfiguration(org.craftercms.studio.api.v2.utils.StudioConfiguration) PATTERN_SITE(org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) Optional(java.util.Optional) CLUSTER_NODE_REMOTE_NAME_PREFIX(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CLUSTER_NODE_REMOTE_NAME_PREFIX) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) SANDBOX_PATH(org.craftercms.studio.api.v2.utils.StudioConfiguration.SANDBOX_PATH) CloneCommand(org.eclipse.jgit.api.CloneCommand) Logger(org.craftercms.studio.api.v1.log.Logger) HashMap(java.util.HashMap) DeploymentService(org.craftercms.studio.api.v1.service.deployment.DeploymentService) CollectionUtils(org.apache.commons.collections4.CollectionUtils) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand) EventService(org.craftercms.studio.api.v1.service.event.EventService) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) EVENT_PREVIEW_SYNC(org.craftercms.studio.api.v1.ebus.EBusConstants.EVENT_PREVIEW_SYNC) LoggerFactory(org.craftercms.studio.api.v1.log.LoggerFactory) PreviewEventContext(org.craftercms.studio.api.v1.ebus.PreviewEventContext) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) StudioConstants(org.craftercms.studio.api.v1.constant.StudioConstants) Files(java.nio.file.Files) StudioClusterUtils(org.craftercms.studio.impl.v2.service.cluster.StudioClusterUtils) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) CryptoException(org.craftercms.commons.crypto.CryptoException) File(java.io.File) Deployer(org.craftercms.studio.api.v2.deployment.Deployer) TransportException(org.eclipse.jgit.api.errors.TransportException) GIT_ROOT(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.GIT_ROOT) SANDBOX(org.craftercms.studio.api.v1.constant.GitRepositories.SANDBOX) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) Paths(java.nio.file.Paths) GeneralLockService(org.craftercms.studio.api.v1.service.GeneralLockService) REPO_BASE_PATH(org.craftercms.studio.api.v2.utils.StudioConfiguration.REPO_BASE_PATH) Git(org.eclipse.jgit.api.Git) Repository(org.eclipse.jgit.lib.Repository) Path(java.nio.file.Path) CloneCommand(org.eclipse.jgit.api.CloneCommand) IOException(java.io.IOException) ClusterSiteRecord(org.craftercms.studio.api.v2.dal.ClusterSiteRecord) TransportException(org.eclipse.jgit.api.errors.TransportException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) Git(org.eclipse.jgit.api.Git) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) File(java.io.File)

Aggregations

Path (java.nio.file.Path)5 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)5 ClusterMember (org.craftercms.studio.api.v2.dal.ClusterMember)5 ClusterSiteRecord (org.craftercms.studio.api.v2.dal.ClusterSiteRecord)5 Git (org.eclipse.jgit.api.Git)5 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)5 Repository (org.eclipse.jgit.lib.Repository)5 FileRepositoryBuilder (org.eclipse.jgit.storage.file.FileRepositoryBuilder)5 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 Files (java.nio.file.Files)3 Paths (java.nio.file.Paths)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 Set (java.util.Set)3 UUID (java.util.UUID)3 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)3