Search in sources :

Example 46 with ApplicationEnvironment

use of alien4cloud.model.application.ApplicationEnvironment in project alien4cloud by alien4cloud.

the class ServiceUsageReporter method reportServiceUsage.

@EventListener
private void reportServiceUsage(ServiceUsageRequestEvent serviceChangedEvent) {
    GetMultipleDataResult<Deployment> usageResult = alienDAO.buildQuery(Deployment.class).setFilters(fromKeyValueCouples("endDate", null, "serviceResourceIds", serviceChangedEvent.getServiceId())).prepareSearch().search(0, Integer.MAX_VALUE);
    if (usageResult.getTotalResults() > 0) {
        Usage[] usages = Arrays.stream(usageResult.getData()).map(deployment -> {
            ApplicationEnvironment environment = environmentService.getOrFail(deployment.getEnvironmentId());
            String usageName = "App (" + deployment.getSourceName() + "), Env (" + environment.getName() + ")";
            return new Usage(usageName, "Deployment", deployment.getId(), null);
        }).toArray(Usage[]::new);
        serviceChangedEvent.addUsages(usages);
    }
}
Also used : FilterUtil.fromKeyValueCouples(alien4cloud.dao.FilterUtil.fromKeyValueCouples) Arrays(java.util.Arrays) ApplicationEnvironmentService(alien4cloud.application.ApplicationEnvironmentService) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Usage(alien4cloud.model.common.Usage) Resource(javax.annotation.Resource) EventListener(org.springframework.context.event.EventListener) ServiceUsageRequestEvent(org.alien4cloud.alm.service.events.ServiceUsageRequestEvent) IGenericSearchDAO(alien4cloud.dao.IGenericSearchDAO) Inject(javax.inject.Inject) Deployment(alien4cloud.model.deployment.Deployment) Service(org.springframework.stereotype.Service) Usage(alien4cloud.model.common.Usage) Deployment(alien4cloud.model.deployment.Deployment) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) EventListener(org.springframework.context.event.EventListener)

Example 47 with ApplicationEnvironment

use of alien4cloud.model.application.ApplicationEnvironment in project alien4cloud by alien4cloud.

the class FlowExecutionContext method getConfiguration.

/**
 * Get a configuration object related to the deployment flow.
 *
 * This operation also updates the lastFlowParamUpdate that may be used by later processor to skip some processing when nothing has changed.
 *
 * The operation is also caching aware to avoid requesting multiple times the same object from elasticsearch.
 *
 * The configuration object is not annotated with {@link ESObject}, no request to elasticsearch will be made.
 *
 * @param cfgClass The class of the configuration object.
 * @param modifierName Name of the modifier that tries to access a deployment configuration object (related to the environment).
 * @param <T> The type of the configuration object.
 * @return An instance of the requested configuration object.
 */
public <T extends AbstractDeploymentConfig> Optional<T> getConfiguration(Class<T> cfgClass, String modifierName) {
    environmentContext.orElseThrow(() -> new EnvironmentContextRequiredException(modifierName));
    ApplicationEnvironment env = environmentContext.get().getEnvironment();
    String cfgId = AbstractDeploymentConfig.generateId(env.getTopologyVersion(), env.getId());
    String configCacheId = cfgClass.getSimpleName() + "/" + cfgId;
    T config = (T) executionCache.get(configCacheId);
    // If the config object is annotated with ESObject then it may be cached in ElasticSearch
    if (config == null && cfgClass.isAnnotationPresent(ESObject.class)) {
        config = deploymentConfigurationDao.findById(cfgClass, cfgId);
        executionCache.put(configCacheId, config);
    }
    if (config == null) {
        return Optional.empty();
    }
    if (lastFlowParamUpdate.before(config.getLastUpdateDate())) {
        lastFlowParamUpdate = config.getLastUpdateDate();
    }
    return Optional.of(config);
}
Also used : ESObject(org.elasticsearch.annotation.ESObject) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment)

Example 48 with ApplicationEnvironment

use of alien4cloud.model.application.ApplicationEnvironment in project alien4cloud by alien4cloud.

the class InputArtifactService method onCopyConfiguration.

