Search in sources :

Example 61 with Instance

use of software.amazon.awssdk.services.ec2.model.Instance in project GNS by MobilityFirst.

the class AWSEC2 method getInstances.

/**
   * Returns all the instances in this region.
   *
   * @param ec2
   * @return a set of instance instances
   */
public static Set<Instance> getInstances(AmazonEC2 ec2) {
    Set<Instance> instances = new HashSet<>();
    DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesResult.getReservations();
    // add all instances to a Set.
    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
    }
    return instances;
}
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 62 with Instance

use of software.amazon.awssdk.services.ec2.model.Instance in project GNS by MobilityFirst.

the class EC2Runner method initAndUpdateEC2Host.

/**
   * This is called to initialize an EC2 host for use as A GNS server in a region. It starts the host, loads all the necessary
   * software and copies the JAR files over. We also collect info about this host, like it's IP address and geographic location.
   * When every host is initialized and we have collected all the IPs, phase two is called.
   *
   * @param region - the EC2 region where we are starting this host
   * @param runSetName - so we can terminate them all together
   * @param id - the GNS ID of this server
   * @param elasticIP
   * @param timeout
   */
public static void initAndUpdateEC2Host(RegionRecord region, String runSetName, String id, String elasticIP, int timeout) {
    String installScript;
    AMIRecord amiRecord = AMIRecord.getAMI(amiRecordType, region);
    if (amiRecord == null) {
        System.out.println("Invalid combination of " + amiRecordType + " and Region " + region.name());
        return;
    }
    switch(dataStoreType) {
        case CASSANDRA:
            installScript = cassandraInstallScript;
            break;
        default:
            // MONGO
            if (amiRecordType.toString().contains("Amazon_Linux")) {
                installScript = mongoInstallScript;
            } else {
                switch(amiRecordType) {
                    case MongoDB_2_4_8_with_1000_IOPS:
                        installScript = mongoShortInstallScript;
                        break;
                    case Mongo_2014_5_6:
                        installScript = null;
                        break;
                    case Mongo_2014_5_6_micro:
                        installScript = null;
                        break;
                    case Mongo_2015_6_25_vpc:
                        installScript = null;
                        break;
                    case Mongo_2016_6_16_micro:
                        installScript = null;
                        break;
                    default:
                        System.out.println("Invalid combination of " + amiRecordType + " and " + dataStoreType);
                        return;
                }
            }
    }
    String idString = id.toString();
    //    StatusModel.getInstance().queueUpdate(id, region.name() + ": [Unknown hostname]", null, null);
    try {
        AWSCredentials credentials = new PropertiesCredentials(new File(CREDENTIALSFILE));
        //Create Amazon Client object
        AmazonEC2 ec2 = new AmazonEC2Client(credentials);
        String nodeName = "GNS Node " + idString;
        System.out.println("Starting install for " + nodeName + " in " + region.name() + " as part of run set " + runSetName);
        HashMap<String, String> tags = new HashMap<>();
        tags.put("runset", runSetName);
        tags.put("id", idString);
        //      StatusModel.getInstance().queueUpdate(id, "Creating instance");
        // create an instance
        Instance instance = AWSEC2.createAndInitInstance(ec2, region, amiRecord, nodeName, keyName, amiRecord.getSecurityGroup(), installScript, tags, elasticIP, timeout);
        if (instance != null) {
            //        StatusModel.getInstance().queueUpdate(id, "Instance created");
            //        StatusModel.getInstance().queueUpdate(id, StatusEntry.State.INITIALIZING);
            // toString our ip
            String hostname = instance.getPublicDnsName();
            InetAddress inetAddress = InetAddress.getByName(hostname);
            String ip = inetAddress.getHostAddress();
            // and take a guess at the location (lat, long) of this host
            Point2D location = GEOLocator.lookupIPLocation(ip);
            //        StatusModel.getInstance().queueUpdate(id, hostname, ip, location);
            // update our table of instance information
            hostTable.put(id, new HostInfo(id, hostname, location));
        // and we're done
        //        StatusModel.getInstance().queueUpdate(id, "Waiting for other servers");
        } else {
            System.out.println("EC2 Instance " + idString + " in " + region.name() + " did not in start.");
            //        StatusModel.getInstance().queueUpdate(id, StatusEntry.State.ERROR, "Did not start");
            hostsThatDidNotStart.put(id, id);
        }
    } catch (IOException e) {
        System.out.println("Problem creating EC2 instance " + idString + " in " + region.name() + ": " + e);
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        System.out.println("Problem creating EC2 instance " + idString + " in " + region.name() + ": " + e);
        e.printStackTrace();
    }
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AMIRecord(edu.umass.cs.aws.support.AMIRecord) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Instance(com.amazonaws.services.ec2.model.Instance) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) IOException(java.io.IOException) AWSCredentials(com.amazonaws.auth.AWSCredentials) Point2D(java.awt.geom.Point2D) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) File(java.io.File) InetAddress(java.net.InetAddress)

