Search in sources :

Example 1 with SANDBOX_PATH

use of org.craftercms.studio.api.v2.utils.StudioConfiguration.SANDBOX_PATH in project studio by craftercms.

the class StudioClusterSandboxRepoSyncTask method addRemotes.

protected void addRemotes(String siteId, List<ClusterMember> clusterNodes) throws InvalidRemoteUrlException, ServiceLayerException {
    Map<String, String> existingRemotes = remotesMap.get(siteId);
    logger.debug("Add cluster members as remotes to local sandbox repository");
    for (ClusterMember member : clusterNodes) {
        if (existingRemotes != null && existingRemotes.containsKey(member.getGitRemoteName())) {
            continue;
        }
        try {
            if (existingRemotes == null) {
                existingRemotes = new HashMap<String, String>();
                remotesMap.put(siteId, existingRemotes);
            }
            String remoteUrl = member.getGitUrl().replace("{siteId}", siteId) + "/" + studioConfiguration.getProperty(SANDBOX_PATH);
            addRemoteRepository(siteId, member, remoteUrl);
            existingRemotes.put(member.getGitRemoteName(), StringUtils.EMPTY);
        } catch (IOException e) {
            logger.error("Failed to open repository for site " + siteId, e);
        }
    }
}
Also used : ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) IOException(java.io.IOException)

Example 2 with SANDBOX_PATH

use of org.craftercms.studio.api.v2.utils.StudioConfiguration.SANDBOX_PATH 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

IOException (java.io.IOException)2 ClusterMember (org.craftercms.studio.api.v2.dal.ClusterMember)2 File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1 CollectionUtils (org.apache.commons.collections4.CollectionUtils)1 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)1 ImmutableNode (org.apache.commons.configuration2.tree.ImmutableNode)1 FileUtils (org.apache.commons.io.FileUtils)1 StringUtils (org.apache.commons.lang3.StringUtils)1 CryptoException (org.craftercms.commons.crypto.CryptoException)1