use of alien4cloud.exception.AlreadyExistException 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;
}
use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.
the class ManagedServiceResourceService method create.
/**
* Create a Service resource associated with the given environment.
*
* @param environmentId The environment to create a service for, the service version will be the one of the environment current associated version.
* @param serviceName The name of the service as it should appears.
* @param fromRuntime If we should try to create the service from the runtime topology related to the environment.
* @return the id of the created service
*
* @throws AlreadyExistException if a service with the given name, or related to the given environment already exists
* @throws alien4cloud.exception.NotFoundException if <b>fromRuntime</b> is set to true, but the environment is not deployed
* @throws MissingSubstitutionException if topology related to the environment doesn't define a substitution type
*/
public synchronized String create(String serviceName, String environmentId, boolean fromRuntime) {
ApplicationEnvironment environment = checkAndGetApplicationEnvironment(environmentId);
// check that the service does not exists already for this environment
if (alienDAO.buildQuery(ServiceResource.class).setFilters(fromKeyValueCouples("environmentId", environmentId)).count() > 0) {
throw new AlreadyExistException("A service resource for environment <" + environmentId + "> and version <" + environment.getTopologyVersion() + "> already exists.");
}
Topology topology;
String state = ToscaNodeLifecycleConstants.INITIAL;
Deployment deployment = null;
if (fromRuntime) {
deployment = deploymentService.getActiveDeploymentOrFail(environmentId);
topology = deploymentRuntimeStateService.getRuntimeTopology(deployment.getId());
DeploymentStatus currentStatus = deploymentRuntimeStateService.getDeploymentStatus(deployment);
state = managedServiceResourceEventService.getInstanceStateFromDeploymentStatus(currentStatus);
if (state == null) {
// We need a valid deployment state to expose as service.
throw new InvalidDeploymentStatusException("Creating a service out of a running deployment is possible only when it's status is one of [DEPLOYED, FAILURE, UNDEPLOYED] current was <" + currentStatus + ">", currentStatus);
}
} else {
topology = topologyServiceCore.getOrFail(Csar.createId(environment.getApplicationId(), environment.getTopologyVersion()));
}
if (topology.getSubstitutionMapping() == null) {
throw new MissingSubstitutionException("Substitution is required to expose a topology.");
}
// The elementId of the type created out of the substitution is currently the archive name.
String serviceId = serviceResourceService.create(serviceName, environment.getTopologyVersion(), topology.getArchiveName(), environment.getTopologyVersion(), environmentId);
// Update the service relationships definition from the topology substitution
updateServiceRelationship(serviceId, topology);
if (fromRuntime) {
managedServiceResourceEventService.updateRunningService((DeploymentTopology) topology, serviceResourceService.getOrFail(serviceId), deployment, state);
}
ServiceResource serviceResource = serviceResourceService.getOrFail(serviceId);
// trigger a ManagedServiceCreatedEvent
publisher.publishEvent(new ManagedServiceCreatedEvent(this, serviceResource));
return serviceId;
}
use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.
the class ServiceResourceService method save.
/**
* Save the service resource and optionally checks that the name/version couple is unique.
* Check must be done for new resource or when the name or version has changed.
*
* @param serviceResource The service to save.
* @param ensureUniqueness True if we should process unicity check, false if not.
*/
public synchronized void save(ServiceResource serviceResource, boolean ensureUniqueness) {
if (ensureUniqueness) {
long count = alienDAO.buildQuery(ServiceResource.class).setFilters(fromKeyValueCouples("name", serviceResource.getName(), "version", serviceResource.getVersion())).count();
if (count > 0) {
throw new AlreadyExistException("A service with name <" + serviceResource.getName() + "> and version <" + serviceResource.getVersion() + "> already exists.");
}
}
// ensure that the nested version just reflects the version.
Version version = VersionUtil.parseVersion(serviceResource.getVersion());
serviceResource.setNestedVersion(version);
alienDAO.save(serviceResource);
publisher.publishEvent(new ServiceChangedEvent(this, serviceResource.getId()));
}
use of alien4cloud.exception.AlreadyExistException 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);
}
use of alien4cloud.exception.AlreadyExistException 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());
}
}
}
Aggregations