Search in sources :

Example 11 with DeploymentMatchingConfiguration

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration in project alien4cloud by alien4cloud.

the class PolicyMatchingCandidateModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class, PolicyMatchingCandidateModifier.class.getSimpleName());
    if (!configurationOptional.isPresent()) {
        // we should not end-up here as location matching should be processed first
        context.log().error(new LocationPolicyTask());
        return;
    }
    DeploymentMatchingConfiguration matchingConfiguration = configurationOptional.get();
    if (matchingConfiguration.getMatchedPolicies() == null) {
        matchingConfiguration.setMatchedPolicies(Maps.newHashMap());
    }
    Map<String, Location> locationMap = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
    // TODO avoid update if the matching configuration is strickly younger than the context last conf update ?
    // Fetch available substitutions on the selected locations.
    Map<String, List<PolicyLocationResourceTemplate>> availableSubstitutions = getAvailableMatches(topology, context, matchingConfiguration.getLocationGroups(), locationMap, context.getEnvironmentContext().get().getEnvironment().getId());
    context.getExecutionCache().put(FlowExecutionContext.MATCHED_POLICY_LOCATION_TEMPLATES_BY_NODE_ID_MAP, availableSubstitutions);
}
Also used : LocationPolicyTask(alien4cloud.topology.task.LocationPolicyTask) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) List(java.util.List) Map(java.util.Map) Location(alien4cloud.model.orchestrators.locations.Location)

Example 12 with DeploymentMatchingConfiguration

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration in project alien4cloud by alien4cloud.

the class DeploymentTopologyDTOBuilder method build.

/**
 * Create a deployment topology dto from the context of the execution of a deployment flow.
 *
 * @param executionContext The deployment flow execution context.
 * @return The deployment topology.
 */
