use of com.amazonaws.services.ec2.model.DescribeInstancesResult in project SimianArmy by Netflix.
the class AWSClient method describeInstances.
/**
* Describe a set of specific instances.
*
* @param instanceIds the instance ids
* @return the instances
*/
public List<Instance> describeInstances(String... instanceIds) {
if (instanceIds == null || instanceIds.length == 0) {
LOGGER.info(String.format("Getting all EC2 instances in region %s.", region));
} else {
LOGGER.info(String.format("Getting EC2 instances for %d ids in region %s.", instanceIds.length, region));
}
List<Instance> instances = new LinkedList<Instance>();
AmazonEC2 ec2Client = ec2Client();
DescribeInstancesRequest request = new DescribeInstancesRequest();
if (instanceIds != null) {
request.withInstanceIds(Arrays.asList(instanceIds));
}
DescribeInstancesResult result = ec2Client.describeInstances(request);
for (Reservation reservation : result.getReservations()) {
instances.addAll(reservation.getInstances());
}
LOGGER.info(String.format("Got %d EC2 instances in region %s.", instances.size(), region));
return instances;
}
use of com.amazonaws.services.ec2.model.DescribeInstancesResult in project h2o-2 by h2oai.
the class EC2 method resize.
//@formatter:on
/**
* Create or terminate EC2 instances. Uses their Name tag to find existing ones.
*/
public Cloud resize() throws Exception {
AmazonEC2Client ec2 = new AmazonEC2Client(new PersistS3.H2OAWSCredentialsProviderChain());
ec2.setEndpoint("ec2." + region + ".amazonaws.com");
DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
List<Reservation> reservations = describeInstancesResult.getReservations();
List<Instance> instances = new ArrayList<Instance>();
for (Reservation reservation : reservations) {
for (Instance instance : reservation.getInstances()) {
String ip = ip(instance);
if (ip != null) {
String name = null;
if (instance.getTags().size() > 0)
name = instance.getTags().get(0).getValue();
if (NAME.equals(name))
instances.add(instance);
}
}
}
System.out.println("Found " + instances.size() + " EC2 instances for user " + USER);
if (instances.size() > boxes) {
for (int i = 0; i < instances.size() - boxes; i++) {
// TODO terminate?
}
} else if (instances.size() < boxes) {
int launchCount = boxes - instances.size();
System.out.println("Creating " + launchCount + " EC2 instances.");
if (confirm) {
System.out.println("Please confirm [y/n]");
String s = Utils.readConsole();
if (s == null || !s.equalsIgnoreCase("y"))
throw new Exception("Aborted");
}
CreatePlacementGroupRequest group = new CreatePlacementGroupRequest();
group.withGroupName(USER);
group.withStrategy(PlacementStrategy.Cluster);
try {
ec2.createPlacementGroup(group);
} catch (AmazonServiceException ex) {
if (!"InvalidPlacementGroup.Duplicate".equals(ex.getErrorCode()))
throw ex;
}
RunInstancesRequest run = new RunInstancesRequest();
run.withInstanceType(type);
run.withImageId(image);
run.withMinCount(launchCount).withMaxCount(launchCount);
run.withSecurityGroupIds(securityGroup);
Placement placement = new Placement();
placement.setGroupName(USER);
run.withPlacement(placement);
BlockDeviceMapping map = new BlockDeviceMapping();
map.setDeviceName("/dev/sdb");
map.setVirtualName("ephemeral0");
run.withBlockDeviceMappings(map);
run.withUserData(new String(Base64.encodeBase64(cloudConfig.getBytes())));
RunInstancesResult runRes = ec2.runInstances(run);
ArrayList<String> ids = new ArrayList<String>();
for (Instance instance : runRes.getReservation().getInstances()) ids.add(instance.getInstanceId());
List<Instance> created = wait(ec2, ids);
System.out.println("Created " + created.size() + " EC2 instances.");
instances.addAll(created);
}
String[] pub = new String[boxes];
String[] prv = new String[boxes];
for (int i = 0; i < boxes; i++) {
pub[i] = instances.get(i).getPublicIpAddress();
prv[i] = instances.get(i).getPrivateIpAddress();
}
System.out.println("EC2 public IPs: " + Utils.join(' ', pub));
System.out.println("EC2 private IPs: " + Utils.join(' ', prv));
Cloud cloud = new Cloud();
cloud.publicIPs.addAll(Arrays.asList(pub));
cloud.privateIPs.addAll(Arrays.asList(prv));
return cloud;
}
use of com.amazonaws.services.ec2.model.DescribeInstancesResult in project GNS by MobilityFirst.
the class AWSEC2 method describeInstances.
/**
* Describe Current Instances
*
* @param ec2
*/
public static void describeInstances(AmazonEC2 ec2) {
StringBuilder output = new StringBuilder();
String prefix = currentTab + "Current Instances: ";
DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
List<Reservation> reservations = describeInstancesResult.getReservations();
Set<Instance> instances = new HashSet<Instance>();
// add all instances to a Set.
for (Reservation reservation : reservations) {
instances.addAll(reservation.getInstances());
}
prefix = prefix.concat(" [" + instances.size() + " total] ");
for (Instance ins : instances) {
// instance state
output.append(prefix);
prefix = ", ";
output.append(ins.getPublicDnsName());
output.append(" (");
output.append(ins.getInstanceId());
output.append(") ");
output.append(ins.getState().getName());
}
System.out.println(output);
}
use of com.amazonaws.services.ec2.model.DescribeInstancesResult in project GNS by MobilityFirst.
the class AWSEC2 method findInstance.
/**
* Find an instance
*
* @param ec2
* @param createdInstanceId
* @return the name of the instance or null
*/
public static Instance findInstance(AmazonEC2 ec2, String createdInstanceId) {
DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
List<Reservation> reservations = describeInstancesResult.getReservations();
Set<Instance> instances = new HashSet<>();
// add all instances to a Set.
for (Reservation reservation : reservations) {
instances.addAll(reservation.getInstances());
}
for (Instance instance : instances) {
if (createdInstanceId.equals(instance.getInstanceId())) {
return instance;
}
}
return null;
}
use of com.amazonaws.services.ec2.model.DescribeInstancesResult in project aws-doc-sdk-examples by awsdocs.
the class DescribeInstances method main.
public static void main(String[] args) {
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
boolean done = false;
while (!done) {
DescribeInstancesRequest request = new DescribeInstancesRequest();
DescribeInstancesResult response = ec2.describeInstances(request);
for (Reservation reservation : response.getReservations()) {
for (Instance instance : reservation.getInstances()) {
System.out.printf("Found reservation with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.getInstanceId(), instance.getImageId(), instance.getInstanceType(), instance.getState().getName(), instance.getMonitoring().getState());
}
}
request.setNextToken(response.getNextToken());
if (response.getNextToken() == null) {
done = true;
}
}
}
Aggregations