com.amazonaws.util.EC2MetadataUtils Java Examples
The following examples show how to use
com.amazonaws.util.EC2MetadataUtils.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AmazonEc2InstanceDataPropertySource.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Override public Object getProperty(String name) { Map<String, String> userData = getUserData(); if (userData.containsKey(name)) { return userData.get(name); } if (!KNOWN_PROPERTY_NAMES.containsKey(getRootPropertyName(name))) { return null; } try { return EC2MetadataUtils.getData(EC2_METADATA_ROOT + "/" + name); } 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 meta-data with name '{}' error message is '{}'", name, e.getMessage()); return null; } }
Example #2
Source File: HostnameUtil.java From genie with Apache License 2.0 | 6 votes |
/** * Get the local hostname string. * This implementation actually return an IP address string. * * @return a hostname string * @throws UnknownHostException if hostname resolution fails */ public static String getHostname() throws UnknownHostException { final String hostname; if (AwsCloudEnvironmentCheckUtils.isRunningOnCloudEnvironment()) { hostname = EC2MetadataUtils.getPrivateIpAddress(); } else { // Fallback if not on AWS hostname = InetAddress.getLocalHost().getCanonicalHostName(); } if (StringUtils.isBlank(hostname)) { throw new IllegalStateException("Unable to create a Genie Host Info instance as hostname is blank"); } return hostname; }
Example #3
Source File: BaragonAgentEc2Metadata.java From Baragon with Apache License 2.0 | 6 votes |
private static Optional<String> findVpc() { try { Optional<String> maybeManuallySet = Optional.fromNullable(System.getenv("VPC_ID")); if (maybeManuallySet.isPresent()) { return maybeManuallySet; } List<EC2MetadataUtils.NetworkInterface> networkInterfaces = EC2MetadataUtils.getNetworkInterfaces(); if (EC2MetadataUtils.getNetworkInterfaces().isEmpty()) { return Optional.absent(); } else { return Optional.fromNullable(networkInterfaces.get(0).getVpcId()); } } catch (Exception e) { return Optional.absent(); } }
Example #4
Source File: AWSObjectStoreFactory.java From athenz with Apache License 2.0 | 6 votes |
String getAuthToken(String hostname, int port, String rdsUser, String rdsIamRole) { InstanceProfileCredentialsProvider awsCredProvider = new InstanceProfileCredentialsProvider(true); if (LOG.isDebugEnabled()) { LOG.debug("getAuthToken: Access key id: {}", awsCredProvider.getCredentials().getAWSAccessKeyId()); } RdsIamAuthTokenGenerator generator = RdsIamAuthTokenGenerator.builder() .credentials(awsCredProvider) .region(EC2MetadataUtils.getEC2InstanceRegion()) .build(); if (LOG.isDebugEnabled()) { LOG.debug("Instance {} Port {} User {} Region: {} Role: {}", hostname, port, rdsUser, EC2MetadataUtils.getEC2InstanceRegion(), rdsIamRole); } return generator.getAuthToken(GetIamAuthTokenRequest.builder() .hostname(hostname).port(port).userName(rdsUser) .build()); }
Example #5
Source File: DynamoDBUtil.java From emr-dynamodb-connector with Apache License 2.0 | 6 votes |
/** * Calculates DynamoDB end-point. * * Algorithm details: * <ol> * <li> Use endpoint in job configuration "dynamodb.endpoint" value if available * <li> Use endpoint from region in job configuration "dynamodb.region" value if available * <li> Use endpoint from region in job configuration "dynamodb.regionid" value if available * <li> Use endpoint from EC2 Metadata of instance if available * <li> If all previous attempts at retrieving endpoint fail, default to us-east-1 endpoint * </ol> * * @param conf Job Configuration * @param region optional preferred region * @return end-point for DynamoDb service */ public static String getDynamoDBEndpoint(Configuration conf, String region) { String endpoint = getValueFromConf(conf, DynamoDBConstants.ENDPOINT); if (Strings.isNullOrEmpty(endpoint)) { if (Strings.isNullOrEmpty(region)) { region = getValueFromConf(conf, DynamoDBConstants.REGION); } if (Strings.isNullOrEmpty(region)) { region = getValueFromConf(conf, DynamoDBConstants.REGION_ID); } if (Strings.isNullOrEmpty(region)) { try { region = EC2MetadataUtils.getEC2InstanceRegion(); } catch (Exception e) { log.warn(String.format("Exception when attempting to get AWS region information. Will " + "ignore and default " + "to %s", DynamoDBConstants.DEFAULT_AWS_REGION), e); } } if (Strings.isNullOrEmpty(region)) { region = DynamoDBConstants.DEFAULT_AWS_REGION; } endpoint = RegionUtils.getRegion(region).getServiceEndpoint(ServiceAbbreviations.Dynamodb); } log.info("Using endpoint for DynamoDB: " + endpoint); return endpoint; }
Example #6
Source File: Ec2MetadataRegionProvider.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
protected Region getCurrentRegion() { try { InstanceInfo instanceInfo = EC2MetadataUtils.getInstanceInfo(); return instanceInfo != null && instanceInfo.getRegion() != null ? RegionUtils.getRegion(instanceInfo.getRegion()) : null; } catch (AmazonClientException e) { return null; } }
Example #7
Source File: CustomRegionProviderChain.java From strongbox with Apache License 2.0 | 5 votes |
private Optional<Region> getRegionFromMetadata() { try { Region resolvedRegion = null; if (EC2MetadataUtils.getInstanceInfo() != null) { if (EC2MetadataUtils.getInstanceInfo().getRegion() != null) { resolvedRegion = Region.fromName(EC2MetadataUtils.getInstanceInfo().getRegion()); } else { // fallback to provider chain if region is not exposed resolvedRegion = Region.fromName(new DefaultAwsRegionProviderChain().getRegion()); } } return Optional.ofNullable(resolvedRegion); } catch (SdkClientException e) { return Optional.empty(); } }
Example #8
Source File: BaragonAgentEc2Metadata.java From Baragon with Apache License 2.0 | 5 votes |
private static Optional<String> findPrivateIp() { try { return Optional.fromNullable(EC2MetadataUtils.getPrivateIpAddress()); } catch (Exception e) { return Optional.absent(); } }
Example #9
Source File: BaragonAgentEc2Metadata.java From Baragon with Apache License 2.0 | 5 votes |
private static Optional<String> findSubnet() { try { List<EC2MetadataUtils.NetworkInterface> networkInterfaces = EC2MetadataUtils.getNetworkInterfaces(); if (EC2MetadataUtils.getNetworkInterfaces().isEmpty()) { return Optional.absent(); } else { return Optional.fromNullable(networkInterfaces.get(0).getSubnetId()); } } catch (Exception e) { return Optional.absent(); } }
Example #10
Source File: BaragonAgentEc2Metadata.java From Baragon with Apache License 2.0 | 5 votes |
public static Optional<String> findAvailabilityZone() { try { return Optional.fromNullable(EC2MetadataUtils.getAvailabilityZone()); } catch (Exception e) { return Optional.absent(); } }
Example #11
Source File: BaragonAgentEc2Metadata.java From Baragon with Apache License 2.0 | 5 votes |
public static Optional<String> findInstanceId() { try { return Optional.fromNullable(EC2MetadataUtils.getInstanceId()); } catch (Exception e) { return Optional.absent(); } }
Example #12
Source File: RunApplication.java From vertx-deploy-tools with Apache License 2.0 | 5 votes |
private Observable<DeployApplicationRequest> startApplication(DeployApplicationRequest deployApplicationRequest) { List<String> command = new ArrayList<>(Arrays.asList(deployConfig.getVertxHome().resolve("bin/vertx").toString(), "start", getMavenCommand(deployApplicationRequest), "-id", deployApplicationRequest.getModuleId())); if (deployConfig.isMavenRemote()) { command.add("-Dvertx.maven.remoteRepos=" + buildRemoteRepo()); command.add("-Dvertx.maven.remoteSnapshotPolicy=" + deployConfig.getRemoteRepoPolicy()); } String applicationConfig = deployApplicationRequest.getConfigLocation().isEmpty() ? deployConfig.getConfigLocation() : deployApplicationRequest.getConfigLocation(); if (!applicationConfig.isEmpty()) { command.add("-conf"); command.add(applicationConfig); } if (!deployApplicationRequest.getJavaOpts().isEmpty() || !deployConfig.getDefaultJavaOpts().isEmpty()) { command.add("--java-opts"); command.add(deployApplicationRequest.getJavaOpts()); command.add(deployConfig.getDefaultJavaOpts()); } command.add("--instances"); command.add(deployApplicationRequest.getInstances()); if (deployConfig.asCluster()) { command.add("-cluster"); if (deployConfig.isAwsEnabled()) { command.add("-cluster-host"); command.add(EC2MetadataUtils.getPrivateIpAddress()); } } command.add("-Dvertxdeploy.port=" + deployConfig.getHttpPort()); command.add("-Dvertxdeploy.scope.test=" + deployApplicationRequest.isTestScope()); ProcessBuilder processBuilder = new ProcessBuilder().command(command); ObservableCommand<DeployApplicationRequest> observableCommand = new ObservableCommand<>(deployApplicationRequest, 0, rxVertx); return observableCommand.execute(processBuilder) .flatMap(exitCode -> handleExitCode(deployApplicationRequest, exitCode)) .flatMap(x -> just(deployApplicationRequest)) .doOnCompleted(() -> LOG.info("[{} - {}]: Started module '{}' with applicationID '{}'", LogConstants.DEPLOY_REQUEST, deployApplicationRequest.getId(), deployApplicationRequest.getModuleId(), deployApplicationRequest.getMavenArtifactId())) .doOnError(t -> LOG.error("[{} - {}]: Failed to initialize application {} with error '{}'", LogConstants.DEPLOY_REQUEST, deployApplicationRequest.getId(), deployApplicationRequest.getModuleId(), t)); }
Example #13
Source File: AmazonWebserviceProfileValueSource.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Override public String get(String key) { if ("amazon-webservice-region-available".equals(key)) { return Boolean.toString(EC2MetadataUtils.getAvailabilityZone() != null); } return null; }
Example #14
Source File: TestStackInstanceIdService.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
private static void clearMetadataCache() { try { Field metadataCacheField = EC2MetadataUtils.class.getDeclaredField("cache"); metadataCacheField.setAccessible(true); metadataCacheField.set(null, new HashMap<String, String>()); } catch (Exception e) { throw new IllegalStateException("Unable to clear metadata cache in '" + EC2MetadataUtils.class.getName() + "'", e); } }
Example #15
Source File: MetaDataServer.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
private static void resetMetaDataCache() { try { Field metadataCacheField = EC2MetadataUtils.class.getDeclaredField("cache"); metadataCacheField.setAccessible(true); metadataCacheField.set(null, new HashMap<String, String>()); } catch (Exception e) { throw new IllegalStateException("Unable to clear metadata cache in '" + EC2MetadataUtils.class.getName() + "'", e); } }
Example #16
Source File: AwsCloudEnvironmentCheckUtils.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
public static boolean isRunningOnCloudEnvironment() { if (isCloudEnvironment == null) { try { isCloudEnvironment = EC2MetadataUtils .getData(EC2_METADATA_ROOT + "/instance-id", 1) != null; } catch (AmazonClientException e) { isCloudEnvironment = false; } } return isCloudEnvironment; }
Example #17
Source File: MetaDataServer.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
private static void resetMetaDataCache() { try { Field metadataCacheField = EC2MetadataUtils.class.getDeclaredField("cache"); metadataCacheField.setAccessible(true); metadataCacheField.set(null, new HashMap<String, String>()); } catch (Exception e) { throw new IllegalStateException("Unable to clear metadata cache in '" + EC2MetadataUtils.class.getName() + "'", e); } }
Example #18
Source File: AmazonEc2InstanceDataPropertySource.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
private Map<String, String> getUserData() { if (this.cachedUserData == null) { Map<String, String> userDataMap = new LinkedHashMap<>(); String userData = null; 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; }
Example #19
Source File: DynamoDBUtilTest.java From emr-dynamodb-connector with Apache License 2.0 | 5 votes |
@Before public void setUp() { PowerMockito.spy(RegionUtils.class); PowerMockito.mockStatic(EC2MetadataUtils.class); region = mock(Region.class); conf.clear(); }
Example #20
Source File: InstanceProfileCredentialsProvider.java From bazel with Apache License 2.0 | 5 votes |
@Override public URI getCredentialsEndpoint() throws URISyntaxException, IOException { String host = EC2MetadataUtils.getHostAddressForEC2MetadataService(); String securityCredentialsList = EC2CredentialsUtils.getInstance().readResource(new URI(host + EC2MetadataUtils.SECURITY_CREDENTIALS_RESOURCE)); String[] securityCredentials = securityCredentialsList.trim().split("\n"); if (securityCredentials.length == 0) { throw new SdkClientException("Unable to load credentials path"); } return new URI(host + EC2MetadataUtils.SECURITY_CREDENTIALS_RESOURCE + securityCredentials[0]); }
Example #21
Source File: ServerInstanceContext.java From chassis with Apache License 2.0 | 5 votes |
private ServerInstanceContext(){ amazonElasticLoadBalancing = new AmazonElasticLoadBalancingClient(); amazonEC2 = new AmazonEC2Client(); ec2MetadataClient = new Ec2MetadataClient() { @Override 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(); }
Example #22
Source File: DynamoDBUtilTest.java From emr-dynamodb-connector with Apache License 2.0 | 5 votes |
@Test public void getsEndpointFromEc2InstanceRegion() { when(EC2MetadataUtils.getEC2InstanceRegion()).thenReturn("ec2-instance-region"); when(RegionUtils.getRegion("ec2-instance-region")).thenReturn(region); when(region.getServiceEndpoint(ServiceAbbreviations.Dynamodb)).thenReturn(TEST_ENDPOINT); assertEquals(TEST_ENDPOINT, DynamoDBUtil.getDynamoDBEndpoint(conf, null)); PowerMockito.verifyStatic(); EC2MetadataUtils.getEC2InstanceRegion(); verify(region, never()).getServiceEndpoint(TEST_REGION); }
Example #23
Source File: CloudWatchReporterFactory.java From dropwizard-metrics-cloudwatch with Apache License 2.0 | 5 votes |
protected Region region() { String az = null; if (isEC2MetadataAvailable()) { az = EC2MetadataUtils.getAvailabilityZone(); } String regionName = awsRegion; if (!Strings.isNullOrEmpty(az)) { regionName = az.substring(0, az.length() - 1); // strip the AZ letter } return RegionUtils.getRegion(regionName); }
Example #24
Source File: CloudWatchReporterFactory.java From dropwizard-metrics-cloudwatch with Apache License 2.0 | 5 votes |
protected String machineId() { String machine = machineDimension; if (machine == null && isEC2MetadataAvailable()) { machine = EC2MetadataUtils.getInstanceId(); } if (Strings.isNullOrEmpty(machine)) { machine = "localhost"; } return machine; }
Example #25
Source File: EC2LocalityInfoProvider.java From singer with Apache License 2.0 | 5 votes |
@Override public void init(Map<String, String> conf) throws IllegalArgumentException, IOException { // check if EC2 AZ info is working try { EC2MetadataUtils.getAvailabilityZone(); // cache locality info to avoid service based lookups cachedLocality = EC2MetadataUtils.getAvailabilityZone(); } catch (Exception e) { LOG.error("Failed to get AZ info", e); Stats.addMetric(SingerMetrics.LOCALITY_MISSING, 1); cachedLocality = LOCALITY_NOT_AVAILABLE; } }
Example #26
Source File: DynamoDBUtilTest.java From emr-dynamodb-connector with Apache License 2.0 | 5 votes |
@Test public void getsEndpointFromDefaultAwsRegion() { PowerMockito.mockStatic(RegionUtils.class); when(EC2MetadataUtils.getEC2InstanceRegion()).thenThrow(new AmazonClientException("Unable to " + "get region from EC2 instance data")); when(RegionUtils.getRegion(DynamoDBConstants.DEFAULT_AWS_REGION)).thenReturn(region); when(region.getServiceEndpoint(ServiceAbbreviations.Dynamodb)).thenReturn(TEST_ENDPOINT); assertEquals(TEST_ENDPOINT, DynamoDBUtil.getDynamoDBEndpoint(conf, null)); PowerMockito.verifyStatic(); RegionUtils.getRegion(DynamoDBConstants.DEFAULT_AWS_REGION); }
Example #27
Source File: Worker.java From conductor with Apache License 2.0 | 5 votes |
/** * Override this method to app specific rules. * * @return returns the serverId as the id of the instance that the worker is running. */ default String getIdentity() { String serverId; try { serverId = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { serverId = System.getenv("HOSTNAME"); } if (serverId == null) { serverId = (EC2MetadataUtils.getInstanceId() == null) ? System.getProperty("user.name") : EC2MetadataUtils.getInstanceId(); } LoggerHolder.logger.debug("Setting worker id to {}", serverId); return serverId; }
Example #28
Source File: AwsElbUtil.java From vertx-deploy-tools with Apache License 2.0 | 4 votes |
public AwsElbUtil(DeployConfig config) { this.elbAsyncClient = AmazonElasticLoadBalancingAsyncClientBuilder.standard().withRegion(config.getAwsRegion()).build(); this.instanceId = EC2MetadataUtils.getInstanceId(); }
Example #29
Source File: Metrics.java From StubbornJava with MIT License | 4 votes |
private static String getHost() { return EC2MetadataUtils.getLocalHostName().split("\\.")[0]; }
Example #30
Source File: Metrics.java From StubbornJava with MIT License | 4 votes |
private static String getHost() { return EC2MetadataUtils.getLocalHostName().split("\\.")[0]; }