private DeploymentTopologyDTO build(FlowExecutionContext executionContext) {
    // re-create the deployment topology object for api compatibility purpose
    DeploymentTopology deploymentTopology = new DeploymentTopology();
    ReflectionUtil.mergeObject(executionContext.getTopology(), deploymentTopology);
    deploymentTopology.setInitialTopologyId(executionContext.getTopology().getId());
    deploymentTopology.setEnvironmentId(executionContext.getEnvironmentContext().get().getEnvironment().getId());
    deploymentTopology.setVersionId(executionContext.getEnvironmentContext().get().getEnvironment().getTopologyVersion());
    DeploymentTopologyDTO deploymentTopologyDTO = new DeploymentTopologyDTO();
    topologyDTOBuilder.initTopologyDTO(deploymentTopology, deploymentTopologyDTO);
    // Convert log result to validation result.
    TopologyValidationResult validationResult = new TopologyValidationResult();
    for (AbstractTask task : executionContext.getLog().getInfos()) {
        validationResult.addInfo(task);
    }
    for (AbstractTask task : executionContext.getLog().getWarnings()) {
        validationResult.addWarning(task);
    }
    for (AbstractTask task : executionContext.getLog().getErrors()) {
        validationResult.addTask(task);
    }
    validationResult.setValid(validationResult.getTaskList() == null || validationResult.getTaskList().isEmpty());
    deploymentTopologyDTO.setValidation(validationResult);
    Optional<PreconfiguredInputsConfiguration> preconfiguredInputsConfiguration = executionContext.getConfiguration(PreconfiguredInputsConfiguration.class, DeploymentTopologyDTOBuilder.class.getSimpleName());
    if (!preconfiguredInputsConfiguration.isPresent()) {
        deploymentTopology.setPreconfiguredInputProperties(Maps.newHashMap());
    } else {
        deploymentTopology.setPreconfiguredInputProperties(preconfiguredInputsConfiguration.get().getInputs());
    }
    Optional<DeploymentInputs> inputsOptional = executionContext.getConfiguration(DeploymentInputs.class, DeploymentTopologyDTOBuilder.class.getSimpleName());
    if (!inputsOptional.isPresent()) {
        deploymentTopology.setDeployerInputProperties(Maps.newHashMap());
        deploymentTopology.setUploadedInputArtifacts(Maps.newHashMap());
    } else {
        deploymentTopology.setDeployerInputProperties(inputsOptional.get().getInputs());
        deploymentTopology.setUploadedInputArtifacts(inputsOptional.get().getInputArtifacts());
    }
    Optional<DeploymentMatchingConfiguration> matchingConfigurationOptional = executionContext.getConfiguration(DeploymentMatchingConfiguration.class, DeploymentTopologyDTOBuilder.class.getSimpleName());
    if (!matchingConfigurationOptional.isPresent()) {
        return deploymentTopologyDTO;
    }
    DeploymentMatchingConfiguration matchingConfiguration = matchingConfigurationOptional.get();
    deploymentTopology.setOrchestratorId(matchingConfiguration.getOrchestratorId());
    deploymentTopology.setLocationGroups(matchingConfiguration.getLocationGroups());
    deploymentTopologyDTO.setLocationPolicies(matchingConfiguration.getLocationIds());
    // Good enough approximation as it doesn't contains just the location dependencies.
    deploymentTopology.setLocationDependencies(executionContext.getTopology().getDependencies());
    DeploymentSubstitutionConfiguration substitutionConfiguration = new DeploymentSubstitutionConfiguration();
    substitutionConfiguration.setSubstitutionTypes(new LocationResourceTypes());
    // fill DTO with policies substitution stuffs
    fillDTOWithPoliciesSubstitutionConfiguration(executionContext, deploymentTopology, deploymentTopologyDTO, matchingConfiguration, substitutionConfiguration);
    // fill DTO with nodes substitution stuffs
    fillDTOWithNodesSubstitutionConfiguration(executionContext, deploymentTopology, deploymentTopologyDTO, matchingConfiguration, substitutionConfiguration);
    deploymentTopologyDTO.setAvailableSubstitutions(substitutionConfiguration);
    ApplicationEnvironment environment = executionContext.getEnvironmentContext().get().getEnvironment();
    OrchestratorDeploymentProperties orchestratorDeploymentProperties = executionContext.getConfiguration(OrchestratorDeploymentProperties.class, this.getClass().getSimpleName()).orElse(new OrchestratorDeploymentProperties(environment.getTopologyVersion(), environment.getId(), matchingConfiguration.getOrchestratorId()));
    deploymentTopology.setProviderDeploymentProperties(orchestratorDeploymentProperties.getProviderDeploymentProperties());
    deploymentTopologyDTO.setSecretCredentialInfos((List<SecretCredentialInfo>) executionContext.getExecutionCache().get(FlowExecutionContext.SECRET_CREDENTIAL));
    return deploymentTopologyDTO;
}
Also used : AbstractTask(alien4cloud.topology.task.AbstractTask) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) PreconfiguredInputsConfiguration(org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration) SecretCredentialInfo(org.alien4cloud.alm.deployment.configuration.model.SecretCredentialInfo) DeploymentSubstitutionConfiguration(alien4cloud.deployment.model.DeploymentSubstitutionConfiguration) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) OrchestratorDeploymentProperties(org.alien4cloud.alm.deployment.configuration.model.OrchestratorDeploymentProperties) TopologyValidationResult(alien4cloud.topology.TopologyValidationResult) LocationResourceTypes(alien4cloud.orchestrators.locations.services.LocationResourceTypes) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration)

Example 13 with DeploymentMatchingConfiguration

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration in project alien4cloud by alien4cloud.

the class AbstractPostMatchingSetupModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class, AbstractPostMatchingSetupModifier.class.getSimpleName());
    if (!configurationOptional.isPresent()) {
        // we should not end-up here as location matching should be processed first
        context.log().error(new LocationPolicyTask());
        return;
    }
    DeploymentMatchingConfiguration deploymentMatchingConfiguration = configurationOptional.get();
    Map<String, String> lastUserSubstitutions = getUserMatches(deploymentMatchingConfiguration);
    Map<String, NodePropsOverride> propertiesOverrides = getPropertiesOverrides(deploymentMatchingConfiguration);
    boolean configChanged = false;
    Iterator<Entry<String, NodePropsOverride>> propertiesOverrideIter = safe(propertiesOverrides).entrySet().iterator();
    while (propertiesOverrideIter.hasNext()) {
        Entry<String, NodePropsOverride> nodePropsOverrideEntry = propertiesOverrideIter.next();
        if (lastUserSubstitutions.containsKey(nodePropsOverrideEntry.getKey())) {
            // Merge the user overrides into the node.
            configChanged = mergeNode(topology, context, nodePropsOverrideEntry.getKey(), nodePropsOverrideEntry.getValue());
        } else {
            // This node is no more a matched node, remove from configuration
            configChanged = true;
            context.log().info("Definition of user properties for " + getSubject() + " [" + nodePropsOverrideEntry.getKey() + "] have been removed as the " + getSubject() + " is not available for matching anymore.");
            propertiesOverrideIter.remove();
        }
    }
    // If the configuration has changed then update it.
    if (configChanged) {
        context.saveConfiguration(deploymentMatchingConfiguration);
    }
}
Also used : Entry(java.util.Map.Entry) LocationPolicyTask(alien4cloud.topology.task.LocationPolicyTask) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride)

