-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththings-js
executable file
·296 lines (271 loc) · 9.61 KB
/
things-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
#! /usr/bin/env node
var fs = require('fs');
var path = require('path');
var program = require('commander');
var jsBeautify = require('js-beautify').js_beautify;
program
.version('0.0.1')
.description('Framework for building IoT systems')
program
.command('dashboard')
.option('-c, --config <conf_path>', 'Config File Path')
.alias('dash')
.description('Start a Dashboard application')
.action(function(options){
var config = options.config ? require(options.config) : {};
var startApp = require('../util/dashboard/startApp.js');
startApp(config);
});
program
.command('worker <conf_path>')
.option('-d, --debug', 'Set environment variable DEBUG to "true" and see debug output')
.option('--statusLED', 'Set CodeEngine to indicate busy status on LED connected to GPIO (works only if device has GPIO)')
.alias('engine')
.description('Start a ThingsJS Worker')
.action(function(conf_path, options){
process.env['DEBUG'] = options.debug ? "true": "false";
var CodeEngine = require('../lib/core/CodeEngine.js');
var config = CodeEngine.validateConfig(conf_path);
var engine = new CodeEngine(config, {
enable_status_led: options.statusLED
});
process.on('SIGINT', function(){
engine.kill().then(function(){
process.exit();
})
});
});
program
.command('scheduler <conf_path>')
.option('-s, --save <log_path>', 'File path to optionally log resource usage')
.alias('sched')
.description('Start a ThingsJS Scheduler')
.action(function(conf_path, options){
var CodeEngine = require('../lib/core/CodeEngine.js');
var Scheduler = require('../lib/core/Scheduler.js');
var config = CodeEngine.validateConfig(conf_path);
// var engine = new things.CodeEngine(config);
// console.log(options);
var scheduler = new Scheduler(config, {
log_path: options.save
});
process.on('SIGINT', function(){
scheduler.kill().then(function(){
process.exit();
})
});
});
program
.command('instrument <file_path>')
.option('-p, --pubsub <pubsub_url>', 'Pubsub URL')
.option('-s, --save <save_path>', 'File path to optionally save instrumented code')
.option('-d, --debug', 'Set environment variable DEBUG to "true" and see debug output')
.alias('inst')
.description('Instrument raw JavaScript code to make it migrate-able')
.action(function(file_path, options){
process.env['DEBUG'] = options.debug ? "true": "false";
var Code = require('../lib/core/Code.js');
var pubsub = { url: (options.pubsub_url || 'mqtt://localhost') }; // use a dummy pubsub, no need to connect
var code = Code.fromFile(pubsub, file_path);
if (options.save) code.save(options.save);
else console.log(code.source);
});
program
.command('restore <file_path>')
.option('-s, --save <save_path>', 'File path to optionally save instrumented code')
.option('-d, --debug', 'Set environment variable DEBUG to "true" and see debug output')
.description('Instrument raw JavaScript code to make it migrate-able')
.action(function(file_path, options){
process.env['DEBUG'] = options.debug ? "true": "false";
var Code = require('../lib/core/Code.js');
var code = Code.fromSnapshotFile(file_path, true);
if (options.save) code.save(options.save);
else console.log(code.source);
});
program
.command('run <file_path>')
.option('-r, --restore', 'Restore from snapshot and run')
.option('-p, --pubsub <pubsub_url>', 'Pubsub URL')
.option('-s, --save <save_path>', 'File path to optionally save instrumented code')
.option('-d, --debug', 'Set environment variable DEBUG to "true" and see debug output')
.option('-nr, --noReport', 'Turn off resource reporting')
.description('Instrument and run raw JavaScript code.')
.action(function(file_path, options){
process.env['DEBUG'] = options.debug ? "true": "false";
var Pubsub = require("../lib/core/Pubsub.js");
var Code = require('../lib/core/Code.js');
if (options.restore){
var code = Code.fromSnapshotFile(file_path);
}
else {
var pubsub = new Pubsub(options.pubsub_url || 'mqtt://localhost');
var code = Code.fromFile(pubsub, file_path);
}
if (options.save) code.save(options.save);
code.run({
enableStats: !(options.noReport)
})
.then(function(instance){
instance.on('finished', function(){
code.kill();
process.exit();
})
if(process.stdin.isTTY){
process.stdin.setRawMode( true );
}
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(key){
switch(key.trim()){
case 'p':
instance.pause();
break;
case 'r':
instance.resume();
break;
case 's':
instance.snapshot(true).then(function(snap){
console.log(JSON.stringify(snap));
// console.log(jsBeautify(JSON.stringify(snap)));
instance.saveLastSnapshot(file_path+'.snap.json');
})
break;
case 'q':
code.kill();
process.exit();
break;
case '\u0003':
code.kill();
process.exit();
default:
console.log(key);
}
});
})
});
program
.command('dispatch <pubsub_url> <ctrl> [args...]')
.description('Use the dispatcher to perform operations over the network such as running or migrating processes')
.action(function(pubsub_url, ctrl, args){
var Dispatcher = require('../lib/core/Dispatcher.js');
var helpers = require('../lib/helpers.js');
// console.log(pubsub_url, ctrl, args);
var prepare = helpers.defer();
switch(ctrl){
case 'run':
var engine_id = args[0];
var file_path = args[1];
if (engine_id && file_path){
fs.readFile(file_path, function(err, data){
if (err) prepare.reject({ reason: 'Error reading file at '+file_path});
else {
var source = data.toString();
prepare.resolve({
type: ctrl,
args: [engine_id, path.basename(file_path), source]
})
}
})
}
else {
prepare.reject({ reason: 'Invalid arguments for "run" command. It should be "run <engine_id> <local_file_path>"'});
}
break;
case 'pause':
if (args[0]){
prepare.resolve({ type: ctrl, args: args })
}
else {
prepare.reject({ reason: 'Invalid argument for "'+ctrl+'" command. It should be "'+ctrl+' <process_id>"'})
}
break;
case 'resume':
if (args[0]){
prepare.resolve({ type: ctrl, args: args })
}
else {
prepare.reject({ reason: 'Invalid argument for "'+ctrl+'" command. It should be "'+ctrl+' <process_id>"'})
}
break;
case 'kill':
if (args[0]){
prepare.resolve({ type: ctrl, args: args })
}
else {
prepare.reject({ reason: 'Invalid argument for "'+ctrl+'" command. It should be "'+ctrl+' <process_id>"'})
}
break;
case 'migrate':
var from_id = args[0];
var to_id = args[1];
var code_name = args[2];
var proc_id = args[3];
if (from_id && to_id && code_name && proc_id){
prepare.resolve({ type: ctrl, args: args });
}
else {
prepare.reject({ reason: 'Invalid arguments for "migrate" command. It should be "run <from_engine_id> <to_engine_id> <code_name> <process_id>"'});
}
break;
default:
console.log('Invalid <ctrl> argument. It should be one of: run, pause, resume, kill, migrate')
break;
}
prepare.promise.then(function(action){
var dispatcher = new Dispatcher({ pubsub_url: pubsub_url });
var apply = helpers.defer();
dispatcher.on('ready', function(){
setTimeout(function(){
dispatcher.applyActions([action])
.then(function(results){
apply.resolve({ dispatcher: dispatcher, result: results[0] });
}).catch(function(error){
apply.reject(error);
})
}, 500); // Giving 500 ms timeout to ensure the target engine is discovered by the Dispatcher
});
return apply.promise;
}).catch(function(error){
console.log(error.reason);
process.exit(1);
}).then(function(data){
console.log(data.result);
data.dispatcher.kill();
})
});
program
.command('pubsub')
.option('-p, --port <port>', 'Port to Listen to')
.option('-d, --debug', 'Set environment variable DEBUG to "true" and see debug output')
.description('Start a Pubsub Server')
.action(function(options){
process.env['DEBUG'] = options.debug ? "true": "false";
var Pubsub = require('../lib/core/Pubsub.js');
var server = new Pubsub.Server();
process.on('SIGINT', function(){
server.kill().then(function(){
process.exit();
})
});
});
program
.command('debug <action> [args...]')
.description('Use Debug Commands')
.action(function(action, args){
var Pubsub = require('../lib/core/Pubsub.js');
var pubsub = new Pubsub('mqtt://localhost');
pubsub.on('ready', function(){
console.log(args);
pubsub.publish(args[0], JSON.parse(args[1]))
.then(function(){
pubsub.kill()
.then(function(){
process.exit();
})
})
})
});
program.parse(process.argv);
if (process.argv.length === 2){
var Shell = require('../lib/util/Shell.js');
var shell = new Shell;
}