Search in sources :

Example 1 with ToscaTypeAlreadyDefinedInOtherCSAR

use of alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR in project alien4cloud by alien4cloud.

the class CloudServiceArchiveController method uploadCSAR.

@ApiOperation(value = "Upload a csar zip file.")
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<CsarUploadResult> uploadCSAR(@RequestParam(required = false) String workspace, @RequestParam("file") MultipartFile csar) throws IOException {
    Path csarPath = null;
    try {
        if (workspace == null) {
            workspace = AlienConstants.GLOBAL_WORKSPACE_ID;
        }
        // Perform check that the user has one of ARCHITECT, COMPONENT_MANAGER or ADMIN role
        archiveIndexerAuthorizationFilter.preCheckAuthorization(workspace);
        log.info("Serving file upload with name [" + csar.getOriginalFilename() + "]");
        csarPath = Files.createTempFile(tempDirPath, null, '.' + CsarFileRepository.CSAR_EXTENSION);
        // save the archive in the temp directory
        FileUploadUtil.safeTransferTo(csarPath, csar);
        // load, parse the archive definitions and save on disk
        ParsingResult<Csar> result = csarUploadService.upload(csarPath, CSARSource.UPLOAD, workspace);
        RestError error = null;
        if (result.hasError(ParsingErrorLevel.ERROR)) {
            error = RestErrorBuilder.builder(RestErrorCode.CSAR_PARSING_ERROR).build();
        }
        return RestResponseBuilder.<CsarUploadResult>builder().error(error).data(CsarUploadUtil.toUploadResult(result)).build();
    } catch (ParsingException e) {
        log.error("Error happened while parsing csar file <" + e.getFileName() + ">", e);
        String fileName = e.getFileName() == null ? csar.getOriginalFilename() : e.getFileName();
        CsarUploadResult uploadResult = new CsarUploadResult();
        uploadResult.getErrors().put(fileName, e.getParsingErrors());
        return RestResponseBuilder.<CsarUploadResult>builder().error(RestErrorBuilder.builder(RestErrorCode.CSAR_INVALID_ERROR).build()).data(uploadResult).build();
    } catch (AlreadyExistException | CSARUsedInActiveDeployment | ToscaTypeAlreadyDefinedInOtherCSAR e) {
        CsarUploadResult uploadResult = new CsarUploadResult();
        uploadResult.getErrors().put(csar.getOriginalFilename(), Lists.newArrayList(UploadExceptionUtil.parsingErrorFromException(e)));
        return RestResponseBuilder.<CsarUploadResult>builder().error(RestErrorBuilder.builder(RestErrorCode.ALREADY_EXIST_ERROR).build()).data(uploadResult).build();
    } finally {
        if (csarPath != null) {
            // Clean up
            try {
                FileUtil.delete(csarPath);
            } catch (IOException e) {
            // The repository might just move the file instead of copying to save IO disk access
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Csar(org.alien4cloud.tosca.model.Csar) RestError(alien4cloud.rest.model.RestError) ParsingException(alien4cloud.tosca.parser.ParsingException) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) IOException(java.io.IOException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ToscaTypeAlreadyDefinedInOtherCSAR

use of alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR in project alien4cloud by alien4cloud.

the class InitialLoader method loadArchives.

private void loadArchives(Path rootDirectory) {
    if (!Files.exists(rootDirectory) || !Files.isDirectory(rootDirectory)) {
        log.warn("Skipping archives' initial loading: directory cannot be found {}.", rootDirectory.toString());
        return;
    }
    // this operation is performed using admin role
    SecurityContextImpl adminContext = new SecurityContextImpl();
    Set<SimpleGrantedAuthority> authorities = Sets.newHashSet();
    authorities.add(new SimpleGrantedAuthority(Role.ADMIN.name()));
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("alien4cloud_init_loader", "alien4cloud_init_loader", authorities);
    adminContext.setAuthentication(auth);
    SecurityContextHolder.setContext(adminContext);
    // archives must be in zip format and placed in the actual folder
    try {
        List<Path> archives = FileUtil.listFiles(rootDirectory, ".+\\.(zip|csar)");
        Collections.sort(archives);
        for (Path archive : archives) {
            try {
                log.debug("Initial load of archives from [ {} ].", archive.toString());
                csarUploadService.upload(archive, CSARSource.ALIEN, AlienConstants.GLOBAL_WORKSPACE_ID);
            } catch (AlreadyExistException e) {
                log.debug("Skipping initial upload of archive [ {} ]. Archive has already been loaded.", archive.toString(), e);
            } catch (CSARUsedInActiveDeployment e) {
                log.debug("Skipping initial upload of archive [ {} ]. Archive is used in an active depoyment, and then cannot be overrided.", archive.toString(), e);
            } catch (ParsingException e) {
                log.error("Initial upload of archive [ {} ] has failed.", archive.toString(), e);
            } catch (ToscaTypeAlreadyDefinedInOtherCSAR e) {
                log.debug("Skipping initial upload of archive [ {} ], it's archive contain's a tosca type already defined in an other archive." + e.getMessage(), archive.toString(), e);
            }
        }
    } catch (IOException e) {
        log.error("Failed to load initial archives", e);
    } finally {
        // clear the security context
        SecurityContextHolder.clearContext();
    }
}
Also used : Path(java.nio.file.Path) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) ParsingException(alien4cloud.tosca.parser.ParsingException) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) IOException(java.io.IOException) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Example 3 with ToscaTypeAlreadyDefinedInOtherCSAR

use of alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR in project alien4cloud by alien4cloud.

the class CsarGitService method processImport.

private List<ParsingResult<Csar>> processImport(CsarGitRepository csarGitRepository, CsarGitCheckoutLocation csarGitCheckoutLocation, String gitHash) {
    // find all the archives under the given hierarchy and zip them to create archives
    Path archiveZipRoot = tempZipDirPath.resolve(csarGitRepository.getId());
    Path archiveGitRoot = tempDirPath.resolve(csarGitRepository.getId());
    if (csarGitCheckoutLocation.getSubPath() != null && !csarGitCheckoutLocation.getSubPath().isEmpty()) {
        archiveGitRoot = archiveGitRoot.resolve(csarGitCheckoutLocation.getSubPath());
    }
    Set<Path> archivePaths = csarFinderService.prepare(archiveGitRoot, archiveZipRoot);
    List<ParsingResult<Csar>> parsingResults = Lists.newArrayList();
    Map<CSARDependency, CsarDependenciesBean> csarDependenciesBeans = uploadService.preParsing(archivePaths, parsingResults);
    List<CsarDependenciesBean> sorted = sort(csarDependenciesBeans);
    for (CsarDependenciesBean csarBean : sorted) {
        String archiveRepoPath = archiveZipRoot.relativize(csarBean.getPath().getParent()).toString();
        if (csarGitCheckoutLocation.getLastImportedHash() != null && csarGitCheckoutLocation.getLastImportedHash().equals(gitHash) && csarService.get(csarBean.getSelf().getName(), csarBean.getSelf().getVersion()) != null) {
            // no commit since last import and the archive still exist in the repo, so do not import
            addAlreadyImportParsingResult(archiveRepoPath, parsingResults);
            continue;
        }
        try {
            // FIXME Add possibility to choose an workspace
            ParsingResult<Csar> result = uploadService.upload(csarBean.getPath(), CSARSource.GIT, AlienConstants.GLOBAL_WORKSPACE_ID);
            result.getContext().setFileName(archiveRepoPath + "/" + result.getContext().getFileName());
            parsingResults.add(result);
        } catch (ParsingException e) {
            ParsingResult<Csar> failedResult = new ParsingResult<>();
            failedResult.setContext(new ParsingContext(archiveRepoPath));
            failedResult.getContext().setParsingErrors(e.getParsingErrors());
            parsingResults.add(failedResult);
            log.debug("Failed to import archive from git as it cannot be parsed", e);
        } catch (AlreadyExistException | ToscaTypeAlreadyDefinedInOtherCSAR | CSARUsedInActiveDeployment e) {
            ParsingResult<Csar> failedResult = new ParsingResult<>();
            failedResult.setContext(new ParsingContext(archiveRepoPath));
            failedResult.getContext().setParsingErrors(Lists.newArrayList(UploadExceptionUtil.parsingErrorFromException(e)));
            parsingResults.add(failedResult);
        }
    }
    return parsingResults;
}
Also used : Path(java.nio.file.Path) Csar(org.alien4cloud.tosca.model.Csar) ParsingContext(alien4cloud.tosca.parser.ParsingContext) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) CsarDependenciesBean(org.alien4cloud.tosca.model.CsarDependenciesBean) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) ParsingResult(alien4cloud.tosca.parser.ParsingResult) ParsingException(alien4cloud.tosca.parser.ParsingException) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Example 4 with ToscaTypeAlreadyDefinedInOtherCSAR

