Search in sources :

Example 6 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class StudioClockExecutor method executeTasks.

private void executeTasks() {
    for (Job job : globalTasks) {
        job.execute();
    }
    cleanupDeletedSites();
    List<String> sites = siteService.getAllCreatedSites();
    for (String site : sites) {
        taskExecutor.execute(new Runnable() {

            @Override
            public void run() {
                String tasksLock = STUDIO_CLOCK_EXECUTOR_SITE_LOCK.replaceAll(PATTERN_SITE, site);
                if (generalLockService.tryLock(tasksLock)) {
                    try {
                        for (SiteJob siteTask : siteTasks) {
                            siteTask.execute(site);
                        }
                    } finally {
                        generalLockService.unlock(tasksLock);
                    }
                }
            }
        });
    }
}
Also used : SiteJob(org.craftercms.studio.api.v2.job.SiteJob) SiteJob(org.craftercms.studio.api.v2.job.SiteJob) Job(org.craftercms.studio.api.v1.job.Job)

Example 7 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class GitContentRepository method copyContent.

@Override
public String copyContent(String site, String fromPath, String toPath) {
    String commitId = null;
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
            Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
            String gitFromPath = helper.getGitPath(fromPath);
            String gitToPath = helper.getGitPath(toPath);
            try (Git git = new Git(repo)) {
                Path sourcePath = Paths.get(repo.getDirectory().getParent(), fromPath);
                File sourceFile = sourcePath.toFile();
                Path targetPath = Paths.get(repo.getDirectory().getParent(), toPath);
                File targetFile = targetPath.toFile();
                // Check if we're copying a single file or whole subtree
                FileUtils.copyDirectory(sourceFile, targetFile);
                // The operation is done on disk, now it's time to commit
                git.add().addFilepattern(gitToPath).call();
                CommitCommand commitCommand = git.commit().setOnly(gitFromPath).setOnly(gitToPath).setAuthor(helper.getCurrentUserIdent()).setCommitter(helper.getCurrentUserIdent()).setMessage(helper.getCommitMessage(REPO_COPY_CONTENT_COMMIT_MESSAGE).replaceAll(PATTERN_FROM_PATH, fromPath).replaceAll(PATTERN_TO_PATH, toPath));
                RevCommit commit = retryingRepositoryOperationFacade.call(commitCommand);
                commitId = commit.getName();
            }
        }
    } catch (IOException | GitAPIException | ServiceLayerException | UserNotFoundException | CryptoException e) {
        logger.error("Error while copying content for site: " + site + " fromPath: " + fromPath + " toPath: " + toPath + " newName: ");
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    return commitId;
}
Also used : Path(java.nio.file.Path) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) 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) CommitCommand(org.eclipse.jgit.api.CommitCommand) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) File(java.io.File) LockFile(org.eclipse.jgit.internal.storage.file.LockFile) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 8 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE 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)

Example 9 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class GitContentRepository method resetStagingRepository.

@Override
public void resetStagingRepository(String siteId) throws ServiceLayerException, CryptoException {
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    Repository repo = helper.getRepository(siteId, PUBLISHED);
    String stagingName = servicesConfig.getStagingEnvironment(siteId);
    String liveName = servicesConfig.getLiveEnvironment(siteId);
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
    generalLockService.lock(gitLockKey);
    synchronized (repo) {
        try (Git git = new Git(repo)) {
            logger.debug("Checkout live first becuase it is not allowed to delete checkedout branch");
            CheckoutCommand checkoutCommand = git.checkout().setName(liveName);
            retryingRepositoryOperationFacade.call(checkoutCommand);
            logger.debug("Delete staging branch in order to reset it for site: " + siteId);
            DeleteBranchCommand deleteBranchCommand = git.branchDelete().setBranchNames(stagingName).setForce(true);
            retryingRepositoryOperationFacade.call(deleteBranchCommand);
            logger.debug("Create new branch for staging with live HEAD as starting point");
            CreateBranchCommand createBranchCommand = git.branchCreate().setName(stagingName).setStartPoint(liveName);
            retryingRepositoryOperationFacade.call(createBranchCommand);
        } catch (GitAPIException e) {
            logger.error("Error while reseting staging environment for site: " + siteId);
            throw new ServiceLayerException(e);
        } finally {
            generalLockService.unlock(gitLockKey);
        }
    }
}
Also used : CreateBranchCommand(org.eclipse.jgit.api.CreateBranchCommand) 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) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) DeleteBranchCommand(org.eclipse.jgit.api.DeleteBranchCommand) Git(org.eclipse.jgit.api.Git) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper)

Example 10 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class GitContentRepository method moveContent.

