mirror of
https://github.com/remittor/zapret-openwrt.git
synced 2026-01-30 22:30:36 +03:00
luci: tools: Fix function execAndRead
This commit is contained in:
@@ -49,7 +49,7 @@ return baseclass.extend({
|
|||||||
log: '/tmp/'+tools.appName+'_dwc.log',
|
log: '/tmp/'+tools.appName+'_dwc.log',
|
||||||
logArea: this.logArea,
|
logArea: this.logArea,
|
||||||
callback: this.execAndReadCallback,
|
callback: this.execAndReadCallback,
|
||||||
cbarg: this, // wnd
|
ctx: this,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -70,27 +70,27 @@ return baseclass.extend({
|
|||||||
log: '/tmp/'+tools.appName+'_dwc.log',
|
log: '/tmp/'+tools.appName+'_dwc.log',
|
||||||
logArea: this.logArea,
|
logArea: this.logArea,
|
||||||
callback: this.execAndReadCallback,
|
callback: this.execAndReadCallback,
|
||||||
cbarg: this, // wnd
|
ctx: this,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
execAndReadCallback: function(wnd, rc, txt = '')
|
execAndReadCallback: function(rc, txt = '')
|
||||||
{
|
{
|
||||||
wnd.setBtnMode(1, 1, 1);
|
this.setBtnMode(1, 1, 1);
|
||||||
if (rc == 0 && txt) {
|
if (rc == 0 && txt) {
|
||||||
wnd.appendLog('=========================================================');
|
this.appendLog('=========================================================');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (rc >= 500) {
|
if (rc >= 500) {
|
||||||
if (txt) {
|
if (txt) {
|
||||||
wnd.appendLog(txt.startsWith('ERROR') ? txt : 'ERROR: ' + txt);
|
this.appendLog(txt.startsWith('ERROR') ? txt : 'ERROR: ' + txt);
|
||||||
} else {
|
} else {
|
||||||
wnd.appendLog('ERROR: ' + wnd._action + ': Terminated with error code = ' + rc);
|
this.appendLog('ERROR: ' + this._action + ': Terminated with error code = ' + rc);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
wnd.appendLog('ERROR: Process finished with retcode = ' + rc);
|
this.appendLog('ERROR: Process finished with retcode = ' + rc);
|
||||||
}
|
}
|
||||||
wnd.appendLog('=========================================================');
|
this.appendLog('=========================================================');
|
||||||
},
|
},
|
||||||
|
|
||||||
openDiagnostDialog: function(pkg_arch)
|
openDiagnostDialog: function(pkg_arch)
|
||||||
|
|||||||
@@ -740,7 +740,7 @@ return baseclass.extend({
|
|||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|
||||||
execAndRead: async function({ cmd = [ ], log = '', logArea = null, callback = null, cbarg = null, hiderow = [ ], rpc_timeout = 5, rpc_root = false } = {})
|
execAndRead: async function({ cmd = [ ], log = '', logArea = null, callback = null, ctx = null, hiderow = [ ], rpc_timeout = 5, rpc_root = false } = {})
|
||||||
{
|
{
|
||||||
function appendLog(msg, end = '\n')
|
function appendLog(msg, end = '\n')
|
||||||
{
|
{
|
||||||
@@ -764,23 +764,23 @@ return baseclass.extend({
|
|||||||
await fs.exec('/bin/busybox', [ 'rm', '-f', logFile + '*' ], null, rpc_opt);
|
await fs.exec('/bin/busybox', [ 'rm', '-f', logFile + '*' ], null, rpc_opt);
|
||||||
appendLog('Output file cleared!');
|
appendLog('Output file cleared!');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return callback(cbarg, 500, 'ERROR: Failed to clear output file');
|
return callback.call(ctx, 500, 'ERROR: Failed to clear output file');
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
let opt_list = [ logFile ];
|
let opt_list = [ logFile ];
|
||||||
opt_list.push(...cmd);
|
opt_list.push(...cmd);
|
||||||
let res = await fs.exec(this.appDir+'/script-exec.sh', opt_list, null, rpc_opt);
|
let res = await fs.exec(this.appDir+'/script-exec.sh', opt_list, null, rpc_opt);
|
||||||
if (res.code != 0) {
|
if (res.code != 0) {
|
||||||
return callback(cbarg, 525, 'ERROR: cannot run "' + cmd[0] + '" script! (error = ' + res.code + ')');
|
return callback.call(ctx, 525, 'ERROR: cannot run "' + cmd[0] + '" script! (error = ' + res.code + ')');
|
||||||
}
|
}
|
||||||
appendLog('Process started...');
|
appendLog('Process started...');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return callback(cbarg, 520, 'ERROR: Failed on execute process: ' + e.message);
|
return callback.call(ctx, 520, 'ERROR: Failed on execute process: ' + e.message);
|
||||||
}
|
}
|
||||||
let lastLen = 0;
|
let lastLen = 0;
|
||||||
let retCode = -1;
|
let retCode = -1;
|
||||||
return await new Promise(async (resolve, reject) => {
|
return await new Promise(async (resolve, reject) => {
|
||||||
async function poll()
|
async function epoll()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
let res = await fs.exec('/bin/cat', [ logFile ], null, rpc_opt);
|
let res = await fs.exec('/bin/cat', [ logFile ], null, rpc_opt);
|
||||||
@@ -796,7 +796,7 @@ return baseclass.extend({
|
|||||||
let rc = await fs.exec('/bin/cat', [ rcFile ], null, rpc_opt);
|
let rc = await fs.exec('/bin/cat', [ rcFile ], null, rpc_opt);
|
||||||
if (rc.code != 0) {
|
if (rc.code != 0) {
|
||||||
fixLogEnd();
|
fixLogEnd();
|
||||||
resolve(callback(cbarg, 545, 'ERROR: cannot read file "' + rcFile + '"'));
|
resolve(callback.call(ctx, 545, 'ERROR: cannot read file "' + rcFile + '"'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (rc.stdout) {
|
if (rc.stdout) {
|
||||||
@@ -806,13 +806,13 @@ return baseclass.extend({
|
|||||||
if (retCode >= 0) {
|
if (retCode >= 0) {
|
||||||
fixLogEnd();
|
fixLogEnd();
|
||||||
if (retCode == 0 && res.stdout) {
|
if (retCode == 0 && res.stdout) {
|
||||||
resolve(callback(cbarg, 0, res.stdout));
|
resolve(callback.call(ctx, 0, res.stdout));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
resolve(callback(cbarg, retCode, 'ERROR: Process failed with error ' + retCode));
|
resolve(callback.call(ctx, retCode, 'ERROR: Process failed with error ' + retCode));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setTimeout(poll, 500);
|
setTimeout(epoll, 500);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
let skip_err = false;
|
let skip_err = false;
|
||||||
if (e.message?.includes('RPC call to file/exec failed with error -32000: Object not found')) {
|
if (e.message?.includes('RPC call to file/exec failed with error -32000: Object not found')) {
|
||||||
@@ -823,17 +823,17 @@ return baseclass.extend({
|
|||||||
}
|
}
|
||||||
if (skip_err) {
|
if (skip_err) {
|
||||||
console.warn('WARN: execAndRead: ' + e.message);
|
console.warn('WARN: execAndRead: ' + e.message);
|
||||||
setTimeout(poll, 500);
|
setTimeout(epoll, 500);
|
||||||
return; // goto next poll iteration
|
return; // goto next epoll iteration
|
||||||
}
|
}
|
||||||
fixLogEnd();
|
fixLogEnd();
|
||||||
let errtxt = 'ERROR: execAndRead: ' + e.message;
|
let errtxt = 'ERROR: execAndRead: ' + e.message;
|
||||||
errtxt += 'ERROR: execAndRead: ' + e.stack?.trim().split('\n')[0];
|
errtxt += 'ERROR: execAndRead: ' + e.stack?.trim().split('\n')[0];
|
||||||
callback(cbarg, 540, errtxt);
|
callback.call(ctx, 540, errtxt);
|
||||||
reject(e);
|
reject(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
poll();
|
epoll();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ return baseclass.extend({
|
|||||||
log: '/tmp/'+tools.appName+'_pkg_check.log',
|
log: '/tmp/'+tools.appName+'_pkg_check.log',
|
||||||
logArea: this.logArea,
|
logArea: this.logArea,
|
||||||
callback: this.execAndReadCallback,
|
callback: this.execAndReadCallback,
|
||||||
cbarg: this, // wnd
|
ctx: this,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -84,54 +84,54 @@ return baseclass.extend({
|
|||||||
logArea: this.logArea,
|
logArea: this.logArea,
|
||||||
hiderow: /^ \* resolve_conffiles.*(?:\r?\n|$)/gm,
|
hiderow: /^ \* resolve_conffiles.*(?:\r?\n|$)/gm,
|
||||||
callback: this.execAndReadCallback,
|
callback: this.execAndReadCallback,
|
||||||
cbarg: this, // wnd
|
ctx: this,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
execAndReadCallback: function(wnd, rc, txt = '')
|
execAndReadCallback: function(rc, txt = '')
|
||||||
{
|
{
|
||||||
//console.log('execAndReadCallback = ' + rc + '; _action = ' + wnd._action);
|
//console.log('execAndReadCallback = ' + rc + '; _action = ' + this._action);
|
||||||
if (rc == 0 && txt) {
|
if (rc == 0 && txt) {
|
||||||
let code = txt.match(/^RESULT:\s*\(([^)]+)\)\s+.+$/m);
|
let code = txt.match(/^RESULT:\s*\(([^)]+)\)\s+.+$/m);
|
||||||
if (wnd._action == 'checkUpdates') {
|
if (this._action == 'checkUpdates') {
|
||||||
wnd.appendLog('=========================================================');
|
this.appendLog('=========================================================');
|
||||||
if (code && code[1] == 'E') {
|
if (code && code[1] == 'E') {
|
||||||
wnd.btn_install.textContent = _('Reinstall');
|
this.btn_install.textContent = _('Reinstall');
|
||||||
} else {
|
} else {
|
||||||
wnd.btn_install.textContent = _('Install');
|
this.btn_install.textContent = _('Install');
|
||||||
}
|
}
|
||||||
let pkg_url = txt.match(/^ZAP_PKG_URL\s*=\s*(.+)$/m);
|
let pkg_url = txt.match(/^ZAP_PKG_URL\s*=\s*(.+)$/m);
|
||||||
if (code && pkg_url) {
|
if (code && pkg_url) {
|
||||||
if (!wnd.forced_reinstall) {
|
if (!this.forced_reinstall) {
|
||||||
if (code[1] == 'E' || code[1] == 'G') {
|
if (code[1] == 'E' || code[1] == 'G') {
|
||||||
wnd.setStage(0); // install not needed
|
this.setStage(0); // install not needed
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wnd.pkg_url = pkg_url[1];
|
this.pkg_url = pkg_url[1];
|
||||||
wnd.setStage(2); // enable all buttons
|
this.setStage(2); // enable all buttons
|
||||||
return; // install allowed
|
return; // install allowed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (wnd._action == 'installUpdates') {
|
if (this._action == 'installUpdates') {
|
||||||
if (wnd._test || (code && code[1] == '+')) {
|
if (this._test || (code && code[1] == '+')) {
|
||||||
wnd.setStage(9);
|
this.setStage(9);
|
||||||
wnd.appendLog('Please update WEB-page (press F5)');
|
this.appendLog('Please update WEB-page (press F5)');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wnd.setStage(0);
|
this.setStage(0);
|
||||||
if (rc >= 500) {
|
if (rc >= 500) {
|
||||||
if (txt) {
|
if (txt) {
|
||||||
wnd.appendLog(txt.startsWith('ERROR') ? txt : 'ERROR: ' + txt);
|
this.appendLog(txt.startsWith('ERROR') ? txt : 'ERROR: ' + txt);
|
||||||
} else {
|
} else {
|
||||||
wnd.appendLog('ERROR: ' + wnd._action + ': Terminated with error code = ' + rc);
|
this.appendLog('ERROR: ' + this._action + ': Terminated with error code = ' + rc);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
wnd.appendLog('ERROR: Process finished with retcode = ' + rc);
|
this.appendLog('ERROR: Process finished with retcode = ' + rc);
|
||||||
}
|
}
|
||||||
wnd.appendLog('=========================================================');
|
this.appendLog('=========================================================');
|
||||||
},
|
},
|
||||||
|
|
||||||
openUpdateDialog: function(pkg_arch)
|
openUpdateDialog: function(pkg_arch)
|
||||||
|
|||||||
Reference in New Issue
Block a user