use of com.amazonaws.services.ec2.model.Region in project eureka by Netflix.
the class EIPManager method getEC2Service.
/**
* Gets the EC2 service object to call AWS APIs.
*
* @return the EC2 service object to call AWS APIs.
*/
private AmazonEC2 getEC2Service() {
String aWSAccessId = serverConfig.getAWSAccessId();
String aWSSecretKey = serverConfig.getAWSSecretKey();
AmazonEC2 ec2Service;
if (null != aWSAccessId && !"".equals(aWSAccessId) && null != aWSSecretKey && !"".equals(aWSSecretKey)) {
ec2Service = new AmazonEC2Client(new BasicAWSCredentials(aWSAccessId, aWSSecretKey));
} else {
ec2Service = new AmazonEC2Client(new InstanceProfileCredentialsProvider());
}
String region = clientConfig.getRegion();
region = region.trim().toLowerCase();
ec2Service.setEndpoint("ec2." + region + ".amazonaws.com");
return ec2Service;
}
use of com.amazonaws.services.ec2.model.Region in project eureka by Netflix.
the class ElasticNetworkInterfaceBinder method getEC2Service.
private AmazonEC2 getEC2Service() {
String aWSAccessId = serverConfig.getAWSAccessId();
String aWSSecretKey = serverConfig.getAWSSecretKey();
AmazonEC2 ec2Service;
if (null != aWSAccessId && !"".equals(aWSAccessId) && null != aWSSecretKey && !"".equals(aWSSecretKey)) {
ec2Service = new AmazonEC2Client(new BasicAWSCredentials(aWSAccessId, aWSSecretKey));
} else {
ec2Service = new AmazonEC2Client(new InstanceProfileCredentialsProvider());
}
String region = clientConfig.getRegion();
region = region.trim().toLowerCase();
ec2Service.setEndpoint("ec2." + region + ".amazonaws.com");
return ec2Service;
}
use of com.amazonaws.services.ec2.model.Region in project ice by Netflix.
the class ReservationCapacityPoller method poll.
@Override
protected void poll() throws Exception {
ProcessorConfig config = ProcessorConfig.getInstance();
// read from s3 if not exists
File file = new File(config.localDir, "reservation_capacity.txt");
if (!file.exists()) {
logger.info("downloading " + file + "...");
AwsUtils.downloadFileIfNotExist(config.workS3BucketName, config.workS3BucketPrefix, file);
logger.info("downloaded " + file);
}
// read from file
Map<String, ReservedInstances> reservations = Maps.newTreeMap();
if (file.exists()) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(",");
String accountId = tokens[0];
String region = tokens[1];
String reservationId = tokens[2];
String zone = tokens[3];
Long start = Long.parseLong(tokens[4]);
long duration = Long.parseLong(tokens[5]);
String instanceType = tokens[6];
String productDescription = tokens[7];
int instanceCount = Integer.parseInt(tokens[8]);
String offeringType = tokens[9];
String state = tokens[10];
Long end = tokens.length > 11 ? Long.parseLong(tokens[11]) : null;
float fixedPrice = tokens.length > 12 ? Float.parseFloat(tokens[12]) : 0;
float usagePrice = tokens.length > 13 ? Float.parseFloat(tokens[13]) : 0;
ReservedInstances reservation = new ReservedInstances().withAvailabilityZone(zone).withStart(new Date(start)).withDuration(duration).withInstanceType(instanceType).withProductDescription(productDescription).withInstanceCount(instanceCount).withOfferingType(offeringType).withState(state).withFixedPrice(fixedPrice).withUsagePrice(usagePrice);
if (end != null)
reservation.setEnd(new Date(end));
else
reservation.setEnd(new Date(start + duration * 1000));
reservations.put(accountId + "," + region + "," + reservationId, reservation);
}
} catch (Exception e) {
logger.error("error in reading " + file, e);
} finally {
if (reader != null)
try {
reader.close();
} catch (Exception e) {
}
}
}
logger.info("read " + reservations.size() + " reservations.");
for (Account account : config.accountService.getReservationAccounts().keySet()) {
try {
AmazonEC2Client ec2Client;
String assumeRole = config.accountService.getReservationAccessRoles().get(account);
if (assumeRole != null) {
String externalId = config.accountService.getReservationAccessExternalIds().get(account);
final Credentials credentials = AwsUtils.getAssumedCredentials(account.id, assumeRole, externalId);
ec2Client = new AmazonEC2Client(new AWSSessionCredentials() {
public String getAWSAccessKeyId() {
return credentials.getAccessKeyId();
}
public String getAWSSecretKey() {
return credentials.getSecretAccessKey();
}
public String getSessionToken() {
return credentials.getSessionToken();
}
});
} else
ec2Client = new AmazonEC2Client(AwsUtils.awsCredentialsProvider.getCredentials(), AwsUtils.clientConfig);
for (Region region : Region.getAllRegions()) {
ec2Client.setEndpoint("ec2." + region.name + ".amazonaws.com");
try {
DescribeReservedInstancesResult result = ec2Client.describeReservedInstances();
for (ReservedInstances reservation : result.getReservedInstances()) {
String key = account.id + "," + region.name + "," + reservation.getReservedInstancesId();
reservations.put(key, reservation);
if (reservation.getEnd() == null)
reservation.setEnd(new Date(reservation.getStart().getTime() + reservation.getDuration() * 1000L));
if (reservation.getFixedPrice() == null)
reservation.setFixedPrice(0f);
if (reservation.getUsagePrice() == null)
reservation.setUsagePrice(0f);
}
} catch (Exception e) {
logger.error("error in describeReservedInstances for " + region.name + " " + account.name, e);
}
}
ec2Client.shutdown();
} catch (Exception e) {
logger.error("Error in describeReservedInstances for " + account.name, e);
}
}
config.reservationService.updateEc2Reservations(reservations);
updatedConfig = true;
// archive to disk
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
for (String key : reservations.keySet()) {
ReservedInstances reservation = reservations.get(key);
String[] line = new String[] { key, reservation.getAvailabilityZone(), reservation.getStart().getTime() + "", reservation.getDuration().toString(), reservation.getInstanceType(), reservation.getProductDescription(), reservation.getInstanceCount().toString(), reservation.getOfferingType(), reservation.getState(), reservation.getEnd().getTime() + "", reservation.getFixedPrice() + "", reservation.getUsagePrice() + "" };
writer.write(StringUtils.join(line, ","));
writer.newLine();
}
} catch (Exception e) {
logger.error("", e);
} finally {
if (writer != null)
try {
writer.close();
} catch (Exception e) {
}
}
logger.info("archived " + reservations.size() + " reservations.");
// archive to s3
logger.info("uploading " + file + "...");
AwsUtils.upload(config.workS3BucketName, config.workS3BucketPrefix, config.localDir, file.getName());
logger.info("uploaded " + file);
}
use of com.amazonaws.services.ec2.model.Region 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();
}
}
use of com.amazonaws.services.ec2.model.Region 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;
}
Aggregations