@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]); } }
private static List<String> getItems(String path, int tries, boolean slurp) { if (tries == 0) throw new SdkClientException( "Unable to contact EC2 metadata service."); List<String> items; try { String hostAddress = getHostAddressForEC2MetadataService(); String response = EC2CredentialsUtils.getInstance().readResource(new URI(hostAddress + path)); if (slurp) items = Collections.singletonList(response); else items = Arrays.asList(response.split("\n")); return items; } catch (AmazonClientException ace) { log.warn("Unable to retrieve the requested metadata (" + path + "). " + ace.getMessage(), ace); return null; } catch (Exception e) { // Retry on any other exceptions int pause = (int) (Math.pow(2, DEFAULT_QUERY_RETRIES - tries) * MINIMUM_RETRY_WAIT_TIME_MILLISECONDS); try { Thread.sleep(pause < MINIMUM_RETRY_WAIT_TIME_MILLISECONDS ? MINIMUM_RETRY_WAIT_TIME_MILLISECONDS : pause); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } return getItems(path, tries - 1, slurp); } }
@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]); } }
private static List<String> getItems(String path, int tries, boolean slurp) { if (tries == 0) throw new SdkClientException( "Unable to contact EC2 metadata service."); List<String> items; try { String hostAddress = getHostAddressForEC2MetadataService(); String response = EC2CredentialsUtils.getInstance().readResource(new URI(hostAddress + path)); if (slurp) items = Collections.singletonList(response); else items = Arrays.asList(response.split("\n")); return items; } catch (AmazonClientException ace) { log.warn("Unable to retrieve the requested metadata (" + path + "). " + ace.getMessage(), ace); return null; } catch (Exception e) { // Retry on any other exceptions int pause = (int) (Math.pow(2, DEFAULT_QUERY_RETRIES - tries) * MINIMUM_RETRY_WAIT_TIME_MILLISECONDS); try { Thread.sleep(pause < MINIMUM_RETRY_WAIT_TIME_MILLISECONDS ? MINIMUM_RETRY_WAIT_TIME_MILLISECONDS : pause); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } return getItems(path, tries - 1, slurp); } }
@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]); } }
private static List<String> getItems(String path, int tries, boolean slurp) { if (tries == 0) throw new SdkClientException( "Unable to contact EC2 metadata service."); List<String> items; try { String hostAddress = getHostAddressForEC2MetadataService(); String response = EC2CredentialsUtils.getInstance().readResource(new URI(hostAddress + path)); if (slurp) items = Collections.singletonList(response); else items = Arrays.asList(response.split("\n")); return items; } catch (AmazonClientException ace) { log.warn("Unable to retrieve the requested metadata."); return null; } catch (Exception e) { // Retry on any other exceptions int pause = (int) (Math.pow(2, DEFAULT_QUERY_RETRIES - tries) * MINIMUM_RETRY_WAIT_TIME_MILLISECONDS); try { Thread.sleep(pause < MINIMUM_RETRY_WAIT_TIME_MILLISECONDS ? MINIMUM_RETRY_WAIT_TIME_MILLISECONDS : pause); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } return getItems(path, tries - 1, slurp); } }
/** This will attempt to retrieve metadata about the instance this code is running on. */ public void fetchMetadata() { HttpClient httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000).build(); HttpUriRequest get = RequestBuilder.get().setUri(EC2MetadataUtils.getHostAddressForEC2MetadataService()) .setConfig(requestConfig).build(); try { httpClient.execute(get); machineImage = EC2MetadataUtils.getAmiId(); instanceType = EC2MetadataUtils.getInstanceType(); instanceId = EC2MetadataUtils.getInstanceId(); // There is a method to get a Region object, but stick to strings for deserialization simplicity. region = EC2MetadataUtils.getEC2InstanceRegion(); // IP address fetching should really not be tied to EC2 but for now this lets us get a useable IP. privateIp = EC2MetadataUtils.getPrivateIpAddress(); //EC2MetadataUtils.getInstanceInfo(); } catch (IOException ex) { LOG.warn("Connection to metadata URL failed, probably not running on EC2."); } }
/** This will attempt to retrieve metadata about the instance this code is running on. */ public void fetchMetadata() { HttpClient httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000).build(); HttpUriRequest get = RequestBuilder.get().setUri(EC2MetadataUtils.getHostAddressForEC2MetadataService()) .setConfig(requestConfig).build(); try { httpClient.execute(get); machineImage = EC2MetadataUtils.getAmiId(); instanceType = EC2MetadataUtils.getInstanceType(); instanceId = EC2MetadataUtils.getInstanceId(); // There is a method to get a Region object, but stick to strings for deserialization simplicity. region = EC2MetadataUtils.getEC2InstanceRegion(); // IP address fetching should really not be tied to EC2 but for now this lets us get a useable IP. privateIp = EC2MetadataUtils.getPrivateIpAddress(); //EC2MetadataUtils.getInstanceInfo(); } catch (IOException ex) { LOG.warn("Connection to metadata URL failed, probably not running on EC2."); } }