@@ -23,9 +23,11 @@ var util = require('util');
2323var semver = require ( 'semver' ) ;
2424
2525var v8debugapi = require ( './v8debugapi.js' ) ;
26+ var Debuggee = require ( '../debuggee.js' ) ;
2627var DebugletApi = require ( '../controller.js' ) ;
2728var scanner = require ( './scanner.js' ) ;
28- var Logger = require ( '@google/cloud-diagnostics-common' ) . logger ;
29+ var common = require ( '@google/cloud-diagnostics-common' ) ;
30+ var Logger = common . logger ;
2931var StatusMessage = require ( '../status-message.js' ) ;
3032var SourceMapper = require ( './sourcemapper.js' ) ;
3133
@@ -72,7 +74,10 @@ function Debuglet(debug, config, logger) {
7274 this . logger_ = logger ;
7375
7476 /** @private {DebugletApi} */
75- this . debugletApi_ = new DebugletApi ( this . config_ , this . debug_ ) ;
77+ this . debugletApi_ = new DebugletApi ( this . debug_ ) ;
78+
79+ /** @private {Debuggee} */
80+ this . debuggee_ = null ;
7681
7782 /** @private {Object.<string, Breakpoint>} */
7883 this . activeBreakpointMap_ = { } ;
@@ -125,33 +130,86 @@ Debuglet.prototype.start = function() {
125130
126131 that . logger_ . info ( 'Unique ID for this Application: ' + id ) ;
127132
128- that . debugletApi_ . init ( id , that . logger_ , function ( err , project ) {
133+ that . getProjectId_ ( function ( err , project , onGCP ) {
129134 if ( err ) {
130- that . logger_ . error ( 'Unable to initialize the debuglet api' +
131- ' -- disabling debuglet' , err ) ;
135+ that . logger_ . error ( 'Unable to discover projectId. Please provide ' +
136+ 'the projectId to be able to use the Debuglet' ,
137+ err ) ;
132138 that . emit ( 'initError' , err ) ;
133139 return ;
134140 }
135141
136- if ( semver . satisfies ( process . version , '5.2 || <0.12' ) ) {
137- // Using an unsupported version. We report an error message about the
138- // Node.js version, but we keep on running. The idea is that the user
139- // may miss the error message on the console. This way we can report the
140- // error when the user tries to set a breakpoint.
141- that . logger_ . error ( NODE_VERSION_MESSAGE ) ;
142- }
142+ that . getSourceContext_ ( function ( err , sourceContext ) {
143+ if ( err ) {
144+ that . logger_ . warn ( 'Unable to discover source context' , err ) ;
145+ // This is ignorable.
146+ }
143147
144- // We can register as a debuggee now.
145- that . running_ = true ;
146- that . project_ = project ;
147- that . scheduleRegistration_ ( 0 /* immediately */ ) ;
148- that . emit ( 'started' ) ;
148+ if ( semver . satisfies ( process . version , '5.2 || <0.12' ) ) {
149+ // Using an unsupported version. We report an error message about the
150+ // Node.js version, but we keep on running. The idea is that the user
151+ // may miss the error message on the console. This way we can report the
152+ // error when the user tries to set a breakpoint.
153+ that . logger_ . error ( NODE_VERSION_MESSAGE ) ;
154+ }
155+
156+ // We can register as a debuggee now.
157+ that . running_ = true ;
158+ that . project_ = project ;
159+ that . debuggee_ = new Debuggee (
160+ project , id , that . config_ . serviceContext , sourceContext ,
161+ that . config_ . description , null , onGCP ) ;
162+ that . scheduleRegistration_ ( 0 /* immediately */ ) ;
163+ that . emit ( 'started' ) ;
164+ } ) ;
149165 } ) ;
150166 } ) ;
151167 } ) ;
152168 } ) ;
153169} ;
154170
171+
172+ /**
173+ * @private
174+ */
175+ Debuglet . prototype . getProjectId_ = function ( callback ) {
176+ var that = this ;
177+
178+ // We need to figure out whether we are running on GCP. We can use our ability
179+ // to access the metadata service as a test for that.
180+ // TODO: change this to getProjectId in the future.
181+ common . utils . getProjectNumber ( function ( err , metadataProject ) {
182+ // We should get an error if we are not on GCP.
183+ var onGCP = ! err ;
184+
185+ // We perfer to use the locally available projectId as that is least
186+ // surprising to users.
187+ var project = that . config_ . projectId || process . env . GCLOUD_PROJECT ||
188+ metadataProject ;
189+
190+ // We if don't have a projectId by now, we fail with an error.
191+ if ( ! project ) {
192+ return callback ( err ) ;
193+ }
194+ return callback ( null , project , onGCP ) ;
195+ } ) ;
196+ } ;
197+
198+ Debuglet . prototype . getSourceContext_ =
199+ function ( callback ) {
200+ fs . readFile ( 'source-context.json' , 'utf8' , function ( err , data ) {
201+ // TODO: deal with err here
202+ var sourceContext ;
203+ try {
204+ sourceContext = JSON . parse ( data ) ;
205+ } catch ( e ) {
206+ err = 'Malformed source-context.json file:' + e ;
207+ // But we keep on going.
208+ }
209+ return callback ( err , sourceContext ) ;
210+ } ) ;
211+ } ;
212+
155213/**
156214 * @param {number } seconds
157215 * @private
@@ -172,7 +230,7 @@ Debuglet.prototype.scheduleRegistration_ = function(seconds) {
172230 return ;
173231 }
174232
175- that . debugletApi_ . register ( function ( err , result ) {
233+ that . debugletApi_ . register ( that . debuggee_ , function ( err , result ) {
176234 if ( err ) {
177235 onError ( err ) ;
178236 return ;
@@ -185,7 +243,7 @@ Debuglet.prototype.scheduleRegistration_ = function(seconds) {
185243 }
186244
187245 that . logger_ . info ( 'Registered as debuggee:' , result . debuggee . id ) ;
188-
246+ that . debuggee_ . id = result . debuggee . id ;
189247 that . emit ( 'registered' , result . debuggee . id ) ;
190248 if ( ! that . fetcherActive_ ) {
191249 that . scheduleBreakpointFetch_ ( 0 ) ;
0 commit comments