Search in sources :

Example 1 with InstanceStatus

use of com.amazonaws.services.ec2.model.InstanceStatus in project camel by apache.

the class AmazonEC2ClientMock method describeInstanceStatus.

@Override
public DescribeInstanceStatusResult describeInstanceStatus(DescribeInstanceStatusRequest describeInstanceStatusRequest) {
    DescribeInstanceStatusResult result = new DescribeInstanceStatusResult();
    Collection<InstanceStatus> instanceStatuses = new ArrayList();
    if (describeInstanceStatusRequest.getInstanceIds().isEmpty()) {
        InstanceStatus status = new InstanceStatus();
        status.setInstanceId("test-1");
        status.setInstanceState(new InstanceState().withName(InstanceStateName.Running));
        instanceStatuses.add(status);
        status.setInstanceId("test-2");
        status.setInstanceState(new InstanceState().withName(InstanceStateName.Stopped));
        instanceStatuses.add(status);
    } else {
        if (describeInstanceStatusRequest.getInstanceIds().contains("test-1")) {
            InstanceStatus status = new InstanceStatus();
            status.setInstanceId("test-1");
            status.setInstanceState(new InstanceState().withName(InstanceStateName.Running));
            instanceStatuses.add(status);
        }
        if (describeInstanceStatusRequest.getInstanceIds().contains("test-2")) {
            InstanceStatus status = new InstanceStatus();
            status.setInstanceId("test-2");
            status.setInstanceState(new InstanceState().withName(InstanceStateName.Stopped));
            instanceStatuses.add(status);
        }
    }
    result.setInstanceStatuses(instanceStatuses);
    return result;
}
Also used : DescribeInstanceStatusResult(com.amazonaws.services.ec2.model.DescribeInstanceStatusResult) InstanceState(com.amazonaws.services.ec2.model.InstanceState) InstanceStatus(com.amazonaws.services.ec2.model.InstanceStatus) ArrayList(java.util.ArrayList)

Example 2 with InstanceStatus

use of com.amazonaws.services.ec2.model.InstanceStatus in project cloudbreak by hortonworks.

the class ASGroupStatusCheckerTaskTest method successTest.

@Test
public void successTest() throws Exception {
    int requiredInstances = 160;
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    String regionName = "eu-west-1";
    when(cloudContext.getLocation()).thenReturn(Location.location(Region.region(regionName)));
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    String asGroupName = "as-group";
    AwsClient awsClient = mock(AwsClient.class);
    AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
    when(awsClient.createAccess(any(AwsCredentialView.class), eq(regionName))).thenReturn(amazonEC2Client);
    DescribeInstanceStatusResult firstDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
    List<InstanceStatus> firstInstanceStatuses = returnInstanceStatus(0, 100);
    firstDescribeInstanceStatusResult.setInstanceStatuses(firstInstanceStatuses);
    DescribeInstanceStatusResult secondDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
    List<InstanceStatus> secondInstanceStatuses = returnInstanceStatus(100, 160);
    secondDescribeInstanceStatusResult.setInstanceStatuses(secondInstanceStatuses);
    when(amazonEC2Client.describeInstanceStatus(any(DescribeInstanceStatusRequest.class))).thenReturn(firstDescribeInstanceStatusResult).thenReturn(secondDescribeInstanceStatusResult);
    CloudFormationStackUtil cloudFormationStackUtil = mock(CloudFormationStackUtil.class);
    List<String> instancIds = new ArrayList<>();
    for (int i = 0; i < requiredInstances; i++) {
        instancIds.add(Integer.toString(i));
    }
    AmazonAutoScalingClient autoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(autoScalingClient);
    when(autoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(new DescribeScalingActivitiesResult());
    when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq(asGroupName))).thenReturn(instancIds);
    ASGroupStatusCheckerTask asGroupStatusCheckerTask = new ASGroupStatusCheckerTask(authenticatedContext, asGroupName, requiredInstances, awsClient, cloudFormationStackUtil);
    Boolean taskResult = asGroupStatusCheckerTask.call();
    ArgumentCaptor<DescribeInstanceStatusRequest> instanceStatusRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeInstanceStatusRequest.class);
    verify(amazonEC2Client, times(2)).describeInstanceStatus(instanceStatusRequestArgumentCaptor.capture());
    List<DescribeInstanceStatusRequest> allValues = instanceStatusRequestArgumentCaptor.getAllValues();
    assertEquals(100, allValues.get(0).getInstanceIds().size());
    assertEquals(60, allValues.get(1).getInstanceIds().size());
    assertTrue(taskResult);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DescribeInstanceStatusRequest(com.amazonaws.services.ec2.model.DescribeInstanceStatusRequest) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Matchers.anyString(org.mockito.Matchers.anyString) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) DescribeInstanceStatusResult(com.amazonaws.services.ec2.model.DescribeInstanceStatusResult) DescribeScalingActivitiesResult(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesResult) InstanceStatus(com.amazonaws.services.ec2.model.InstanceStatus) AwsClient(com.sequenceiq.cloudbreak.cloud.aws.AwsClient) DescribeScalingActivitiesRequest(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) Test(org.junit.Test)

