Search in sources :

Example 6 with GitOpException

use of io.hops.hopsworks.exceptions.GitOpException in project hopsworks by logicalclocks.

the class GitExecutionController method lockRepository.

private synchronized void lockRepository(Integer repositoryId) throws GitOpException {
    Optional<GitRepository> optional = gitRepositoryFacade.findById(repositoryId);
    if (!optional.isPresent()) {
        throw new GitOpException(RESTCodes.GitOpErrorCode.REPOSITORY_NOT_FOUND, Level.SEVERE, "Git " + "repository with id [" + optional + "] was not found.");
    } else {
        GitRepository repository = optional.get();
        if (repository.getCid() != null) {
            throw new GitOpException(RESTCodes.GitOpErrorCode.GIT_OPERATION_ERROR, Level.WARNING, "There is another ongoing operation in the repository.");
        }
        // lock repository
        repository.setCid(String.valueOf(System.currentTimeMillis()));
        gitRepositoryFacade.updateRepository(repository);
    }
}
Also used : GitRepository(io.hops.hopsworks.persistence.entity.git.GitRepository) GitOpException(io.hops.hopsworks.exceptions.GitOpException)

Example 7 with GitOpException

use of io.hops.hopsworks.exceptions.GitOpException in project hopsworks by logicalclocks.

the class GitCommitsBuilder method build.

public GitCommitDTO build(UriInfo uriInfo, ResourceRequest resourceRequest, Project project, GitRepository repository, String branchName) throws GitOpException {
    GitCommitDTO dto = new GitCommitDTO();
    dto.setHref(uri(uriInfo, project, repository.getId(), branchName));
    dto.setExpand(expand(resourceRequest));
    if (dto.isExpand()) {
        Integer limit = resourceRequest.getLimit() == null ? DEFAULT_BRANCH_COMMITS_LIMIT : resourceRequest.getLimit();
        Integer offset = resourceRequest.getOffset() == null ? 0 : resourceRequest.getOffset();
        AbstractFacade.CollectionInfo<GitCommit> branchCommits = gitCommitsFacade.getBranchCommits(repository, branchName, limit, offset);
        List<GitCommitDTO> commitDTOs = branchCommits.getItems().stream().map(gitCommit -> new GitCommitDTO(gitCommit.getName(), gitCommit.getEmail(), gitCommit.getMessage(), gitCommit.getHash(), gitCommit.getDate())).collect(Collectors.toList());
        dto.setItems(commitDTOs);
        dto.setCount(branchCommits.getCount());
    }
    return dto;
}
Also used : Stateless(javax.ejb.Stateless) GitCommitsFacade(io.hops.hopsworks.common.dao.git.GitCommitsFacade) AbstractFacade(io.hops.hopsworks.common.dao.AbstractFacade) GitOpException(io.hops.hopsworks.exceptions.GitOpException) GitCommit(io.hops.hopsworks.persistence.entity.git.GitCommit) Collectors(java.util.stream.Collectors) Project(io.hops.hopsworks.persistence.entity.project.Project) GitRepository(io.hops.hopsworks.persistence.entity.git.GitRepository) List(java.util.List) TransactionAttributeType(javax.ejb.TransactionAttributeType) TransactionAttribute(javax.ejb.TransactionAttribute) ResourceRequest(io.hops.hopsworks.common.api.ResourceRequest) UriInfo(javax.ws.rs.core.UriInfo) URI(java.net.URI) GitController(io.hops.hopsworks.common.git.GitController) GitCommitDTO(io.hops.hopsworks.common.git.GitCommitDTO) EJB(javax.ejb.EJB) GitCommit(io.hops.hopsworks.persistence.entity.git.GitCommit) AbstractFacade(io.hops.hopsworks.common.dao.AbstractFacade) GitCommitDTO(io.hops.hopsworks.common.git.GitCommitDTO)

Example 8 with GitOpException

use of io.hops.hopsworks.exceptions.GitOpException in project hopsworks by logicalclocks.

the class GitCommandConfigurationValidator method validateProviderConfiguration.