Example 14 with DeploymentMatchingConfiguration

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration 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 15 with DeploymentMatchingConfiguration

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration in project alien4cloud by alien4cloud.

the class AbstractMatchingConfigAutoSelectModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class, this.getClass().getSimpleName());
    if (!configurationOptional.isPresent()) {
        // we should not end-up here as location matching should be processed first
        context.log().error(new LocationPolicyTask());
        return;
    }
    DeploymentMatchingConfiguration matchingConfiguration = configurationOptional.get();
    Map<String, String> lastUserMatches = getLastUserMatches(matchingConfiguration);
    Map<String, List<T>> availableMatches = getAvailableMatches(context);
    // Last user substitution may be incomplete or not valid anymore so let's check them and eventually select default values
    Map<String, T> availableResourceTemplatesById = Maps.newHashMap();
    // map of nodeId -> location resource template ids required to create
    Map<String, Set<String>> resourceTemplatesByTemplateId = Maps.newHashMap();
    // historical deployment topology dto object
    for (Map.Entry<String, List<T>> entry : availableMatches.entrySet()) {
        // Fill locResTemplateIdsPerNodeIds
        Set<String> lrtIds = Sets.newHashSet();
        resourceTemplatesByTemplateId.put(entry.getKey(), lrtIds);
        // We leverage the loop to also create a map of resources by id for later usage.
        for (T lrt : entry.getValue()) {
            availableResourceTemplatesById.put(lrt.getId(), lrt);
            lrtIds.add(lrt.getId());
        }
        // select default values
        if (!lastUserMatches.containsKey(entry.getKey())) {
            if (entry.getValue().isEmpty()) {
                // report that no node has been found on the location with the topology criteria
                consumeNoMatchingFound(context, new NodeMatchingTask(entry.getKey()));
            } else {
                // Only take the first element as selected if no configuration has been set before
                // let an info so the user know that we made a default selection for him
                context.log().info("Automatic matching for template <" + entry.getKey() + ">");
                lastUserMatches.put(entry.getKey(), entry.getValue().iterator().next().getId());
            }
        }
    }
    // This is required for next modifier so we cache them here for performance reasons.
    context.getExecutionCache().put(getResourceTemplateByIdMapCacheKey(), availableResourceTemplatesById);
    context.getExecutionCache().put(getResourceTemplateByTemplateIdCacheKey(), resourceTemplatesByTemplateId);
    // matchingConfiguration.setMatchedPolicies(lastUserMatches);
    // TODO Do that only if updated...
    context.saveConfiguration(matchingConfiguration);
}
Also used : Set(java.util.Set) NodeMatchingTask(alien4cloud.topology.task.NodeMatchingTask) LocationPolicyTask(alien4cloud.topology.task.LocationPolicyTask) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) List(java.util.List) Map(java.util.Map)

Aggregations

DeploymentMatchingConfiguration (org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration)23 LocationPolicyTask (alien4cloud.topology.task.LocationPolicyTask)10 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)8 Map (java.util.Map)8 List (java.util.List)7 NotFoundException (alien4cloud.exception.NotFoundException)6 Location (alien4cloud.model.orchestrators.locations.Location)5 Set (java.util.Set)5 Topology (org.alien4cloud.tosca.model.templates.Topology)5 FlowExecutionContext (org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext)4 AlienUtils.safe (alien4cloud.utils.AlienUtils.safe)3 Maps (com.google.common.collect.Maps)3 Collectors (java.util.stream.Collectors)3 Inject (javax.inject.Inject)3 ITopologyModifier (org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier)3 NodeMatchingConfigAutoSelectModifier (org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.NodeMatchingConfigAutoSelectModifier)3 NodePropsOverride (org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride)3 OrchestratorDeploymentProperties (org.alien4cloud.alm.deployment.configuration.model.OrchestratorDeploymentProperties)3 Csar (org.alien4cloud.tosca.model.Csar)3 MapUtils (org.apache.commons.collections4.MapUtils)3