use of com.amazonaws.services.ec2.model.Reservation in project druid by druid-io.
the class EC2AutoScaler method terminate.
@Override
public AutoScalingData terminate(List<String> ips) {
if (ips.isEmpty()) {
return new AutoScalingData(Lists.<String>newArrayList());
}
DescribeInstancesResult result = amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", ips)));
List<Instance> instances = Lists.newArrayList();
for (Reservation reservation : result.getReservations()) {
instances.addAll(reservation.getInstances());
}
try {
return terminateWithIds(Lists.transform(instances, new Function<Instance, String>() {
@Override
public String apply(Instance input) {
return input.getInstanceId();
}
}));
} catch (Exception e) {
log.error(e, "Unable to terminate any instances.");
}
return null;
}
use of com.amazonaws.services.ec2.model.Reservation in project druid by druid-io.
the class EC2AutoScalerTest method testScale.
@Test
public void testScale() {
RunInstancesResult runInstancesResult = EasyMock.createMock(RunInstancesResult.class);
EC2AutoScaler autoScaler = new EC2AutoScaler(0, 1, ENV_CONFIG, amazonEC2Client, managementConfig);
EasyMock.expect(amazonEC2Client.runInstances(EasyMock.anyObject(RunInstancesRequest.class))).andReturn(runInstancesResult);
EasyMock.expect(amazonEC2Client.describeInstances(EasyMock.anyObject(DescribeInstancesRequest.class))).andReturn(describeInstancesResult);
EasyMock.expect(amazonEC2Client.terminateInstances(EasyMock.anyObject(TerminateInstancesRequest.class))).andReturn(null);
EasyMock.replay(amazonEC2Client);
EasyMock.expect(runInstancesResult.getReservation()).andReturn(reservation).atLeastOnce();
EasyMock.replay(runInstancesResult);
EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Collections.singletonList(reservation)).atLeastOnce();
EasyMock.replay(describeInstancesResult);
EasyMock.expect(reservation.getInstances()).andReturn(Collections.singletonList(instance)).atLeastOnce();
EasyMock.replay(reservation);
AutoScalingData created = autoScaler.provision();
Assert.assertEquals(created.getNodeIds().size(), 1);
Assert.assertEquals("theInstance", created.getNodeIds().get(0));
AutoScalingData deleted = autoScaler.terminate(Arrays.asList("dummyIP"));
Assert.assertEquals(deleted.getNodeIds().size(), 1);
Assert.assertEquals(INSTANCE_ID, deleted.getNodeIds().get(0));
EasyMock.verify(runInstancesResult);
}
use of com.amazonaws.services.ec2.model.Reservation in project elasticsearch by elastic.
the class AwsEc2UnicastHostsProvider method fetchDynamicNodes.
protected List<DiscoveryNode> fetchDynamicNodes() {
List<DiscoveryNode> discoNodes = new ArrayList<>();
DescribeInstancesResult descInstances;
try {
// Query EC2 API based on AZ, instance state, and tag.
// NOTE: we don't filter by security group during the describe instances request for two reasons:
// 1. differences in VPCs require different parameters during query (ID vs Name)
// 2. We want to use two different strategies: (all security groups vs. any security groups)
descInstances = SocketAccess.doPrivileged(() -> client.describeInstances(buildDescribeInstancesRequest()));
} catch (AmazonClientException e) {
logger.info("Exception while retrieving instance list from AWS API: {}", e.getMessage());
logger.debug("Full exception:", e);
return discoNodes;
}
logger.trace("building dynamic unicast discovery nodes...");
for (Reservation reservation : descInstances.getReservations()) {
for (Instance instance : reservation.getInstances()) {
// lets see if we can filter based on groups
if (!groups.isEmpty()) {
List<GroupIdentifier> instanceSecurityGroups = instance.getSecurityGroups();
ArrayList<String> securityGroupNames = new ArrayList<String>();
ArrayList<String> securityGroupIds = new ArrayList<String>();
for (GroupIdentifier sg : instanceSecurityGroups) {
securityGroupNames.add(sg.getGroupName());
securityGroupIds.add(sg.getGroupId());
}
if (bindAnyGroup) {
// We check if we can find at least one group name or one group id in groups.
if (disjoint(securityGroupNames, groups) && disjoint(securityGroupIds, groups)) {
logger.trace("filtering out instance {} based on groups {}, not part of {}", instance.getInstanceId(), instanceSecurityGroups, groups);
// continue to the next instance
continue;
}
} else {
// We need tp match all group names or group ids, otherwise we ignore this instance
if (!(securityGroupNames.containsAll(groups) || securityGroupIds.containsAll(groups))) {
logger.trace("filtering out instance {} based on groups {}, does not include all of {}", instance.getInstanceId(), instanceSecurityGroups, groups);
// continue to the next instance
continue;
}
}
}
String address = null;
if (hostType.equals(PRIVATE_DNS)) {
address = instance.getPrivateDnsName();
} else if (hostType.equals(PRIVATE_IP)) {
address = instance.getPrivateIpAddress();
} else if (hostType.equals(PUBLIC_DNS)) {
address = instance.getPublicDnsName();
} else if (hostType.equals(PUBLIC_IP)) {
address = instance.getPublicIpAddress();
} else if (hostType.startsWith(TAG_PREFIX)) {
// Reading the node host from its metadata
String tagName = hostType.substring(TAG_PREFIX.length());
logger.debug("reading hostname from [{}] instance tag", tagName);
List<Tag> tags = instance.getTags();
for (Tag tag : tags) {
if (tag.getKey().equals(tagName)) {
address = tag.getValue();
logger.debug("using [{}] as the instance address", address);
}
}
} else {
throw new IllegalArgumentException(hostType + " is unknown for discovery.ec2.host_type");
}
if (address != null) {
try {
// we only limit to 1 port per address, makes no sense to ping 100 ports
TransportAddress[] addresses = transportService.addressesFromString(address, 1);
for (int i = 0; i < addresses.length; i++) {
logger.trace("adding {}, address {}, transport_address {}", instance.getInstanceId(), address, addresses[i]);
discoNodes.add(new DiscoveryNode(instance.getInstanceId(), "#cloud-" + instance.getInstanceId() + "-" + i, addresses[i], emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion()));
}
} catch (Exception e) {
final String finalAddress = address;
logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to add {}, address {}", instance.getInstanceId(), finalAddress), e);
}
} else {
logger.trace("not adding {}, address is null, host_type {}", instance.getInstanceId(), hostType);
}
}
}
logger.debug("using dynamic discovery nodes {}", discoNodes);
return discoNodes;
}
use of com.amazonaws.services.ec2.model.Reservation in project SimianArmy by Netflix.
the class AWSClient method describeInstances.
/**
* Describe a set of specific instances.
*
* @param instanceIds the instance ids
* @return the instances
*/
public List<Instance> describeInstances(String... instanceIds) {
if (instanceIds == null || instanceIds.length == 0) {
LOGGER.info(String.format("Getting all EC2 instances in region %s.", region));
} else {
LOGGER.info(String.format("Getting EC2 instances for %d ids in region %s.", instanceIds.length, region));
}
List<Instance> instances = new LinkedList<Instance>();
AmazonEC2 ec2Client = ec2Client();
DescribeInstancesRequest request = new DescribeInstancesRequest();
if (instanceIds != null) {
request.withInstanceIds(Arrays.asList(instanceIds));
}
DescribeInstancesResult result = ec2Client.describeInstances(request);
for (Reservation reservation : result.getReservations()) {
instances.addAll(reservation.getInstances());
}
LOGGER.info(String.format("Got %d EC2 instances in region %s.", instances.size(), region));
return instances;
}
use of com.amazonaws.services.ec2.model.Reservation in project h2o-2 by h2oai.
the class EC2 method resize.
//@formatter:on
/**
* Create or terminate EC2 instances. Uses their Name tag to find existing ones.
*/
public Cloud resize() throws Exception {
AmazonEC2Client ec2 = new AmazonEC2Client(new PersistS3.H2OAWSCredentialsProviderChain());
ec2.setEndpoint("ec2." + region + ".amazonaws.com");
DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
List<Reservation> reservations = describeInstancesResult.getReservations();
List<Instance> instances = new ArrayList<Instance>();
for (Reservation reservation : reservations) {
for (Instance instance : reservation.getInstances()) {
String ip = ip(instance);
if (ip != null) {
String name = null;
if (instance.getTags().size() > 0)
name = instance.getTags().get(0).getValue();
if (NAME.equals(name))
instances.add(instance);
}
}
}
System.out.println("Found " + instances.size() + " EC2 instances for user " + USER);
if (instances.size() > boxes) {
for (int i = 0; i < instances.size() - boxes; i++) {
// TODO terminate?
}
} else if (instances.size() < boxes) {
int launchCount = boxes - instances.size();
System.out.println("Creating " + launchCount + " EC2 instances.");
if (confirm) {
System.out.println("Please confirm [y/n]");
String s = Utils.readConsole();
if (s == null || !s.equalsIgnoreCase("y"))
throw new Exception("Aborted");
}
CreatePlacementGroupRequest group = new CreatePlacementGroupRequest();
group.withGroupName(USER);
group.withStrategy(PlacementStrategy.Cluster);
try {
ec2.createPlacementGroup(group);
} catch (AmazonServiceException ex) {
if (!"InvalidPlacementGroup.Duplicate".equals(ex.getErrorCode()))
throw ex;
}
RunInstancesRequest run = new RunInstancesRequest();
run.withInstanceType(type);
run.withImageId(image);
run.withMinCount(launchCount).withMaxCount(launchCount);
run.withSecurityGroupIds(securityGroup);
Placement placement = new Placement();
placement.setGroupName(USER);
run.withPlacement(placement);
BlockDeviceMapping map = new BlockDeviceMapping();
map.setDeviceName("/dev/sdb");
map.setVirtualName("ephemeral0");
run.withBlockDeviceMappings(map);
run.withUserData(new String(Base64.encodeBase64(cloudConfig.getBytes())));
RunInstancesResult runRes = ec2.runInstances(run);
ArrayList<String> ids = new ArrayList<String>();
for (Instance instance : runRes.getReservation().getInstances()) ids.add(instance.getInstanceId());
List<Instance> created = wait(ec2, ids);
System.out.println("Created " + created.size() + " EC2 instances.");
instances.addAll(created);
}
String[] pub = new String[boxes];
String[] prv = new String[boxes];
for (int i = 0; i < boxes; i++) {
pub[i] = instances.get(i).getPublicIpAddress();
prv[i] = instances.get(i).getPrivateIpAddress();
}
System.out.println("EC2 public IPs: " + Utils.join(' ', pub));
System.out.println("EC2 private IPs: " + Utils.join(' ', prv));
Cloud cloud = new Cloud();
cloud.publicIPs.addAll(Arrays.asList(pub));
cloud.privateIPs.addAll(Arrays.asList(prv));
return cloud;
}
Aggregations