@Override
public Map<String, String> moveContent(String site, String fromPath, String toPath, String newName) {
    Map<String, String> toRet = new TreeMap<String, String>();
    String commitId;
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
            Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
            String gitFromPath = helper.getGitPath(fromPath);
            String gitToPath;
            if (StringUtils.isEmpty(newName)) {
                gitToPath = helper.getGitPath(toPath);
            } else {
                gitToPath = helper.getGitPath(toPath + FILE_SEPARATOR + newName);
            }
            try (Git git = new Git(repo)) {
                // Check if destination is a file, then this is a rename operation
                // Perform rename and exit
                Path sourcePath = Paths.get(repo.getDirectory().getParent(), gitFromPath);
                File sourceFile = sourcePath.toFile();
                Path targetPath = Paths.get(repo.getDirectory().getParent(), gitToPath);
                File targetFile = targetPath.toFile();
                if (sourceFile.getCanonicalFile().equals(targetFile.getCanonicalFile())) {
                    sourceFile.renameTo(targetFile);
                } else {
                    if (targetFile.isFile()) {
                        if (sourceFile.isFile()) {
                            sourceFile.renameTo(targetFile);
                        } else {
                            // This is not a valid operation
                            logger.error("Invalid move operation: Trying to rename a directory to a file " + "for site: " + site + " fromPath: " + fromPath + " toPath: " + toPath + " newName: " + newName);
                        }
                    } else if (sourceFile.isDirectory()) {
                        // Check if we're moving a single file or whole subtree
                        File[] dirList = sourceFile.listFiles();
                        for (File child : dirList) {
                            if (!child.equals(sourceFile)) {
                                FileUtils.moveToDirectory(child, targetFile, true);
                            }
                        }
                        FileUtils.deleteDirectory(sourceFile);
                    } else {
                        if (sourceFile.isFile()) {
                            FileUtils.moveFile(sourceFile, targetFile);
                        } else {
                            FileUtils.moveToDirectory(sourceFile, targetFile, true);
                        }
                    }
                }
                // The operation is done on disk, now it's time to commit
                AddCommand addCommand = git.add().addFilepattern(gitToPath);
                retryingRepositoryOperationFacade.call(addCommand);
                StatusCommand statusCommand = git.status().addPath(gitToPath);
                Status gitStatus = retryingRepositoryOperationFacade.call(statusCommand);
                Set<String> changeSet = gitStatus.getAdded();
                for (String pathToCommit : changeSet) {
                    String pathRemoved = pathToCommit.replace(gitToPath, gitFromPath);
                    CommitCommand commitCommand = git.commit().setOnly(pathToCommit).setOnly(pathRemoved).setAuthor(helper.getCurrentUserIdent()).setCommitter(helper.getCurrentUserIdent()).setMessage(helper.getCommitMessage(REPO_MOVE_CONTENT_COMMIT_MESSAGE).replaceAll(PATTERN_FROM_PATH, fromPath).replaceAll(PATTERN_TO_PATH, toPath + (StringUtils.isNotEmpty(newName) ? newName : EMPTY)));
                    RevCommit commit = retryingRepositoryOperationFacade.call(commitCommand);
                    commitId = commit.getName();
                    toRet.put(pathToCommit, commitId);
                }
            }
        }
    } catch (IOException | GitAPIException | ServiceLayerException | UserNotFoundException | CryptoException e) {
        logger.error("Error while moving content for site: " + site + " fromPath: " + fromPath + " toPath: " + toPath + " newName: " + newName);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    return toRet;
}
Also used : Path(java.nio.file.Path) Status(org.eclipse.jgit.api.Status) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) StatusCommand(org.eclipse.jgit.api.StatusCommand) 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) CommitCommand(org.eclipse.jgit.api.CommitCommand) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) File(java.io.File) LockFile(org.eclipse.jgit.internal.storage.file.LockFile) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) AddCommand(org.eclipse.jgit.api.AddCommand) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

Repository (org.eclipse.jgit.lib.Repository)25 Git (org.eclipse.jgit.api.Git)23 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)23 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)21 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)21 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)20 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)20 IOException (java.io.IOException)17 CryptoException (org.craftercms.commons.crypto.CryptoException)16 Path (java.nio.file.Path)14 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)12 File (java.io.File)7 CommitCommand (org.eclipse.jgit.api.CommitCommand)6 Status (org.eclipse.jgit.api.Status)6 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)5 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)5 InvalidRemoteRepositoryCredentialsException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException)4 InvalidRemoteRepositoryException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException)4 RemoteRepositoryNotFoundException (org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException)4 AddCommand (org.eclipse.jgit.api.AddCommand)4