Example 63 with Instance

use of software.amazon.awssdk.services.ec2.model.Instance in project GNS by MobilityFirst.

the class AWSStatusCheck method main.

/**
   *
   * @param args
   * @throws Exception
   */
public static void main(String[] args) throws Exception {
    init();
    /*
     * Amazon EC2
     */
    for (String endpoint : endpoints) {
        try {
            ec2.setEndpoint(endpoint);
            System.out.println("**** Endpoint: " + endpoint);
            DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
            System.out.println("You have access to " + availabilityZonesResult.getAvailabilityZones().size() + " Availability Zones.");
            for (AvailabilityZone zone : availabilityZonesResult.getAvailabilityZones()) {
                System.out.println(zone.getZoneName());
            }
            DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
            List<Reservation> reservations = describeInstancesRequest.getReservations();
            Set<Instance> instances = new HashSet<Instance>();
            System.out.println("Instances: ");
            for (Reservation reservation : reservations) {
                for (Instance instance : reservation.getInstances()) {
                    instances.add(instance);
                    System.out.println(instance.getPublicDnsName() + " is " + instance.getState().getName());
                }
            }
            System.out.println("Security groups: ");
            DescribeSecurityGroupsResult describeSecurityGroupsResult = ec2.describeSecurityGroups();
            for (SecurityGroup securityGroup : describeSecurityGroupsResult.getSecurityGroups()) {
                System.out.println(securityGroup.getGroupName());
            }
        //System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running.");
        } catch (AmazonServiceException ase) {
            System.out.println("Caught Exception: " + ase.getMessage());
            System.out.println("Reponse Status Code: " + ase.getStatusCode());
            System.out.println("Error Code: " + ase.getErrorCode());
            System.out.println("Request ID: " + ase.getRequestId());
        }
        /*
       * Amazon SimpleDB
       *
       */
        try {
            ListDomainsRequest sdbRequest = new ListDomainsRequest().withMaxNumberOfDomains(100);
            ListDomainsResult sdbResult = sdb.listDomains(sdbRequest);
            int totalItems = 0;
            for (String domainName : sdbResult.getDomainNames()) {
                DomainMetadataRequest metadataRequest = new DomainMetadataRequest().withDomainName(domainName);
                DomainMetadataResult domainMetadata = sdb.domainMetadata(metadataRequest);
                totalItems += domainMetadata.getItemCount();
            }
            System.out.println("You have " + sdbResult.getDomainNames().size() + " Amazon SimpleDB domain(s)" + "containing a total of " + totalItems + " items.");
        } catch (AmazonServiceException ase) {
            System.out.println("Caught Exception: " + ase.getMessage());
            System.out.println("Reponse Status Code: " + ase.getStatusCode());
            System.out.println("Error Code: " + ase.getErrorCode());
            System.out.println("Request ID: " + ase.getRequestId());
        }
        /*
       * Amazon S3
       *.
       */
        try {
            List<Bucket> buckets = s3.listBuckets();
            long totalSize = 0;
            int totalItems = 0;
            for (Bucket bucket : buckets) {
                /*
           * In order to save bandwidth, an S3 object listing does not
           * contain every object in the bucket; after a certain point the
           * S3ObjectListing is truncated, and further pages must be
           * obtained with the AmazonS3Client.listNextBatchOfObjects()
           * method.
           */
                ObjectListing objects = s3.listObjects(bucket.getName());
                do {
                    for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                        totalSize += objectSummary.getSize();
                        totalItems++;
                    }
                    objects = s3.listNextBatchOfObjects(objects);
                } while (objects.isTruncated());
            }
            System.out.println("You have " + buckets.size() + " Amazon S3 bucket(s), " + "containing " + totalItems + " objects with a total size of " + totalSize + " bytes.");
        } catch (AmazonServiceException ase) {
            /*
         * AmazonServiceExceptions represent an error response from an AWS
         * services, i.e. your request made it to AWS, but the AWS service
         * either found it invalid or encountered an error trying to execute
         * it.
         */
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            /*
         * AmazonClientExceptions represent an error that occurred inside
         * the client on the local host, either while trying to send the
         * request to AWS or interpret the response. For example, if no
         * network connection is available, the client won't be able to
         * connect to AWS to execute a request and will throw an
         * AmazonClientException.
         */
            System.out.println("Error Message: " + ace.getMessage());
        }
    }
}
Also used : ListDomainsResult(com.amazonaws.services.simpledb.model.ListDomainsResult) Instance(com.amazonaws.services.ec2.model.Instance) DomainMetadataResult(com.amazonaws.services.simpledb.model.DomainMetadataResult) AmazonClientException(com.amazonaws.AmazonClientException) AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) DescribeAvailabilityZonesResult(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) DomainMetadataRequest(com.amazonaws.services.simpledb.model.DomainMetadataRequest) Reservation(com.amazonaws.services.ec2.model.Reservation) Bucket(com.amazonaws.services.s3.model.Bucket) ListDomainsRequest(com.amazonaws.services.simpledb.model.ListDomainsRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult) HashSet(java.util.HashSet)