@EventListener
@Order(11)
public void onCopyConfiguration(OnDeploymentConfigCopyEvent onDeploymentConfigCopyEvent) {
    ApplicationEnvironment source = onDeploymentConfigCopyEvent.getSourceEnvironment();
    ApplicationEnvironment target = onDeploymentConfigCopyEvent.getTargetEnvironment();
    DeploymentInputs deploymentInputs = deploymentConfigurationDao.findById(DeploymentInputs.class, AbstractDeploymentConfig.generateId(source.getTopologyVersion(), source.getId()));
    if (deploymentInputs == null || MapUtils.isEmpty(deploymentInputs.getInputArtifacts())) {
        // Nothing to copy
        return;
    }
    Topology topology = topologyServiceCore.getOrFail(Csar.createId(target.getApplicationId(), target.getTopologyVersion()));
    if (MapUtils.isNotEmpty(topology.getInputArtifacts())) {
        Map<String, DeploymentArtifact> inputsArtifactsDefinitions = topology.getInputArtifacts();
        // Copy only artifacts which exists in the new topology's definition
        Map<String, DeploymentArtifact> inputsArtifactsToCopy = deploymentInputs.getInputArtifacts().entrySet().stream().filter(inputEntry -> inputsArtifactsDefinitions.containsKey(inputEntry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        if (MapUtils.isNotEmpty(inputsArtifactsToCopy)) {
            // There's something to copy
            DeploymentInputs targetDeploymentInputs = deploymentConfigurationDao.findById(DeploymentInputs.class, AbstractDeploymentConfig.generateId(target.getTopologyVersion(), target.getId()));
            if (targetDeploymentInputs == null) {
                targetDeploymentInputs = new DeploymentInputs(target.getTopologyVersion(), target.getId());
            }
            targetDeploymentInputs.setInputArtifacts(inputsArtifactsToCopy);
            deploymentConfigurationDao.save(targetDeploymentInputs);
        }
    }
}
Also used : MapUtils(org.apache.commons.collections4.MapUtils) Order(org.springframework.core.annotation.Order) Csar(org.alien4cloud.tosca.model.Csar) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) TopologyServiceCore(alien4cloud.topology.TopologyServiceCore) Resource(javax.annotation.Resource) EventListener(org.springframework.context.event.EventListener) IOException(java.io.IOException) HashMap(java.util.HashMap) AbstractDeploymentConfig(org.alien4cloud.alm.deployment.configuration.model.AbstractDeploymentConfig) Collectors(java.util.stream.Collectors) NotFoundException(alien4cloud.exception.NotFoundException) Inject(javax.inject.Inject) IFileRepository(alien4cloud.component.repository.IFileRepository) Service(org.springframework.stereotype.Service) Map(java.util.Map) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) MultipartFile(org.springframework.web.multipart.MultipartFile) Topology(org.alien4cloud.tosca.model.templates.Topology) ArtifactRepositoryConstants(alien4cloud.component.repository.ArtifactRepositoryConstants) OnDeploymentConfigCopyEvent(org.alien4cloud.alm.deployment.configuration.events.OnDeploymentConfigCopyEvent) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) InputStream(java.io.InputStream) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) Topology(org.alien4cloud.tosca.model.templates.Topology) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) HashMap(java.util.HashMap) Map(java.util.Map) Order(org.springframework.core.annotation.Order) EventListener(org.springframework.context.event.EventListener)

Example 49 with ApplicationEnvironment

use of alien4cloud.model.application.ApplicationEnvironment in project alien4cloud by alien4cloud.

the class PolicyMatchingSubstitutionService method onCopyConfiguration.

