Search in sources :

Example 21 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project cloudbreak by hortonworks.

the class AwsInstanceConnector method check.

@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext ac, List<CloudInstance> vms) {
    List<CloudVmInstanceStatus> cloudVmInstanceStatuses = new ArrayList<>();
    for (CloudInstance vm : vms) {
        try {
            String region = ac.getCloudContext().getLocation().getRegion().value();
            DescribeInstancesResult result = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()).describeInstances(new DescribeInstancesRequest().withInstanceIds(vm.getInstanceId()));
            for (Reservation reservation : result.getReservations()) {
                for (Instance instance : reservation.getInstances()) {
                    if ("Stopped".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.STOPPED));
                    } else if ("Running".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.STARTED));
                    } else if ("Terminated".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.TERMINATED));
                    } else {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.IN_PROGRESS));
                    }
                }
            }
        } catch (AmazonEC2Exception e) {
            LOGGER.warn("Instance does not exist with this id: {}, original message: {}", vm.getInstanceId(), e.getMessage());
        }
    }
    return cloudVmInstanceStatuses;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) Reservation(com.amazonaws.services.ec2.model.Reservation) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 22 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project cloudbreak by hortonworks.

the class AwsMetadataCollector method collectGroupMetaData.

private Collection<CloudVmMetaDataStatus> collectGroupMetaData(AuthenticatedContext ac, AmazonAutoScaling amazonASClient, AmazonEC2 amazonEC2Client, AmazonCloudFormation amazonCFClient, String groupName, Iterable<CloudInstance> cloudInstances) {
    Collection<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = new ArrayList<>();
    String asGroupName = cloudFormationStackUtil.getAutoscalingGroupName(ac, amazonCFClient, groupName);
    List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(amazonASClient, asGroupName);
    DescribeInstancesRequest instancesRequest = cloudFormationStackUtil.createDescribeInstancesRequest(instanceIds);
    DescribeInstancesResult instancesResult = amazonEC2Client.describeInstances(instancesRequest);
    // contains instances with instanceId
    Map<String, CloudInstance> mapByInstanceId = mapByInstanceId(cloudInstances);
    // contains instances with privateId (without instanceId)
    Queue<CloudInstance> untrackedInstances = untrackedInstances(cloudInstances);
    for (Reservation reservation : instancesResult.getReservations()) {
        LOGGER.info("Number of instances found in reservation: {}", reservation.getInstances().size());
        for (Instance instance : reservation.getInstances()) {
            String instanceId = instance.getInstanceId();
            CloudInstance cloudInstance = ensureInstanceTag(mapByInstanceId, instance, instanceId, untrackedInstances, amazonEC2Client);
            if (cloudInstance != null) {
                CloudInstanceMetaData md = new CloudInstanceMetaData(instance.getPrivateIpAddress(), instance.getPublicIpAddress());
                CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
                CloudVmMetaDataStatus cloudVmMetaDataStatus = new CloudVmMetaDataStatus(cloudVmInstanceStatus, md);
                cloudVmMetaDataStatuses.add(cloudVmMetaDataStatus);
            }
        }
    }
    return cloudVmMetaDataStatuses;
}
Also used : CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)

Example 23 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project cloudbreak by hortonworks.

the class EC2ClientActions method listTagsByInstanceId.

