Search in sources :

Example 66 with InstanceGroup

use of com.sequenceiq.cloudbreak.domain.InstanceGroup in project cloudbreak by hortonworks.

the class ServiceProviderMetadataAdapter method getState.

public InstanceSyncState getState(Stack stack, InstanceGroup instanceGroup, String instanceId) {
    Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone()));
    CloudContext cloudContext = new CloudContext(stack.getId(), stack.getName(), stack.cloudPlatform(), stack.getOwner(), stack.getPlatformVariant(), location);
    CloudCredential cloudCredential = credentialConverter.convert(stack.getCredential());
    InstanceGroup ig = stack.getInstanceGroupByInstanceGroupName(instanceGroup.getGroupName());
    CloudInstance instance = null;
    for (InstanceMetaData metaData : ig.getAllInstanceMetaData()) {
        if (instanceId.equalsIgnoreCase(metaData.getInstanceId())) {
            instance = metadataConverter.convert(metaData);
            break;
        }
    }
    if (instance != null) {
        GetInstancesStateRequest<GetInstancesStateResult> stateRequest = new GetInstancesStateRequest<>(cloudContext, cloudCredential, Collections.singletonList(instance));
        LOGGER.info("Triggering event: {}", stateRequest);
        eventBus.notify(stateRequest.selector(), eventFactory.createEvent(stateRequest));
        try {
            GetInstancesStateResult res = stateRequest.await();
            LOGGER.info("Result: {}", res);
            if (res.isFailed()) {
                LOGGER.error("Failed to retrieve instance state", res.getErrorDetails());
                throw new OperationException(res.getErrorDetails());
            }
            return transform(res.getStatuses().get(0).getStatus());
        } catch (InterruptedException e) {
            LOGGER.error(format("Error while retrieving instance state of: %s", cloudContext), e);
            throw new OperationException(e);
        }
    } else {
        return InstanceSyncState.DELETED;
    }
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) GetInstancesStateRequest(com.sequenceiq.cloudbreak.cloud.event.resource.GetInstancesStateRequest) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) GetInstancesStateResult(com.sequenceiq.cloudbreak.cloud.event.resource.GetInstancesStateResult) OperationException(com.sequenceiq.cloudbreak.service.stack.connector.OperationException) Location(com.sequenceiq.cloudbreak.cloud.model.Location) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup)

Example 67 with InstanceGroup

use of com.sequenceiq.cloudbreak.domain.InstanceGroup in project cloudbreak by hortonworks.

the class MetadataSetupService method saveInstanceMetaData.

public int saveInstanceMetaData(Stack stack, Iterable<CloudVmMetaDataStatus> cloudVmMetaDataStatusList, InstanceStatus status) {
    int newInstances = 0;
    boolean ambariServerFound = false;
    Set<InstanceMetaData> allInstanceMetadata = instanceMetaDataRepository.findNotTerminatedForStack(stack.getId());
    boolean primaryIgSelected = allInstanceMetadata.stream().anyMatch(imd -> imd.getInstanceMetadataType() == InstanceMetadataType.GATEWAY_PRIMARY);
    for (CloudVmMetaDataStatus cloudVmMetaDataStatus : cloudVmMetaDataStatusList) {
        CloudInstance cloudInstance = cloudVmMetaDataStatus.getCloudVmInstanceStatus().getCloudInstance();
        CloudInstanceMetaData md = cloudVmMetaDataStatus.getMetaData();
        long timeInMillis = Calendar.getInstance().getTimeInMillis();
        Long privateId = cloudInstance.getTemplate().getPrivateId();
        String instanceId = cloudInstance.getInstanceId();
        InstanceMetaData instanceMetaDataEntry = createInstanceMetadataIfAbsent(allInstanceMetadata, privateId, instanceId);
        if (instanceMetaDataEntry.getInstanceId() == null) {
            newInstances++;
        }
        // CB 1.0.x clusters do not have private id thus we cannot correlate them with instance groups thus keep the original one
        InstanceGroup ig = instanceMetaDataEntry.getInstanceGroup();
        String group = ig == null ? cloudInstance.getTemplate().getGroupName() : ig.getGroupName();
        InstanceGroup instanceGroup = instanceGroupRepository.findOneByGroupNameInStack(stack.getId(), group);
        instanceMetaDataEntry.setPrivateIp(md.getPrivateIp());
        instanceMetaDataEntry.setPublicIp(md.getPublicIp());
        instanceMetaDataEntry.setSshPort(md.getSshPort());
        instanceMetaDataEntry.setLocalityIndicator(md.getLocalityIndicator());
        instanceMetaDataEntry.setInstanceGroup(instanceGroup);
        instanceMetaDataEntry.setInstanceId(instanceId);
        instanceMetaDataEntry.setPrivateId(privateId);
        instanceMetaDataEntry.setStartDate(timeInMillis);
        instanceMetaDataEntry.setSubnetId(cloudInstance.getStringParameter(CloudInstance.SUBNET_ID));
        if (instanceMetaDataEntry.getInstanceMetadataType() == null) {
            if (ig != null) {
                if (InstanceGroupType.GATEWAY.equals(ig.getInstanceGroupType())) {
                    if (!primaryIgSelected) {
                        primaryIgSelected = true;
                        instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY_PRIMARY);
                    } else {
                        instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY);
                    }
                } else {
                    instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE);
                }
            } else {
                instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE);
            }
        }
        if (!ambariServerFound && InstanceGroupType.GATEWAY.equals(instanceGroup.getInstanceGroupType())) {
            instanceMetaDataEntry.setAmbariServer(Boolean.TRUE);
            ambariServerFound = true;
        } else {
            instanceMetaDataEntry.setAmbariServer(Boolean.FALSE);
        }
        if (status != null) {
            instanceMetaDataEntry.setInstanceStatus(status);
        }
        instanceMetaDataRepository.save(instanceMetaDataEntry);
    }
    return newInstances;
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup)

