Search in sources :

Example 1 with Tag

use of com.amazonaws.services.s3.model.Tag 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 2 with Tag

use of com.amazonaws.services.s3.model.Tag in project elasticsearch by elastic.

the class Ec2DiscoveryTests method testFilterByMultipleTags.

public void testFilterByMultipleTags() throws InterruptedException {
    int nodes = randomIntBetween(5, 10);
    Settings nodeSettings = Settings.builder().putArray(DISCOVERY_EC2.TAG_SETTING.getKey() + "stage", "prod", "preprod").build();
    int prodInstances = 0;
    List<List<Tag>> tagsList = new ArrayList<>();
    for (int node = 0; node < nodes; node++) {
        List<Tag> tags = new ArrayList<>();
        if (randomBoolean()) {
            tags.add(new Tag("stage", "prod"));
            if (randomBoolean()) {
                tags.add(new Tag("stage", "preprod"));
                prodInstances++;
            }
        } else {
            tags.add(new Tag("stage", "dev"));
            if (randomBoolean()) {
                tags.add(new Tag("stage", "preprod"));
            }
        }
        tagsList.add(tags);
    }
    logger.info("started [{}] instances with [{}] stage=prod tag", nodes, prodInstances);
    List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes, tagsList);
    assertThat(discoveryNodes, hasSize(prodInstances));
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Tag(com.amazonaws.services.ec2.model.Tag) Settings(org.elasticsearch.common.settings.Settings)

Example 3 with Tag

use of com.amazonaws.services.s3.model.Tag in project elasticsearch by elastic.

the class Ec2DiscoveryTests method testReadHostFromTag.

public void testReadHostFromTag() throws InterruptedException, UnknownHostException {
    int nodes = randomIntBetween(5, 10);
    String[] addresses = new String[nodes];
    for (int node = 0; node < nodes; node++) {
        addresses[node] = "192.168.0." + (node + 1);
        poorMansDNS.put("node" + (node + 1), new TransportAddress(InetAddress.getByName(addresses[node]), 9300));
    }
    Settings nodeSettings = Settings.builder().put(DISCOVERY_EC2.HOST_TYPE_SETTING.getKey(), "tag:foo").build();
    List<List<Tag>> tagsList = new ArrayList<>();
    for (int node = 0; node < nodes; node++) {
        List<Tag> tags = new ArrayList<>();
        tags.add(new Tag("foo", "node" + (node + 1)));
        tagsList.add(tags);
    }
    logger.info("started [{}] instances", nodes);
    List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes, tagsList);
    assertThat(discoveryNodes, hasSize(nodes));
    for (DiscoveryNode discoveryNode : discoveryNodes) {
        TransportAddress address = discoveryNode.getAddress();
        TransportAddress expected = poorMansDNS.get(discoveryNode.getName());
        assertEquals(address, expected);
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Matchers.containsString(org.hamcrest.Matchers.containsString) Tag(com.amazonaws.services.ec2.model.Tag) Settings(org.elasticsearch.common.settings.Settings)

Example 4 with Tag

use of com.amazonaws.services.s3.model.Tag in project elasticsearch by elastic.

the class Ec2DiscoveryTests method testFilterByTags.

public void testFilterByTags() throws InterruptedException {
    int nodes = randomIntBetween(5, 10);
    Settings nodeSettings = Settings.builder().put(DISCOVERY_EC2.TAG_SETTING.getKey() + "stage", "prod").build();
    int prodInstances = 0;
    List<List<Tag>> tagsList = new ArrayList<>();
    for (int node = 0; node < nodes; node++) {
        List<Tag> tags = new ArrayList<>();
        if (randomBoolean()) {
            tags.add(new Tag("stage", "prod"));
            prodInstances++;
        } else {
            tags.add(new Tag("stage", "dev"));
        }
        tagsList.add(tags);
    }
    logger.info("started [{}] instances with [{}] stage=prod tag", nodes, prodInstances);
    List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes, tagsList);
    assertThat(discoveryNodes, hasSize(prodInstances));
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Tag(com.amazonaws.services.ec2.model.Tag) Settings(org.elasticsearch.common.settings.Settings)

Example 5 with Tag

use of com.amazonaws.services.s3.model.Tag in project GNS by MobilityFirst.

the class AWSEC2 method addInstanceTags.

/**
   * Adds keys and values from tagmap to the tags of the given instance.
   *
   * @param ec2
   * @param createdInstanceId
   * @param tagmap
   */
public static void addInstanceTags(AmazonEC2 ec2, String createdInstanceId, Map<String, String> tagmap) {
    List<String> resources = new LinkedList<>();
    resources.add(createdInstanceId);
    List<Tag> tags = new LinkedList<>();
    for (Entry<String, String> entry : tagmap.entrySet()) {
        Tag nameTag = new Tag(entry.getKey(), entry.getValue());
        tags.add(nameTag);
    }
    CreateTagsRequest ctr = new CreateTagsRequest(resources, tags);
    ec2.createTags(ctr);
}
Also used : CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Aggregations

Tag (com.amazonaws.services.ec2.model.Tag)38 ArrayList (java.util.ArrayList)30 Tag (com.amazonaws.services.s3.model.Tag)19 HashMap (java.util.HashMap)18 Test (org.junit.Test)17 List (java.util.List)16 Instance (com.amazonaws.services.ec2.model.Instance)15 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)14 Map (java.util.Map)12 HashSet (java.util.HashSet)10 GetObjectTaggingRequest (com.amazonaws.services.s3.model.GetObjectTaggingRequest)9 GetObjectTaggingResult (com.amazonaws.services.s3.model.GetObjectTaggingResult)9 Utils (com.vmware.xenon.common.Utils)9 Set (java.util.Set)9 File (java.io.File)8 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)7 Reservation (com.amazonaws.services.ec2.model.Reservation)7 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)6 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)6 DeferredResult (com.vmware.xenon.common.DeferredResult)6