Search in sources :

Example 21 with ClusterMember

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

the class StudioClusterSandboxRepoSyncTask method addRemoteRepository.

private void addRemoteRepository(String siteId, ClusterMember member, String remoteUrl) throws IOException, InvalidRemoteUrlException, ServiceLayerException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(buildRepoPath(siteId).resolve(GIT_ROOT).toFile()).readEnvironment().findGitDir().build();
    try (Git git = new Git(repo)) {
        Config storedConfig = repo.getConfig();
        Set<String> remotes = storedConfig.getSubsections(CONFIG_SECTION_REMOTE);
        if (remotes.contains(member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""))) {
            try {
                removeRemote(git, member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""));
            } catch (GitAPIException e) {
                logger.debug("Error while cleaning up remote repository for site " + siteId, e);
            }
        }
        if (remotes.contains(member.getGitRemoteName())) {
            logger.debug("Remote " + member.getGitRemoteName() + " already exists for SANDBOX repo for site " + siteId);
            String storedRemoteUrl = storedConfig.getString(CONFIG_SECTION_REMOTE, member.getGitRemoteName(), CONFIG_PARAMETER_URL);
            if (!StringUtils.equals(storedRemoteUrl, remoteUrl)) {
                RemoteSetUrlCommand remoteSetUrlCommand = git.remoteSetUrl();
                remoteSetUrlCommand.setName(member.getGitRemoteName());
                remoteSetUrlCommand.setUri(new URIish(remoteUrl));
                remoteSetUrlCommand.call();
            }
        } else {
            logger.debug("Add " + member.getLocalAddress() + " as remote to SANDBOX");
            RemoteAddCommand remoteAddCommand = git.remoteAdd();
            remoteAddCommand.setName(member.getGitRemoteName());
            remoteAddCommand.setUri(new URIish(remoteUrl));
            remoteAddCommand.call();
        }
    } catch (URISyntaxException e) {
        logger.error("Remote URL is invalid " + remoteUrl, e);
        throw new InvalidRemoteUrlException();
    } catch (GitAPIException e) {
        logger.error("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
        throw new ServiceLayerException("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Config(org.eclipse.jgit.lib.Config) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) URISyntaxException(java.net.URISyntaxException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) 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) Git(org.eclipse.jgit.api.Git) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand)

Example 22 with ClusterMember

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

Example 23 with ClusterMember

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

the class GitContentRepository method insertClusterRemoteRepository.

@RetryingOperation
public void insertClusterRemoteRepository(RemoteRepository remoteRepository) {
    HierarchicalConfiguration<ImmutableNode> registrationData = studioConfiguration.getSubConfig(CLUSTERING_NODE_REGISTRATION);
    if (registrationData != null && !registrationData.isEmpty()) {
        String localAddress = registrationData.getString(CLUSTER_MEMBER_LOCAL_ADDRESS);
        ClusterMember member = clusterDao.getMemberByLocalAddress(localAddress);
        if (member != null) {
            clusterDao.addClusterRemoteRepository(member.getId(), remoteRepository.getId());
        }
    }
}
Also used : ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation)

Example 24 with ClusterMember

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

the class ClusterNodeRegistrationImpl method init.

public void init() {
    logger.debug("Autoregister cluster if cluster node is configured");
    HierarchicalConfiguration<ImmutableNode> registrationData = getConfiguration();
    ClusterMember clusterMember = new ClusterMember();
    if (registrationData != null && !registrationData.isEmpty()) {
        try {
            logger.debug("Collect and populate data for cluster node registration");
            clusterMember.setLocalAddress(registrationData.getString(CLUSTER_MEMBER_LOCAL_ADDRESS));
            Path path = Paths.get(studioConfiguration.getProperty(REPO_BASE_PATH), studioConfiguration.getProperty(SITES_REPOS_PATH));
            String authenticationType = registrationData.getString(CLUSTER_MEMBER_AUTHENTICATION_TYPE);
            String username = registrationData.getString(CLUSTER_MEMBER_USERNAME);
            String password = registrationData.getString(CLUSTER_MEMBER_PASSWORD);
            String token = registrationData.getString(CLUSTER_MEMBER_TOKEN);
            String privateKey = registrationData.getString(CLUSTER_MEMBER_PRIVATE_KEY);
            String gitUrl = studioConfiguration.getProperty(CLUSTERING_SYNC_URL_FORMAT);
            if (StringUtils.isEmpty(username)) {
                gitUrl = gitUrl.replace("{username}@", "");
            } else {
                gitUrl = gitUrl.replace("{username}", username);
            }
            gitUrl = gitUrl.replace("{localAddress}", clusterMember.getLocalAddress()).replace("{absolutePath}", path.toAbsolutePath().normalize().toString()) + "/{siteId}";
            clusterMember.setGitUrl(gitUrl);
            clusterMember.setState(ClusterMember.State.ACTIVE);
            clusterMember.setGitRemoteName(getGitRemoteName(clusterMember));
            clusterMember.setGitAuthType(authenticationType.toLowerCase());
            clusterMember.setGitUsername(username);
            if (StringUtils.isEmpty(password)) {
                clusterMember.setGitPassword(password);
            } else {
                String hashedPassword = encryptor.encrypt(password);
                clusterMember.setGitPassword(hashedPassword);
            }
            if (StringUtils.isEmpty(token)) {
                clusterMember.setGitToken(token);
            } else {
                String hashedToken = encryptor.encrypt(token);
                clusterMember.setGitToken(hashedToken);
            }
            if (StringUtils.isEmpty(privateKey)) {
                clusterMember.setGitPrivateKey(privateKey);
            } else {
                String hashedPrivateKey = encryptor.encrypt(privateKey);
                clusterMember.setGitPrivateKey(hashedPrivateKey);
            }
            clusterMember.setAvailable(1);
            logger.debug("Register cluster member");
            registerClusterNode(clusterMember);
        } catch (CryptoException e) {
            logger.error("Failed to register cluster member");
        }
    }
}
Also used : Path(java.nio.file.Path) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 25 with ClusterMember

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

the class ClusterManagementController method getAllMembers.

@GetMapping("/api/2/cluster")
public ResponseBody getAllMembers() throws ServiceLayerException {
    List<ClusterMember> clusterMembers = clusterManagementService.getAllMemebers();
    ResponseBody responseBody = new ResponseBody();
    ResultList<ClusterMember> result = new ResultList<ClusterMember>();
    result.setEntities(RESULT_KEY_CLUSTER_MEMBERS, clusterMembers);
    result.setResponse(ApiResponse.OK);
    responseBody.setResult(result);
    return responseBody;
}
Also used : ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) ResultList(org.craftercms.studio.model.rest.ResultList) ResponseBody(org.craftercms.studio.model.rest.ResponseBody) GetMapping(org.springframework.web.bind.annotation.GetMapping)

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