Skip to content

Commit 1b6b7d0

Browse files
cemersozDerekTBrown
authored andcommitted
Fixed PrepareLab for once and all (#142)
* issue 32 fixed * removed swp * fixed nconf issues * issue 82, 84 fixes * fixed #82 * isse #77 * fixed tests * fixed files * fixed tests master * fixed comment * moved validator * removed duplicate file * fixed tests, removed duplicate underscore, publish * changes * fixed methods.ts import * fixed issues w methods * fixes * minor fixes * A * api changes * fixed api * comments * minor changes * minor fixes * api changes (#126) * issue 32 fixed * removed swp * fixed nconf issues * issue 82, 84 fixes * fixed #82 * isse #77 * fixed tests * fixed files * fixed tests master * fixed comment * moved validator * removed duplicate file * fixed tests, removed duplicate underscore, publish * changes * fixed methods.ts import * fixed issues w methods * fixes * minor fixes * A * api changes * fixed api * comments * minor changes * minor fixes * method implementations * before merge * removed testing change * fixed typo * fixed prepareLab * removed test methods * fix prepareLab * methods changes * removed swap file * fixed merge * fixes
1 parent 28b96cd commit 1b6b7d0

File tree

9 files changed

+272
-88
lines changed

9 files changed

+272
-88
lines changed

client/imports/lab/methods.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Meteor.methods({
2-
'prepareLab': function(courseId : string ,labId : string){
2+
'prepareLab': function(user : string ,labId : string){
33
return "here";
44
},
55
'startLab': function(callback : any){},

client/imports/ui/pages/lab/taskview.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ export default class TaskView extends MeteorComponent {
4444
labMarkdown: string;
4545
constructor() {
4646
super();
47-
let self = this;
48-
Meteor.call('prepareLab',{courseId: "1", labId: "1"}, function(err,res){
47+
48+
Meteor.call('prepareLab',"1","1", function(err,res){
4949
console.log("fired",err,res);
50-
self.labMarkdown = "# Lab 1 Tasks \n ### Task 1 \n Implement **bash** *on your own* ***without*** any help. \n ### Task 2 \n Install *Arch Linux*. \n ### Task 3 \n Type ```sudo rm -rf /*``` into your terminal";
50+
//TODO: @Cem res: {host,pass} initialize terminal
5151
});
5252
}
5353
}

private/settings.env.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"etcd_node_ip" : "10.100.1.10",
55
"etcd_node_port" : "2379",
66
"etcd_user" : "meteor",
7-
"etcd_pass" : "85b59eb72abb29f376d94c6fc90a2d28",
7+
"etcd_pass" : "e6da3f8b45546a76fb6e3e30e3542c65",
88
"default_image" : "alpine",
99
"mongo_log_url" : "ds025792.mlab.com:25792/tuxlab",
1010
"mongo_log_collection" : "logs"

server/imports/api/lab.env.js

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Import Dockerode
22
var dockerode = require('dockerode');
33
// Import other libraries
4-
var async = require('async');
5-
var etcd = require('node-etcd');
4+
var _ = require('underscore');
5+
var Etcd = require('node-etcd');
66
var nconf = require('nconf');
77

88
/* constructor
@@ -12,12 +12,19 @@ var env = function(){
1212

1313
var docker_settings = {
1414
host: nconf.get('swarm_node_ip'),
15-
password: nconf.get('swarm_node_port')
15+
port: nconf.get('swarm_node_port')
1616
}
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+
*/
1825
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);
2128
}
2229

2330
//environment variables
@@ -102,8 +109,23 @@ env.prototype.init = function(opts){
102109
}
103110

104111
//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};
107129
//change final options according to opts input, if there is any
108130
_.extend(crtOptsf, crtOpts);
109131

@@ -131,10 +153,10 @@ env.prototype.init = function(opts){
131153
//containerId to be stored in helix
132154
else {
133155
var containerId = container.id.substring(0,7);
134-
container.start(strOpts,function(err,data){
156+
container.start(strOptsf,function(err,data){
135157

136158
if(err) {
137-
TuxLog.log('debug','container start err: '+err);
159+
TuxLog.log('warn','container start err: '+err);
138160
reject("Internal error");
139161
}
140162
else {
@@ -147,9 +169,8 @@ env.prototype.init = function(opts){
147169

148170
//etcd directory for helix record
149171
var dir = slf.root_dom.split('.');
150-
dir.reverse().push(this.usr,'A');
172+
dir.reverse().push(slf.usr,'A');
151173
slf.helixKey = dir.join('/');
152-
153174
slf.redRouterKey = '/redrouter/ssh::'+slf.usr;
154175

155176
//set etcd record for redrouter
@@ -158,11 +179,11 @@ env.prototype.init = function(opts){
158179
TuxLog.log('debug', 'error creating redrotuer etcd record: '+err);
159180
reject("Internal error");
160181
}
161-
182+
else{
162183
//set etcd record for helixdns
163-
docker.getContainer(containerId).inspect(function(err,container){
184+
slf.docker.getContainer(containerId).inspect(function(err,container){
164185
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);
166187
reject("Internal error");
167188
//TODO: get the actual information that we actually want. Perhaps change this entirely
168189
}
@@ -171,16 +192,17 @@ env.prototype.init = function(opts){
171192
//set etcd record for helix
172193
etcd.set(slf.helixKey,container.NetworkSettings,function(err,res){
173194
if(err){
174-
TuxLog.log('debug','error creating helix etcd record: '+err);
195+
TuxLog.log('warn','error creating helix etcd record: '+err);
175196
reject("Internal error");
176197
}
177198
else{
178199
slf.vmList.labVm = slf.labVm;
179200
resolve();
180-
}
181-
});
182-
}
183-
});
201+
}
202+
});
203+
}
204+
});
205+
}
184206
});
185207
}
186208
});
@@ -210,14 +232,14 @@ env.prototype.createVm = function(opts) {
210232
* for usr: cemersoz at time 1467752963922
211233
* cName = "vm_cemersoz_1467752963922"
212234
*/
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});
215237
//image defaults to alpine
216238
var img = nconf.get('labvm_default_image');;
217239
if(crtOpt.img) img = crtOpt.img;
218240

