Example usage for com.amazonaws.util EC2MetadataUtils getPrivateIpAddress

List of usage examples for com.amazonaws.util EC2MetadataUtils getPrivateIpAddress

Introduction

In this page you can find the example usage for com.amazonaws.util EC2MetadataUtils getPrivateIpAddress.

Prototype

public static String getPrivateIpAddress() 

Source Link

Document

Get the private IP address of the instance.

Usage

From source file:com.bodybuilding.turbine.servlet.ClusterListServlet.java

License:Apache License

/**
 * Returns the EC2 private address if available
 * @return// ww  w .  j ava  2 s  .  com
 */
private synchronized Optional<String> getPrivateAddress() {
    if (privateAddress == null) {
        // this is expensive so we check only once
        privateAddress = Optional.ofNullable(EC2MetadataUtils.getPrivateIpAddress());
    }

    return privateAddress;
}

From source file:com.kixeye.chassis.bootstrap.aws.ServerInstanceContext.java

License:Apache License

private ServerInstanceContext() {
    amazonElasticLoadBalancing = new AmazonElasticLoadBalancingClient();
    amazonEC2 = new AmazonEC2Client();

    ec2MetadataClient = new Ec2MetadataClient() {
        @Override//from   ww  w .j a  va  2  s. c  o  m
        public String getAvailabilityZone() {
            return EC2MetadataUtils.getAvailabilityZone();
        }

        @Override
        public String getInstanceId() {
            return EC2MetadataUtils.getInstanceId();
        }

        @Override
        public String getUserData() {
            return EC2MetadataUtils.getUserData();
        }

        @Override
        public String getPrivateIpAddress() {
            return EC2MetadataUtils.getPrivateIpAddress();
        }

        @Override
        public String getPublicIpAddress() {
            for (EC2MetadataUtils.NetworkInterface net : EC2MetadataUtils.getNetworkInterfaces()) {
                List<String> ips = net.getPublicIPv4s();
                if (ips != null && ips.size() > 0) {
                    return ips.get(0);
                }
            }
            return null;
        }
    };

    init();
}

From source file:com.netflix.genie.web.configs.aws.AwsMvcConfig.java

License:Apache License

/**
 * Create an instance of {@link GenieHostInfo} using the EC2 metadata service as we're deployed in an AWS cloud
 * environment./*  w  w  w. ja v a 2  s  . com*/
 *
 * @return The {@link GenieHostInfo} instance
 * @throws UnknownHostException If all EC2 host instance calculation AND local resolution can't determine a hostname
 * @throws IllegalStateException If an instance can't be created
 */
@Bean
public GenieHostInfo genieHostInfo() throws UnknownHostException {
    final String ec2LocalHostName = EC2MetadataUtils.getLocalHostName();
    if (StringUtils.isNotBlank(ec2LocalHostName)) {
        return new GenieHostInfo(ec2LocalHostName);
    }

    final String ec2Ipv4Address = EC2MetadataUtils.getPrivateIpAddress();
    if (StringUtils.isNotBlank(ec2Ipv4Address)) {
        return new GenieHostInfo(ec2Ipv4Address);
    }

    final String localHostname = InetAddress.getLocalHost().getCanonicalHostName();
    if (StringUtils.isNotBlank(localHostname)) {
        return new GenieHostInfo(localHostname);
    }

    throw new IllegalStateException("Unable to resolve Genie host info");
}

From source file:com.netflix.genie.web.configs.aws.GenieAwsApiAutoConfiguration.java

License:Apache License

/**
 * Create an instance of {@link GenieHostInfo} using the EC2 metadata service as we're deployed in an AWS cloud
 * environment./* w  w  w.ja  v  a 2 s  .co  m*/
 *
 * @return The {@link GenieHostInfo} instance
 * @throws IllegalStateException If an instance can't be created
 */
@Bean
@ConditionalOnMissingBean(GenieHostInfo.class)
public GenieHostInfo genieHostInfo() {
    final String ec2Ipv4Address = EC2MetadataUtils.getPrivateIpAddress();
    if (StringUtils.isNotBlank(ec2Ipv4Address)) {
        return new GenieHostInfo(ec2Ipv4Address);
    }

    throw new IllegalStateException("Unable to resolve Genie host info");
}