Search in sources :

Example 1 with ClusterMember

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

the class RepositoryManagementServiceInternalImpl method isRemovableRemote.

private boolean isRemovableRemote(String siteId, String remoteName) {
    boolean toRet = true;
    if (StringUtils.startsWith(remoteName, CLUSTER_NODE_REMOTE_NAME_PREFIX)) {
        RemoteRepository remoteRepository = getRemoteRepository(siteId, remoteName);
        List<ClusterMember> clusterMembers = getClusterMembersByRemoteName(remoteName);
        toRet = !(Objects.isNull(remoteRepository) && CollectionUtils.isNotEmpty(clusterMembers));
    }
    return toRet;
}
Also used : ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository)

Example 2 with ClusterMember

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

the class ClusterManagementServiceImpl method removeMembers.

@Override
@HasPermission(type = DefaultPermission.class, action = "delete_cluster")
public boolean removeMembers(List<Long> memberIds) throws SiteNotFoundException {
    List<ClusterMember> members = getAllMemebers();
    boolean toRet = clusterManagementServiceInternal.removeMembers(memberIds);
    if (toRet) {
        SiteFeed siteFeed = siteService.getSite(studioConfiguration.getProperty(CONFIGURATION_GLOBAL_SYSTEM_SITE));
        AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
        auditLog.setSiteId(siteFeed.getId());
        auditLog.setOperation(OPERATION_REMOVE_CLUSTER_NODE);
        auditLog.setActorId(securityService.getCurrentUser());
        auditLog.setPrimaryTargetId(siteFeed.getSiteId());
        auditLog.setPrimaryTargetType(TARGET_TYPE_CLUSTER_NODE);
        auditLog.setPrimaryTargetValue(siteFeed.getName());
        List<AuditLogParameter> paramters = new ArrayList<AuditLogParameter>();
        for (ClusterMember m : members) {
            AuditLogParameter paramter = new AuditLogParameter();
            paramter.setTargetId(Long.toString(m.getId()));
            paramter.setTargetType(TARGET_TYPE_CLUSTER_NODE);
            paramter.setTargetValue(m.getLocalAddress());
            paramters.add(paramter);
        }
        auditLog.setParameters(paramters);
        auditServiceInternal.insertAuditLog(auditLog);
    }
    return toRet;
}
Also used : ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) ArrayList(java.util.ArrayList) AuditLogParameter(org.craftercms.studio.api.v2.dal.AuditLogParameter) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission)

Example 3 with ClusterMember

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

the class ClusterNodeRegistrationImpl method registerClusterNode.

@Override
public boolean registerClusterNode(ClusterMember clusterMember) {
    ClusterMember existingRecord = clusterDao.getMemberByLocalAddress(clusterMember.getLocalAddress());
    int result = 0;
    if (Objects.isNull(existingRecord)) {
        result = clusterDao.addMember(clusterMember);
    } else {
        clusterMember.setId(existingRecord.getId());
        result = clusterDao.updateMember(clusterMember);
    }
    return result > 0;
}
Also used : ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember)

Example 4 with ClusterMember

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

the class StudioClusterUtils method cloneGlobalRepository.

public boolean cloneGlobalRepository(List<ClusterMember> clusterNodes) 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 = GLOBAL_REPOSITORY_GIT_LOCK;
    if (generalLockService.tryLock(gitLockKey)) {
        try {
            while (!cloned && idx < clusterNodes.size()) {
                ClusterMember remoteNode = clusterNodes.get(idx++);
                logger.debug("Cloning global repository from " + remoteNode.getLocalAddress());
                // prepare a new folder for the cloned repository
                Path siteSandboxPath = Paths.get(studioConfiguration.getProperty(StudioConfiguration.REPO_BASE_PATH), studioConfiguration.getProperty(StudioConfiguration.GLOBAL_REPO_PATH));
                File localPath = siteSandboxPath.toFile();
                localPath.delete();
                // then clone
                logger.debug("Cloning from " + remoteNode.getGitUrl() + " to " + localPath);
                CloneCommand cloneCommand = Git.cloneRepository();
                Git cloneResult = null;
                try {
                    final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
                    logger.debug("Add user credentials if provided");
                    configureAuthenticationForCommand(remoteNode, cloneCommand, tempKey);
                    String cloneUrl = remoteNode.getGitUrl().replace("/sites/{siteId}", "/global");
                    logger.debug("Executing clone command");
                    cloneResult = cloneCommand.setURI(cloneUrl).setRemote(remoteNode.getGitRemoteName()).setDirectory(localPath).setCloneAllBranches(true).call();
                    Files.deleteIfExists(tempKey);
                    cloned = true;
                } 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.toString(), e);
                } finally {
                    if (cloneResult != null) {
                        cloneResult.close();
                    }
                }
            }
        } finally {
            generalLockService.unlock(gitLockKey);
        }
    } else {
        logger.debug("Failed to get lock " + gitLockKey);
    }
    return cloned;
}
Also used : Path(java.nio.file.Path) CloneCommand(org.eclipse.jgit.api.CloneCommand) IOException(java.io.IOException) 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)

Example 5 with ClusterMember

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

the class StudioClusterGlobalRepoSyncTask method updateContent.

protected void updateContent(List<ClusterMember> clusterNodes) throws IOException, CryptoException, ServiceLayerException {
    logger.debug("Update global repo");
    Path siteSandboxPath = Paths.get(studioConfiguration.getProperty(StudioConfiguration.REPO_BASE_PATH), studioConfiguration.getProperty(StudioConfiguration.GLOBAL_REPO_PATH)).resolve(GIT_ROOT);
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(siteSandboxPath.toFile()).readEnvironment().findGitDir().build();
    try (Git git = new Git(repo)) {
        logger.debug("Update content from each active cluster memeber");
        for (ClusterMember remoteNode : clusterNodes) {
            updateBranch(git, remoteNode);
        }
    } catch (GitAPIException e) {
        logger.error("Error while syncing cluster node global repo content", e);
    }
}
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) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Aggregations

ClusterMember (org.craftercms.studio.api.v2.dal.ClusterMember)26 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)10 IOException (java.io.IOException)9 Path (java.nio.file.Path)9 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)9 Git (org.eclipse.jgit.api.Git)9 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)9 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)8 Repository (org.eclipse.jgit.lib.Repository)8 ImmutableNode (org.apache.commons.configuration2.tree.ImmutableNode)7 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)7 FileRepositoryBuilder (org.eclipse.jgit.storage.file.FileRepositoryBuilder)7 HashMap (java.util.HashMap)6 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)6 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)6 CryptoException (org.craftercms.commons.crypto.CryptoException)5 ClusterSiteRecord (org.craftercms.studio.api.v2.dal.ClusterSiteRecord)5 RemoteAddCommand (org.eclipse.jgit.api.RemoteAddCommand)5 URISyntaxException (java.net.URISyntaxException)4 File (java.io.File)3