Search in sources :

Example 56 with AmazonEC2

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

the class Amazontool method main.

/**
   * @param args the command line arguments
   * @throws java.io.IOException
   */
public static void main(String[] args) throws IOException {
    AWSCredentials credentials = new PropertiesCredentials(AWSEC2.class.getResourceAsStream("resources/AwsCredentials.properties"));
    //Create Amazon Client object
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);
    AWSEC2.describeAllEndpoints(ec2);
//    Instance instance = AWSEC2.findInstance(ec2, "i-86983af6");
//    Address elasticIP = AWSEC2.findElasticIP(ec2, "23.21.120.250");
//    System.out.println(instance.getPublicDnsName());
//    System.out.println(elasticIP.getPublicIp());
//AWSEC2.associateAddress(ec2, "23.21.120.250", instance);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AWSCredentials(com.amazonaws.auth.AWSCredentials)

Example 57 with AmazonEC2

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

the class AWSEC2 method createAndInitInstance.

/**
   * Creates an EC2 instance in the region given. Timeout in milleseconds can be specified.
   *
   * @param ec2
   * @param region
   * @param amiRecord
   * @param instanceName
   * @param keyName
   * @param securityGroupName
   * @param script
   * @param tags
   * @param elasticIP
   * @param timeout
   * @return a new instance instance
   */
public static Instance createAndInitInstance(AmazonEC2 ec2, RegionRecord region, AMIRecord amiRecord, String instanceName, String keyName, String securityGroupName, String script, Map<String, String> tags, String elasticIP, int timeout) {
    try {
        // set the region (AKA endpoint)
        setRegion(ec2, region);
        // create the instance
        SecurityGroup securityGroup = findOrCreateSecurityGroup(ec2, securityGroupName);
        String keyPair = findOrCreateKeyPair(ec2, keyName);
        String instanceID = createInstanceAndWait(ec2, amiRecord, keyPair, securityGroup);
        if (instanceID == null) {
            return null;
        }
        System.out.println("Instance " + instanceName + " is running in " + region.name());
        // add a name to the instance
        addInstanceTag(ec2, instanceID, "Name", instanceName);
        if (tags != null) {
            addInstanceTags(ec2, instanceID, tags);
        }
        Instance instance = findInstance(ec2, instanceID);
        if (instance == null) {
            return null;
        }
        String hostname = instance.getPublicDnsName();
        System.out.println("Waiting " + timeout / 1000 + " seconds for " + instanceName + " (" + hostname + ", " + instanceID + ") to be reachable.");
        long startTime = System.currentTimeMillis();
        while (!Pinger.isReachable(hostname, SSHPORT, 2000)) {
            ThreadUtils.sleep(1000);
            System.out.print(".");
            if (System.currentTimeMillis() - startTime > timeout) {
                System.out.println(instanceName + " (" + hostname + ")" + " timed out during reachability check.");
                return null;
            }
        }
        System.out.println();
        System.out.println(instanceName + " (" + hostname + ")" + " is reachable.");
        // associate the elasticIP if one is provided
        if (elasticIP != null) {
            System.out.println("Using ElasticIP " + elasticIP + " for instance " + instanceName + " (" + instanceID + ")");
            AWSEC2.associateAddress(ec2, elasticIP, instance);
            // get a new copy cuz things have changed
            instance = findInstance(ec2, instanceID);
            if (instance == null) {
                return null;
            }
            // recheck reachability
            hostname = instance.getPublicDnsName();
            System.out.println("Waiting " + timeout / 1000 + " s for " + instanceName + " (" + hostname + ", " + instanceID + ") to be reachable after Elastic IP change.");
            startTime = System.currentTimeMillis();
            while (!Pinger.isReachable(hostname, SSHPORT, 2000)) {
                ThreadUtils.sleep(1000);
                System.out.print(".");
                if (System.currentTimeMillis() - startTime > timeout) {
                    // give it a minute and ahalf
                    System.out.println(instanceName + " (" + hostname + ")" + " timed out during second (elastic IP) reachability check.");
                    return null;
                }
            }
            System.out.println();
            System.out.println(instanceName + " (" + hostname + ")" + " is still reachable.");
        }
        if (script != null) {
            File keyFile = new File(KEYHOME + FILESEPARATOR + keyName + PRIVATEKEYFILEEXTENSION);
            ExecuteBash.executeBashScript("ec2-user", hostname, keyFile, true, "installScript.sh", script);
        }
        return instance;
    } 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());
    }
    return null;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AmazonServiceException(com.amazonaws.AmazonServiceException) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) File(java.io.File)

Example 58 with AmazonEC2

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

the class AWSEC2 method importKeyPair.

/**
   *
   * @param ec2
   * @param name
   * @param publicKeyMaterial
   */
public static void importKeyPair(AmazonEC2 ec2, String name, String publicKeyMaterial) {
    ImportKeyPairRequest newKeyRequest = new ImportKeyPairRequest(name, publicKeyMaterial);
    ec2.importKeyPair(newKeyRequest);
}
Also used : ImportKeyPairRequest(com.amazonaws.services.ec2.model.ImportKeyPairRequest)

Example 59 with AmazonEC2

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

the class AWSEC2 method describeKeyPairs.

/**
   * Describe Key Pairs
   *
   * @param ec2
   */
public static void describeKeyPairs(AmazonEC2 ec2) {
    StringBuilder output = new StringBuilder();
    String prefix = currentTab + "Key Pairs: ";
    DescribeKeyPairsResult dkr = ec2.describeKeyPairs();
    for (KeyPairInfo keyPairInfo : dkr.getKeyPairs()) {
        output.append(prefix);
        prefix = ", ";
        output.append(keyPairInfo.getKeyName());
    }
    System.out.println(output);
}
Also used : KeyPairInfo(com.amazonaws.services.ec2.model.KeyPairInfo) DescribeKeyPairsResult(com.amazonaws.services.ec2.model.DescribeKeyPairsResult)

Example 60 with AmazonEC2

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

the class AWSEC2 method findOrCreateSecurityGroup.

/**
   * Returns an existing security group of the given name or creates one if it does not exist.
   *
   * @param ec2
   * @param name
   * @return the name of the group
   */
public static SecurityGroup findOrCreateSecurityGroup(AmazonEC2 ec2, String name) {
    SecurityGroup result = findSecurityGroup(ec2, name);
    if (result == null) {
        createSecurityGroup(ec2, name);
        System.out.println("Created security group " + name);
    }
    return findSecurityGroup(ec2, name);
}
Also used : SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)42 Instance (com.amazonaws.services.ec2.model.Instance)12 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)9 AWSCredentials (com.amazonaws.auth.AWSCredentials)5 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 Address (com.amazonaws.services.ec2.model.Address)5 InstanceInfo (com.netflix.appinfo.InstanceInfo)5 File (java.io.File)5 LinkedList (java.util.LinkedList)5 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)4 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)4 DryRunResult (com.amazonaws.services.ec2.model.DryRunResult)4 Reservation (com.amazonaws.services.ec2.model.Reservation)4 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)4 Tag (com.amazonaws.services.ec2.model.Tag)4 AmazonInfo (com.netflix.appinfo.AmazonInfo)4 AssociateAddressRequest (com.amazonaws.services.ec2.model.AssociateAddressRequest)3 AvailabilityZone (com.amazonaws.services.ec2.model.AvailabilityZone)3 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)3 DescribeAvailabilityZonesResult (com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)3