public Map<String, Map<String, String>> listTagsByInstanceId(List<String> instanceIds) {
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(instanceIds);
    DescribeInstancesResult describeInstancesResult = buildEC2Client().describeInstances(describeInstancesRequest);
    return describeInstancesResult.getReservations().stream().flatMap(reservation -> reservation.getInstances().stream()).collect(Collectors.toMap(Instance::getInstanceId, this::getTagsForInstance));
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) EC2UnexpectedException(com.amazonaws.services.lambda.model.EC2UnexpectedException) EbsInstanceBlockDevice(com.amazonaws.services.ec2.model.EbsInstanceBlockDevice) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) WaiterParameters(com.amazonaws.waiters.WaiterParameters) LoggerFactory(org.slf4j.LoggerFactory) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) BooleanUtils(org.apache.commons.lang3.BooleanUtils) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) EC2Client(com.sequenceiq.it.cloudbreak.util.aws.amazonec2.client.EC2Client) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) TestFailException(com.sequenceiq.it.cloudbreak.exception.TestFailException) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) Volume(com.amazonaws.services.ec2.model.Volume) InstanceState(com.amazonaws.services.ec2.model.InstanceState) Instance(com.amazonaws.services.ec2.model.Instance) WaiterUnrecoverableException(com.amazonaws.waiters.WaiterUnrecoverableException) SdxUtil(com.sequenceiq.it.cloudbreak.util.SdxUtil) Reservation(com.amazonaws.services.ec2.model.Reservation) FixedDelayStrategy(com.amazonaws.waiters.FixedDelayStrategy) Logger(org.slf4j.Logger) Collection(java.util.Collection) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) Log(com.sequenceiq.it.cloudbreak.log.Log) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) MaxAttemptsRetryStrategy(com.amazonaws.waiters.MaxAttemptsRetryStrategy) List(java.util.List) Component(org.springframework.stereotype.Component) PollingStrategy(com.amazonaws.waiters.PollingStrategy) Tag(com.amazonaws.services.ec2.model.Tag) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 24 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project cloudbreak by hortonworks.

the class EC2ClientActions method stopHostGroupInstances.

public void stopHostGroupInstances(List<String> instanceIds) {
    AmazonEC2 ec2Client = buildEC2Client();
    StopInstancesResult stopInstancesResult = ec2Client.stopInstances(new StopInstancesRequest().withInstanceIds(instanceIds));
    for (String instanceId : instanceIds) {
        try {
            Log.log(LOGGER, format(" EC2 instance [%s] state is [%s] ", instanceId, Objects.requireNonNull(stopInstancesResult.getStoppingInstances().stream().filter(instance -> instance.getInstanceId().equals(instanceId)).findAny().orElse(null)).getCurrentState().getName()));
            ec2Client.waiters().instanceStopped().run(new WaiterParameters<DescribeInstancesRequest>(new DescribeInstancesRequest().withInstanceIds(instanceId)).withPollingStrategy(new PollingStrategy(new MaxAttemptsRetryStrategy(80), new FixedDelayStrategy(30))));
            DescribeInstancesResult describeInstanceResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceId));
            InstanceState actualInstanceState = describeInstanceResult.getReservations().get(0).getInstances().get(0).getState();
            if (STOPPED_STATE.equals(actualInstanceState.getName())) {
                Log.log(LOGGER, format(" EC2 Instance: %s state is: %s ", instanceId, STOPPED_STATE));
            } else {
                LOGGER.error("EC2 Instance: {} stop has not been successful. So the actual state is: {} ", instanceId, actualInstanceState.getName());
                throw new TestFailException(" EC2 Instance: " + instanceId + " stop has not been successful, because of the actual state is: " + actualInstanceState.getName());
            }
        } catch (WaiterUnrecoverableException e) {
            LOGGER.error("EC2 Instance {} stop has not been successful, because of WaiterUnrecoverableException: {}", instanceId, e);
        } catch (WaiterTimedOutException e) {
            LOGGER.error("EC2 Instance {} stop has not been successful, because of WaiterTimedOutException: {}", instanceId, e);
        } catch (EC2UnexpectedException e) {
            LOGGER.error("EC2 Instance {} stop has not been successful, because of EC2UnexpectedException: {}", instanceId, e);
        }
    }
}
Also used : EC2UnexpectedException(com.amazonaws.services.lambda.model.EC2UnexpectedException) EbsInstanceBlockDevice(com.amazonaws.services.ec2.model.EbsInstanceBlockDevice) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) WaiterParameters(com.amazonaws.waiters.WaiterParameters) LoggerFactory(org.slf4j.LoggerFactory) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) BooleanUtils(org.apache.commons.lang3.BooleanUtils) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) EC2Client(com.sequenceiq.it.cloudbreak.util.aws.amazonec2.client.EC2Client) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) TestFailException(com.sequenceiq.it.cloudbreak.exception.TestFailException) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) Volume(com.amazonaws.services.ec2.model.Volume) InstanceState(com.amazonaws.services.ec2.model.InstanceState) Instance(com.amazonaws.services.ec2.model.Instance) WaiterUnrecoverableException(com.amazonaws.waiters.WaiterUnrecoverableException) SdxUtil(com.sequenceiq.it.cloudbreak.util.SdxUtil) Reservation(com.amazonaws.services.ec2.model.Reservation) FixedDelayStrategy(com.amazonaws.waiters.FixedDelayStrategy) Logger(org.slf4j.Logger) Collection(java.util.Collection) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) Log(com.sequenceiq.it.cloudbreak.log.Log) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) MaxAttemptsRetryStrategy(com.amazonaws.waiters.MaxAttemptsRetryStrategy) List(java.util.List) Component(org.springframework.stereotype.Component) PollingStrategy(com.amazonaws.waiters.PollingStrategy) Tag(com.amazonaws.services.ec2.model.Tag) EC2UnexpectedException(com.amazonaws.services.lambda.model.EC2UnexpectedException) WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) MaxAttemptsRetryStrategy(com.amazonaws.waiters.MaxAttemptsRetryStrategy) WaiterUnrecoverableException(com.amazonaws.waiters.WaiterUnrecoverableException) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) WaiterParameters(com.amazonaws.waiters.WaiterParameters) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) InstanceState(com.amazonaws.services.ec2.model.InstanceState) PollingStrategy(com.amazonaws.waiters.PollingStrategy) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) TestFailException(com.sequenceiq.it.cloudbreak.exception.TestFailException) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) FixedDelayStrategy(com.amazonaws.waiters.FixedDelayStrategy)