Example 3 with InstanceStatus

use of com.amazonaws.services.ec2.model.InstanceStatus in project cloudbreak by hortonworks.

the class ASGroupStatusCheckerTaskTest method returnInstanceStatus.

private List<InstanceStatus> returnInstanceStatus(int start, int end) {
    List<InstanceStatus> instanceStatuses = new ArrayList<>();
    for (int i = start; i < end; i++) {
        InstanceStatus instanceStatus = new InstanceStatus();
        InstanceState instanceState = new InstanceState();
        instanceState.setCode(16);
        instanceStatus.setInstanceState(instanceState);
        instanceStatus.setInstanceId(Integer.toString(i));
        instanceStatuses.add(instanceStatus);
    }
    return instanceStatuses;
}
Also used : InstanceState(com.amazonaws.services.ec2.model.InstanceState) InstanceStatus(com.amazonaws.services.ec2.model.InstanceStatus) ArrayList(java.util.ArrayList)

Example 4 with InstanceStatus

use of com.amazonaws.services.ec2.model.InstanceStatus in project cloudbreak by hortonworks.

the class ASGroupStatusCheckerTaskTest method failTest.

@Test
public void failTest() throws Exception {
    int requiredInstances = 160;
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    String regionName = "eu-west-1";
    when(cloudContext.getLocation()).thenReturn(Location.location(Region.region(regionName)));
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    String asGroupName = "as-group";
    AwsClient awsClient = mock(AwsClient.class);
    AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
    when(awsClient.createAccess(any(AwsCredentialView.class), eq(regionName))).thenReturn(amazonEC2Client);
    DescribeInstanceStatusResult firstDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
    List<InstanceStatus> firstInstanceStatuses = returnInstanceStatus(0, 99);
    firstDescribeInstanceStatusResult.setInstanceStatuses(firstInstanceStatuses);
    DescribeInstanceStatusResult secondDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
    List<InstanceStatus> secondInstanceStatuses = returnInstanceStatus(100, 160);
    secondDescribeInstanceStatusResult.setInstanceStatuses(secondInstanceStatuses);
    when(amazonEC2Client.describeInstanceStatus(any(DescribeInstanceStatusRequest.class))).thenReturn(firstDescribeInstanceStatusResult).thenReturn(secondDescribeInstanceStatusResult);
    CloudFormationStackUtil cloudFormationStackUtil = mock(CloudFormationStackUtil.class);
    List<String> instancIds = new ArrayList<>();
    for (int i = 0; i < requiredInstances; i++) {
        instancIds.add(Integer.toString(i));
    }
    AmazonAutoScalingClient autoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(autoScalingClient);
    when(autoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(new DescribeScalingActivitiesResult());
    when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq(asGroupName))).thenReturn(instancIds);
    ASGroupStatusCheckerTask asGroupStatusCheckerTask = new ASGroupStatusCheckerTask(authenticatedContext, asGroupName, requiredInstances, awsClient, cloudFormationStackUtil);
    Boolean taskResult = asGroupStatusCheckerTask.call();
    ArgumentCaptor<DescribeInstanceStatusRequest> instanceStatusRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeInstanceStatusRequest.class);
    verify(amazonEC2Client, times(2)).describeInstanceStatus(instanceStatusRequestArgumentCaptor.capture());
    List<DescribeInstanceStatusRequest> allValues = instanceStatusRequestArgumentCaptor.getAllValues();
    assertEquals(100, allValues.get(0).getInstanceIds().size());
    assertEquals(60, allValues.get(1).getInstanceIds().size());
    assertFalse(taskResult);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DescribeInstanceStatusRequest(com.amazonaws.services.ec2.model.DescribeInstanceStatusRequest) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Matchers.anyString(org.mockito.Matchers.anyString) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) DescribeInstanceStatusResult(com.amazonaws.services.ec2.model.DescribeInstanceStatusResult) DescribeScalingActivitiesResult(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesResult) InstanceStatus(com.amazonaws.services.ec2.model.InstanceStatus) AwsClient(com.sequenceiq.cloudbreak.cloud.aws.AwsClient) DescribeScalingActivitiesRequest(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) Test(org.junit.Test)

Example 5 with InstanceStatus

use of com.amazonaws.services.ec2.model.InstanceStatus in project cloudbreak by hortonworks.

the class ASGroupStatusCheckerTask method doCall.

