Skip to content

Commit 992d03d

Browse files
cemersozDerekTBrown
authored andcommitted
Env.util, etcd fixes (#204)
* 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 * prepareLab WORKS * further fixes * merge * all works * deleted debug line * fixed minor excess code * minor indentation fix * reference fixes * fixes to nextTask * merged with master * minor changes * fixed merge problems * fixed md view and swithces * fixed error messages * removed debug code * removed swp * fixes * fixed typescript warning * added student object * changed function calls * deleted unnecessary files * updated api, added md feedback * fixed ts warn * fixed example_data and partially tests * update validator, merge * fixed indentation * changes * it's pretty now * what * fixes to api changes * fixed api, deleted swap * fixed etcd add * fixed #187, #188 * minor fixes * fixed indentation * added equal sign * updated example courses to match schema * added md parser * removed debug code * fixed debug, import * changed dockerode settings * implemented to-from json * from-to json * fixed etcd err * etcd cache changes * added env.util
1 parent a67b041 commit 992d03d

File tree

12 files changed

+205
-91
lines changed

12 files changed

+205
-91
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default class LabView extends MeteorComponent {
6868
var slf = this;
6969
Meteor.call('prepareLab',"1", function(err,res){
7070
console.log('here');
71+
console.log("fired",err,res);
7172
//slf.labMarkdown = "# Sander \n ## are you sure this will work?";
7273
slf.tasks = res.taskList;
7374
slf.toTask(slf.tasks[0]);

collections/courses.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ courses.allow({
7575
}
7676
});
7777
var courseSchema = new SimpleSchema({
78+
_id: {
79+
type: String,
80+
81+
},
7882
course_name: {
7983
type: String
8084
},

server/imports/api/env.util.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var util = function(){}
2+
3+
util.prototype.parent = null;
4+
util.prototype.gitDiff = function(){}
5+
module.exports = util;

server/imports/api/lab.env.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Import other libraries
22
var nconf = require('nconf');
3-
3+
var util = require('./env.util.js');
44
/* constructor
55
* intializes docker, etcd connection
66
*/
77
var env = function(){
8+
this.util = new util();
9+
this.util.parent = this;
810
this.docker = docker;
911
this.root_dom = nconf.get('domain_root');
1012
}
@@ -68,7 +70,7 @@ env.deleteRecords = function(user,callback){
6870
{dockerodeStartOptions: {--your options here--}}
6971
*/
7072
env.prototype.init = function(opts){
71-
73+
TuxLog.log("warn","env.init");
7274
/* create unique labVm name to avoid collisions
7375
* for usr: cemersoz, at time 1467752963922
7476
* labvm = "labVm_cemersoz_1467752963922"
@@ -165,21 +167,20 @@ env.prototype.init = function(opts){
165167

166168
//create etcd directory for helix record
167169
var dir = slf.root_dom.split('.');
168-
dir.reverse().push(slf.usr,'A');
170+
dir.reverse().push(slf.usr);
169171
slf.dnsKey = dir.join('/');
170172
slf.dnsKey = "/skydns/"+slf.dnsKey;
171-
173+
console.log(slf.dnsKey);
172174
slf.redRouterKey = '/redrouter/SSH::'+slf.usr;
173175

174176
//set etcd record for redrouter
175177
etcd.set(slf.redRouterKey,JSON.stringify(etcd_redrouter),function(err,res){
176178

177179
if(err){
178-
TuxLog.log('debug',err);
180+
TuxLog.log('warn',err);
179181
reject(err);
180182
}
181183
else{
182-
183184
//set etcd record for helixdns
184185
slf.docker.getContainer(containerId).inspect(function(err,container){
185186
if(err){
@@ -189,12 +190,14 @@ env.prototype.init = function(opts){
189190
else{
190191
var dnsIP = container.Node.IP;
191192
//set etcd record for helix
192-
etcd.set(slf.helixKey,{host: dnsIP},function(err,res){
193+
etcd.set(slf.dnsKey,JSON.stringify({host: dnsIP}),function(err,res){
194+
193195
if(err){
194196
TuxLog.log('warn',err);
195197
reject(err);
196198
}
197199
else{
200+
TuxLog.log("warn","go ME!");
198201
slf.vmList.labVm = slf.labVm;
199202
resolve();
200203
}
@@ -426,6 +429,7 @@ env.prototype.shell = function(vmName,command,opts) {
426429
}
427430
});//TODO: split stdout and stderr
428431
stream.on('end',function(){
432+
console.log(dat);
429433
resolve(dat,stdErr);
430434
});
431435
}
@@ -440,6 +444,7 @@ env.prototype.shell = function(vmName,command,opts) {
440444
* calls callback(password)
441445
*/
442446
env.prototype.getPass = function(callback){
447+
TuxLog.log("warn","getPass");
443448
this.shell("labVm", "cat /pass")()
444449
.then(function(sOut,sErr){ callback(null,sOut); }, function(err){ callback(err,null)});
445450
}
@@ -452,4 +457,4 @@ env.prototype.listVolumes = function() {} //Don't know what this does
452457
env.prototype.createNetwork = function() {} //Don't know what this does
453458
env.prototype.listNetworks = function() {} //Don't know what this does
454459
env.prototype.run = function() {} //Runs Docker commands
455-
module.exports = new env();
460+
module.exports = env;

server/imports/api/lab.session.js

Lines changed: 110 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,110 @@
11
/// <reference path="./lab.exec.d.ts" />
2+
//require session submodules
23
var lab = require('./lab.js');
34
var student = require('./lab.student.js');
4-
var session = function(){};
5+
var env = require('./lab.env.js');
56

7+
8+
9+
//labCache.get wrapper
10+
11+
function getLab(id, callback){
12+
var lab_data = Collections.labs.findOne({_id: id});
13+
14+
if(!lab_data || lab_data.length < 0){
15+
TuxLog.log("warn",new Error("Lab not found"));
16+
callback(new Error("Lab Not Found."), null);
17+
}
18+
19+
var labcache_id = id + "#" + lab_data.updated;
20+
LabCache.get(labcache_id,function(err,value){
21+
if(err){
22+
TuxLog.log("warn",err);
23+
callback(err,null);
24+
}
25+
else if(!value){
26+
TuxLog.log("warn","didn't get from labcache");
27+
var file = lab_data.file;
28+
29+
var Lab = eval(file);
30+
LabCache.set(labcache_id,Lab,function(err,res){
31+
TuxLog.log("warn","oooh no");
32+
if(err){
33+
TuxLog.log("warn",err);
34+
}
35+
});
36+
callback(null,Lab);
37+
}
38+
else{
39+
TuxLog.log("warn","got from cache");
40+
callback(null,value);
41+
}
42+
});
43+
}
44+
45+
//constructor
46+
var session = function(){
47+
};
48+
49+
//define session fields
650
session.prototype.env = null;
751
session.prototype.lab = null;
852
session.prototype.student = null;
53+
session.prototype.pass = null;
54+
session.prototype.started = false;
55+
session.prototype.courseId = null;
56+
session.prototype.user = null;
57+
session.prototype.userId = null;
58+
959
session.prototype.taskUpdates = [];
1060

61+
62+
session.prototype.fromJson = function(data, callback){
63+
//set session fields
64+
this.user = data.user;
65+
this.courseId = data.courseId;
66+
this.pass = data.pass;
67+
this.userId = data.userId;
68+
69+
this.env = new env();
70+
this.env.setUser(data.user);
71+
this.taskUpdates = data.taskUpdates;
72+
73+
this.student = new student(this,data.userId,data.labId,data.courseId);
74+
75+
getLab(data.labId,function(err,lab){
76+
if(err){
77+
callback(err,null);
78+
}
79+
else{
80+
this.lab = lab;
81+
callback(null,null);
82+
}
83+
});
84+
85+
}
86+
session.prototype.changeStarted = function(){
87+
this.started = true;
88+
console.log(this.started);
89+
}
1190
/* init: pulls labFile and initializes session object from it
1291
*/
1392
session.prototype.init = function(user,userId,labId,callback){
1493
var slf = this;
15-
this.env = require('./lab.env.js');
16-
this.env.setUser(user);
94+
95+
//set session fields;
96+
this.user = user;
97+
this.userId = userId;
1798

99+
this.env = new env();
100+
this.env.setUser(user);
18101
// Get Metadata from Database
19102
var lab_data = Collections.labs.findOne({_id: labId}, {fields: {'labfile' : 0}});
103+
20104
var courseId = lab_data.course_id;
21105

106+
this.courseId = courseId;
107+
22108
//initialize student object
23109
this.student = new student(this, userId,labId,courseId);
24110

@@ -31,74 +117,47 @@ session.prototype.init = function(user,userId,labId,callback){
31117
// Get Course Metadata
32118
var course = Collections.courses.findOne({_id: lab_data.course_id}, {fields: {'labs' : 1 }});
33119

34-
// Format LabFile Cache URL
35-
var labfile_id = labId + "#" + lab_data.updated;
36-
37-
// Check Cache for LabFile Object
38-
LabCache.get(labfile_id, function(err, value){
120+
getLab(labId,function(err,lab){
39121
if(err){
40-
TuxLog.log("warn",err);
41-
callback(err, null);
122+
callback(err,null);
42123
}
43-
else if(typeof value === "undefined"){
44-
// Get LabFile from Database
45-
var labfile_data = Collections.labs.findOne({_id: labId}, {fields : {'field' : 0}});
46-
var Lab = eval(labfile_data.labfile);
47-
Lab.taskNo = 0;
48-
slf.lab = Lab;
49-
50-
// Cache LabFile -runs sync, not central to anything
51-
LabCache.set(labfile_id, slf.lab, function(err, success){
52-
if(err || !success){
53-
TuxLog.log("warn", err);
54-
}
55-
});
56-
124+
else if(!slf.started){
125+
slf.lab = lab;
57126
slf.start(function(err){
58127
if(err){
59128
//err logged in slf.start
60129
callback(err,null);
61130
}
62131
else{
63132
slf.env.getPass(function(err,res){
64-
if(err){
65-
//TODO: separate streams in env.js
66-
callback(err,null);
67-
}
68-
else{
69-
slf.lab.pass = res;
70-
callback(null,{taskNo: slf.lab.taskNo,sshPass: res});
71-
}
72-
});
133+
//TODO: separate streams in env.js
134+
if(err){
135+
callback(err,null);
136+
}
137+
else{
138+
console.log("calling back");
139+
callback(null,{taskNo: slf.lab.taskNo,sshPass: res});
140+
}
141+
});
73142
}
74143
});
75-
}
144+
}
76145
else{
77-
// Get LabFile from Cache
78-
var Lab = value;
79-
Lab.taskNo = 0;
80-
slf.lab = Lab;
81-
slf.start(function(err){
146+
slf.env.getPass(function(err,res){
147+
console.log("already started");
82148
if(err){
83149
callback(err,null);
84150
}
85151
else{
86-
slf.env.getPass(function(err,res){
87-
if(err){
88-
//TODO : same as above
89-
callback(err,null);
90-
}
91-
else{
92-
//slf.lab.pass = res;
93-
callback(null,{taskNo: slf.lab.taskNo,sshPass: res});
94-
}
95-
});
152+
slf.pass = res;
153+
callback(null,{taskNo: slf.lab.taskNo,sshPass: res});
96154
}
97-
});
155+
});
98156
}
99-
});
157+
})
100158
}
101159
}
160+
102161
/* start: runs setup and moves task header to first task
103162
* runs callback(err) on err if there is an error,
104163
* (null) no error

server/imports/lab/cache.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/*
22
Creates Session and Lab Caches
33
*/
4-
var async = require('async');
54
var NodeCache = require('node-cache');
65

6+
var session = require('../api/lab.session.js');
7+
78
// Lab Cache
89
LabCache = new NodeCache({
910
stdTTL: nconf.get('labvm_idle_timeout'),
@@ -46,6 +47,7 @@ var NodeCache = require('node-cache');
4647
Gets a session from the local memory cache or ETCD
4748
*/
4849
SessionCache.get = function(userid, labid, callback){
50+
var slf = this;
4951
SessionCache._NodeCache.get(userid+'#'+labid, function(err, value){
5052
if(err){
5153
TuxLog.log('warn', "SessionCache NodeCache Error.");
@@ -56,8 +58,18 @@ var NodeCache = require('node-cache');
5658
}
5759
else{
5860
var data = etcd.get('/tuxlab/sessions/'+userid+'/'+labid, function(err, value){
59-
//TODO @cemersoz from_data method
60-
});
61+
62+
var session = new session();
63+
session.fillJson(data,function(err,res){
64+
if(err){
65+
callback(err,null);
66+
}
67+
else{
68+
slf.add(session);
69+
callback(null,session);
70+
}
71+
});
72+
});
6173
}
6274
});
6375
}
@@ -87,10 +99,19 @@ var NodeCache = require('node-cache');
8799
});
88100
},
89101
function(cb){
90-
//TODO @cemersoz to_data method
102+
103+
//convert session object to JSON for etcd
104+
var json = {
105+
taskNo: session.lab.taskNo,
106+
taskUpdates: session.taskUpdates,
107+
pass: session.pass,
108+
user: session.user,
109+
userId: userid,
110+
labId: labid,
111+
courseId: session.courseId
112+
};
91113

92-
var json = null;
93-
etcd.set('tuxlab/sessions/'+userid+'/'+labid, json, function(err){
114+
etcd.set('tuxlab/sessions/'+userid+'/'+labid, Json.stringify(json), function(err){
94115
cb(err);
95116
});
96117
}

0 commit comments

Comments
 (0)