Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
start using gcp-metadata for metadata queries (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofrobots committed Dec 23, 2016
1 parent 5e3a0ef commit e5d9eb9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"async": "^2.1.2",
"coffee-script": "^1.9.3",
"findit2": "^2.2.3",
"gcp-metadata": "^0.1.0",
"google-auth-library": "^0.9.5",
"lodash": "^4.12.0",
"request": "^2.61.0",
Expand Down
40 changes: 21 additions & 19 deletions src/agent/debuglet.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var extend = require('extend');
var util = require('util');
var semver = require('semver');
var _ = require('lodash');
var metadata = require('gcp-metadata');

var v8debugapi = require('./v8debugapi.js');
var Debuggee = require('../debuggee.js');
Expand Down Expand Up @@ -263,30 +264,31 @@ Debuglet.createDebuggee =
return new Debuggee(properties);
};

/**
* @private
*/
Debuglet.prototype.getProjectId_ = function(callback) {
/**
* @private
*/
Debuglet.prototype.getProjectId_ = function(callback) {
var that = this;

// We need to figure out whether we are running on GCP. We can use our ability
// to access the metadata service as a test for that.
// TODO: change this to getProjectId in the future.
common.utils.getProjectNumber(function(err, metadataProject) {
// We should get an error if we are not on GCP.
var onGCP = !err;

// We perfer to use the locally available projectId as that is least
// surprising to users.
var project = that.debug_.options.projectId || process.env.GCLOUD_PROJECT ||
metadataProject;

// We if don't have a projectId by now, we fail with an error.
if (!project) {
return callback(err);
}
return callback(null, project, onGCP);
});
metadata.project(
'numeric-project-id', function(err, response, metadataProject) {
// We should get an error if we are not on GCP.
var onGCP = !err;

// We perfer to use the locally available projectId as that is least
// surprising to users.
var project = that.debug_.options.projectId ||
process.env.GCLOUD_PROJECT || metadataProject;

// We if don't have a projectId by now, we fail with an error.
if (!project) {
return callback(err);
}
return callback(null, project, onGCP);
});
};

Debuglet.prototype.getSourceContext_ =
Expand Down
2 changes: 2 additions & 0 deletions test/standalone/test-debuglet.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe(__filename, function() {
});

it('should not start when projectId is not available', function(done) {
this.timeout(8000);
var debug = require('../..')();
var debuglet = new Debuglet(debug, defaultConfig);

Expand All @@ -70,6 +71,7 @@ describe(__filename, function() {
});

it('should not crash without project num', function(done) {
this.timeout(8000);
var debug = require('../..')();
var debuglet = new Debuglet(debug, defaultConfig);

Expand Down

0 comments on commit e5d9eb9

Please sign in to comment.