use of alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR in project alien4cloud by alien4cloud.

the class UploadExceptionUtil method parsingErrorFromException.

public static ParsingError parsingErrorFromException(Exception e) {
    log.debug("Archive import failed", e);
    if (e instanceof AlreadyExistException) {
        return new ParsingError(ErrorCode.CSAR_ALREADY_EXISTS, "CSAR already exists", null, "Unable to override an existing CSAR if the version is not a SNAPSHOT version.", null, null);
    } else if (e instanceof CSARUsedInActiveDeployment) {
        return new ParsingError(ErrorCode.CSAR_USED_IN_ACTIVE_DEPLOYMENT, "CSAR used in active deployment", null, "Unable to override a csar used in an active deployment.", null, null);
    } else if (e instanceof ToscaTypeAlreadyDefinedInOtherCSAR) {
        return new ParsingError(ErrorCode.TOSCA_TYPE_ALREADY_EXISTS_IN_OTHER_CSAR, "Tosca type conflict", null, e.getMessage(), null, null);
    }
    log.error("Unexpected error while parsing archive.", e);
    return new ParsingError(ErrorCode.ERRONEOUS_ARCHIVE_FILE, "Failed to process archive for unexpected reason", null, e.getMessage(), null, null);
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Example 5 with ToscaTypeAlreadyDefinedInOtherCSAR

use of alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR in project alien4cloud by alien4cloud.

the class PluginArchiveIndexer method indexOrchestratorArchives.

/**
 * Index archives defined at the orchestrator level by a plugin.
 *
 * @param orchestratorFactory The orchestrator factory.
 * @param orchestratorInstance The instance of the orchestrator (created by the factory).
 */
public void indexOrchestratorArchives(IOrchestratorPluginFactory<IOrchestratorPlugin<?>, ?> orchestratorFactory, IOrchestratorPlugin<Object> orchestratorInstance) {
    for (PluginArchive pluginArchive : orchestratorInstance.pluginArchives()) {
        try {
            archiveIndexer.importArchive(pluginArchive.getArchive(), CSARSource.ORCHESTRATOR, pluginArchive.getArchiveFilePath(), Lists.<ParsingError>newArrayList());
            publishLocationTypeIndexedEvent(pluginArchive.getArchive().getNodeTypes().values(), orchestratorFactory, null);
        } catch (AlreadyExistException e) {
            log.debug("Skipping orchestrator archive import as the released version already exists in the repository. " + e.getMessage());
        } catch (CSARUsedInActiveDeployment e) {
            log.debug("Skipping orchestrator archive import as it is used in an active deployment. " + e.getMessage());
        } catch (ToscaTypeAlreadyDefinedInOtherCSAR e) {
            log.debug("Skipping orchestrator archive import, it's archive contain's a tosca type already defined in an other archive." + e.getMessage());
        }
    }
}
Also used : PluginArchive(alien4cloud.orchestrators.plugin.model.PluginArchive) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Aggregations

CSARUsedInActiveDeployment (alien4cloud.component.repository.exception.CSARUsedInActiveDeployment)6 ToscaTypeAlreadyDefinedInOtherCSAR (alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR)6 AlreadyExistException (alien4cloud.exception.AlreadyExistException)6 ParsingException (alien4cloud.tosca.parser.ParsingException)3 Path (java.nio.file.Path)3 ParsingError (alien4cloud.tosca.parser.ParsingError)2 IOException (java.io.IOException)2 Csar (org.alien4cloud.tosca.model.Csar)2 Audit (alien4cloud.audit.annotation.Audit)1 PluginArchive (alien4cloud.orchestrators.plugin.model.PluginArchive)1 RestError (alien4cloud.rest.model.RestError)1 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)1 ParsingContext (alien4cloud.tosca.parser.ParsingContext)1 ParsingResult (alien4cloud.tosca.parser.ParsingResult)1 ApiOperation (io.swagger.annotations.ApiOperation)1 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)1 CsarDependenciesBean (org.alien4cloud.tosca.model.CsarDependenciesBean)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1