git-off

git off handles large files in git repos
git clone https://noulin.net/git/git-off.git
Log | Files | Refs | README

metadata_service.js (4066B)


      1 var AWS = require('./core');
      2 require('./http');
      3 var inherit = AWS.util.inherit;
      4 
      5 /**
      6  * Represents a metadata service available on EC2 instances. Using the
      7  * {request} method, you can receieve metadata about any available resource
      8  * on the metadata service.
      9  *
     10  * @!attribute [r] httpOptions
     11  *   @return [map] a map of options to pass to the underlying HTTP request:
     12  *
     13  *     * **timeout** (Number) — a timeout value in milliseconds to wait
     14  *       before aborting the connection. Set to 0 for no timeout.
     15  *
     16  * @!macro nobrowser
     17  */
     18 AWS.MetadataService = inherit({
     19   /**
     20    * @return [String] the hostname of the instance metadata service
     21    */
     22   host: '169.254.169.254',
     23 
     24   /**
     25    * @!ignore
     26    */
     27 
     28   /**
     29    * Default HTTP options. By default, the metadata service is set to not
     30    * timeout on long requests. This means that on non-EC2 machines, this
     31    * request will never return. If you are calling this operation from an
     32    * environment that may not always run on EC2, set a `timeout` value so
     33    * the SDK will abort the request after a given number of milliseconds.
     34    */
     35   httpOptions: { timeout: 0 },
     36 
     37   /**
     38    * Creates a new MetadataService object with a given set of options.
     39    *
     40    * @option options host [String] the hostname of the instance metadata
     41    *   service
     42    * @option options httpOptions [map] a map of options to pass to the
     43    *   underlying HTTP request:
     44    *
     45    *   * **timeout** (Number) — a timeout value in milliseconds to wait
     46    *     before aborting the connection. Set to 0 for no timeout.
     47    * @option options maxRetries [Integer] the maximum number of retries to
     48    *   perform for timeout errors
     49    * @option options retryDelayOptions [map] A set of options to configure the
     50    *   retry delay on retryable errors. See AWS.Config for details.
     51    */
     52   constructor: function MetadataService(options) {
     53     AWS.util.update(this, options);
     54   },
     55 
     56   /**
     57    * Sends a request to the instance metadata service for a given resource.
     58    *
     59    * @param path [String] the path of the resource to get
     60    * @callback callback function(err, data)
     61    *   Called when a response is available from the service.
     62    *   @param err [Error, null] if an error occurred, this value will be set
     63    *   @param data [String, null] if the request was successful, the body of
     64    *     the response
     65    */
     66   request: function request(path, callback) {
     67     path = path || '/';
     68     var httpRequest = new AWS.HttpRequest('http://' + this.host + path);
     69     httpRequest.method = 'GET';
     70     AWS.util.handleRequestWithRetries(httpRequest, this, callback);
     71   },
     72 
     73   /**
     74   * @api private
     75   */
     76   loadCredentialsCallbacks: [],
     77 
     78   /**
     79    * Loads a set of credentials stored in the instance metadata service
     80    *
     81    * @api private
     82    * @callback callback function(err, credentials)
     83    *   Called when credentials are loaded from the resource
     84    *   @param err [Error] if an error occurred, this value will be set
     85    *   @param credentials [Object] the raw JSON object containing all
     86    *     metadata from the credentials resource
     87    */
     88   loadCredentials: function loadCredentials(callback) {
     89     var self = this;
     90     var basePath = '/latest/meta-data/iam/security-credentials/';
     91     self.loadCredentialsCallbacks.push(callback);
     92     if (self.loadCredentialsCallbacks.length > 1) { return; }
     93 
     94     function callbacks(err, creds) {
     95       var cb;
     96       while ((cb = self.loadCredentialsCallbacks.shift()) !== undefined) {
     97         cb(err, creds);
     98       }
     99     }
    100 
    101     self.request(basePath, function (err, roleName) {
    102       if (err) callbacks(err);
    103       else {
    104         roleName = roleName.split('\n')[0]; // grab first (and only) role
    105         self.request(basePath + roleName, function (credErr, credData) {
    106           if (credErr) callbacks(credErr);
    107           else {
    108             try {
    109               var credentials = JSON.parse(credData);
    110               callbacks(null, credentials);
    111             } catch (parseError) {
    112               callbacks(parseError);
    113             }
    114           }
    115         });
    116       }
    117     });
    118   }
    119 });
    120 
    121 module.exports = AWS.MetadataService;