use of com.amazonaws.services.ec2.AmazonEC2 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());
}
}
use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.
the class AWSEC2 method describeAvailabilityZones.
/**
* Describe Availability Zones
*
* @param ec2
*/
public static void describeAvailabilityZones(AmazonEC2 ec2) {
StringBuilder output = new StringBuilder();
String prefix = currentTab + "Availability Zones: ";
DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
prefix = prefix.concat(" [" + availabilityZonesResult.getAvailabilityZones().size() + " total] ");
for (AvailabilityZone zone : availabilityZonesResult.getAvailabilityZones()) {
output.append(prefix);
prefix = ", ";
output.append(zone.getZoneName());
}
System.out.println(output);
}
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);
}
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;
}
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);
}
Aggregations