-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinstall.js
463 lines (424 loc) · 20 KB
/
install.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* Copyright (c) 2020-2024 LG Electronics Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
const async = require('async'),
crypto = require('crypto'),
fs = require('fs'),
npmlog = require('npmlog'),
path = require('path'),
streamBuffers = require('stream-buffers'),
util = require('util'),
sessionLib = require('./session'),
Appdata = require('./base/cli-appdata'),
errHndl = require('./base/error-handler'),
luna = require('./base/luna'),
novacom = require('./base/novacom'),
spinner = require('./util/spinner');
(function() {
const cliData = new Appdata(),
log = npmlog;
log.heading = 'installer';
log.level = 'warn';
const installer = {
/**
* @property {Object} log an npm log instance
*/
log: log,
/**
* Install the given package on the given target device
* @param {Object} options installation options
* @options options {Object} device the device to install the package onto, or null to select the default device
* @param {String} hostPkgPath absolute path on the host of the package to be installed
* @param {Function} next common-js callback
*/
install: function(options, hostPkgPath, next, middleCb) {
if (typeof next !== 'function') {
throw errHndl.getErrMsg("MISSING_CALLBACK", "next", util.inspect(next));
}
if (!hostPkgPath) {
return next(errHndl.getErrMsg("EMPTY_VALUE", "PACKAGE_FILE"));
}
const hostPkgName = path.basename(hostPkgPath),
configData = cliData.getConfig(true),
config = {
'tempDirForIpk': '/media/developer/temp',
'changeTempDir': true,
'removeIpkAfterInst': true
};
if (configData.install) {
const conf = configData.install;
for (const prop in conf) {
if (Object.prototype.hasOwnProperty.call(config, prop)) {
config[prop] = conf[prop];
}
}
}
const appId = 'com.ares.defaultName',
devicePkgPath = path.join(config.tempDirForIpk, hostPkgName).replace(/\\/g,'/'),
os = new streamBuffers.WritableStreamBuffer();
let srcMd5, dstMd5, md5DataSize = 200;
options = options || {};
log.info('install#install()', 'installing ' + hostPkgPath);
async.waterfall([
function(next) {
options.nReplies = 0; // -i
makeSession(options, next);
},
function(session, next) {
if (options.opkg) {
// FIXME: Need more consideration whether this condition is necessary or not.
if (options.session.getDevice().username !== 'root') {
return setImmediate(next, errHndl.getErrMsg("NEED_ROOT_PERMISSION", "opkg install"));
}
}
let cmd = '/usr/bin/test -d ' + config.tempDirForIpk + ' || /bin/mkdir -p ' + config.tempDirForIpk;
if (options.session.getDevice().username === 'root') {
cmd += ' && /bin/chmod 777 ' + config.tempDirForIpk;
}
options.op = (options.session.target.files || 'stream') + 'Put';
options.session.run(cmd, null, null, null, next);
},
function(next) {
middleCb("Installing package " + hostPkgPath);
spinner.start();
options.session.put(hostPkgPath, devicePkgPath, next);
},
function(next) {
options.session.run("/bin/ls -l \"" + devicePkgPath + "\"", null, os, null, next);
},
function(next) {
log.verbose("install#install()", "ls -l:", os.getContents().toString());
next();
},
function(next) {
const md5 = crypto.createHash('md5'),
buffer = Buffer.alloc(md5DataSize);
let pos = 0;
async.waterfall([
fs.lstat.bind(fs, hostPkgPath),
function(stat, next) {
if (stat.size > md5DataSize) {
pos = stat.size-md5DataSize;
} else {
pos = 0;
md5DataSize = stat.size;
}
next();
},
fs.open.bind(fs, hostPkgPath, 'r'),
function(fd, next) {
fs.read(fd, buffer, 0, md5DataSize, pos, function() {
md5.update(buffer);
next();
});
},
function() {
srcMd5 = md5.digest('hex');
if (!srcMd5) {
log.warn("install#install()", "Failed to get md5sum from the ipk file");
}
log.silly("install#install()", "srcMd5:", srcMd5);
next();
}
], function(err) {
next(err);
});
},
function(next) {
const cmd = "/usr/bin/tail -c " + md5DataSize + " \"" + devicePkgPath + "\" | /usr/bin/md5sum";
async.series([
function(next) {
options.session.run(cmd, null, _onData, null, next);
}
], function(err) {
if (err) {
return next(err);
}
});
function _onData(data) {
let str;
if (Buffer.isBuffer(data)) {
str = data.toString().trim();
} else {
str = data.trim();
}
if (str) {
dstMd5 = str.split('-')[0].trim();
log.silly("install#install()", "dstMd5:", dstMd5);
}
if (!dstMd5) {
log.warn("install#install()", "Failed to get md5sum from the transmitted file");
}
next();
}
},
function(next) {
if (!srcMd5 || !dstMd5) {
log.warn("install#install()", "Cannot verify transmitted file");
} else {
log.verbose("install#install()", "srcMd5:", srcMd5, ", dstMd5:", dstMd5);
if (srcMd5 !== dstMd5) {
return next(errHndl.getErrMsg("FAILED_TRANSMIT_FILE"));
}
}
next();
},
function(next) {
const op = (options.opkg) ? _opkg : _appinstalld;
op(next);
function _opkg(next) {
let cmd = '/usr/bin/opkg install "' + devicePkgPath + '"';
cmd = cmd.concat((options.opkg_param) ? ' ' + options.opkg_param : '');
async.series([
options.session.run.bind(options.session, cmd, null, __data, __data),
options.session.run.bind(options.session, '/usr/sbin/ls-control scan-services ', null, null, __data)
], function(err) {
if (err) {
return next(err);
}
next(null, null);
});
function __data(data) {
const str = (Buffer.isBuffer(data)) ? data.toString() : data;
middleCb(str.trim());
}
}
function _appinstalld(next) {
const target = options.session.getDevice(),
addr = target.lunaAddr.install,
returnValue = addr.returnValue.split('.'),
param = {
// luna param
id: appId,
ipkUrl: devicePkgPath,
subscribe: true
};
options.sessionCall = false;
luna.send(options, addr, param, function(lineObj, next) {
let resultValue = lineObj;
for (let index = 1; index < returnValue.length; index++) {
resultValue = resultValue[returnValue[index]];
}
if (resultValue.match(/FAILED/i)) {
// failure: stop
log.verbose("install#install()", "failure");
const errValue = ((lineObj.details && lineObj.details.reason) ? lineObj.details.reason :
(resultValue ? resultValue : ''));
next(errHndl.getErrMsg("FAILED_CALL_LUNA", errValue, null, addr.service));
} else if (resultValue.match(/installed|^SUCCESS/i)) {
// success: stop
log.verbose("install#install()", "success");
next(null, resultValue);
} else {
// no err & no status : continue
log.verbose("install#install()", "waiting");
next(null, null);
}
}, next);
}
},
function(status, next) {
if (typeof status === 'function') {
next = status;
}
if (config.removeIpkAfterInst) {
options.session.run('/bin/rm -f "' + devicePkgPath + '"', null, null, null, next);
} else {
next();
}
},
function(next) {
next(null, {msg: "Success"});
}
], function(err, result) {
next(err, result);
});
},
remove: function(options, packageName, next, middleCb) {
if (typeof next !== 'function') {
throw errHndl.getErrMsg("MISSING_CALLBACK", "next", util.inspect(next));
}
options = options || {};
async.waterfall([
function(next) {
options.nReplies = 0; // -i
makeSession(options, next);
},
function(session, next) {
if (options.opkg) {
// FIXME: Need more consideration whether this condition is necessary or not.
if (options.session.getDevice().username !== 'root') {
return setImmediate(next, errHndl.getErrMsg("NEED_ROOT_PERMISSION","opkg remove"));
}
}
setImmediate(next);
},
function(next) {
const op = (options.opkg) ? _opkg : _appinstalld;
op(next);
function _opkg(next) {
let cmd = '/usr/bin/opkg remove ' + packageName;
cmd = cmd.concat((options.opkg_param) ? ' ' + options.opkg_param : '');
async.series([
options.session.run.bind(options.session, cmd, null, __data, __error),
options.session.run.bind(options.session, '/usr/sbin/ls-control scan-services ',null, null, __error)
], function(err) {
if (err) {
return next(err);
}
next(null, {});
});
function __data(data) {
const str = (Buffer.isBuffer(data)) ? data.toString() : data;
if (str.match(/No packages installed or removed/g)) {
return next(errHndl.getErrMsg("FAILED_REMOVE_PACKAGE", packageName));
} else {
middleCb(str.trim());
}
}
function __error(data) {
const str = (Buffer.isBuffer(data)) ? data.toString() : data;
return next(new Error(str));
}
}
function _appinstalld(next) {
const target = options.session.getDevice(),
addr = target.lunaAddr.remove,
returnValue = addr.returnValue.split('.'),
param = {
// luna param
id: packageName,
subscribe: true
};
let exit = 0;
options.sessionCall = false;
luna.send(options, addr, param, function(lineObj, next) {
let resultValue = lineObj;
for (let index = 1; index < returnValue.length; index++) {
resultValue = resultValue[returnValue[index]];
}
if (resultValue.match(/FAILED/i)) {
// failure: stop
log.verbose("install#remove()", "failure");
if (!exit) {
exit++;
const errValue = ((lineObj.details && lineObj.details.reason) ? lineObj.details.reason :
(resultValue ? resultValue : ''));
next(errHndl.getErrMsg("FAILED_CALL_LUNA", errValue, null, addr.service));
}
} else if (resultValue.match(/removed|^SUCCESS/i)) {
log.verbose("install#remove()", "success");
// success: stop
next(null, {
status: resultValue
});
} else {
// no err & no status : continue
log.verbose("install#remove()", "waiting");
next();
}
}, next);
}
}
], function(err, result) {
log.silly("install#remove()", "err:", err, ", result:", result);
if (!err) {
result.msg = 'Removed package ' + packageName;
}
next(err, result);
});
},
list: function(options, next, middleCb) {
if (typeof next !== 'function') {
throw errHndl.getErrMsg("MISSING_CALLBACK", "next", util.inspect(next));
}
options = options || {};
async.series([
function(next) {
options.nReplies = 1; // -n 1
makeSession(options, next);
},
function(next) {
if (options.opkg) {
// FIXME: Need more consideration whether this condition is necessary or not.
if (options.session.getDevice().username !== 'root') {
return setImmediate(next, errHndl.getErrMsg("NEED_ROOT_PERMISSION", "opkg list"));
}
}
setImmediate(next);
},
function(next) {
sessionLib.getSessionList(options, next);
},
function(next) {
const op = (options.opkg) ? _opkg : _appinstalld;
op(next);
function _opkg(next) {
let cmd = '/usr/bin/opkg list';
cmd = cmd.concat((options.opkg_param) ? ' ' + options.opkg_param : '');
async.series([
options.session.run.bind(options.session, cmd,
null, __data, __data)
], function(err) {
if (err) {
return next(err);
}
next(null, {});
});
function __data(data) {
const str = (Buffer.isBuffer(data)) ? data.toString() : data;
middleCb(str.trim());
}
}
function _appinstalld(next) {
const addr = options.session.getDevice().lunaAddr.list,
returnValue = addr.returnValue.split('.'),
param = {
// luna param
subscribe: false
};
luna.send(options, addr, param, function(lineObj, next) {
let resultValue = lineObj;
for (let index = 1; index < returnValue.length; index++) {
resultValue = resultValue[returnValue[index]];
}
if (Array.isArray(resultValue)) {
// success: stop
for (let index = 0; index < resultValue.length; index++) {
if (!resultValue[index].visible) {
resultValue.splice(index, 1);
index--;
}
}
log.verbose("install#list()", "success");
next(null, resultValue);
} else {
// failure: stop
log.verbose("install#list()", "failure");
next(errHndl.getErrMsg("INVALID_OBJECT"));
}
}, next);
}
}
], function(err, results) {
log.silly("install#list()", "err:", err, ", results:", results[3]);
next(err, results[3]);
});
}
};
function makeSession(options, next) {
if (!options.session) {
log.info("install#makeSession()", "need to make new session");
const printTarget = true;
options.session = new novacom.Session(options.device, printTarget, next);
} else {
log.info("install#makeSession()", "already exist session");
next(null, options.session);
}
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = installer;
}
}());