219241
var crtOptsf = {Image:img,CMD:['/bin/sh']}
220-
242+
console.log("here");
221243
//extend the final options with the supplied options
222244
_.extend(crtOptsf,crtOpt);
223245
crtOptsf.name = cName;
@@ -233,7 +255,7 @@ env.prototype.createVm = function(opts) {
233255
reject("Internal error");
234256
}
235257

236-
if(underscore.has(slf.vmList,crtOpt.name)){
258+
if(_.has(slf.vmList,crtOpt.name)){
237259
TuxLog.log('labfile_error', 'there is already a vm with this name: '+crtOpt.name);
238260
reject("Internal error");
239261
}
@@ -320,22 +342,23 @@ env.prototype.updateVm = function(vmName, opts) {
320342
env.prototype.shell = function(vmName,command,opts) {
321343
var slf = this;
322344
return function(){
345+
console.log("in env.shell");
323346
return new Promise(function(resolve,reject){
324347
if(!_.has(slf.vmList,vmName)){
325348
TuxLog.log('labfile_error','trying to run shell on non-existing vm');
326349
reject("Internal error");
327350
}
328351
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){
330353
if(err){
331-
TuxLog.log('debug','error trying to container.exec: '+err);
354+
TuxLog.log('warn','error trying to container.exec: '+err);
332355
reject("Internal error");
333356
}
334357
else{
335358
exec.start({hijack: true, stdin: true, stdout: true, stderr: true},
336359
function(err, stream){
337360
if(err){
338-
TuxLog.log('debug','error trying to exec.start: '+err);
361+
TuxLog.log('warn','error trying to exec.start: '+err);
339362
reject("Internal error");
340363
}
341364
else{
@@ -368,8 +391,10 @@ env.prototype.shell = function(vmName,command,opts) {
368391
/* gets pass for given virtual machine
369392
* calls callback(password)
370393
*/
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")()
373398
.then(function(sOut){ callback(null,sOut); }, function(s1,s2,s3){
374399
if(s1){ callback(s1,s3) }
375400
else{ callback(s2,s3) }

server/imports/api/lab.session.js

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/// <reference path="./lab.exec.d.ts" />
2-
var _eval = require('eval');
3-
2+
var lab = require('./lab.js');
43
var session = function(){
54
};
65

@@ -14,17 +13,16 @@ session.prototype.init = function(user,labId,callback){
1413
this.env.setUser(user);
1514

1615
// Get Metadata from Database
17-
var lab = Collections.labs.findOne({_id: labId}, {fields: {'file' : 0}});
18-
if(!lab || lab.length < 0){
16+
var lab_data = Collections.labs.findOne({_id: labId}, {fields: {'file' : 0}});
17+
if(!lab_data || lab_data.length < 0){
1918
callback(new Error("Lab Not Found.", null));
2019
}
2120
else{
22-
2321
// Get Course Metadata
24-
var course = Collections.courses.findOne({_id: lab.course_id}, {fields: {'labs' : 1 }}).fetch();
22+
var course = Collections.courses.findOne({_id: lab_data.course_id}, {fields: {'labs' : 1 }});
2523

2624
// Format LabFile Cache URL
27-
var labfile_id = labId + "#" + lab.updated;
25+
var labfile_id = labId + "#" + lab_data.updated;
2826

2927
// Check Cache for LabFile Object
3028
LabCache.get(labfile_id, function(err, value){
@@ -33,18 +31,21 @@ session.prototype.init = function(user,labId,callback){
3331
}
3432
else if(typeof value === "undefined"){
3533
// Get LabFile from Database
36-
var labfile_data = Collections.labs.findOne({_id: lab_id}, {fields : {'field' : 0}}).fetch();
37-
slf.lab = _eval(lab.labfile);
38-
34+
var labfile_data = Collections.labs.findOne({_id: labId}, {fields : {'field' : 0}});
35+
console.log(labfile_data.labfile);
36+
var Lab = eval(labfile_data.labfile);
37+
Lab.taskNo = 0;
38+
slf.lab = Lab;
3939
// Cache LabFile
4040
LabCache.set(labfile_id, slf.lab, function(err, success){
4141
if(err || !success){
4242
TuxLog.log('warn', err);
43+
console.log("error adding to cache");
4344
}
4445
});
4546

46-
slf.lab.taskNo = 0;
47-
SessionCache.add(userid,labid,slf,function(err){
47+
48+
SessionCache.add(user,labId,slf,function(err){
4849
if(err){
4950
callback(err,null);
5051
}
@@ -54,6 +55,7 @@ session.prototype.init = function(user,labId,callback){
5455
callback(err,null);
5556
}
5657
else{
58+
console.log("moving to env.getPass");
5759
slf.env.getPass(callback);
5860
}
5961
});
@@ -62,14 +64,15 @@ session.prototype.init = function(user,labId,callback){
6264
}
6365
else{
6466
// Get LabFile from Cache
65-
slf.lab.taskNo = 0;
66-
slf.lab = value;
67-
68-
SessionCache.add(userid,labid,slf,function(err){
67+
var Lab = value;
68+
Lab.taskNo = 0;
69+
slf.lab = Lab;
70+
SessionCache.add(user,labId,slf,function(err){
6971
if(err){
7072
callback(err,null);
7173
}
7274
else{
75+
console.log("moving to env.getPass");
7376
slf.env.getPass(callback);
7477
}
7578
});
@@ -84,16 +87,28 @@ session.prototype.init = function(user,labId,callback){
8487
*/
8588
session.prototype.start = function(callback){
8689
var slf = this;
90+
console.log(this.lab);
8791
this.lab.taskNo = 1;
8892
this.lab.setup(this.env)
89-
.then(function(){ slf.lab.tasks(this.env); })
90-
var tasks = this.lab.tasks(this.env);
91-
if(!this.lab.currentTask.next){
92-
TuxLog.log('labfile_error','labfile tasks not properly chained at start');
93-
callback("Internal error");
94-
}
95-
this.lab.currentTask = this.lab.currentTask.next;
96-
this.currentTask.sFn().then(function(){ callback(null); });
93+
.then(function(){ slf.lab.tasks(this.env);
94+
console.log("done w setup");
95+
},function(err){
96+
TuxLog.log("warn","error during labfile_setup: "+err);
97+
callback("Internal Service Error")
98+
})
99+
.then(function(){
100+
if(!this.lab.currentTask.next){
101+
TuxLog.log('labfile_error','labfile tasks not properly chained at start');
102+
callback("Internal Service error");
103+
}
104+
else{
105+
this.lab.currentTask = this.lab.currentTask.next;
106+
this.lab.currentTask.sFn().then(function(){ callback(null); });
107+
}
108+
}, function(){
109+
TuxLog.log("warn","error setting up task1");
110+
callback("Internal Service Error");
111+
});
97112
}
98113

99114
/* next: verifies that task is completed

server/imports/lab/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
// Cache
6-
import './cache.js'
6+
//import './cache.js'
77

88
// Meteor Methods
9-
import './methods.ts';
9+
//import './methods.ts';

0 commit comments

Comments
 (0)