Search in sources :

Example 1 with RemoteRepository

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

the class RepositoryManagementServiceInternalImpl method fetchRemote.

private void fetchRemote(String siteId, Git git, RemoteConfig conf) throws CryptoException, IOException, ServiceLayerException, GitAPIException {
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    RemoteRepository remoteRepository = getRemoteRepository(siteId, conf.getName());
    if (remoteRepository != null) {
        Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
        FetchCommand fetchCommand = git.fetch().setRemote(conf.getName());
        fetchCommand = helper.setAuthenticationForCommand(fetchCommand, remoteRepository.getAuthenticationType(), remoteRepository.getRemoteUsername(), remoteRepository.getRemotePassword(), remoteRepository.getRemoteToken(), remoteRepository.getRemotePrivateKey(), tempKey, true);
        retryingRepositoryOperationFacade.call(fetchCommand);
    }
}
Also used : Path(java.nio.file.Path) FetchCommand(org.eclipse.jgit.api.FetchCommand) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper)

Example 2 with RemoteRepository

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

the class RepositoryManagementServiceInternalImpl method pullFromRemote.

@Override
public boolean pullFromRemote(String siteId, String remoteName, String remoteBranch, String mergeStrategy) throws InvalidRemoteUrlException, ServiceLayerException, CryptoException {
    logger.debug("Get remote data from database for remote " + remoteName + " and site " + siteId);
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
    RemoteRepository remoteRepository = getRemoteRepository(siteId, remoteName);
    logger.debug("Prepare pull command");
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    Repository repo = helper.getRepository(siteId, SANDBOX);
    generalLockService.lock(gitLockKey);
    try (Git git = new Git(repo)) {
        PullResult pullResult = null;
        PullCommand pullCommand = git.pull();
        logger.debug("Set remote " + remoteName);
        pullCommand.setRemote(remoteRepository.getRemoteName());
        logger.debug("Set branch to be " + remoteBranch);
        pullCommand.setRemoteBranchName(remoteBranch);
        Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
        pullCommand = helper.setAuthenticationForCommand(pullCommand, remoteRepository.getAuthenticationType(), remoteRepository.getRemoteUsername(), remoteRepository.getRemotePassword(), remoteRepository.getRemoteToken(), remoteRepository.getRemotePrivateKey(), tempKey, true);
        switch(mergeStrategy) {
            case THEIRS:
                pullCommand.setStrategy(MergeStrategy.THEIRS);
                break;
            case OURS:
                pullCommand.setStrategy(MergeStrategy.OURS);
                break;
            default:
                break;
        }
        pullCommand.setFastForward(MergeCommand.FastForwardMode.NO_FF);
        pullResult = retryingRepositoryOperationFacade.call(pullCommand);
        String pullResultMessage = pullResult.toString();
        if (StringUtils.isNotEmpty(pullResultMessage)) {
            logger.info(pullResultMessage);
        }
        Files.delete(tempKey);
        if (!pullResult.isSuccessful() && conflictNotificationEnabled()) {
            List<String> conflictFiles = new ArrayList<String>();
            if (pullResult.getMergeResult() != null) {
                pullResult.getMergeResult().getConflicts().forEach((m, v) -> {
                    conflictFiles.add(m);
                });
            }
            notificationService.notifyRepositoryMergeConflict(siteId, conflictFiles, Locale.ENGLISH);
        }
        if (pullResult.isSuccessful()) {
            String lastCommitId = contentRepository.getRepoLastCommitId(siteId);
            contentRepositoryV2.upsertGitLogList(siteId, Arrays.asList(lastCommitId), false, false);
            List<String> newMergedCommits = extractCommitIdsFromPullResult(siteId, repo, pullResult);
            List<String> commitIds = new ArrayList<String>();
            if (Objects.nonNull(newMergedCommits) && newMergedCommits.size() > 0) {
                logger.debug("Really pulled commits:");
                int cnt = 0;
                for (int i = 0; i < newMergedCommits.size(); i++) {
                    String commitId = newMergedCommits.get(i);
                    logger.debug(commitId);
                    if (!StringUtils.equals(lastCommitId, commitId)) {
                        commitIds.add(commitId);
                        if (cnt++ >= batchSizeGitLog) {
                            contentRepositoryV2.upsertGitLogList(siteId, commitIds, true, true);
                            cnt = 0;
                            commitIds.clear();
                        }
                    }
                }
                if (Objects.nonNull(commitIds) && commitIds.size() > 0) {
                    contentRepositoryV2.upsertGitLogList(siteId, commitIds, true, true);
                }
            }
            siteService.updateLastCommitId(siteId, lastCommitId);
        }
        return pullResult != null && pullResult.isSuccessful();
    } catch (InvalidRemoteException e) {
        logger.error("Remote is invalid " + remoteName, e);
        throw new InvalidRemoteUrlException();
    } catch (GitAPIException e) {
        logger.error("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
        throw new ServiceLayerException("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
    } catch (CryptoException | IOException e) {
        throw new ServiceLayerException(e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
}
Also used : Path(java.nio.file.Path) PullCommand(org.eclipse.jgit.api.PullCommand) ArrayList(java.util.ArrayList) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) IOException(java.io.IOException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) PullResult(org.eclipse.jgit.api.PullResult) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) 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) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 3 with RemoteRepository

use of org.craftercms.studio.api.v2.dal.RemoteRepository 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 4 with RemoteRepository

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

the class StudioClusterSandboxRepoSyncTask method syncRemoteRepositories.

private void syncRemoteRepositories(String siteId, String localAddress) {
    List<RemoteRepository> remoteRepositories = clusterDao.getMissingClusterNodeRemoteRepositories(localAddress, siteId);
    if (CollectionUtils.isNotEmpty(remoteRepositories)) {
        ClusterMember currentNode = clusterDao.getMemberByLocalAddress(localAddress);
        for (RemoteRepository remoteRepository : remoteRepositories) {
            try {
                addRemoteRepository(siteId, remoteRepository);
                clusterDao.addClusterRemoteRepository(currentNode.getId(), remoteRepository.getId());
            } catch (IOException | InvalidRemoteUrlException | ServiceLayerException e) {
                logger.error("Error while adding remote " + remoteRepository.getRemoteName() + " (url: " + remoteRepository.getRemoteUrl() + ") for site " + siteId, e);
            }
        }
    }
}
Also used : ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) IOException(java.io.IOException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)

