1
1
// Import Dockerode
2
2
var dockerode = require ( 'dockerode' ) ;
3
3
// Import other libraries
4
- var async = require ( 'async ' ) ;
5
- var etcd = require ( 'node-etcd' ) ;
4
+ var _ = require ( 'underscore ' ) ;
5
+ var Etcd = require ( 'node-etcd' ) ;
6
6
var nconf = require ( 'nconf' ) ;
7
7
8
8
/* constructor
@@ -12,12 +12,19 @@ var env = function(){
12
12
13
13
var docker_settings = {
14
14
host : nconf . get ( 'swarm_node_ip' ) ,
15
- password : nconf . get ( 'swarm_node_port' )
15
+ port : nconf . get ( 'swarm_node_port' )
16
16
}
17
-
17
+ /*
18
+ var etcd_auth = {
19
+ user: nconf.get('etcd_user'),
20
+ pass: nconf.get('etcd_pass')
21
+ };
22
+ var etcd_address = nconf.get('etcd_node_ip')+':'+nconf.get('etcd_node_port');
23
+ this.etcd = new Etcd(etcd_address,etcd_auth);
24
+ */
18
25
this . docker = new dockerode ( docker_settings ) ;
19
- this . root_dom = nconf . get ( "root_domain" ) ;
20
-
26
+ this . root_dom = nconf . get ( 'domain_root' ) ;
27
+ console . log ( this . root_dom ) ;
21
28
}
22
29
23
30
//environment variables
@@ -102,8 +109,23 @@ env.prototype.init = function(opts){
102
109
}
103
110
104
111
//declare final options
105
- var crtOptsf = { Image : img , CMD : [ '/bin/sh' ] , name : this . labVm }
106
-
112
+ var crtOptsf = {
113
+ 'Image' : img ,
114
+ 'Cmd' : [ '/bin/sh' ] ,
115
+ 'name' : this . labVm ,
116
+ 'Hostname' : '' ,
117
+ 'User' : '' ,
118
+ 'AttachStdin' : false ,
119
+ 'AttachStdout' : false ,
120
+ 'AttachStderr' : false ,
121
+ 'Tty' : true ,
122
+ 'OpenStdin' : false ,
123
+ 'StdinOnce' : false ,
124
+ 'Env' : null ,
125
+ 'Volumes' : { } ,
126
+ 'VolumesFrom' : ''
127
+ }
128
+ var strOptsf = { attach : false , detach : true } ;
107
129
//change final options according to opts input, if there is any
108
130
_ . extend ( crtOptsf , crtOpts ) ;
109
131
@@ -131,10 +153,10 @@ env.prototype.init = function(opts){
131
153
//containerId to be stored in helix
132
154
else {
133
155
var containerId = container . id . substring ( 0 , 7 ) ;
134
- container . start ( strOpts , function ( err , data ) {
156
+ container . start ( strOptsf , function ( err , data ) {
135
157
136
158
if ( err ) {
137
- TuxLog . log ( 'debug ' , 'container start err: ' + err ) ;
159
+ TuxLog . log ( 'warn ' , 'container start err: ' + err ) ;
138
160
reject ( "Internal error" ) ;
139
161
}
140
162
else {
@@ -147,9 +169,8 @@ env.prototype.init = function(opts){
147
169
148
170
//etcd directory for helix record
149
171
var dir = slf . root_dom . split ( '.' ) ;
150
- dir . reverse ( ) . push ( this . usr , 'A' ) ;
172
+ dir . reverse ( ) . push ( slf . usr , 'A' ) ;
151
173
slf . helixKey = dir . join ( '/' ) ;
152
-
153
174
slf . redRouterKey = '/redrouter/ssh::' + slf . usr ;
154
175
155
176
//set etcd record for redrouter
@@ -158,11 +179,11 @@ env.prototype.init = function(opts){
158
179
TuxLog . log ( 'debug' , 'error creating redrotuer etcd record: ' + err ) ;
159
180
reject ( "Internal error" ) ;
160
181
}
161
-
182
+ else {
162
183
//set etcd record for helixdns
163
- docker . getContainer ( containerId ) . inspect ( function ( err , container ) {
184
+ slf . docker . getContainer ( containerId ) . inspect ( function ( err , container ) {
164
185
if ( err ) {
165
- TuxLog . log ( 'debug ' , 'docker cannot find the container it just created: ' + err ) ;
186
+ TuxLog . log ( 'warn ' , 'docker cannot find the container it just created: ' + err ) ;
166
187
reject ( "Internal error" ) ;
167
188
//TODO: get the actual information that we actually want. Perhaps change this entirely
168
189
}
@@ -171,16 +192,17 @@ env.prototype.init = function(opts){
171
192
//set etcd record for helix
172
193
etcd . set ( slf . helixKey , container . NetworkSettings , function ( err , res ) {
173
194
if ( err ) {
174
- TuxLog . log ( 'debug ' , 'error creating helix etcd record: ' + err ) ;
195
+ TuxLog . log ( 'warn ' , 'error creating helix etcd record: ' + err ) ;
175
196
reject ( "Internal error" ) ;
176
197
}
177
198
else {
178
199
slf . vmList . labVm = slf . labVm ;
179
200
resolve ( ) ;
180
- }
181
- } ) ;
182
- }
183
- } ) ;
201
+ }
202
+ } ) ;
203
+ }
204
+ } ) ;
205
+ }
184
206
} ) ;
185
207
}
186
208
} ) ;
@@ -210,14 +232,14 @@ env.prototype.createVm = function(opts) {
210
232
* for usr: cemersoz at time 1467752963922
211
233
* cName = "vm_cemersoz_1467752963922"
212
234
*/
213
- var cName = "vm_" + usr + '_' + ( ( new Date ) . getTime ( ) ) . toString ( ) ;
214
-
235
+ var cName = "vm_" + this . usr + '_' + ( ( new Date ) . getTime ( ) ) . toString ( ) ;
236
+ _ . extend ( strOpt , { daemon : true } ) ;
215
237
//image defaults to alpine
216
238
var img = nconf . get ( 'labvm_default_image' ) ; ;
217
239
if ( crtOpt . img ) img = crtOpt . img ;
218
240
219
241
var crtOptsf = { Image :img , CMD :[ '/bin/sh' ] }
220
-
242
+ console . log ( "here" ) ;
221
243
//extend the final options with the supplied options
222
244
_ . extend ( crtOptsf , crtOpt ) ;
223
245
crtOptsf . name = cName ;
@@ -233,7 +255,7 @@ env.prototype.createVm = function(opts) {
233
255
reject ( "Internal error" ) ;
234
256
}
235
257
236
- if ( underscore . has ( slf . vmList , crtOpt . name ) ) {
258
+ if ( _ . has ( slf . vmList , crtOpt . name ) ) {
237
259
TuxLog . log ( 'labfile_error' , 'there is already a vm with this name: ' + crtOpt . name ) ;
238
260
reject ( "Internal error" ) ;
239
261
}
@@ -320,22 +342,23 @@ env.prototype.updateVm = function(vmName, opts) {
320
342
env . prototype . shell = function ( vmName , command , opts ) {
321
343
var slf = this ;
322
344
return function ( ) {
345
+ console . log ( "in env.shell" ) ;
323
346
return new Promise ( function ( resolve , reject ) {
324
347
if ( ! _ . has ( slf . vmList , vmName ) ) {
325
348
TuxLog . log ( 'labfile_error' , 'trying to run shell on non-existing vm' ) ;
326
349
reject ( "Internal error" ) ;
327
350
}
328
351
var options = { AttachStdout : true , AttachStderr : true , Cmd : command . split ( " " ) } ;
329
- this . docker . getContainer ( slf . vmList [ vmName ] ) . exec ( options , function ( err , exec ) {
352
+ slf . docker . getContainer ( slf . vmList [ vmName ] ) . exec ( options , function ( err , exec ) {
330
353
if ( err ) {
331
- TuxLog . log ( 'debug ' , 'error trying to container.exec: ' + err ) ;
354
+ TuxLog . log ( 'warn ' , 'error trying to container.exec: ' + err ) ;
332
355
reject ( "Internal error" ) ;
333
356
}
334
357
else {
335
358
exec . start ( { hijack : true , stdin : true , stdout : true , stderr : true } ,
336
359
function ( err , stream ) {
337
360
if ( err ) {
338
- TuxLog . log ( 'debug ' , 'error trying to exec.start: ' + err ) ;
361
+ TuxLog . log ( 'warn ' , 'error trying to exec.start: ' + err ) ;
339
362
reject ( "Internal error" ) ;
340
363
}
341
364
else {
@@ -368,8 +391,10 @@ env.prototype.shell = function(vmName,command,opts) {
368
391
/* gets pass for given virtual machine
369
392
* calls callback(password)
370
393
*/
371
- env . prototype . getPass = function ( vmName , callback ) {
372
- this . shell1 ( vmName , "cat /pass" )
394
+ env . prototype . getPass = function ( callback ) {
395
+ console . log ( "hereeee" ) ;
396
+ TuxLog . log ( "warn" , "here in getPass" ) ;
397
+ this . shell ( "labVm" , "cat /pass" ) ( )
373
398
. then ( function ( sOut ) { callback ( null , sOut ) ; } , function ( s1 , s2 , s3 ) {
374
399
if ( s1 ) { callback ( s1 , s3 ) }
375
400
else { callback ( s2 , s3 ) }
0 commit comments