/
setup.js
422 lines (360 loc) · 14.2 KB
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
const spawn = require('child_process').spawn;
const fs = require('fs');
const tcpportused = require('tcp-port-used');
const os = require('os');
const path = require('path');
const process = require('process');
const async = require('async');
//where we are going to start novnc
let docker_hostname = "0.0.0.0";
const minport = 11000;
const maxport = 11200;
function findFree(min_port, max_port, host="0.0.0.0") {
return new Promise((resolve, reject)=>{
//create a list of ports
var ports = [];
for(var p = min_port; p <= max_port; ++p) ports.push(p);
//then itereate
async.eachSeries(ports, (p, next)=>{
tcpportused.check(p, host).then(inuse=>{
if(inuse) next();
else resolve(p);
}, next);
}, (err)=>{
if(err) reject(err);
else reject(new Error("Couldn't find an open port"));
});
});
}
//load config from local directory
const config = require('./config.json');
console.log("starting setup");
//TODO - these path findings can be simplified if start by figuring out the workdir path
//then override that with BRAINLIFE_HOST_SCRATCH, then contatenate various inst/task dirs
let inst_path = '../../'+config.input_instance_id;
let input_dir = "/input-instance/"+config.input_task_id;
let src_path = inst_path+'/'+config.input_task_id;
let urlbase = "https://"+os.hostname();
if(config.subdir) {
input_dir += '/'+config.subdir;
src_path += '/'+config.subdir;
}
//resolve to absolute path
let abs_task_dir = path.resolve(".");
let abs_inst_dir = path.resolve(inst_path);
let abs_src_path = path.resolve(src_path);
//on local dev environment, vis server container is run on the host
//the volume src inside container is actually the paths on the host machine.
//we need to specify the host path to access the scratch volume mounted inside the
//container for it to be able to access it
if(process.env.BRAINLIFE_HOSTSCRATCH) {
//grab inst/task dir from cwd
const tokens = process.cwd().split("/");
const insttask = tokens.slice(-2).join("/");
abs_task_dir = process.env.BRAINLIFE_HOSTSCRATCH+"/"+insttask;
abs_inst_dir = process.env.BRAINLIFE_HOSTSCRATCH+"/"+config.input_instance_id;
abs_src_path = abs_inst_dir+"/"+config.input_task_id;
if(config.subdir) abs_src_path += "/"+config.subdir;
//point it to the localhost (TODO - port might not be 8080?)
urlbase = "http://localhost:8080";
//we use docker engine on the host, so what's where we need to reach to find out
//if are running things
docker_hostname = "host.docker.internal";
}
console.log(`input_dir ${input_dir}`);
console.log(`abs_src_path ${abs_src_path}`);
console.log(`abs_inst_dir ${abs_inst_dir}`);
console.log(`abs_task_dir ${abs_task_dir}`);
const mappings = {
//nonvc apps
fibernavigator: "soichih/vncserver-fibernavigator",
conn: "soichih/ui-conn",
trackvis: "brainlife/ui-trackvis",
wb_view: "brainlife/ui-wb_view",
fslview: "soichih/vncserver-fslview:18",
fsleyes: "brainlife/ui-fsleyes:2.0",
mricrogl: "soichih/vncserver-mricrogl:1.3",
"freeview-gpu": "soichih/vncserver-freeview-gpu:2.1",
mrview: "soichih/vncserver-mrview:4.2",
dsistudio: "brainlife/ui-dsistudio:1.0",
itksnap: "brainlife/ui-itksnap:5.0.9",
brainstorm: "brainlife/ui-brainstorm:210128",
//web apps
//html: "nginx:1.16.1", //last version that didn't change uid to 101
html: "brainlife/nginx:1.0",
mnefif: "brainlife/ui-mne:0.22.1",
}
const container_name = mappings[config.type];
if(!container_name) {
console.error("unknown container type", config.type);
process.exit(1);
}
//use docker port command to find out which host:port the container is using
function getDockerPort(id, cb) {
//find host:port that container listens to
const getp = spawn('docker', ['port', id]);
let rep = "";
let err = "";
getp.stdout.on('data', (data)=>{
rep += data.toString();
});
getp.stderr.on('data', (data)=>{
err += data.toString();
});
getp.on('close', (code)=>{
if(code != 0) return cb(err);
//sometime obtaining port fails.. this is for future debugging
console.debug(rep);
console.error(err);
const first = rep.split("\n")[0]; //grab the first line
let hostport = first.split(" ")[2]; //grab the 3rd token "0.0.0.0:49163"
let port = parseInt(hostport.split(":")[1]); //32780
cb(null, port);
});
}
function startContainer(name, opts, cb) {
console.log("starting", name);
console.debug(['docker', ...opts, name].join(" "));
let cont_id = "";
let err = "";
const cont = spawn('docker', [...opts, name]);
cont.stdout.on('data', (data)=>{
cont_id+=data.toString().trim();
});
cont.stderr.on('data', (data)=>{
err += data.toString();
});
cont.on('close', (code)=>{
if(code != 0) return cb("failed to start container. code:"+ code);
fs.writeFileSync("cont.id", cont_id);
cb(null, cont_id);
});
}
function dockerPull(container_name, cb) {
const pull = spawn('docker', ['pull', container_name]);
pull.stdout.on('data', data=>{
console.log(data.toString());
});
pull.stderr.on('data', data=>{
console.error(data.toString());
});
pull.on('close', code=>{
if(code != 0) return cb("failed to pull container. code:"+ code);
cb();
});
}
let password;
let port; //novnc or nginx port between 11000 - 12000
async.series([
next=>{
console.log("docker pulling container:", container_name);
dockerPull(container_name, next);
},
next=>{
console.log("creating vncserver password");
require('crypto').randomBytes(8, (err, buffer)=>{
password = buffer.toString('hex');
next();
});
},
next=>{
console.log("looking for an open port");
findFree(minport, maxport).then(_port=>{
port = _port;
console.log("going to use ", port);
next();
});
},
next=>{
//do container specific things.
if(config.type == "html") startNginx(next);
else if(config.type == "mnefif") startWeb(next);
else startNOVNC(next);
},
], err=>{
if(err) throw err;
console.log("all done");
});
function startNginx(cb) {
async.series([
//if index_html is not specified, find the first .html file under abs_src_path
next=>{
console.log("finding *.html in ", abs_src_path);
const pull = spawn('find', [abs_src_path, '-name', '*.html']);
let out = "";
pull.stdout.on('data', data=>{
out += data.toString();
});
pull.stderr.on('data', data=>{
console.error(data.toString());
});
pull.on('close', code=>{
if(code != 0) return next("failed to find index.html"+ code);
//pick the last one on the list
const files = out.trim().split("\n");
//sort by depth
files.sort((a,b)=>{
const ad = a.split("/").length;
const bd = b.split("/").length;
return bd - ad;
});
console.dir(files);
if(files.length) {
const lastfile = files.pop();
console.log("laastfile found", lastfile);
index_html = lastfile.substring(abs_src_path.length+1);
}
next();
});
},
next=>{
console.log("starting nginx container");
let opts = ['run', '-d'];
opts = opts.concat(['-v', abs_src_path+':/usr/share/nginx/html/'+password+':ro']);
opts = opts.concat(['-e', 'INPUT_DIR='+input_dir]);
opts = opts.concat(['-p', "0.0.0.0:"+port+":80"]);
startContainer(container_name, opts, next);
},
next=>{
const url = urlbase+"/vnc/"+port+"/"+password+"/"+index_html;
console.debug(url);
console.log("waiting for web server to start on", port);
tcpportused.waitUntilUsed(port, 200, 9000) //port, retry, timeout
.then(()=>{
fs.writeFileSync("url.txt", url);
next();
}).catch(next);
},
], cb);
}
function startWeb(cb) {
async.series([
next=>{
console.log("starting webserver container");
let opts = ['run', '-d'];
//for Jupyter notebook
opts = opts.concat(['-v', abs_inst_dir+':/input-instance:ro']);
opts = opts.concat(['-e', 'INPUT_DIR='+input_dir]);
//TODO - can't get it work through nginx .. so we don't need this at the moment.. but
//klet's set it for now for future
opts = opts.concat(['-e', 'BASEURL=/vnc/'+port+'/'+password+'/']); //need trailling /
opts = opts.concat(['-p', "0.0.0.0:"+port+":80"]);
startContainer(container_name, opts, next);
},
next=>{
//notebook to open first
const index_html = "notebooks/main.ipynb";
//does urlbase need to be http?
const url = urlbase+":"+port+"/vnc/"+port+"/"+password+'/'+index_html;
console.debug("-----------------------------");
console.debug(url);
console.debug("-----------------------------");
console.log("waiting for web server to start on", port);
tcpportused.waitUntilUsed(port, 200, 9000) //port, retry, timeout
.then(()=>{
fs.writeFileSync("url.txt", url);
next();
}).catch(next);
},
], cb);
}
function startNOVNC(cb) {
let vncPort;
let gpus = []; //list of gpu bus IDs
async.series([
//list number of available gpus
next=>{
//nvidia-smi --query-gpu=gpu_bus_id --format=csv,noheader
const smi = spawn('nvidia-smi', ['--query-gpu=gpu_bus_id', '--format=csv,noheader']);
let out = "";
smi.stdout.on('data', data=>{
out += data.toString();
});
smi.stderr.on('data', data=>{
console.error(data.toString());
});
smi.on('close', code=>{
if(code != 0) {
console.error("failed to run nvidia-smi");
return next();
}
gpus = out.trim().split("\n");
next();
});
//without this ENOENT exception is thrown
smi.on('error', err=>{
console.log("ale?");
});
},
next=>{
console.log("starting ui container");
let opts = ['run', '-d'];
opts = opts.concat(['--publish-all']);
opts = opts.concat(['-e', 'INPUT_DIR='+input_dir]);
opts = opts.concat(['-e', 'X11VNC_PASSWORD='+password]);
opts = opts.concat(['-v', '/tmp/.X11-unix:/tmp/.X11-unix:ro']);
opts = opts.concat(['-e', 'LD_LIBRARY_PATH=/usr/lib/host']);
opts = opts.concat(['-v', '/usr/local/licensed-bin:/usr/local/licensed-bin:ro']);
opts = opts.concat(['-v', abs_inst_dir+':/input-instance:ro']);
opts = opts.concat(['-v', abs_src_path+':/input:ro']);//deprecated.. use /input-instance
opts = opts.concat(['-v', abs_task_dir+'/lib:/usr/lib/host:ro']);
if(gpus.length) {
//decide on VGL_DISPLAY to use
let dindex = Math.floor(Math.random()*gpus.length);
let display = [":0.0", ":0.1"][dindex];
//:0.0 is too slow on gpu2 for some reason.. it's stuck on P8 (powerstate).. but gpu1 is like that
//and it's not too slow..
if(urlbase == "https://gpu2-pestillilab.psych.indiana.edu") display = ":0.0";
opts = opts.concat(['--gpus', 'all']);
opts = opts.concat(['-e', 'VGL_DISPLAY='+display]);
}
/*
//I need to set this if the host has gpu on local machine -- how can I tell?
if(process.env.BRAINLIFE_HOSTSCRATCH) {
//allow access to gpu on local machine
opts = opts.concat(['--gpus', 'all']);
opts = opts.concat(['-e', 'VGL_DISPLAY=:0.0']);
}
*/
startContainer(container_name, opts, (err, cont_id)=>{
if(err) return next(err);
//find out which vncport it's using
getDockerPort(cont_id, (err, _vncPort)=>{
if(err) return next(err);
vncPort = _vncPort;
console.log("container started", cont_id, "port", vncPort);
next();
});
});
},
next=>{
console.log("waiting for container.vncserver", vncPort);
tcpportused.waitUntilUsedOnHost(vncPort, docker_hostname, 200, 9000) //port, retry, timeout
.then(()=>{
console.log("vncserver is ready!");
next();
});
},
next=>{
const novnc_out = fs.openSync('./novnc.log', 'a');
const novnc_err = fs.openSync('./novnc.log', 'a');
const novnc = spawn('/usr/local/noVNC/utils/novnc_proxy', ['--listen', port, '--vnc', docker_hostname+":"+vncPort], {
detached: true, stdio: ['ignore', novnc_out, novnc_err]
});
novnc.unref();
fs.writeFileSync("novnc.pid", novnc.pid.toString());
fs.writeFileSync("novnc.pid", novnc.pid.toString());
next();
},
next=>{
let url = urlbase+"/vnc/"+port+"/vnc_lite.html?path=vnc/"+port+"/websockify&password="+password+"&reconnect=true&title="+config.title||"brainlife";
console.log("waiting for novnc to become ready", url);
tcpportused.waitUntilUsed(port, 200, 9000) //port, retry, timeout
.then(()=>{
console.log("started novnc");
fs.writeFileSync("url.txt", url);
next();
}).catch(next);
},
], cb);
}