Example 5 with RemoteRepository

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

the class GitContentRepository method pullFromRemote.

@Override
public boolean pullFromRemote(String siteId, String remoteName, String remoteBranch) throws ServiceLayerException, InvalidRemoteUrlException, CryptoException {
    logger.debug("Get remote data from database for remote " + remoteName + " and site " + siteId);
    Map<String, String> params = new HashMap<String, String>();
    params.put("siteId", siteId);
    params.put("remoteName", remoteName);
    RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(params);
    logger.debug("Prepare pull command");
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    Repository repo = helper.getRepository(siteId, SANDBOX);
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
    generalLockService.lock(gitLockKey);
    try (Git git = new Git(repo)) {
        PullResult pullResult = null;
        PullCommand pullCommand = git.pull();
        logger.debug("Set remote " + remoteName);
        pullCommand.setRemote(remoteRepository.getRemoteName());
        logger.debug("Set branch to be " + remoteBranch);
        pullCommand.setRemoteBranchName(remoteBranch);
        switch(remoteRepository.getAuthenticationType()) {
            case NONE:
                logger.debug("No authentication");
                pullResult = pullCommand.call();
                break;
            case BASIC:
                logger.debug("Basic authentication");
                String hashedPassword = remoteRepository.getRemotePassword();
                String password = encryptor.decrypt(hashedPassword);
                pullCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteRepository.getRemoteUsername(), password));
                pullResult = pullCommand.call();
                break;
            case TOKEN:
                logger.debug("Token based authentication");
                String hashedToken = remoteRepository.getRemoteToken();
                String token = encryptor.decrypt(hashedToken);
                pullCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(token, EMPTY));
                pullResult = pullCommand.call();
                break;
            case PRIVATE_KEY:
                logger.debug("Private key authentication");
                final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
                String hashedPrivateKey = remoteRepository.getRemotePrivateKey();
                String privateKey = encryptor.decrypt(hashedPrivateKey);
                tempKey.toFile().deleteOnExit();
                pullCommand.setTransportConfigCallback(new TransportConfigCallback() {

                    @Override
                    public void configure(Transport transport) {
                        SshTransport sshTransport = (SshTransport) transport;
                        sshTransport.setSshSessionFactory(getSshSessionFactory(privateKey, tempKey));
                    }
                });
                pullResult = retryingRepositoryOperationFacade.call(pullCommand);
                Files.delete(tempKey);
                break;
            default:
                throw new ServiceLayerException("Unsupported authentication type " + remoteRepository.getAuthenticationType());
        }
        return pullResult != null ? pullResult.isSuccessful() : false;
    } catch (InvalidRemoteException e) {
        logger.error("Remote is invalid " + remoteName, e);
        throw new InvalidRemoteUrlException();
    } catch (GitAPIException e) {
        logger.error("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
        throw new ServiceLayerException("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
    } catch (CryptoException | IOException e) {
        throw new ServiceLayerException(e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
}
Also used : Path(java.nio.file.Path) PullCommand(org.eclipse.jgit.api.PullCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) HashMap(java.util.HashMap) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) IOException(java.io.IOException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) PullResult(org.eclipse.jgit.api.PullResult) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) 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) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) SshTransport(org.eclipse.jgit.transport.SshTransport) Transport(org.eclipse.jgit.transport.Transport) CryptoException(org.craftercms.commons.crypto.CryptoException) SshTransport(org.eclipse.jgit.transport.SshTransport)

Aggregations

RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)13 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)8 IOException (java.io.IOException)7 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)7 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)7 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)7 Git (org.eclipse.jgit.api.Git)7 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)7 Repository (org.eclipse.jgit.lib.Repository)7 Path (java.nio.file.Path)6 CryptoException (org.craftercms.commons.crypto.CryptoException)6 HashMap (java.util.HashMap)5 ClusterMember (org.craftercms.studio.api.v2.dal.ClusterMember)4 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)4 ArrayList (java.util.ArrayList)3 TransportConfigCallback (org.eclipse.jgit.api.TransportConfigCallback)3 SshTransport (org.eclipse.jgit.transport.SshTransport)3 Transport (org.eclipse.jgit.transport.Transport)3 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)3 URISyntaxException (java.net.URISyntaxException)2