use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.
the class InputsModifier method process.
@Override
public void process(Topology topology, FlowExecutionContext context) {
EnvironmentContext environmentContext = context.getEnvironmentContext().orElseThrow(() -> new IllegalArgumentException("Input modifier requires an environment context."));
ApplicationEnvironment environment = environmentContext.getEnvironment();
DeploymentInputs deploymentInputs = context.getConfiguration(DeploymentInputs.class, InputsModifier.class.getSimpleName()).orElse(new DeploymentInputs(environment.getTopologyVersion(), environment.getId()));
if (deploymentInputs.getInputs() == null) {
deploymentInputs.setInputs(Maps.newHashMap());
}
Map<String, Location> locations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
Map<String, PropertyValue> applicationInputs = inputService.getAppContextualInputs(context.getEnvironmentContext().get().getApplication(), topology.getInputs());
Map<String, PropertyValue> locationInputs = inputService.getLocationContextualInputs(locations, topology.getInputs());
// If the initial topology or any modifier configuration has changed since last inputs update then we refresh inputs.
if (deploymentInputs.getLastUpdateDate() == null || deploymentInputs.getLastUpdateDate().before(context.getLastFlowParamUpdate())) {
// FIXME exclude the application and location provided inputs from this method as it process them...
boolean updated = deploymentInputService.synchronizeInputs(topology.getInputs(), deploymentInputs.getInputs());
if (updated) {
// save the config if changed. This is for ex, if an input has been deleted from the topology
context.saveConfiguration(deploymentInputs);
}
}
PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = context.getConfiguration(PreconfiguredInputsConfiguration.class, InputsModifier.class.getSimpleName()).orElseThrow(() -> new IllegalStateException("PreconfiguredInputsConfiguration must be in the context"));
Map<String, AbstractPropertyValue> inputValues = Maps.newHashMap(deploymentInputs.getInputs());
inputValues.putAll(applicationInputs);
inputValues.putAll(locationInputs);
inputValues.putAll(preconfiguredInputsConfiguration.getInputs());
// Now that we have inputs ready let's process get_input functions in the topology to actually replace values.
if (topology.getNodeTemplates() != null) {
FunctionEvaluatorContext evaluatorContext = new FunctionEvaluatorContext(topology, inputValues);
for (Entry<String, NodeTemplate> entry : topology.getNodeTemplates().entrySet()) {
NodeTemplate nodeTemplate = entry.getValue();
processGetInput(evaluatorContext, nodeTemplate, nodeTemplate.getProperties());
if (nodeTemplate.getRelationships() != null) {
for (Entry<String, RelationshipTemplate> relEntry : nodeTemplate.getRelationships().entrySet()) {
RelationshipTemplate relationshipTemplate = relEntry.getValue();
processGetInput(evaluatorContext, relationshipTemplate, relationshipTemplate.getProperties());
}
}
if (nodeTemplate.getCapabilities() != null) {
for (Entry<String, Capability> capaEntry : nodeTemplate.getCapabilities().entrySet()) {
Capability capability = capaEntry.getValue();
processGetInput(evaluatorContext, nodeTemplate, capability.getProperties());
}
}
if (nodeTemplate.getRequirements() != null) {
for (Entry<String, Requirement> requirementEntry : nodeTemplate.getRequirements().entrySet()) {
Requirement requirement = requirementEntry.getValue();
processGetInput(evaluatorContext, nodeTemplate, requirement.getProperties());
}
}
}
}
}
use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.
the class LocationPolicyValidationService method validateLocationPolicies.
public List<LocationPolicyTask> validateLocationPolicies(DeploymentMatchingConfiguration matchingConfiguration) {
List<LocationPolicyTask> tasks = Lists.newArrayList();
Location location = null;
Orchestrator orchestrator = null;
// TODO change this later, as now we only support one location policy and only for _A4C_ALL group
String locationId = safe(matchingConfiguration.getLocationIds()).get(AlienConstants.GROUP_ALL);
if (StringUtils.isBlank(locationId)) {
tasks.add(new LocationPolicyTask());
} else {
location = locationService.getOrFail(locationId);
orchestrator = orchestratorService.getOrFail(location.getOrchestratorId());
try {
// if a location already exists, then check the rigths on it
locationSecurityService.checkAuthorisation(location, matchingConfiguration.getEnvironmentId());
if (!Objects.equals(orchestrator.getState(), OrchestratorState.CONNECTED)) {
UnavailableLocationTask task = new UnavailableLocationTask(location.getName(), orchestrator.getName());
task.setCode(TaskCode.LOCATION_DISABLED);
tasks.add(task);
}
} catch (AccessDeniedException e) {
UnavailableLocationTask task = new UnavailableLocationTask(location.getName(), orchestrator.getName());
task.setCode(TaskCode.LOCATION_UNAUTHORIZED);
tasks.add(task);
}
}
return tasks;
}
use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.
the class LocationMatchingModifier method processLocationMatching.
private void processLocationMatching(Topology topology, FlowExecutionContext context) {
// The configuration has already been loaded by a previous topology modifier.
Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class, LocationMatchingModifier.class.getSimpleName());
if (!configurationOptional.isPresent()) {
return;
}
DeploymentMatchingConfiguration matchingConfiguration = configurationOptional.get();
// If some of the locations defined does not exist anymore then just reset the deployment matching configuration
Map<String, String> locationIds = matchingConfiguration.getLocationIds();
if (MapUtils.isEmpty(locationIds)) {
return;
}
// returns null if at least one of the expected location is missing.
Map<String, Location> locations = getLocations(locationIds);
if (locations == null) {
// TODO Add an info log to explain that previous location does not exist anymore
// Reset and save the configuration
resetMatchingConfiguration(context);
return;
}
context.getExecutionCache().put(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY, locations);
// Now we must check that the selected locations are still valid choices for deployment
// Somehow if the initial topology and none of the previous modifiers had changed then we could assume that the choice is still valid.
// We had an approximation for that in the past that was not correct enough as we checked only initial topology and location. Actually inputs and
// potential snapshot substitution merged through composition may also impact this. We know always re-process location matching here.
// We have to fetch valid location matches anyway to know if the location is a potential valid match.
List<ILocationMatch> locationMatches = locationMatchingService.match(topology, context.getEnvironmentContext().get().getEnvironment());
context.getExecutionCache().put(FlowExecutionContext.LOCATION_MATCH_CACHE_KEY, locationMatches);
Map<String, ILocationMatch> locationMatchMap = Maps.newHashMap();
// Check that current choices exist in actual location matches
for (ILocationMatch match : safe(locationMatches)) {
locationMatchMap.put(match.getLocation().getId(), match);
}
for (String locationId : locationIds.values()) {
if (!locationMatchMap.containsKey(locationId)) {
// A matched location is not a valid choice anymore.
resetMatchingConfiguration(context);
// TODO info log the reason why the location is no more a valid match
return;
}
}
// update the TOSCA context with the new dependencies so that next step runs with an up-to-date context
ToscaContext.get().resetDependencies(topology.getDependencies());
}
use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.
the class LocationMatchingModifier method getLocations.
/**
* Get location map from the deployment topology
*
* @param locationIds map of group id to location id
* @return map of location group id to location or null if at least one of the expected locations is missing.
*/
private Map<String, Location> getLocations(Map<String, String> locationIds) {
Map<String, Location> locations = locationService.getMultiple(locationIds.values());
Map<String, Location> locationMap = Maps.newHashMap();
for (Map.Entry<String, String> locationIdsEntry : locationIds.entrySet()) {
Location location = locations.get(locationIdsEntry.getValue());
if (location == null) {
return null;
}
locationMap.put(locationIdsEntry.getKey(), location);
}
return locationMap;
}
use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.
the class DeployService method deploy.
/**
* Deploy a topology and return the deployment ID.
*
* @param deploymentTopology Location aware and matched topology.
* @param deploymentSource Application to be deployed or the Csar that contains test toplogy to be deployed
* @return The id of the generated deployment.
*/
public String deploy(final User deployer, final SecretProviderCredentials secretProviderCredentials, final DeploymentTopology deploymentTopology, IDeploymentSource deploymentSource) {
Map<String, String> locationIds = TopologyLocationUtils.getLocationIds(deploymentTopology);
Map<String, Location> locations = deploymentTopologyService.getLocations(locationIds);
final Location firstLocation = locations.values().iterator().next();
String deploymentPaaSId = generateOrchestratorDeploymentId(deploymentTopology.getEnvironmentId(), firstLocation.getOrchestratorId());
return deploymentLockService.doWithDeploymentWriteLock(deploymentPaaSId, () -> {
// Get the orchestrator that will perform the deployment
IOrchestratorPlugin orchestratorPlugin = orchestratorPluginService.getOrFail(firstLocation.getOrchestratorId());
// Create a deployment object to be kept in ES.
final Deployment deployment = new Deployment();
deployment.setId(UUID.randomUUID().toString());
deployment.setOrchestratorId(firstLocation.getOrchestratorId());
deployment.setLocationIds(locationIds.values().toArray(new String[locationIds.size()]));
deployment.setOrchestratorDeploymentId(deploymentPaaSId);
deployment.setSourceId(deploymentSource.getId());
deployment.setDeployerUsername(deployer.getUsername());
String sourceName;
if (deploymentSource.getName() == null) {
sourceName = UUID.randomUUID().toString();
} else {
sourceName = deploymentSource.getName();
}
deployment.setSourceName(sourceName);
deployment.setSourceType(DeploymentSourceType.fromSourceType(deploymentSource.getClass()));
// mandatory for the moment since we could have deployment with no environment (csar test)
deployment.setEnvironmentId(deploymentTopology.getEnvironmentId());
deployment.setVersionId(deploymentTopology.getVersionId());
deployment.setStartDate(new Date());
setUsedServicesResourcesIds(deploymentTopology, deployment);
alienDao.save(deployment);
// publish an event for the eventual managed service
eventPublisher.publishEvent(new DeploymentCreatedEvent(this, deployment.getId()));
PaaSTopologyDeploymentContext deploymentContext = saveDeploymentTopologyAndGenerateDeploymentContext(secretProviderCredentials, deploymentTopology, deployment, locations);
// Build the context for deployment and deploy
orchestratorPlugin.deploy(deploymentContext, new IPaaSCallback<Object>() {
@Override
public void onSuccess(Object data) {
log.debug("Deployed topology [{}] on location [{}], generated deployment with id [{}]", deploymentTopology.getInitialTopologyId(), firstLocation.getId(), deployment.getId());
}
@Override
public void onFailure(Throwable t) {
log.error("Deployment failed with cause", t);
log(deployment, t);
}
});
log.debug("Triggered deployment of topology [{}] on location [{}], generated deployment with id [{}]", deploymentTopology.getInitialTopologyId(), firstLocation.getId(), deployment.getId());
return deployment.getId();
});
}
Aggregations