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;
}
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;
}
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));
}
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);
}
}
}
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;
}
Aggregations