Search in sources :

Example 61 with InstanceMetaData

use of com.sequenceiq.cloudbreak.domain.InstanceMetaData 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 62 with InstanceMetaData

use of com.sequenceiq.cloudbreak.domain.InstanceMetaData 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 63 with InstanceMetaData

use of com.sequenceiq.cloudbreak.domain.InstanceMetaData 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 64 with InstanceMetaData

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

the class StackSyncService method updateInstances.

public void updateInstances(Stack stack, Iterable<InstanceMetaData> instanceMetaDataList, Collection<CloudVmInstanceStatus> instanceStatuses, boolean stackStatusUpdateEnabled) {
    Map<InstanceSyncState, Integer> counts = initInstanceStateCounts();
    for (InstanceMetaData metaData : instanceMetaDataList) {
        Optional<CloudVmInstanceStatus> status = instanceStatuses.stream().filter(is -> is != null && is.getCloudInstance().getInstanceId() != null && is.getCloudInstance().getInstanceId().equals(metaData.getInstanceId())).findFirst();
        InstanceSyncState state = status.isPresent() ? transform(status.get().getStatus()) : InstanceSyncState.DELETED;
        syncInstanceStatusByState(stack, counts, metaData, state);
    }
    handleSyncResult(stack, counts, stackStatusUpdateEnabled);
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) Arrays(java.util.Arrays) Resource(com.sequenceiq.cloudbreak.domain.Resource) InstanceMetaDataRepository(com.sequenceiq.cloudbreak.repository.InstanceMetaDataRepository) ResourceRepository(com.sequenceiq.cloudbreak.repository.ResourceRepository) LoggerFactory(org.slf4j.LoggerFactory) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup) CloudbreakMessagesService(com.sequenceiq.cloudbreak.service.messages.CloudbreakMessagesService) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) HostMetadata(com.sequenceiq.cloudbreak.domain.HostMetadata) WAIT_FOR_SYNC(com.sequenceiq.cloudbreak.api.model.Status.WAIT_FOR_SYNC) Calendar(java.util.Calendar) Service(org.springframework.stereotype.Service) Map(java.util.Map) Stack(com.sequenceiq.cloudbreak.domain.Stack) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) HostMetadataState(com.sequenceiq.cloudbreak.common.type.HostMetadataState) AmbariClusterConnector(com.sequenceiq.cloudbreak.service.cluster.ambari.AmbariClusterConnector) AmbariDecommissioner(com.sequenceiq.cloudbreak.service.cluster.ambari.AmbariDecommissioner) HostMetadataRepository(com.sequenceiq.cloudbreak.repository.HostMetadataRepository) ServiceProviderMetadataAdapter(com.sequenceiq.cloudbreak.service.stack.connector.adapter.ServiceProviderMetadataAdapter) Logger(org.slf4j.Logger) InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) AVAILABLE(com.sequenceiq.cloudbreak.api.model.Status.AVAILABLE) CREATE_FAILED(com.sequenceiq.cloudbreak.api.model.Status.CREATE_FAILED) EnumMap(java.util.EnumMap) DetailedStackStatus(com.sequenceiq.cloudbreak.api.model.DetailedStackStatus) Collection(java.util.Collection) InstanceGroupRepository(com.sequenceiq.cloudbreak.repository.InstanceGroupRepository) Set(java.util.Set) DELETE_FAILED(com.sequenceiq.cloudbreak.api.model.Status.DELETE_FAILED) NotFoundException(com.sequenceiq.cloudbreak.controller.NotFoundException) Cluster(com.sequenceiq.cloudbreak.domain.Cluster) StackUpdater(com.sequenceiq.cloudbreak.repository.StackUpdater) List(java.util.List) STOPPED(com.sequenceiq.cloudbreak.api.model.Status.STOPPED) InstanceStatus(com.sequenceiq.cloudbreak.api.model.InstanceStatus) Optional(java.util.Optional) Collections(java.util.Collections) StackService(com.sequenceiq.cloudbreak.service.stack.StackService) CloudbreakEventService(com.sequenceiq.cloudbreak.service.events.CloudbreakEventService) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)

Example 65 with InstanceMetaData

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

the class StackResponseHardwareInfoProvider method providerEntriesToStackResponse.

@Override
public StackResponse providerEntriesToStackResponse(Stack stack, StackResponse stackResponse) {
    Set<HardwareInfoResponse> hardwareInfoResponses = new HashSet<>();
    for (InstanceGroup instanceGroup : stack.getInstanceGroups()) {
        for (InstanceMetaData instanceMetaData : instanceGroup.getAllInstanceMetaData()) {
            HostMetadata hostMetadata = null;
            if (stack.getCluster() != null && instanceMetaData.getDiscoveryFQDN() != null) {
                hostMetadata = hostMetadataRepository.findHostInClusterByName(stack.getCluster().getId(), instanceMetaData.getDiscoveryFQDN());
            }
            HardwareInfoResponse hardwareInfoResponse = new HardwareInfoResponse();
            hardwareInfoResponse.setInstanceMetaData(conversionService.convert(instanceMetaData, InstanceMetaDataJson.class));
            hardwareInfoResponse.setHostMetadata(conversionService.convert(hostMetadata, HostMetadataResponse.class));
            hardwareInfoResponses.add(hardwareInfoResponse);
        }
    }
    stackResponse.setHardwareInfos(hardwareInfoResponses);
    return stackResponse;
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) HostMetadataResponse(com.sequenceiq.cloudbreak.api.model.HostMetadataResponse) InstanceMetaDataJson(com.sequenceiq.cloudbreak.api.model.InstanceMetaDataJson) HardwareInfoResponse(com.sequenceiq.cloudbreak.api.model.HardwareInfoResponse) HashSet(java.util.HashSet) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup) HostMetadata(com.sequenceiq.cloudbreak.domain.HostMetadata)

Aggregations

InstanceMetaData (com.sequenceiq.cloudbreak.domain.InstanceMetaData)71 InstanceGroup (com.sequenceiq.cloudbreak.domain.InstanceGroup)24 Stack (com.sequenceiq.cloudbreak.domain.Stack)23 ArrayList (java.util.ArrayList)18 HashSet (java.util.HashSet)17 Map (java.util.Map)16 HostMetadata (com.sequenceiq.cloudbreak.domain.HostMetadata)14 Test (org.junit.Test)14 HashMap (java.util.HashMap)13 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)12 Cluster (com.sequenceiq.cloudbreak.domain.Cluster)12 HostGroup (com.sequenceiq.cloudbreak.domain.HostGroup)11 GatewayConfig (com.sequenceiq.cloudbreak.orchestrator.model.GatewayConfig)9 CloudbreakException (com.sequenceiq.cloudbreak.service.CloudbreakException)9 List (java.util.List)9 Set (java.util.Set)9 Inject (javax.inject.Inject)8 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)7 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)7 Matchers.anyString (org.mockito.Matchers.anyString)7