Example 64 with Instance

use of software.amazon.awssdk.services.ec2.model.Instance in project GNS by MobilityFirst.

the class AWSEC2 method createInstanceAndWait.

/**
   * Create an Instance
   *
   * @param ec2
   * @param amiRecord
   * @param key
   * @param securityGroup
   * @return the instanceID string
   */
public static String createInstanceAndWait(AmazonEC2 ec2, AMIRecord amiRecord, String key, SecurityGroup securityGroup) {
    RunInstancesRequest runInstancesRequest;
    if (amiRecord.getVpcSubnet() != null) {
        System.out.println("subnet: " + amiRecord.getVpcSubnet() + " securityGroup: " + securityGroup.getGroupName());
        // new VPC
        runInstancesRequest = new RunInstancesRequest().withMinCount(1).withMaxCount(1).withImageId(amiRecord.getName()).withInstanceType(amiRecord.getInstanceType()).withKeyName(key).withSubnetId(amiRecord.getVpcSubnet()).withSecurityGroupIds(Arrays.asList(securityGroup.getGroupId()));
    } else {
        runInstancesRequest = new RunInstancesRequest(amiRecord.getName(), 1, 1);
        runInstancesRequest.setInstanceType(amiRecord.getInstanceType());
        runInstancesRequest.setSecurityGroups(new ArrayList<>(Arrays.asList(securityGroup.getGroupName())));
        runInstancesRequest.setKeyName(key);
    }
    RunInstancesResult runInstancesResult = ec2.runInstances(runInstancesRequest);
    Instance instance = runInstancesResult.getReservation().getInstances().get(0);
    String createdInstanceId = instance.getInstanceId();
    System.out.println("Waiting for instance " + amiRecord.getName() + " to start");
    long startTime = System.currentTimeMillis();
    do {
        ThreadUtils.sleep(1000);
        if (System.currentTimeMillis() - startTime > 90000) {
            // give it a minute and a half
            System.out.println(createdInstanceId + " timed out waiting for start.");
            return null;
        }
        // regrab the instance data from the server
        instance = findInstance(ec2, createdInstanceId);
        //System.out.print(instance.getState().getName());
        System.out.print(".");
    } while (instance != null && !instance.getState().getName().equals(InstanceStateRecord.RUNNING.getName()));
    System.out.println();
    return createdInstanceId;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest)

Example 65 with Instance

use of software.amazon.awssdk.services.ec2.model.Instance in project GNS by MobilityFirst.

the class AWSEC2 method describeInstanceDNSAndIP.

/**
   * Print public DNS and IP of an instance
   *
   * @param ec2
   * @param createdInstanceId
   */
public static void describeInstanceDNSAndIP(AmazonEC2 ec2, String createdInstanceId) {
    Instance instance = findInstance(ec2, createdInstanceId);
    if (instance != null) {
        StringBuilder output = new StringBuilder();
        output.append("The public DNS is: ").append(instance.getPublicDnsName());
        output.append(NEWLINE);
        output.append("The private IP is: ").append(instance.getPrivateIpAddress());
        output.append(NEWLINE);
        output.append("The public IP is: ").append(instance.getPublicIpAddress());
        GNSConfig.getLogger().info(output.toString());
    }
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)86 ArrayList (java.util.ArrayList)43 Reservation (com.amazonaws.services.ec2.model.Reservation)39 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)31 List (java.util.List)23 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)22 HashMap (java.util.HashMap)22 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)18 Operation (com.vmware.xenon.common.Operation)18 Tag (com.amazonaws.services.ec2.model.Tag)17 Map (java.util.Map)17 Test (org.junit.Test)16 Filter (com.amazonaws.services.ec2.model.Filter)14 HashSet (java.util.HashSet)13 Volume (com.amazonaws.services.ec2.model.Volume)12 Utils (com.vmware.xenon.common.Utils)12 TimeUnit (java.util.concurrent.TimeUnit)12 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)11 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)11 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)10