Search in sources :

Example 16 with CloudVmInstanceStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus in project cloudbreak by hortonworks.

the class UnhealthyInstancesFinalizerTest method shouldFinalizeInstancesThatAreNotFound.

@Test
public void shouldFinalizeInstancesThatAreNotFound() {
    Stack stack = TestUtil.stack(Status.AVAILABLE, TestUtil.awsCredential());
    CloudCredential cloudCredential = mock(CloudCredential.class);
    when(credentialConverter.convert(stack.getCredential())).thenReturn(cloudCredential);
    String instanceId1 = "i-0f1e0605506aaaaaa";
    String instanceId2 = "i-0f1e0605506bbbbbb";
    Set<InstanceMetaData> candidateUnhealthyInstances = new HashSet<>();
    setupInstanceMetaData(instanceId1, candidateUnhealthyInstances);
    setupInstanceMetaData(instanceId2, candidateUnhealthyInstances);
    List<CloudInstance> cloudInstances = new ArrayList<>();
    CloudInstance cloudInstance1 = setupCloudInstance(instanceId1, cloudInstances);
    when(cloudInstanceConverter.convert(candidateUnhealthyInstances)).thenReturn(cloudInstances);
    List<CloudVmInstanceStatus> cloudVmInstanceStatusList = new ArrayList<>();
    setupCloudVmInstanceStatus(cloudInstance1, InstanceStatus.TERMINATED, cloudVmInstanceStatusList);
    when(instanceStateQuery.getCloudVmInstanceStatuses(eq(cloudCredential), any(CloudContext.class), eq(cloudInstances))).thenReturn(cloudVmInstanceStatusList);
    Set<String> unhealthyInstances = underTest.finalizeUnhealthyInstances(stack, candidateUnhealthyInstances);
    assertEquals(2, unhealthyInstances.size());
    assertTrue(unhealthyInstances.contains(instanceId1));
    assertTrue(unhealthyInstances.contains(instanceId2));
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Stack(com.sequenceiq.cloudbreak.domain.Stack) InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with CloudVmInstanceStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus in project cloudbreak by hortonworks.

the class AwsInstanceConnector method stop.

@Override
public List<CloudVmInstanceStatus> stop(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
    List<CloudVmInstanceStatus> statuses = new ArrayList<>();
    AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    for (String group : getGroups(vms)) {
        Collection<String> instances = new ArrayList<>();
        Collection<CloudInstance> cloudInstances = new ArrayList<>();
        for (CloudInstance vm : vms) {
            if (vm.getTemplate().getGroupName().equals(group)) {
                instances.add(vm.getInstanceId());
                cloudInstances.add(vm);
            }
        }
        try {
            instances = removeInstanceIdsWhichAreNotInCorrectState(instances, amazonEC2Client, "Stopped");
            if (!instances.isEmpty()) {
                amazonEC2Client.stopInstances(new StopInstancesRequest().withInstanceIds(instances));
            }
            for (CloudInstance cloudInstance : cloudInstances) {
                statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.IN_PROGRESS));
            }
        } catch (RuntimeException e) {
            LOGGER.error("Stop instances failed on AWS", e);
            String message = e instanceof AmazonEC2Exception ? ((AmazonEC2Exception) e).getErrorCode() : e.getMessage();
            for (CloudInstance cloudInstance : cloudInstances) {
                statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.FAILED, message));
            }
        }
    }
    return statuses;
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 18 with CloudVmInstanceStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus in project cloudbreak by hortonworks.

the class AwsInstanceConnector method start.

