use of org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext in project alien4cloud by alien4cloud.
the class PolicyMatchingSubstitutionService method updateSubstitution.
/**
* Execute the deployment flow with a modification of changing the substitution for one of the nodes.
*
* @param application The application for which to execute the deployment flow.
* @param environment The environment for which to execute the deployment flow.
* @param topology The topology linked to the specified environment.
* @param nodeId The id of the node to substitute at matching phase.
* @param resourceTemplateId The id of the location resource to substitute.
* @return The flow execution context.
*/
public FlowExecutionContext updateSubstitution(Application application, ApplicationEnvironment environment, Topology topology, String nodeId, String resourceTemplateId) {
FlowExecutionContext executionContext = new FlowExecutionContext(deploymentConfigurationDao, topology, new EnvironmentContext(application, environment));
// Load the actual configuration
// add a modifier that will actually perform the configuration of a substitution from user request (after cleanup and prior to node matching
// auto-selection).
SetMatchedPolicyModifier setMatchedPolicyModifier = new SetMatchedPolicyModifier(nodeId, resourceTemplateId);
List<ITopologyModifier> modifierList = getModifierListWithSelectionAction(setMatchedPolicyModifier);
flowExecutor.execute(topology, modifierList, executionContext);
if (!setMatchedPolicyModifier.isExecuted()) {
throw new NotFoundException("Requested substitution <" + resourceTemplateId + "> for node <" + nodeId + "> is not available as a match. Please check that inputs and location are configured and ask your admin to grant you access to requested resources on the location.");
}
return executionContext;
}
use of org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext 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.flow.EnvironmentContext 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);
}
Aggregations