Example 68 with InstanceGroup

use of com.sequenceiq.cloudbreak.domain.InstanceGroup in project cloudbreak by hortonworks.

the class StackSyncService method updateMetaDataToTerminated.

private void updateMetaDataToTerminated(Stack stack, InstanceMetaData instanceMetaData) {
    InstanceGroup instanceGroup = instanceMetaData.getInstanceGroup();
    List<InstanceMetaData> aliveInstancesInInstanceGroup = instanceMetaDataRepository.findAliveInstancesInInstanceGroup(instanceGroup.getId());
    instanceGroup.setNodeCount(aliveInstancesInInstanceGroup.size() - 1);
    long timeInMillis = Calendar.getInstance().getTimeInMillis();
    instanceMetaData.setTerminationDate(timeInMillis);
    instanceMetaData.setInstanceStatus(InstanceStatus.TERMINATED);
    instanceMetaDataRepository.save(instanceMetaData);
    instanceGroupRepository.save(instanceGroup);
    String name;
    name = instanceMetaData.getDiscoveryFQDN() == null ? instanceMetaData.getInstanceId() : String.format("%s (%s)", instanceMetaData.getInstanceId(), instanceMetaData.getDiscoveryFQDN());
    eventService.fireCloudbreakEvent(stack.getId(), AVAILABLE.name(), cloudbreakMessagesService.getMessage(Msg.STACK_SYNC_INSTANCE_DELETED_CBMETADATA.code(), Collections.singletonList(name)));
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup)

Example 69 with InstanceGroup

use of com.sequenceiq.cloudbreak.domain.InstanceGroup in project cloudbreak by hortonworks.

the class StackSyncService method sync.

private void sync(Stack stack, boolean stackStatusUpdateEnabled) {
    Long stackId = stack.getId();
    Set<InstanceMetaData> instances = instanceMetaDataRepository.findNotTerminatedForStack(stackId);
    Map<InstanceSyncState, Integer> instanceStateCounts = initInstanceStateCounts();
    for (InstanceMetaData instance : instances) {
        InstanceGroup instanceGroup = instance.getInstanceGroup();
        try {
            InstanceSyncState state = metadata.getState(stack, instanceGroup, instance.getInstanceId());
            syncInstanceStatusByState(stack, instanceStateCounts, instance, state);
        } catch (CloudConnectorException e) {
            LOGGER.warn(e.getMessage(), e);
            eventService.fireCloudbreakEvent(stackId, AVAILABLE.name(), cloudbreakMessagesService.getMessage(Msg.STACK_SYNC_INSTANCE_STATUS_RETRIEVAL_FAILED.code(), Collections.singletonList(instance.getInstanceId())));
            instanceStateCounts.put(InstanceSyncState.UNKNOWN, instanceStateCounts.get(InstanceSyncState.UNKNOWN) + 1);
        }
    }
    handleSyncResult(stack, instanceStateCounts, stackStatusUpdateEnabled);
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup)

Example 70 with InstanceGroup

use of com.sequenceiq.cloudbreak.domain.InstanceGroup in project cloudbreak by hortonworks.

the class TerminationService method terminateInstanceGroups.

private void terminateInstanceGroups(Stack stack) {
    for (InstanceGroup instanceGroup : stack.getInstanceGroups()) {
        instanceGroup.setSecurityGroup(null);
        instanceGroup.setTemplate(null);
        instanceGroupRepository.save(instanceGroup);
    }
}
Also used : InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup)

Aggregations

InstanceGroup (com.sequenceiq.cloudbreak.domain.InstanceGroup)89 Test (org.junit.Test)32 Blueprint (com.sequenceiq.cloudbreak.domain.Blueprint)27 HostGroup (com.sequenceiq.cloudbreak.domain.HostGroup)27 InstanceMetaData (com.sequenceiq.cloudbreak.domain.InstanceMetaData)24 HashSet (java.util.HashSet)16 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 Stack (com.sequenceiq.cloudbreak.domain.Stack)14 Cluster (com.sequenceiq.cloudbreak.domain.Cluster)9 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)8 BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)8 Constraint (com.sequenceiq.cloudbreak.domain.Constraint)8 Matchers.anyString (org.mockito.Matchers.anyString)8 Orchestrator (com.sequenceiq.cloudbreak.domain.Orchestrator)6 StackAuthentication (com.sequenceiq.cloudbreak.domain.StackAuthentication)6 Template (com.sequenceiq.cloudbreak.domain.Template)6 ArrayList (java.util.ArrayList)6 InstanceGroupRequest (com.sequenceiq.cloudbreak.api.model.InstanceGroupRequest)5 StackAuthenticationRequest (com.sequenceiq.cloudbreak.api.model.StackAuthenticationRequest)5 FailurePolicy (com.sequenceiq.cloudbreak.domain.FailurePolicy)5