@Override
public List<CloudVmInstanceStatus> start(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
    List<CloudVmInstanceStatus> statuses = new ArrayList<>();
    AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    for (String group : getGroups(vms)) {
        Collection<String> instances = new ArrayList<>();
        Collection<CloudInstance> cloudInstances = new ArrayList<>();
        for (CloudInstance vm : vms) {
            if (vm.getTemplate().getGroupName().equals(group)) {
                instances.add(vm.getInstanceId());
                cloudInstances.add(vm);
            }
        }
        try {
            instances = removeInstanceIdsWhichAreNotInCorrectState(instances, amazonEC2Client, "Running");
            if (!instances.isEmpty()) {
                amazonEC2Client.startInstances(new StartInstancesRequest().withInstanceIds(instances));
            }
            for (CloudInstance cloudInstance : cloudInstances) {
                statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.IN_PROGRESS));
            }
        } catch (RuntimeException e) {
            LOGGER.error("Start instances failed on AWS", e);
            String message = e instanceof AmazonEC2Exception ? ((AmazonEC2Exception) e).getErrorCode() : e.getMessage();
            for (CloudInstance cloudInstance : cloudInstances) {
                statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.FAILED, message));
            }
        }
    }
    return statuses;
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 19 with CloudVmInstanceStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus in project cloudbreak by hortonworks.

the class UnhealthyInstancesFinalizerTest method setupCloudVmInstanceStatus.

private CloudVmInstanceStatus setupCloudVmInstanceStatus(CloudInstance cloudInstance1, InstanceStatus stopped, List<CloudVmInstanceStatus> cloudVmInstanceStatusList) {
    CloudVmInstanceStatus status1 = mock(CloudVmInstanceStatus.class);
    when(status1.getCloudInstance()).thenReturn(cloudInstance1);
    when(status1.getStatus()).thenReturn(stopped);
    cloudVmInstanceStatusList.add(status1);
    return status1;
}
Also used : CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)

Example 20 with CloudVmInstanceStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus in project cloudbreak by hortonworks.

the class UnhealthyInstancesFinalizerTest method shouldFinalizeInstancesMarkedAsTerminated.

@Test
public void shouldFinalizeInstancesMarkedAsTerminated() {
    Stack stack = TestUtil.stack(Status.AVAILABLE, TestUtil.awsCredential());
    CloudCredential cloudCredential = mock(CloudCredential.class);
    when(credentialConverter.convert(stack.getCredential())).thenReturn(cloudCredential);
    String instanceId1 = "i-0f1e0605506aaaaaa";
    String instanceId2 = "i-0f1e0605506bbbbbb";
    Set<InstanceMetaData> candidateUnhealthyInstances = new HashSet<>();
    setupInstanceMetaData(instanceId1, candidateUnhealthyInstances);
    setupInstanceMetaData(instanceId2, candidateUnhealthyInstances);
    List<CloudInstance> cloudInstances = new ArrayList<>();
    CloudInstance cloudInstance1 = setupCloudInstance(instanceId1, cloudInstances);
    CloudInstance cloudInstance2 = setupCloudInstance(instanceId2, cloudInstances);
    when(cloudInstanceConverter.convert(candidateUnhealthyInstances)).thenReturn(cloudInstances);
    List<CloudVmInstanceStatus> cloudVmInstanceStatusList = new ArrayList<>();
    setupCloudVmInstanceStatus(cloudInstance1, InstanceStatus.STOPPED, cloudVmInstanceStatusList);
    setupCloudVmInstanceStatus(cloudInstance2, InstanceStatus.TERMINATED, cloudVmInstanceStatusList);
    when(instanceStateQuery.getCloudVmInstanceStatuses(eq(cloudCredential), any(CloudContext.class), eq(cloudInstances))).thenReturn(cloudVmInstanceStatusList);
    Set<String> unhealthyInstances = underTest.finalizeUnhealthyInstances(stack, candidateUnhealthyInstances);
    assertEquals(1, unhealthyInstances.size());
    assertTrue(unhealthyInstances.contains(instanceId2));
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Stack(com.sequenceiq.cloudbreak.domain.Stack) InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)39 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)32 ArrayList (java.util.ArrayList)21 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)12 CloudInstanceMetaData (com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData)9 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)8 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)8 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)7 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)5 Server (org.openstack4j.model.compute.Server)5 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)4 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)4 InstanceMetaData (com.sequenceiq.cloudbreak.domain.InstanceMetaData)4 List (java.util.List)4 Map (java.util.Map)4 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)3 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)3 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)3 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)3 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)2