Search in sources :

Example 6 with Instance

use of com.amazonaws.services.ec2.model.Instance 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;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Function(com.google.common.base.Function) Reservation(com.amazonaws.services.ec2.model.Reservation) AutoScalingData(io.druid.indexing.overlord.autoscaling.AutoScalingData) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 7 with Instance

use of com.amazonaws.services.ec2.model.Instance 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);
}
Also used : RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) EC2AutoScaler(io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) Test(org.junit.Test)

Example 8 with Instance

use of com.amazonaws.services.ec2.model.Instance in project deeplearning4j by deeplearning4j.

the class Ec2BoxCreator method create.

/**
     * Create the instances
     */
public void create() {
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId(amiId).withInstanceType(size).withKeyName(keyPair).withMinCount(1).withSecurityGroupIds(securityGroupId).withMaxCount(numBoxes);
    AmazonEC2 ec2 = getEc2();
    ec2.setRegion(com.amazonaws.regions.Region.getRegion(regions));
    List<Instance> boxes = ec2.runInstances(runInstancesRequest).getReservation().getInstances();
    if (boxesCreated == null) {
        boxesCreated = new ArrayList<>();
        for (Instance i : boxes) boxesCreated.add(i.getInstanceId());
        log.info("Boxes created " + boxesCreated);
    } else {
        blowupBoxes();
        boxesCreated.clear();
        for (Instance i : boxes) boxesCreated.add(i.getInstanceId());
    }
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Example 9 with Instance

use of com.amazonaws.services.ec2.model.Instance 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;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Instance(com.amazonaws.services.ec2.model.Instance) TransportAddress(org.elasticsearch.common.transport.TransportAddress) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) Collections.disjoint(java.util.Collections.disjoint) AmazonClientException(com.amazonaws.AmazonClientException) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Tag(com.amazonaws.services.ec2.model.Tag)

Example 10 with Instance

use of com.amazonaws.services.ec2.model.Instance in project SimianArmy by Netflix.

the class InstanceInSecurityGroup method getInstanceSecurityGroups.

/**
     * Gets the security groups for a list of instance ids of the same region. The default implementation
     * is using an AWS client. The method can be overridden in subclasses to get the security groups differently.
     * @param region
     *      the region of the instances
     * @param instanceIds
     *      the instance ids, all instances should be in the same region.
     * @return
     *      the map from instance id to the list of security group names the instance has
     */
protected Map<String, List<String>> getInstanceSecurityGroups(String region, String... instanceIds) {
    Map<String, List<String>> result = Maps.newHashMap();
    if (instanceIds == null || instanceIds.length == 0) {
        return result;
    }
    AWSClient awsClient = new AWSClient(region, awsCredentialsProvider);
    for (Instance instance : awsClient.describeInstances(instanceIds)) {
        // Ignore instances that are in VPC
        if (StringUtils.isNotEmpty(instance.getVpcId())) {
            LOGGER.info(String.format("Instance %s is in VPC and is ignored.", instance.getInstanceId()));
            continue;
        }
        if (!"running".equals(instance.getState().getName())) {
            LOGGER.info(String.format("Instance %s is not running, state is %s.", instance.getInstanceId(), instance.getState().getName()));
            continue;
        }
        List<String> sgs = Lists.newArrayList();
        for (GroupIdentifier groupId : instance.getSecurityGroups()) {
            sgs.add(groupId.getGroupName());
        }
        result.put(instance.getInstanceId(), sgs);
    }
    return result;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) List(java.util.List) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)33 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)20 Reservation (com.amazonaws.services.ec2.model.Reservation)15 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)12 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)11 Test (org.junit.Test)10 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)7 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)7 Filter (com.amazonaws.services.ec2.model.Filter)7 Tag (com.amazonaws.services.ec2.model.Tag)7 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)7 Exchange (org.apache.camel.Exchange)7 Processor (org.apache.camel.Processor)7 AmazonServiceException (com.amazonaws.AmazonServiceException)6 ArrayList (java.util.ArrayList)6 LinkedList (java.util.LinkedList)6 AWSCredentials (com.amazonaws.auth.AWSCredentials)5 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)5 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)5