Search in sources :

Example 11 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 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;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) HashSet(java.util.HashSet)

Example 12 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method addInstanceTags.

/**
   * Adds keys and values from tagmap to the tags of the given instance.
   *
   * @param ec2
   * @param createdInstanceId
   * @param tagmap
   */
public static void addInstanceTags(AmazonEC2 ec2, String createdInstanceId, Map<String, String> tagmap) {
    List<String> resources = new LinkedList<>();
    resources.add(createdInstanceId);
    List<Tag> tags = new LinkedList<>();
    for (Entry<String, String> entry : tagmap.entrySet()) {
        Tag nameTag = new Tag(entry.getKey(), entry.getValue());
        tags.add(nameTag);
    }
    CreateTagsRequest ctr = new CreateTagsRequest(resources, tags);
    ec2.createTags(ctr);
}
Also used : CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 13 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method startInstance.

/**
   * Start an instance
   *
   * @param ec2
   * @param createdInstanceId
   */
public static void startInstance(AmazonEC2 ec2, String createdInstanceId) {
    System.out.println("Starting Instance:" + createdInstanceId);
    List<String> instanceIds = new LinkedList<>();
    instanceIds.add(createdInstanceId);
    StartInstancesRequest startIR = new StartInstancesRequest(instanceIds);
    ec2.startInstances(startIR);
}
Also used : StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) LinkedList(java.util.LinkedList)

Example 14 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 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();
    }
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) Instance(com.amazonaws.services.ec2.model.Instance) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) IOException(java.io.IOException) AWSCredentials(com.amazonaws.auth.AWSCredentials) File(java.io.File) RegionRecord(edu.umass.cs.aws.support.RegionRecord)

Example 15 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 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));
                }
            }
        }
    }
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) Instance(com.amazonaws.services.ec2.model.Instance) Point2D(java.awt.geom.Point2D) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) IOException(java.io.IOException) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AWSCredentials(com.amazonaws.auth.AWSCredentials) File(java.io.File) RegionRecord(edu.umass.cs.aws.support.RegionRecord)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)65 Filter (com.amazonaws.services.ec2.model.Filter)25 Instance (com.amazonaws.services.ec2.model.Instance)20 DescribeImagesRequest (com.amazonaws.services.ec2.model.DescribeImagesRequest)17 DescribeImagesResult (com.amazonaws.services.ec2.model.DescribeImagesResult)15 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)12 Reservation (com.amazonaws.services.ec2.model.Reservation)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)9 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)8 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)8 Tag (com.amazonaws.services.ec2.model.Tag)8 AWSCredentials (com.amazonaws.auth.AWSCredentials)6 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)6 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)5 DisassociateAddressRequest (com.amazonaws.services.ec2.model.DisassociateAddressRequest)5 ReleaseAddressRequest (com.amazonaws.services.ec2.model.ReleaseAddressRequest)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4