use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup in project cloudbreak by hortonworks.
the class RegisterPublicDnsHandler method doAccept.
@Override
protected Selectable doAccept(HandlerEvent<RegisterPublicDnsRequest> event) {
RegisterPublicDnsRequest request = event.getData();
Stack stack = request.getStack();
if (gatewayPublicEndpointManagementService.isCertRenewalTriggerable(stack)) {
try {
LOGGER.debug("Fetching instance group and instance metadata for stack.");
InstanceGroup instanceGroup = instanceGroupService.getPrimaryGatewayInstanceGroupByStackId(stack.getId());
List<InstanceMetaData> instanceMetaData = instanceMetaDataService.findAliveInstancesInInstanceGroup(instanceGroup.getId());
if (!instanceMetaData.isEmpty()) {
stack.getInstanceGroups().stream().filter(ig -> ig.getId().equals(instanceGroup.getId())).forEach(ig -> ig.setInstanceMetaData(Set.copyOf(instanceMetaData)));
LOGGER.debug("Registering load balancer public DNS entry");
boolean success = clusterPublicEndpointManagementService.provisionLoadBalancer(stack);
if (!success) {
throw new CloudbreakException("Public DNS registration resulted in failed state. Please consult DNS registration logs.");
}
LOGGER.debug("Load balancer public DNS registration was successful");
return new RegisterPublicDnsSuccess(stack);
} else {
throw new CloudbreakException("Unable to find instance metadata for primary instance group. Certificates cannot " + "be updated.");
}
} catch (Exception e) {
LOGGER.warn("Failed to register load balancer public DNS entries.", e);
return new RegisterPublicDnsFailure(request.getResourceId(), e);
}
} else {
LOGGER.info("Certificates and DNS are not managed by PEM for stack {}. Skipping public DNS registration.", stack.getName());
return new RegisterPublicDnsSuccess(stack);
}
}
use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup in project cloudbreak by hortonworks.
the class CreateLoadBalancerEntityHandler method doAccept.
@Override
protected Selectable doAccept(HandlerEvent<CreateLoadBalancerEntityRequest> event) {
CreateLoadBalancerEntityRequest request = event.getData();
Stack stack = stackService.getById(request.getResourceId());
try {
LOGGER.debug("Creating load balancer entity objects for stack {}", stack.getId());
DetailedEnvironmentResponse environment = environmentClientService.getByCrn(stack.getEnvironmentCrn());
if (environment != null && environment.getNetwork() != null && PublicEndpointAccessGateway.ENABLED.equals(environment.getNetwork().getPublicEndpointAccessGateway())) {
enableEndpointGateway(stack, environment);
}
Set<InstanceGroup> instanceGroups = instanceGroupService.getByStackAndFetchTemplates(stack.getId());
instanceGroups.forEach(ig -> ig.setTargetGroups(targetGroupPersistenceService.findByInstanceGroupId(ig.getId())));
Set<LoadBalancer> existingLoadBalancers = loadBalancerPersistenceService.findByStackId(stack.getId());
stack.setInstanceGroups(instanceGroups);
Set<LoadBalancer> newLoadBalancers = loadBalancerConfigService.createLoadBalancers(stack, environment, null);
Stack savedStack;
if (doLoadBalancersAlreadyExist(existingLoadBalancers, newLoadBalancers)) {
LOGGER.debug("Load balancer entities already exist. Continuing flow.");
savedStack = stack;
} else {
LOGGER.debug("Persisting stack and load balancer objects to database.");
stack.setLoadBalancers(newLoadBalancers);
String stackName = stack.getName();
savedStack = measure(() -> stackService.save(stack), LOGGER, "Stackrepository save took {} ms for stack {}", stackName);
measure(() -> loadBalancerPersistenceService.saveAll(newLoadBalancers), LOGGER, "Load balancers saved in {} ms for stack {}", stackName);
measure(() -> targetGroupPersistenceService.saveAll(newLoadBalancers.stream().flatMap(lb -> lb.getTargetGroupSet().stream()).collect(Collectors.toSet())), LOGGER, "Target groups saved in {} ms for stack {}", stackName);
measure(() -> instanceGroupService.saveAll(newLoadBalancers.stream().flatMap(lb -> lb.getAllInstanceGroups().stream()).collect(Collectors.toSet())), LOGGER, "Instance groups saved in {} ms for stack {}", stackName);
if (stack.getNetwork() != null) {
measure(() -> networkService.pureSave(stack.getNetwork()), LOGGER, "Network saved in {} ms for stack {}", stackName);
}
}
LOGGER.debug("Load balancer entities successfully persisted.");
savedStack.setInstanceGroups(newLoadBalancers.stream().flatMap(lb -> lb.getAllInstanceGroups().stream()).collect(Collectors.toSet()));
savedStack.setNetwork(stack.getNetwork());
return new CreateLoadBalancerEntitySuccess(request.getResourceId(), savedStack);
} catch (Exception e) {
LOGGER.warn("Failed create load balancer entities and persist them to the database.", e);
return new CreateLoadBalancerEntityFailure(request.getResourceId(), e);
}
}
use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup in project cloudbreak by hortonworks.
the class UpdateHostsValidator method validateRegisteredHosts.
private void validateRegisteredHosts(Stack stack, HostGroupAdjustmentV4Request hostGroupAdjustment) {
String hostGroupName = hostGroupAdjustment.getHostGroup();
hostGroupService.getByClusterIdAndName(stack.getCluster().getId(), hostGroupName).ifPresentOrElse(hostGroup -> {
if (hostGroup.getInstanceGroup() == null) {
throw new BadRequestException(String.format("Can't find instancegroup for hostgroup: %s", hostGroupName));
} else {
InstanceGroup instanceGroup = hostGroup.getInstanceGroup();
int hostsCount = instanceGroup.getNotDeletedAndNotZombieInstanceMetaDataSet().size();
int adjustment = Math.abs(hostGroupAdjustment.getScalingAdjustment());
Boolean validateNodeCount = hostGroupAdjustment.getValidateNodeCount();
if (validateNodeCount == null || validateNodeCount) {
if (hostsCount <= adjustment) {
String errorMessage = String.format("[hostGroup: '%s', current hosts: %s, decommissions requested: %s]", hostGroupName, hostsCount, adjustment);
throw new BadRequestException(String.format("The host group must contain at least 1 host after the decommission: %s", errorMessage));
}
} else if (hostsCount - adjustment < 0) {
throw new BadRequestException(String.format("There are not enough hosts in host group: %s to remove", hostGroupName));
}
}
}, () -> {
throw new BadRequestException(String.format("Can't find hostgroup: %s", hostGroupName));
});
}
use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup in project cloudbreak by hortonworks.
the class LoadBalancerConfigService method setupKnoxTargetGroup.
private Optional<TargetGroup> setupKnoxTargetGroup(Stack stack, boolean dryRun) {
TargetGroup knoxTargetGroup = null;
Set<String> knoxGatewayGroupNames = getKnoxGatewayGroups(stack);
Set<InstanceGroup> knoxGatewayInstanceGroups = stack.getInstanceGroups().stream().filter(ig -> knoxGatewayGroupNames.contains(ig.getGroupName())).collect(Collectors.toSet());
if (AZURE.equalsIgnoreCase(stack.getCloudPlatform()) && knoxGatewayInstanceGroups.size() > 1) {
throw new CloudbreakServiceException("For Azure load balancers, Knox must be defined in a single instance group.");
} else if (!knoxGatewayInstanceGroups.isEmpty()) {
LOGGER.info("Knox gateway instance found; enabling Knox load balancer configuration.");
knoxTargetGroup = new TargetGroup();
knoxTargetGroup.setType(TargetGroupType.KNOX);
knoxTargetGroup.setInstanceGroups(knoxGatewayInstanceGroups);
if (!dryRun) {
LOGGER.debug("Adding target group to Knox gateway instances groups.");
TargetGroup finalKnoxTargetGroup = knoxTargetGroup;
knoxGatewayInstanceGroups.forEach(ig -> ig.addTargetGroup(finalKnoxTargetGroup));
} else {
LOGGER.debug("Dry run, skipping instance group/target group linkage.");
}
}
return Optional.ofNullable(knoxTargetGroup);
}
use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup in project cloudbreak by hortonworks.
the class LoadBalancerConfigService method getKnoxGatewayGroups.
public Set<String> getKnoxGatewayGroups(Stack stack) {
LOGGER.debug("Fetching list of instance groups with Knox gateway installed");
Set<String> groupNames = new HashSet<>();
Cluster cluster = stack.getCluster();
if (cluster != null) {
LOGGER.debug("Checking if Knox gateway is explicitly defined");
CmTemplateProcessor cmTemplateProcessor = new CmTemplateProcessor(cluster.getBlueprint().getBlueprintText());
groupNames = cmTemplateProcessor.getHostGroupsWithComponent(KnoxRoles.KNOX_GATEWAY);
}
if (groupNames.isEmpty()) {
LOGGER.debug("Knox gateway is not explicitly defined; searching for CM gateway hosts");
groupNames = stack.getInstanceGroups().stream().filter(i -> InstanceGroupType.isGateway(i.getInstanceGroupType())).map(InstanceGroup::getGroupName).collect(Collectors.toSet());
}
if (groupNames.isEmpty()) {
LOGGER.info("No Knox gateway instance groups found");
}
return groupNames;
}
Aggregations