public void validateProviderConfiguration(BasicAuthSecrets secrets, GitCommandConfiguration gitCommandConfiguration) throws GitOpException {
    GitCommandType commandType = gitCommandConfiguration.getCommandType();
    GitProvider gitProvider = gitCommandConfiguration.getProvider();
    // push we still need username and password/token.
    if (gitProvider == GitProvider.BITBUCKET && (commandType == GitCommandType.PULL || commandType == GitCommandType.PUSH || commandType == GitCommandType.CLONE) && (Strings.isNullOrEmpty(secrets.getUsername()) || Strings.isNullOrEmpty(secrets.getPassword()))) {
        throw new GitOpException(RESTCodes.GitOpErrorCode.GIT_USERNAME_AND_PASSWORD_NOT_SET, Level.WARNING, ". You " + "should setup secrets for " + gitProvider.getProvider() + " to be able to perform a " + commandType.getGitCommand() + " operation");
    } else if ((gitProvider == GitProvider.GIT_HUB || gitProvider == GitProvider.GIT_LAB) && (commandType == GitCommandType.PUSH) && (Strings.isNullOrEmpty(secrets.getUsername()) || Strings.isNullOrEmpty(secrets.getPassword()))) {
        throw new GitOpException(RESTCodes.GitOpErrorCode.GIT_USERNAME_AND_PASSWORD_NOT_SET, Level.WARNING, ". You should setup secrets for " + gitProvider.getProvider() + " to be able to perform a " + commandType.getGitCommand() + " operation");
    }
}
Also used : GitCommandType(io.hops.hopsworks.persistence.entity.git.config.GitCommandType) GitOpException(io.hops.hopsworks.exceptions.GitOpException) GitProvider(io.hops.hopsworks.persistence.entity.git.config.GitProvider)

Example 9 with GitOpException

use of io.hops.hopsworks.exceptions.GitOpException in project hopsworks by logicalclocks.

the class GitExecutionController method createExecution.

/**
 * initializes the execution of all git commands
 *
 * @param gitCommandConfiguration
 * @param project
 * @param hopsworksUser
 * @param repository
 * @return
 * @throws HopsSecurityException
 * @throws GitOpException
 */
public GitOpExecution createExecution(GitCommandConfiguration gitCommandConfiguration, Project project, Users hopsworksUser, GitRepository repository) throws HopsSecurityException, GitOpException {
    String hdfsUsername = hdfsUsersController.getHdfsUserName(project, hopsworksUser);
    BasicAuthSecrets authSecrets = gitCommandOperationUtil.getAuthenticationSecrets(hopsworksUser, repository.getGitProvider());
    commandConfigurationValidator.validateProviderConfiguration(authSecrets, gitCommandConfiguration);
    String configSecret = DigestUtils.sha256Hex(Integer.toString(ThreadLocalRandom.current().nextInt()));
    lockRepository(repository.getId());
    GitOpExecution gitOpExecution = null;
    DistributedFileSystemOps udfso = null;
    try {
        udfso = dfsService.getDfsOps(hdfsUsername);
        GitPaths gitPaths = prepareCommandExecution(project, hopsworksUser, udfso, configSecret);
        gitOpExecution = gitOpExecutionFacade.create(gitCommandConfiguration, hopsworksUser, repository, configSecret);
        argumentsWriter.createArgumentFile(gitOpExecution, gitPaths, authSecrets);
        gitCommandExecutor.execute(gitOpExecution, gitPaths);
        return gitOpExecution;
    } catch (Exception ex) {
        gitRepositoryFacade.updateRepositoryCid(repository, null);
        gitCommandOperationUtil.cleanUp(project, hopsworksUser, configSecret);
        if (ex instanceof IOException) {
            throw new HopsSecurityException(RESTCodes.SecurityErrorCode.CERT_MATERIALIZATION_ERROR, Level.SEVERE, ex.getMessage(), null, ex);
        }
        throw new GitOpException(RESTCodes.GitOpErrorCode.GIT_OPERATION_ERROR, Level.SEVERE, ex.getMessage());
    } finally {
        if (udfso != null) {
            dfsService.closeDfsClient(udfso);
        }
    }
}
Also used : GitPaths(io.hops.hopsworks.common.dao.git.GitPaths) DistributedFileSystemOps(io.hops.hopsworks.common.hdfs.DistributedFileSystemOps) GitOpExecution(io.hops.hopsworks.persistence.entity.git.GitOpExecution) GitOpException(io.hops.hopsworks.exceptions.GitOpException) IOException(java.io.IOException) HopsSecurityException(io.hops.hopsworks.exceptions.HopsSecurityException) GitOpException(io.hops.hopsworks.exceptions.GitOpException) IOException(java.io.IOException) HopsSecurityException(io.hops.hopsworks.exceptions.HopsSecurityException)

Example 10 with GitOpException

use of io.hops.hopsworks.exceptions.GitOpException in project hopsworks by logicalclocks.

the class GitController method executeBranchAction.