@Override
protected Boolean doCall() {
    LOGGER.info("Checking status of Auto Scaling group '{}'", autoScalingGroupName);
    AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(getAuthenticatedContext().getCloudCredential()), getAuthenticatedContext().getCloudContext().getLocation().getRegion().value());
    List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(autoScalingClient, autoScalingGroupName);
    if (instanceIds.size() < requiredInstances) {
        LOGGER.debug("Instances in AS group: {}, needed: {}", instanceIds.size(), requiredInstances);
        List<Activity> activities = getAutoScalingActivities();
        if (latestActivity.isPresent()) {
            checkForSpotRequest(latestActivity.get(), amazonEC2Client);
            activities = activities.stream().filter(activity -> activity.getStartTime().after(latestActivity.get().getStartTime())).collect(Collectors.toList());
        }
        updateLatestActivity(activities);
        checkFailedActivities(activities);
        return false;
    }
    Collection<DescribeInstanceStatusResult> describeInstanceStatusResultList = new ArrayList<>();
    List<List<String>> partitionedInstanceIdsList = Lists.partition(instanceIds, MAX_INSTANCE_ID_SIZE);
    for (List<String> partitionedInstanceIds : partitionedInstanceIdsList) {
        DescribeInstanceStatusRequest describeInstanceStatusRequest = new DescribeInstanceStatusRequest().withInstanceIds(partitionedInstanceIds);
        DescribeInstanceStatusResult describeResult = amazonEC2Client.describeInstanceStatus(describeInstanceStatusRequest);
        describeInstanceStatusResultList.add(describeResult);
    }
    List<InstanceStatus> instanceStatusList = describeInstanceStatusResultList.stream().flatMap(describeInstanceStatusResult -> describeInstanceStatusResult.getInstanceStatuses().stream()).collect(Collectors.toList());
    if (instanceStatusList.size() < requiredInstances) {
        LOGGER.debug("Instances up: {}, needed: {}", instanceStatusList.size(), requiredInstances);
        return false;
    }
    for (InstanceStatus status : instanceStatusList) {
        if (INSTANCE_RUNNING != status.getInstanceState().getCode()) {
            LOGGER.debug("Instances are up but not all of them are in running state.");
            return false;
        }
    }
    return true;
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) Activity(com.amazonaws.services.autoscaling.model.Activity) LoggerFactory(org.slf4j.LoggerFactory) Scope(org.springframework.context.annotation.Scope) ArrayList(java.util.ArrayList) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Matcher(java.util.regex.Matcher) Lists(com.google.common.collect.Lists) InstanceStatus(com.amazonaws.services.ec2.model.InstanceStatus) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DescribeInstanceStatusRequest(com.amazonaws.services.ec2.model.DescribeInstanceStatusRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeScalingActivitiesRequest(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) PollBooleanStateTask(com.sequenceiq.cloudbreak.cloud.task.PollBooleanStateTask) Logger(org.slf4j.Logger) Collection(java.util.Collection) DescribeInstanceStatusResult(com.amazonaws.services.ec2.model.DescribeInstanceStatusResult) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) Collectors(java.util.stream.Collectors) DescribeSpotInstanceRequestsResult(com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsResult) List(java.util.List) Component(org.springframework.stereotype.Component) Optional(java.util.Optional) SpotInstanceRequest(com.amazonaws.services.ec2.model.SpotInstanceRequest) AwsClient(com.sequenceiq.cloudbreak.cloud.aws.AwsClient) Pattern(java.util.regex.Pattern) DescribeSpotInstanceRequestsRequest(com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsRequest) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) DescribeInstanceStatusRequest(com.amazonaws.services.ec2.model.DescribeInstanceStatusRequest) ArrayList(java.util.ArrayList) Activity(com.amazonaws.services.autoscaling.model.Activity) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) DescribeInstanceStatusResult(com.amazonaws.services.ec2.model.DescribeInstanceStatusResult) InstanceStatus(com.amazonaws.services.ec2.model.InstanceStatus) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

InstanceStatus (com.amazonaws.services.ec2.model.InstanceStatus)5 ArrayList (java.util.ArrayList)5 DescribeInstanceStatusResult (com.amazonaws.services.ec2.model.DescribeInstanceStatusResult)4 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)3 DescribeScalingActivitiesRequest (com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest)3 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)3 DescribeInstanceStatusRequest (com.amazonaws.services.ec2.model.DescribeInstanceStatusRequest)3 AwsClient (com.sequenceiq.cloudbreak.cloud.aws.AwsClient)3 CloudFormationStackUtil (com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil)3 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)3 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)3 DescribeScalingActivitiesResult (com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesResult)2 InstanceState (com.amazonaws.services.ec2.model.InstanceState)2 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)2 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Activity (com.amazonaws.services.autoscaling.model.Activity)1 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 DescribeSpotInstanceRequestsRequest (com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsRequest)1