// FIXME fix this, synch with org.alien4cloud.alm.deployment.configuration.services.MatchingSubstitutionService#onCopyConfiguration
// @EventListener
// @Order(30) // Process this after location matching copy (first element).
public void onCopyConfiguration(OnDeploymentConfigCopyEvent onDeploymentConfigCopyEvent) {
    ApplicationEnvironment source = onDeploymentConfigCopyEvent.getSourceEnvironment();
    ApplicationEnvironment target = onDeploymentConfigCopyEvent.getTargetEnvironment();
    DeploymentMatchingConfiguration sourceConfiguration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(source.getTopologyVersion(), source.getId()));
    DeploymentMatchingConfiguration targetConfiguration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(target.getTopologyVersion(), target.getId()));
    if (sourceConfiguration == null || MapUtils.isEmpty(sourceConfiguration.getLocationGroups()) || targetConfiguration == null || MapUtils.isEmpty(targetConfiguration.getLocationGroups())) {
        // Nothing to copy
        return;
    }
    // We have to execute a piece of the deployment flow to find out matching candidates so we copy only required inputs
    Topology topology = topologyServiceCore.getOrFail(Csar.createId(target.getApplicationId(), target.getTopologyVersion()));
    if (MapUtils.isNotEmpty(topology.getNodeTemplates())) {
        Application application = applicationService.getOrFail(target.getApplicationId());
        FlowExecutionContext executionContext = new FlowExecutionContext(deploymentConfigurationDao, topology, new EnvironmentContext(application, target));
        flowExecutor.execute(topology, getMatchingFlow(), executionContext);
        Map<String, Set<String>> locResTemplateIdsPerNodeIds = (Map<String, Set<String>>) executionContext.getExecutionCache().get(FlowExecutionContext.SELECTED_MATCH_NODE_LOCATION_TEMPLATE_BY_NODE_ID_MAP);
        // Update the substitution on the target if available substitution is always compatible
        Map<String, String> validOnNewEnvSubstitutedNodes = safe(sourceConfiguration.getMatchedLocationResources()).entrySet().stream().filter(entry -> locResTemplateIdsPerNodeIds.containsKey(entry.getKey()) && locResTemplateIdsPerNodeIds.get(entry.getKey()).contains(entry.getValue())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        if (MapUtils.isNotEmpty(validOnNewEnvSubstitutedNodes)) {
            if (targetConfiguration.getMatchedLocationResources() == null) {
                targetConfiguration.setMatchedLocationResources(Maps.newHashMap());
            }
            validOnNewEnvSubstitutedNodes.forEach((key, value) -> {
                targetConfiguration.getMatchedLocationResources().put(key, value);
                // Copy properties set on the node to the new one
                targetConfiguration.getMatchedNodesConfiguration().put(key, safe(sourceConfiguration.getMatchedNodesConfiguration()).get(key));
            });
            deploymentConfigurationDao.save(targetConfiguration);
        }
    }
}
Also used : EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) TopologyServiceCore(alien4cloud.topology.TopologyServiceCore) PolicyMatchingCompositeModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.PolicyMatchingCompositeModifier) PolicyMatchingConfigAutoSelectModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.PolicyMatchingConfigAutoSelectModifier) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Inject(javax.inject.Inject) Service(org.springframework.stereotype.Service) Map(java.util.Map) Application(alien4cloud.model.application.Application) ApplicationService(alien4cloud.application.ApplicationService) OnDeploymentConfigCopyEvent(org.alien4cloud.alm.deployment.configuration.events.OnDeploymentConfigCopyEvent) ITopologyModifier(org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier) MapUtils(org.apache.commons.collections4.MapUtils) Csar(org.alien4cloud.tosca.model.Csar) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Set(java.util.Set) SetMatchedPolicyModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.action.SetMatchedPolicyModifier) AbstractDeploymentConfig(org.alien4cloud.alm.deployment.configuration.model.AbstractDeploymentConfig) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) NotFoundException(alien4cloud.exception.NotFoundException) List(java.util.List) EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) FlowExecutor(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutor) AbstractComposedModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.AbstractComposedModifier) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) Topology(org.alien4cloud.tosca.model.templates.Topology) Set(java.util.Set) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) Topology(org.alien4cloud.tosca.model.templates.Topology) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Application(alien4cloud.model.application.Application) Map(java.util.Map)

Example 50 with ApplicationEnvironment

use of alien4cloud.model.application.ApplicationEnvironment in project alien4cloud by alien4cloud.