public GitOpExecution executeBranchAction(GitBranchAction action, Project project, Users hopsworksUser, Integer repositoryId, String branchName, String commit) throws GitOpException, HopsSecurityException, IllegalArgumentException {
    GitRepository repository = commandConfigurationValidator.verifyRepository(project, repositoryId);
    String repositoryFullPath = inodeController.getPath(repository.getInode());
    GitCommandConfigurationBuilder builder = new GitCommandConfigurationBuilder();
    builder.setBranchName(branchName);
    builder.setPath(repositoryFullPath);
    switch(action) {
        case CREATE:
        case CREATE_CHECKOUT:
            if (Strings.isNullOrEmpty(branchName)) {
                throw new GitOpException(RESTCodes.GitOpErrorCode.INVALID_BRANCH_NAME, Level.WARNING, "Branch name is empty" + ".");
            }
            builder.setCommandType(GitCommandType.CREATE_BRANCH);
            builder.setCheckout(action == GitBranchAction.CREATE_CHECKOUT);
            return executionController.createExecution(builder.build(), project, hopsworksUser, repository);
        case DELETE:
            if (Strings.isNullOrEmpty(branchName)) {
                throw new GitOpException(RESTCodes.GitOpErrorCode.INVALID_BRANCH_NAME, Level.WARNING, "Branch name is empty" + ".");
            }
            builder.setCommandType(GitCommandType.DELETE_BRANCH);
            builder.setDeleteOnRemote(false);
            return executionController.createExecution(builder.build(), project, hopsworksUser, repository);
        case CHECKOUT:
        case CHECKOUT_FORCE:
            if (Strings.isNullOrEmpty(branchName) && Strings.isNullOrEmpty(commit)) {
                throw new GitOpException(RESTCodes.GitOpErrorCode.INVALID_BRANCH_AND_COMMIT_CHECKOUT_COMBINATION, Level.WARNING, "Please provide either branch or commit to checkout.");
            } else if (!Strings.isNullOrEmpty(branchName) && !Strings.isNullOrEmpty(commit)) {
                throw new GitOpException(RESTCodes.GitOpErrorCode.INVALID_BRANCH_AND_COMMIT_CHECKOUT_COMBINATION, Level.WARNING, "Checkout requires a commit or branch but not both.");
            }
            builder.setCommandType(GitCommandType.CHECKOUT);
            builder.setCommit(commit);
            builder.setForce(action == GitBranchAction.CHECKOUT_FORCE);
            return executionController.createExecution(builder.build(), project, hopsworksUser, repository);
        default:
            throw new IllegalArgumentException(RESTCodes.GitOpErrorCode.INVALID_BRANCH_ACTION.getMessage());
    }
}
Also used : GitRepository(io.hops.hopsworks.persistence.entity.git.GitRepository) GitOpException(io.hops.hopsworks.exceptions.GitOpException)

Aggregations

GitOpException (io.hops.hopsworks.exceptions.GitOpException)10 GitRepository (io.hops.hopsworks.persistence.entity.git.GitRepository)3 IOException (java.io.IOException)3 TransactionAttribute (javax.ejb.TransactionAttribute)2 ResourceRequest (io.hops.hopsworks.common.api.ResourceRequest)1 AbstractFacade (io.hops.hopsworks.common.dao.AbstractFacade)1 GitCommitsFacade (io.hops.hopsworks.common.dao.git.GitCommitsFacade)1 GitPaths (io.hops.hopsworks.common.dao.git.GitPaths)1 GitCommitDTO (io.hops.hopsworks.common.git.GitCommitDTO)1 GitController (io.hops.hopsworks.common.git.GitController)1 DistributedFileSystemOps (io.hops.hopsworks.common.hdfs.DistributedFileSystemOps)1 HopsSecurityException (io.hops.hopsworks.exceptions.HopsSecurityException)1 DuplicateSigningKeyException (io.hops.hopsworks.jwt.exception.DuplicateSigningKeyException)1 SigningKeyNotFoundException (io.hops.hopsworks.jwt.exception.SigningKeyNotFoundException)1 GitCommit (io.hops.hopsworks.persistence.entity.git.GitCommit)1 GitOpExecution (io.hops.hopsworks.persistence.entity.git.GitOpExecution)1 GitRepositoryRemote (io.hops.hopsworks.persistence.entity.git.GitRepositoryRemote)1 GitCommandConfiguration (io.hops.hopsworks.persistence.entity.git.config.GitCommandConfiguration)1 GitCommandType (io.hops.hopsworks.persistence.entity.git.config.GitCommandType)1 GitProvider (io.hops.hopsworks.persistence.entity.git.config.GitProvider)1