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);
}
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);
}
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);
}
Aggregations