Example 25 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project incubator-gobblin by apache.

the class AWSSdkClient method getInstancesForGroup.

/**
 * Get list of EC2 {@link Instance}s for a auto scaling group
 *
 * @param groupName Auto scaling group name
 * @param status Instance status (eg. running)
 * @return List of EC2 instances found for the input auto scaling group
 */
public List<Instance> getInstancesForGroup(String groupName, String status) {
    final AmazonEC2 amazonEC2 = getEc2Client();
    final DescribeInstancesResult instancesResult = amazonEC2.describeInstances(new DescribeInstancesRequest().withFilters(new Filter().withName("tag:aws:autoscaling:groupName").withValues(groupName)));
    final List<Instance> instances = new ArrayList<>();
    for (Reservation reservation : instancesResult.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            if (null == status || null == instance.getState() || status.equals(instance.getState().getName())) {
                instances.add(instance);
                LOGGER.info("Found instance: " + instance + " which qualified filter: " + status);
            } else {
                LOGGER.info("Found instance: " + instance + " but did not qualify for filter: " + status);
            }
        }
    }
    return instances;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Aggregations

DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)74 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)49 Reservation (com.amazonaws.services.ec2.model.Reservation)47 Instance (com.amazonaws.services.ec2.model.Instance)42 ArrayList (java.util.ArrayList)40 Filter (com.amazonaws.services.ec2.model.Filter)29 List (java.util.List)28 Map (java.util.Map)18 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)17 Collectors (java.util.stream.Collectors)17 Tag (com.amazonaws.services.ec2.model.Tag)13 Inject (javax.inject.Inject)13 Set (java.util.Set)12 Logger (org.slf4j.Logger)12 LoggerFactory (org.slf4j.LoggerFactory)12 TerminateInstancesRequest (com.amazonaws.services.ec2.model.TerminateInstancesRequest)11 TerminateInstancesResult (com.amazonaws.services.ec2.model.TerminateInstancesResult)11 Collection (java.util.Collection)11 DescribeVolumesResult (com.amazonaws.services.ec2.model.DescribeVolumesResult)10 StopInstancesRequest (com.amazonaws.services.ec2.model.StopInstancesRequest)10