Search in sources :

Example 26 with AmazonInfo

use of com.netflix.appinfo.AmazonInfo in project eureka by Netflix.

the class EIPManager method bindEIP.

/**
     * Checks if an EIP is bound and optionally binds the EIP.
     *
     * The list of EIPs are arranged with the EIPs allocated in the zone first
     * followed by other EIPs.
     *
     * If an EIP is already bound to this instance this method simply returns. Otherwise, this method tries to find
     * an unused EIP based on information from AWS. If it cannot find any unused EIP this method, it will be retried
     * for a specified interval.
     *
     * One of the following scenarios can happen here :
     *
     *  1) If the instance is already bound to an EIP as deemed by AWS, no action is taken.
     *  2) If an EIP is already bound to another instance as deemed by AWS, that EIP is skipped.
     *  3) If an EIP is not already bound to an instance and if this instance is not bound to an EIP, then
     *     the EIP is bound to this instance.
     */
public void bindEIP() {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.instanceId);
    String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.availabilityZone);
    Collection<String> candidateEIPs = getCandidateEIPs(myInstanceId, myZone);
    AmazonEC2 ec2Service = getEC2Service();
    boolean isMyinstanceAssociatedWithEIP = false;
    Address selectedEIP = null;
    for (String eipEntry : candidateEIPs) {
        try {
            String associatedInstanceId;
            // Check with AWS, if this EIP is already been used by another instance
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest().withPublicIps(eipEntry);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                associatedInstanceId = eipAddress.getInstanceId();
                // already marked.
                if (((associatedInstanceId == null) || (associatedInstanceId.isEmpty()))) {
                    if (selectedEIP == null) {
                        selectedEIP = eipAddress;
                    }
                } else if (isMyinstanceAssociatedWithEIP = (associatedInstanceId.equals(myInstanceId))) {
                    // This EIP is associated with an instance, check if this is the same as the current instance.
                    // If it is the same, stop searching for an EIP as this instance is already associated with an
                    // EIP
                    selectedEIP = eipAddress;
                    break;
                } else {
                    // The EIP is used by some other instance, hence skip it
                    logger.warn("The selected EIP {} is associated with another instance {} according to AWS," + " hence skipping this", eipEntry, associatedInstanceId);
                }
            }
        } catch (Throwable t) {
            logger.error("Failed to bind elastic IP: {} to {}", eipEntry, myInstanceId, t);
        }
    }
    if (null != selectedEIP) {
        String publicIp = selectedEIP.getPublicIp();
        // Only bind if the EIP is not already associated
        if (!isMyinstanceAssociatedWithEIP) {
            AssociateAddressRequest associateAddressRequest = new AssociateAddressRequest().withInstanceId(myInstanceId);
            String domain = selectedEIP.getDomain();
            if ("vpc".equals(domain)) {
                associateAddressRequest.setAllocationId(selectedEIP.getAllocationId());
            } else {
                associateAddressRequest.setPublicIp(publicIp);
            }
            ec2Service.associateAddress(associateAddressRequest);
            logger.info("\n\n\nAssociated {} running in zone: {} to elastic IP: {}", myInstanceId, myZone, publicIp);
        }
        logger.info("My instance {} seems to be already associated with the EIP {}", myInstanceId, publicIp);
    } else {
        logger.info("No EIP is free to be associated with this instance. Candidate EIPs are: {}", candidateEIPs);
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 27 with AmazonInfo

use of com.netflix.appinfo.AmazonInfo in project eureka by Netflix.

the class ElasticNetworkInterfaceBinder method unbind.

/**
     * Unbind the IP that this instance is associated with.
     */
public void unbind() throws Exception {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId);
    AmazonEC2 ec2 = getEC2Service();
    List<InstanceNetworkInterface> result = instanceData(myInstanceId, ec2).getNetworkInterfaces();
    List<String> ips = getCandidateIps();
    for (InstanceNetworkInterface networkInterface : result) {
        if (ips.contains(networkInterface.getPrivateIpAddress())) {
            String attachmentId = networkInterface.getAttachment().getAttachmentId();
            ec2.detachNetworkInterface(new DetachNetworkInterfaceRequest().withAttachmentId(attachmentId));
            break;
        }
    }
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 28 with AmazonInfo

use of com.netflix.appinfo.AmazonInfo in project eureka by Netflix.

the class Route53Binder method getRegistrationHostnameFromAmazonDataCenterInfo.

private static String getRegistrationHostnameFromAmazonDataCenterInfo(ApplicationInfoManager applicationInfoManager) {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    AmazonInfo dataCenterInfo = (AmazonInfo) myInfo.getDataCenterInfo();
    String ip = dataCenterInfo.get(AmazonInfo.MetaDataKey.publicHostname);
    if (ip == null || ip.length() == 0) {
        return dataCenterInfo.get(AmazonInfo.MetaDataKey.localHostname);
    }
    return ip;
}
Also used : AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 29 with AmazonInfo

use of com.netflix.appinfo.AmazonInfo in project eureka by Netflix.

the class Route53Binder method getMyZone.

private String getMyZone() {
    InstanceInfo info = applicationInfoManager.getInfo();
    AmazonInfo amazonInfo = info != null ? (AmazonInfo) info.getDataCenterInfo() : null;
    String zone = amazonInfo != null ? amazonInfo.get(AmazonInfo.MetaDataKey.availabilityZone) : null;
    if (zone == null) {
        throw new RuntimeException("Cannot extract availabilityZone");
    }
    return zone;
}
Also used : AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 30 with AmazonInfo

use of com.netflix.appinfo.AmazonInfo in project ribbon by Netflix.

the class DefaultNIWSServerListFilterTest method createServer.

private DiscoveryEnabledServer createServer(String host, int port, String zone) {
    AmazonInfo amazonInfo = AmazonInfo.Builder.newBuilder().addMetadata(AmazonInfo.MetaDataKey.availabilityZone, zone).build();
    Builder builder = InstanceInfo.Builder.newBuilder();
    InstanceInfo info = builder.setAppName("l10nservicegeneral").setDataCenterInfo(amazonInfo).setHostName(host).setPort(port).build();
    DiscoveryEnabledServer server = new DiscoveryEnabledServer(info, false, false);
    server.setZone(zone);
    return server;
}
Also used : Builder(com.netflix.appinfo.InstanceInfo.Builder) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Aggregations

AmazonInfo (com.netflix.appinfo.AmazonInfo)30 InstanceInfo (com.netflix.appinfo.InstanceInfo)21 Test (org.junit.Test)7 DataCenterInfo (com.netflix.appinfo.DataCenterInfo)6 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)4 ApplicationInfoManager (com.netflix.appinfo.ApplicationInfoManager)2 Builder (com.netflix.appinfo.InstanceInfo.Builder)2 DiscoveryClient (com.netflix.discovery.DiscoveryClient)2 Address (com.amazonaws.services.ec2.model.Address)1 AssociateAddressRequest (com.amazonaws.services.ec2.model.AssociateAddressRequest)1 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)1 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)1 Ec2EurekaArchaius2InstanceConfig (com.netflix.appinfo.Ec2EurekaArchaius2InstanceConfig)1 EurekaInstanceConfig (com.netflix.appinfo.EurekaInstanceConfig)1 LeaseInfo (com.netflix.appinfo.LeaseInfo)1 MyDataCenterInstanceConfig (com.netflix.appinfo.MyDataCenterInstanceConfig)1 UniqueIdentifier (com.netflix.appinfo.UniqueIdentifier)1 Archaius2VipAddressResolver (com.netflix.appinfo.providers.Archaius2VipAddressResolver)1 VipAddressResolver (com.netflix.appinfo.providers.VipAddressResolver)1 DefaultEurekaClientConfig (com.netflix.discovery.DefaultEurekaClientConfig)1