Search in sources :

Example 11 with GitOpExecution

use of io.hops.hopsworks.persistence.entity.git.GitOpExecution in project hopsworks by logicalclocks.

the class GitExecutionController method updateGitExecutionState.

public GitOpExecution updateGitExecutionState(Project project, Users hopsworksUser, GitCommandExecutionStateUpdateDTO stateDTO, Integer repositoryId, Integer executionId) throws IllegalArgumentException, GitOpException {
    GitOpExecutionState newState = stateDTO.getExecutionState();
    if (newState == null) {
        throw new IllegalArgumentException("Invalid git execution state. Execution state cannot be null.");
    }
    LOGGER.log(Level.INFO, "Updating execution, Id = " + executionId + " to " + newState.getExecutionState());
    GitRepository repository = commandConfigurationValidator.verifyRepository(project, repositoryId);
    GitOpExecution exec = getExecutionInRepository(repository, executionId);
    exec.setCommandResultMessage(stateDTO.getMessage());
    if (newState.isFinalState()) {
        if (newState == GitOpExecutionState.SUCCESS) {
            // Every successful operation should update the repository current commit and branch
            repository.setCurrentBranch(stateDTO.getBranch());
            repository.setCurrentCommit(stateDTO.getCommitHash());
            GitCommandConfiguration executedCommandConfig = exec.getGitCommandConfiguration();
            if (executedCommandConfig.getCommandType() == GitCommandType.DELETE_BRANCH) {
                // if we deleted a branch then we should also delete all the commits for this branch
                gitCommitsFacade.deleteAllInBranchAndRepository(executedCommandConfig.getBranchName(), repository);
            }
            if (executedCommandConfig.getCommandType() == GitCommandType.ADD_REMOTE || executedCommandConfig.getCommandType() == GitCommandType.DELETE_REMOTE) {
                // Update the remotes which are in the execution final message
                String remotesJson = exec.getCommandResultMessage();
                if (!Strings.isNullOrEmpty(remotesJson)) {
                    gitRepositoryRemotesFacade.updateRepositoryRemotes(gitCommandOperationUtil.convertToRemote(repository, remotesJson), repository);
                }
            }
        }
        gitRepositoryFacade.updateRepositoryCid(repository, null);
        gitCommandOperationUtil.cleanUp(project, hopsworksUser, exec.getConfigSecret());
    }
    return gitOpExecutionFacade.updateState(exec, newState, stateDTO.getMessage());
}
Also used : GitRepository(io.hops.hopsworks.persistence.entity.git.GitRepository) GitCommandConfiguration(io.hops.hopsworks.persistence.entity.git.config.GitCommandConfiguration) GitOpExecutionState(io.hops.hopsworks.persistence.entity.git.config.GitOpExecutionState) GitOpExecution(io.hops.hopsworks.persistence.entity.git.GitOpExecution)

Example 12 with GitOpExecution

use of io.hops.hopsworks.persistence.entity.git.GitOpExecution 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)

Aggregations

GitOpExecution (io.hops.hopsworks.persistence.entity.git.GitOpExecution)12 AllowedProjectRoles (io.hops.hopsworks.api.filter.AllowedProjectRoles)7 ApiKeyRequired (io.hops.hopsworks.api.filter.apiKey.ApiKeyRequired)7 ResourceRequest (io.hops.hopsworks.common.api.ResourceRequest)7 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)7 ApiOperation (io.swagger.annotations.ApiOperation)7 Path (javax.ws.rs.Path)7 Produces (javax.ws.rs.Produces)7 Users (io.hops.hopsworks.persistence.entity.user.Users)6 GitOpExecutionDTO (io.hops.hopsworks.api.git.execution.GitOpExecutionDTO)5 POST (javax.ws.rs.POST)5 Consumes (javax.ws.rs.Consumes)4 GitRepository (io.hops.hopsworks.persistence.entity.git.GitRepository)2 GitPaths (io.hops.hopsworks.common.dao.git.GitPaths)1 GitCommitDTO (io.hops.hopsworks.common.git.GitCommitDTO)1 DistributedFileSystemOps (io.hops.hopsworks.common.hdfs.DistributedFileSystemOps)1 GitOpException (io.hops.hopsworks.exceptions.GitOpException)1 HopsSecurityException (io.hops.hopsworks.exceptions.HopsSecurityException)1 GitCommit (io.hops.hopsworks.persistence.entity.git.GitCommit)1 GitCommandConfiguration (io.hops.hopsworks.persistence.entity.git.config.GitCommandConfiguration)1