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);
}
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;
}
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);
}
}
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);
}
}
}
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);
}
Aggregations