the class PreconfiguredInputsModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    EnvironmentContext environmentContext = context.getEnvironmentContext().orElseThrow(() -> new IllegalArgumentException("Preconfigured input modifier requires an environment context."));
    ApplicationEnvironment environment = environmentContext.getEnvironment();
    Map<String, Location> locations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
    AlienContextVariables alienContextVariables = new AlienContextVariables();
    alienContextVariables.setApplicationEnvironment(environment);
    alienContextVariables.setLocation(locations.values().stream().findFirst().get());
    alienContextVariables.setApplication(environmentContext.getApplication());
    // TODO: avoid reloading every time - find a way to know the last update on files (git hash ?)
    Properties appVarProps = quickFileStorageService.loadApplicationVariables(environmentContext.getApplication().getId());
    Properties envTypeVarProps = quickFileStorageService.loadEnvironmentTypeVariables(topology.getId(), environment.getEnvironmentType());
    Properties envVarProps = quickFileStorageService.loadEnvironmentVariables(topology.getId(), environment.getId());
    Map<String, Object> inputsMappingsMap = quickFileStorageService.loadInputsMappingFile(topology.getId());
    InputsMappingFileVariableResolver.InputsResolvingResult inputsResolvingResult = InputsMappingFileVariableResolver.configure(appVarProps, envTypeVarProps, envVarProps, alienContextVariables).resolve(inputsMappingsMap, topology.getInputs());
    if (CollectionUtils.isNotEmpty(inputsResolvingResult.getMissingVariables())) {
        context.log().error(new MissingVariablesTask(inputsResolvingResult.getMissingVariables()));
    }
    if (CollectionUtils.isNotEmpty(inputsResolvingResult.getUnresolved())) {
        context.log().error(new UnresolvablePredefinedInputsTask(inputsResolvingResult.getUnresolved()));
    }
    // checking constraints
    Map<String, ConstraintUtil.ConstraintInformation> violations = Maps.newHashMap();
    Map<String, ConstraintUtil.ConstraintInformation> typesViolations = Maps.newHashMap();
    for (Map.Entry<String, PropertyValue> entry : safe(inputsResolvingResult.getResolved()).entrySet()) {
        try {
            ConstraintPropertyService.checkPropertyConstraint(entry.getKey(), entry.getValue(), topology.getInputs().get(entry.getKey()));
        } catch (ConstraintViolationException e) {
            violations.put(entry.getKey(), getConstraintInformation(e.getMessage(), e.getConstraintInformation()));
        } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
            typesViolations.put(entry.getKey(), getConstraintInformation(e.getMessage(), e.getConstraintInformation()));
        }
    }
    if (MapUtils.isNotEmpty(violations)) {
        context.log().error(new PredefinedInputsConstraintViolationTask(violations, TaskCode.PREDEFINED_INPUTS_CONSTRAINT_VIOLATION));
    }
    if (MapUtils.isNotEmpty(typesViolations)) {
        context.log().error(new PredefinedInputsConstraintViolationTask(typesViolations, TaskCode.PREDEFINED_INPUTS_TYPE_VIOLATION));
    }
    PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = new PreconfiguredInputsConfiguration(environment.getTopologyVersion(), environment.getId());
    preconfiguredInputsConfiguration.setInputs(inputsResolvingResult.getResolved());
    // add unresolved so that they are not considered as deployer input
    inputsResolvingResult.getUnresolved().forEach(unresolved -> preconfiguredInputsConfiguration.getInputs().put(unresolved, null));
    // TODO: improve me
    preconfiguredInputsConfiguration.setLastUpdateDate(new Date());
    preconfiguredInputsConfiguration.setCreationDate(new Date());
    context.saveConfiguration(preconfiguredInputsConfiguration);
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) PredefinedInputsConstraintViolationTask(alien4cloud.topology.task.PredefinedInputsConstraintViolationTask) PreconfiguredInputsConfiguration(org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) Properties(java.util.Properties) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Date(java.util.Date) EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) AlienContextVariables(org.alien4cloud.tosca.variable.AlienContextVariables) InputsMappingFileVariableResolver(org.alien4cloud.tosca.variable.InputsMappingFileVariableResolver) UnresolvablePredefinedInputsTask(alien4cloud.topology.task.UnresolvablePredefinedInputsTask) MissingVariablesTask(alien4cloud.topology.task.MissingVariablesTask) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) Map(java.util.Map) Location(alien4cloud.model.orchestrators.locations.Location)

Aggregations

ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)82 ApiOperation (io.swagger.annotations.ApiOperation)42 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)42 Application (alien4cloud.model.application.Application)40 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)35 Audit (alien4cloud.audit.annotation.Audit)27 List (java.util.List)17 Collectors (java.util.stream.Collectors)16 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)15 RestResponse (alien4cloud.rest.model.RestResponse)15 Topology (org.alien4cloud.tosca.model.templates.Topology)15 Set (java.util.Set)14 ApplicationEnvironmentService (alien4cloud.application.ApplicationEnvironmentService)13 NotFoundException (alien4cloud.exception.NotFoundException)13 Map (java.util.Map)13 Resource (javax.annotation.Resource)12 ApplicationTopologyVersion (alien4cloud.model.application.ApplicationTopologyVersion)11 Deployment (alien4cloud.model.deployment.Deployment)11 Arrays (java.util.Arrays)11 Location (alien4cloud.model.orchestrators.locations.Location)10