1
1
// Import Dockerode
2
2
var dockerode = require ( 'dockerode' ) ;
3
-
4
3
// Import other libraries
5
4
var async = require ( 'async' ) ;
6
5
var underscore = require ( 'underscore' ) ;
7
- var etcd = require ( 'node-etcd' ) ;
8
6
var nconf = require ( 'nconf' ) ;
9
7
10
8
/* constructor
11
9
* intializes docker, etcd connection
12
10
*/
13
11
var env = function ( ) {
14
- var etcd_address = nconf . get ( 'etcd_node_ip' ) + ':' + nconf . get ( 'etcd_node_port' ) ;
15
-
16
- var etcd_auth = {
17
- user : nconf . get ( 'etcd_user' ) ,
18
- password : nconf . get ( 'etcd_pass' )
19
- }
20
12
21
13
var docker_settings = {
22
14
host : nconf . get ( 'swarm_node_ip' ) ,
23
15
password : nconf . get ( 'swarm_node_port' )
24
16
}
25
17
26
18
this . docker = new dockerode ( docker_settings ) ;
27
- this . etcd = new etcd ( etcd_address , etcd_auth ) ;
28
19
this . root_dom = nconf . get ( "root_domain" ) ;
29
20
30
21
}
@@ -114,7 +105,7 @@ env.prototype.init = function(opts){
114
105
var crtOptsf = { Image : img , CMD : [ '/bin/sh' ] , name : this . labVm }
115
106
116
107
//change final options according to opts input, if there is any
117
- underscore . extend ( crtOptsf , crtOpts ) ;
108
+ _ . extend ( crtOptsf , crtOpts ) ;
118
109
119
110
var slf = this ;
120
111
return new Promise ( function ( resolve , reject ) {
@@ -141,50 +132,51 @@ env.prototype.init = function(opts){
141
132
else {
142
133
var containerId = container . id . substring ( 0 , 7 ) ;
143
134
container . start ( strOpts , function ( err , data ) {
144
- if ( err ) {
135
+
136
+ if ( err ) {
145
137
TuxLog . log ( 'debug' , 'container start err: ' + err ) ;
146
138
reject ( "Internal error" ) ;
147
139
}
148
140
else {
149
- var etcd_redrouter = {
150
- docker : containerId ,
151
- port : 22 ,
152
- username : "root" ,
153
- allowed_auth : [ "password" ]
154
- }
155
-
156
- //etcd directory for helix record
157
- var dir = slf . root_dom . split ( '.' ) ;
158
- dir . reverse ( ) . push ( this . usr , 'A' ) ;
159
- slf . helixKey = dir . join ( '/' ) ;
160
-
161
- slf . redRouterKey = '/redrouter/ssh::' + slf . usr ;
162
-
163
- //set etcd record for redrouter
164
- etcd . set ( slf . redRouterKey , etcd_redrouter , function ( err , res ) {
165
- if ( err ) {
166
- TuxLog . log ( 'debug' , 'error creating redrotuer etcd record: ' + err ) ;
167
- reject ( "Internal error" ) ;
168
- }
169
-
170
- //set etcd record for helixdns
171
- docker . getContainer ( containerId ) . inspect ( function ( err , container ) {
172
- if ( err ) {
173
- TuxLog . log ( 'debug' , 'docker cannot find the container it just created: ' + err ) ;
174
- reject ( "Internal error" ) ;
175
- //TODO: get the actual information that we actually want. Perhaps change this entirely
176
- }
177
- else {
178
-
179
- //set etcd record for helix
180
- etcd . set ( slf . helixKey , container . NetworkSettings , function ( err , res ) {
181
- if ( err ) {
182
- TuxLog . log ( 'debug' , 'error creating helix etcd record: ' + err ) ;
183
- reject ( "Internal error" ) ;
184
- }
185
- else {
186
- slf . vmList . labVm = slf . labVm ;
187
- resolve ( ) ;
141
+ var etcd_redrouter = {
142
+ docker : containerId ,
143
+ port : 22 ,
144
+ username : "root" ,
145
+ allowed_auth : [ "password" ]
146
+ }
147
+
148
+ //etcd directory for helix record
149
+ var dir = slf . root_dom . split ( '.' ) ;
150
+ dir . reverse ( ) . push ( this . usr , 'A' ) ;
151
+ slf . helixKey = dir . join ( '/' ) ;
152
+
153
+ slf . redRouterKey = '/redrouter/ssh::' + slf . usr ;
154
+
155
+ //set etcd record for redrouter
156
+ etcd . set ( slf . redRouterKey , etcd_redrouter , function ( err , res ) {
157
+ if ( err ) {
158
+ TuxLog . log ( 'debug' , 'error creating redrotuer etcd record: ' + err ) ;
159
+ reject ( "Internal error" ) ;
160
+ }
161
+
162
+ //set etcd record for helixdns
163
+ docker . getContainer ( containerId ) . inspect ( function ( err , container ) {
164
+ if ( err ) {
165
+ TuxLog . log ( 'debug' , 'docker cannot find the container it just created: ' + err ) ;
166
+ reject ( "Internal error" ) ;
167
+ //TODO: get the actual information that we actually want. Perhaps change this entirely
168
+ }
169
+ else {
170
+
171
+ //set etcd record for helix
172
+ etcd . set ( slf . helixKey , container . NetworkSettings , function ( err , res ) {
173
+ if ( err ) {
174
+ TuxLog . log ( 'debug' , 'error creating helix etcd record: ' + err ) ;
175
+ reject ( "Internal error" ) ;
176
+ }
177
+ else {
178
+ slf . vmList . labVm = slf . labVm ;
179
+ resolve ( ) ;
188
180
}
189
181
} ) ;
190
182
}
@@ -199,7 +191,6 @@ env.prototype.init = function(opts){
199
191
} ) ;
200
192
}
201
193
202
-
203
194
/* creates a new container with an image from the options provided,
204
195
* downloads image if it does not yet exist.
205
196
* @param {{},{} } : two options file, dockerodeCreateOptions,dockerodeStartOptions
@@ -228,7 +219,7 @@ env.prototype.createVm = function(opts) {
228
219
var crtOptsf = { Image :img , CMD :[ '/bin/sh' ] }
229
220
230
221
//extend the final options with the supplied options
231
- underscore . extend ( crtOptsf , crtOpt ) ;
222
+ _ . extend ( crtOptsf , crtOpt ) ;
232
223
crtOptsf . name = cName ;
233
224
//this.vmList.push({name: crtOpt.name,id:cName});
234
225
//clone this into slf to use in the promise
@@ -285,8 +276,8 @@ env.prototype.removeVm = function (vmName,opts) {
285
276
return function ( ) {
286
277
return new Promise ( function ( resolve , reject ) {
287
278
288
- //check if container initialized
289
- if ( ! underscore . has ( this . vmList , vmName ) ) {
279
+ //check if container initialized
280
+ if ( ! _ . has ( this . vmList , vmName ) ) {
290
281
TuxLog . log ( 'labfile_error' , "trying to delete non-existing vm" ) ;
291
282
reject ( "Internal error" ) ;
292
283
}
@@ -307,7 +298,7 @@ env.prototype.updateVm = function(vmName, opts) {
307
298
var slf = this ;
308
299
return function ( ) {
309
300
return new Promise ( function ( resolve , reject ) {
310
- if ( ! underscore . has ( slf . vmList , vmName ) ) {
301
+ if ( ! _ . has ( slf . vmList , vmName ) ) {
311
302
TuxLog . log ( 'labfile_error' , 'trying to update non-existing vm' ) ;
312
303
reject ( "Internal error" ) ;
313
304
}
@@ -330,7 +321,7 @@ env.prototype.shell = function(vmName,command,opts) {
330
321
var slf = this ;
331
322
return function ( ) {
332
323
return new Promise ( function ( resolve , reject ) {
333
- if ( ! underscore . has ( slf . vmList , vmName ) ) {
324
+ if ( ! _ . has ( slf . vmList , vmName ) ) {
334
325
TuxLog . log ( 'labfile_error' , 'trying to run shell on non-existing vm' ) ;
335
326
reject ( "Internal error" ) ;
336
327
}
0 commit comments