use of software.amazon.awssdk.services.ec2.model.Instance in project GNS by MobilityFirst.
the class EC2Runner method terminateRunSet.
/**
* Terminates all the hosts in the named run set.
*
* @param name
*/
public static void terminateRunSet(String name) {
try {
AWSCredentials credentials = new PropertiesCredentials(new File(CREDENTIALSFILE));
//Create Amazon Client object
AmazonEC2 ec2 = new AmazonEC2Client(credentials);
for (RegionRecord region : RegionRecord.values()) {
AWSEC2.setRegion(ec2, region);
for (Instance instance : AWSEC2.getInstances(ec2)) {
if (!instance.getState().getName().equals(InstanceStateRecord.TERMINATED.getName())) {
String idString = getTagValue(instance, "id");
if (name.equals(getTagValue(instance, "runset"))) {
if (idString != null) {
// StatusModel.getInstance().queueUpdate(new String(idString), "Terminating");
}
AWSEC2.terminateInstance(ec2, instance.getInstanceId());
if (idString != null) {
// StatusModel.getInstance().queueUpdate(new String(idString), StatusEntry.State.TERMINATED, "");
}
}
}
}
}
} catch (IOException e) {
System.out.println("Problem terminating EC2 instances: " + e);
e.printStackTrace();
} catch (IllegalArgumentException e) {
System.out.println("Problem terminating EC2 instances: " + e);
e.printStackTrace();
}
}
use of software.amazon.awssdk.services.ec2.model.Instance in project GNS by MobilityFirst.
the class EC2Runner method populateIDTableForRunset.
private static void populateIDTableForRunset(String name) {
AWSCredentials credentials = null;
try {
//
credentials = new PropertiesCredentials(new File(CREDENTIALSFILE));
} catch (IOException e) {
System.out.println("Problem contacting EC2 instances: " + e);
}
//Create Amazon Client object
AmazonEC2 ec2 = new AmazonEC2Client(credentials);
for (RegionRecord region : RegionRecord.values()) {
AWSEC2.setRegion(ec2, region);
System.out.println("Retrieving instance information in " + region.name() + "...");
for (Instance instance : AWSEC2.getInstances(ec2)) {
if (!instance.getState().getName().equals(InstanceStateRecord.TERMINATED.getName())) {
String idString = getTagValue(instance, "id");
if (idString != null && name.equals(getTagValue(instance, "runset"))) {
String id = new String(idString);
String hostname = instance.getPublicDnsName();
String ip = getHostIPSafe(hostname);
// and take a guess at the location (lat, long) of this host
Point2D location = GEOLocator.lookupIPLocation(ip);
hostTable.put(id, new HostInfo(id, hostname, location));
}
}
}
}
}
use of software.amazon.awssdk.services.ec2.model.Instance in project airavata by apache.
the class AmazonUtil method launchInstance.
/**
* Launch a new EC2 instance
*
* @param AMI_ID
* @param type
* @param number
* @return list of newly launched instances
*/
public static List<Instance> launchInstance(String AMI_ID, String type, Integer number) {
List<Instance> resultList = new ArrayList<Instance>();
RunInstancesRequest request = new RunInstancesRequest(AMI_ID, number, number);
request.setInstanceType(type);
RunInstancesResult result = getEC2Client().runInstances(request);
resultList.addAll(result.getReservation().getInstances());
return resultList;
}
use of software.amazon.awssdk.services.ec2.model.Instance in project airavata by apache.
the class AmazonUtil method loadInstances.
/**
* Load instances
*
* @return list of instances
*/
public static List<Instance> loadInstances() {
List<Instance> resultList = new ArrayList<Instance>();
DescribeInstancesResult describeInstancesResult = getEC2Client().describeInstances();
List<Reservation> reservations = describeInstancesResult.getReservations();
for (Iterator<Reservation> iterator = reservations.iterator(); iterator.hasNext(); ) {
Reservation reservation = iterator.next();
for (Instance instance : reservation.getInstances()) {
resultList.add(instance);
}
}
return resultList;
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSRemoteCleanup method deleteAwsEc2instances.
private void deleteAwsEc2instances(List<String> vpcIdsToBeDeleted, DescribeInstancesResult describeInstancesResult, AmazonEC2 ec2Client) {
List<String> instanceIdsToBeDeleted = new ArrayList<>();
List<Reservation> reservations = describeInstancesResult.getReservations();
for (Reservation reservation : reservations) {
List<Instance> instances = reservation.getInstances();
for (Instance instance : instances) {
long instanceLaunchTimeMicros = TimeUnit.MILLISECONDS.toMicros(instance.getLaunchTime().getTime());
long timeDifference = Utils.getNowMicrosUtc() - instanceLaunchTimeMicros;
if (timeDifference > TimeUnit.HOURS.toMicros(1) && vpcIdsToBeDeleted.contains(instance.getVpcId()) && shouldDelete(instance)) {
this.host.log(Level.INFO, "Marking %s instance for deletion", instance.getInstanceId());
instanceIdsToBeDeleted.add(instance.getInstanceId());
}
}
}
triggerEC2Deletion(instanceIdsToBeDeleted, ec2Client);
}
Aggregations