Example usage for com.amazonaws.util EC2MetadataUtils getUserData

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

Introduction

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

Prototype

public static String getUserData() 

Source Link

Document

Get the metadata sent to the instance

Usage

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// www .j  a v  a2  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:org.springframework.cloud.aws.core.env.ec2.AmazonEc2InstanceDataPropertySource.java

License:Apache License

private Map<String, String> getUserData() {
    if (this.cachedUserData == null) {
        Map<String, String> userDataMap = new LinkedHashMap<>();
        String userData = null;// w w w  .  j  ava  2  s .  c om
        try {
            userData = EC2MetadataUtils.getUserData();
        } catch (AmazonClientException e) {
            //Suppress exception if we are not able to contact the service,
            //because that is quite often the case if we run in unit tests outside the environment.
            LOGGER.warn("Error getting instance user-data error message is '{}'", e.getMessage());
        }
        if (StringUtils.hasText(userData)) {
            String[] userDataAttributes = userData.split(this.userDataAttributeSeparator);
            for (String userDataAttribute : userDataAttributes) {
                String[] userDataAttributesParts = StringUtils.split(userDataAttribute,
                        this.userDataValueSeparator);
                if (userDataAttributesParts != null && userDataAttributesParts.length > 0) {
                    String key = userDataAttributesParts[0];

                    String value = null;
                    if (userDataAttributesParts.length > 1) {
                        value = userDataAttributesParts[1];
                    }

                    userDataMap.put(key, value);
                }
            }
        }
        this.cachedUserData = Collections.unmodifiableMap(userDataMap);
    }

    return this.cachedUserData;
}