/
etc.js
126 lines (111 loc) · 3.51 KB
/
etc.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
var net = require('net')
, through = require('through')
, spawn = require('child_process').spawn
, Promise = require('bluebird')
, expandTilde = require('expand-tilde')
, mdns = require('mdns-js')
, fs = require('fs')
, isWin = /^win/.test(process.platform);
var etc = exports;
function chunks (count) {
var bufs = [], len = 0;
return through(function write (buf) {
// console.log(buf, len);
bufs.push(buf);
len += buf.length;
if (len > count) {
var full = Buffer.concat(bufs);
bufs = [];
for (var i = 0; i < Math.floor(full.length/count)*count ; i += count) {
this.queue(full.slice(i, i + count));
}
if (i < full.length) {
bufs.push(full.slice(i));
}
}
}, function end () {
if (bufs.length) {
this.queue(Buffer.concat(bufs));
}
this.queue(null);
})
}
function exec (p, args, opts) {
opts = opts || {};
return new Promise(function (resolve, reject) {
// console.log('exec:', p, args);
var proc = spawn(p, args, opts);
// proc.stdout.pipe(process.stdout);
if (!opts.quiet) {
proc.stderr.pipe(process.stderr);
}
proc.on('exit', function (code) {
code ? reject(code) : resolve();
})
});
}
// windows may or may not have vbox, and that depending on your flavor of Git, that shell may or may not have your Windows user %path% exported into it
// it's much safer to just assume vbox is in the path from the create.js insurePath check on startup
function startvm (name, verbose) {
verbose = typeof verbose !== 'undefined' ? verbose : false;
return spawn('vboxheadless', ['-s', name, '--vrde', verbose ? 'on': 'off']);
}
function stopvm (name) {
return spawn('vboxmanage', ['controlvm', name, 'poweroff']);
}
function seekDevice (hostname, next) {
// Ignore other mdns daemons running, like avahi or bonjour
mdns.excludeInterface('0.0.0.0');
// Create a Tessel browser
var browser = mdns.createBrowser('_tessel._tcp');
// When the browser finds a new device
var isonline = false;
browser.on('update', function (data) {
if (!isonline && data.host == hostname + '.local') {
process.removeListener('uncaughtException', swallow);
browser.stop();
setImmediate(function () {
next(null, data);
})
}
});
function swallow () {
// Swallow this, it may be random encoding errors in mdns
return;
}
process.on('uncaughtException', swallow)
// When the browser becomes ready
browser.once('ready', function(){
try {
// Start discovering Tessels
var onlinepoller = setInterval(function () {
if (isonline) {
clearInterval(onlinepoller);
} else {
browser.discover();
}
}, 2000);
} catch (err) {
console.log(err);
}
});
}
exports.VM_NAME = 't2';
exports.PATH_PRIVATE_KEY = expandTilde('~/.tessel/id_rsa');
exports.PATH_KEY = expandTilde('~/.tessel/id_rsa.pub');
exports.PATH_VM_NAME = expandTilde('~/.tessel/vm_name');
exports.PATH_VM_VDI = expandTilde('~/.tessel/vm.vdi');
// Windows serial ports use predefined pipe sockets
// Google virtualbox serial ports windows for a lot more info on this
exports.PATH_VM_PORT = exports.PATH_VM_PORT = function(){
return isWin ?
'\\\\.\\pipe\\COM1'
:
expandTilde('~/.tessel/vm.port');
}();
exports.VM_URL = "http://storage.googleapis.com/tessel-builds/ccc157a289db14791ee7250733a0b7b5fb9c06c8.vdi";
exports.exec = exec;
exports.chunks = chunks;
exports.startvm = startvm;
exports.stopvm = stopvm;
exports.seekDevice = seekDevice;