mirror of
https://github.com/remittor/zapret-openwrt.git
synced 2026-01-30 14:20:37 +03:00
luci: Restart service on press Save&Apply
This commit is contained in:
@@ -49,5 +49,7 @@ return baseclass.extend({
|
|||||||
dst_obj.packager.path = '/bin/opkg';
|
dst_obj.packager.path = '/bin/opkg';
|
||||||
dst_obj.packager.args = [ 'list-installed', '*'+this.appName+'*' ];
|
dst_obj.packager.args = [ 'list-installed', '*'+this.appName+'*' ];
|
||||||
}
|
}
|
||||||
|
dst_obj.skey_pkg_dict = this.appName + '-pkg-dict';
|
||||||
|
dst_obj.skey_need_restart = this.appName + '-need-restart';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ return view.extend({
|
|||||||
}
|
}
|
||||||
let svc_boot = status_array[0] ? true : false;
|
let svc_boot = status_array[0] ? true : false;
|
||||||
let svc_en = status_array[1]; // stdout: empty or error text
|
let svc_en = status_array[1]; // stdout: empty or error text
|
||||||
let svc_info = status_array[2]; // stdout: JSON as text
|
let svc_info = status_array[2]; // dict for services
|
||||||
let proc_list = status_array[3]; // stdout: multiline text
|
let proc_list = status_array[3]; // stdout: multiline text
|
||||||
let pkg_dict = status_array[4]; // stdout: installed packages
|
let pkg_dict = status_array[4]; // stdout: installed packages
|
||||||
let stratlist = status_array[5]; // array of strat names
|
let stratlist = status_array[5]; // array of strat names
|
||||||
@@ -140,45 +140,21 @@ return view.extend({
|
|||||||
let btn = document.getElementById(button);
|
let btn = document.getElementById(button);
|
||||||
this.disableButtons(true, btn);
|
this.disableButtons(true, btn);
|
||||||
poll.stop();
|
poll.stop();
|
||||||
let errmsg = null;
|
|
||||||
try {
|
try {
|
||||||
let exec_cmd = null;
|
await tools.serviceActionEx(action, args, false);
|
||||||
let exec_arg = [ ];
|
|
||||||
if (action == 'start' || action == 'restart') {
|
|
||||||
if (tools.checkUnsavedChanges()) {
|
|
||||||
ui.changes.apply(true);
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 100));
|
|
||||||
}
|
|
||||||
exec_cmd = tools.syncCfgPath;
|
|
||||||
errmsg = _('Unable to run sync_config.sh script.');
|
|
||||||
}
|
|
||||||
if (action == 'reset') {
|
|
||||||
exec_cmd = tools.defaultCfgPath;
|
|
||||||
exec_arg = args; // (reset_ipset)(sync) ==> restore all configs + sync config
|
|
||||||
errmsg = _('Unable to run restore-def-cfg.sh script.');
|
|
||||||
action = null;
|
|
||||||
}
|
|
||||||
if (exec_cmd) {
|
|
||||||
let res = await fs.exec(exec_cmd, exec_arg);
|
|
||||||
if (res.code != 0) {
|
|
||||||
throw Error('res.code = ' + res.code);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (hide_modal) {
|
if (hide_modal) {
|
||||||
ui.hideModal();
|
ui.hideModal();
|
||||||
}
|
}
|
||||||
errmsg = null;
|
|
||||||
await tools.handleServiceAction(tools.appName, action);
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
let msg = errmsg ? errmsg : _('Unable to run service action') + ' "' + action + '".';
|
//ui.addNotification(null, E('p', 'Error: ' + e.message));
|
||||||
ui.addNotification(null, E('p', msg + ' Error: ' + e.message));
|
|
||||||
} finally {
|
} finally {
|
||||||
if (!poll.active()) {
|
|
||||||
poll.start();
|
|
||||||
}
|
|
||||||
if (btn && btn_dis) {
|
if (btn && btn_dis) {
|
||||||
setTimeout(() => { btn.disabled = true; }, 0);
|
setTimeout(() => { btn.disabled = true; }, 0);
|
||||||
}
|
}
|
||||||
|
if (!poll.active()) {
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 10));
|
||||||
|
poll.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -309,6 +285,8 @@ return view.extend({
|
|||||||
}
|
}
|
||||||
let cfg = uci.get(tools.appName, 'config');
|
let cfg = uci.get(tools.appName, 'config');
|
||||||
|
|
||||||
|
tools.checkAndRestartSvc(status_array[2]); // svc_info
|
||||||
|
|
||||||
let pkgdict = status_array[4];
|
let pkgdict = status_array[4];
|
||||||
if (pkgdict == null) {
|
if (pkgdict == null) {
|
||||||
ui.addNotification(null, E('p', _('Unable to enumerate installed packages') + ': render()'));
|
ui.addNotification(null, E('p', _('Unable to enumerate installed packages') + ': render()'));
|
||||||
|
|||||||
@@ -13,43 +13,18 @@ document.head.appendChild(E('link', {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
return view.extend({
|
return view.extend({
|
||||||
parsers: { },
|
svc_info: null,
|
||||||
|
|
||||||
appStatusCode: null,
|
|
||||||
|
|
||||||
depends: function(elem, key, array, empty=true) {
|
|
||||||
if (empty && array.length === 0) {
|
|
||||||
elem.depends(key, '_dummy');
|
|
||||||
} else {
|
|
||||||
array.forEach(e => elem.depends(key, e));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
validateIpPort: function(section, value) {
|
|
||||||
return (/^$|^([0-9]{1,3}\.){3}[0-9]{1,3}(#[\d]{2,5})?$/.test(value)) ? true : _('Expecting:')
|
|
||||||
+ ` ${_('One of the following:')}\n - ${_('valid IP address')}\n - ${_('valid address#port')}\n`;
|
|
||||||
},
|
|
||||||
|
|
||||||
validateUrl: function(section, value) {
|
|
||||||
return (/^$|^https?:\/\/[\w.-]+(:[0-9]{2,5})?[\w\/~.&?+=-]*$/.test(value)) ? true : _('Expecting:')
|
|
||||||
+ ` ${_('valid URL')}\n`;
|
|
||||||
},
|
|
||||||
|
|
||||||
load: function() {
|
load: function() {
|
||||||
return Promise.all([
|
return tools.baseLoad();
|
||||||
{ code: -1}, // L.resolveDefault(fs.exec(tools.execPath, [ 'raw-status' ]), 1),
|
|
||||||
null, // L.resolveDefault(fs.list(tools.parsersDir), null),
|
|
||||||
uci.load(tools.appName),
|
|
||||||
]).catch(e => {
|
|
||||||
ui.addNotification(null, E('p', _('Unable to read the contents') + ': %s '.format(e.message) ));
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
render: function(data) {
|
render: function(data) {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.appStatusCode = data[0].code;
|
this.svc_info = data.svc_info;
|
||||||
|
tools.checkAndRestartSvc(this.svc_info);
|
||||||
|
|
||||||
let m, s, o, tabname;
|
let m, s, o, tabname;
|
||||||
|
|
||||||
@@ -480,12 +455,14 @@ return view.extend({
|
|||||||
return map_promise;
|
return map_promise;
|
||||||
},
|
},
|
||||||
|
|
||||||
handleSaveApply: function(ev, mode) {
|
handleSaveApply: function(ev, mode)
|
||||||
|
{
|
||||||
return this.handleSave(ev).then(() => {
|
return this.handleSave(ev).then(() => {
|
||||||
ui.changes.apply(mode == '0');
|
ui.changes.apply(mode == '0');
|
||||||
//if (this.appStatusCode != 1 && this.appStatusCode != 2) {
|
if (this.svc_info?.dmn.inited) {
|
||||||
// window.setTimeout(() => fs.exec(tools.execPath, [ 'restart' ]), 3000);
|
localStorage.setItem(tools.skey_need_restart, '1');
|
||||||
//}
|
console.log('STOR KEY: '+tools.skey_need_restart+' = 1');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -103,11 +103,10 @@ return baseclass.extend({
|
|||||||
|
|
||||||
getPackageDict: function()
|
getPackageDict: function()
|
||||||
{
|
{
|
||||||
let ses_var_name = this.appName+'_pkgdict';
|
|
||||||
let exec_cmd = this.packager.path;
|
let exec_cmd = this.packager.path;
|
||||||
let exec_arg = this.packager.args;
|
let exec_arg = this.packager.args;
|
||||||
return fs.exec(exec_cmd, exec_arg).then(res => {
|
return fs.exec(exec_cmd, exec_arg).then(res => {
|
||||||
let pdict_json = sessionStorage.getItem(ses_var_name);
|
let pdict_json = localStorage.getItem(this.skey_pkg_dict);
|
||||||
if (res.code != 0) {
|
if (res.code != 0) {
|
||||||
console.log(this.appName + ': Unable to enumerate installed packages. code = ' + res.code);
|
console.log(this.appName + ': Unable to enumerate installed packages. code = ' + res.code);
|
||||||
if (pdict_json != null) {
|
if (pdict_json != null) {
|
||||||
@@ -117,7 +116,7 @@ return baseclass.extend({
|
|||||||
}
|
}
|
||||||
let pdict = this.decode_pkg_list(res.stdout);
|
let pdict = this.decode_pkg_list(res.stdout);
|
||||||
if (pdict != pdict_json) {
|
if (pdict != pdict_json) {
|
||||||
sessionStorage.setItem(ses_var_name, JSON.stringify(pdict)); // renew cache
|
localStorage.setItem(this.skey_pkg_dict, JSON.stringify(pdict)); // renew cache
|
||||||
}
|
}
|
||||||
return pdict;
|
return pdict;
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
@@ -138,7 +137,9 @@ return baseclass.extend({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
handleServiceAction: function(name, action) {
|
handleServiceAction: function(name, action, throwed = false)
|
||||||
|
{
|
||||||
|
console.log('handleServiceAction: '+name+' '+action);
|
||||||
return this.callInitAction(name, action).then(success => {
|
return this.callInitAction(name, action).then(success => {
|
||||||
if (!success) {
|
if (!success) {
|
||||||
throw _('Command failed');
|
throw _('Command failed');
|
||||||
@@ -146,9 +147,93 @@ return baseclass.extend({
|
|||||||
return true;
|
return true;
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
ui.addNotification(null, E('p', _('Service action failed "%s %s": %s').format(name, action, e)));
|
ui.addNotification(null, E('p', _('Service action failed "%s %s": %s').format(name, action, e)));
|
||||||
|
if (throwed) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
serviceActionEx: async function(action, args = [ ], throwed = false)
|
||||||
|
{
|
||||||
|
let errmsg = null;
|
||||||
|
try {
|
||||||
|
let exec_cmd = null;
|
||||||
|
let exec_arg = [ ];
|
||||||
|
if (action == 'start' || action == 'restart') {
|
||||||
|
if (this.checkUnsavedChanges()) {
|
||||||
|
await ui.changes.apply(true);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100));
|
||||||
|
}
|
||||||
|
exec_cmd = this.syncCfgPath;
|
||||||
|
errmsg = _('Unable to run sync_config.sh script.');
|
||||||
|
}
|
||||||
|
if (action == 'reset') {
|
||||||
|
exec_cmd = this.defaultCfgPath;
|
||||||
|
exec_arg = args; // (reset_ipset)(sync) ==> restore all configs + sync config
|
||||||
|
errmsg = _('Unable to run restore-def-cfg.sh script.');
|
||||||
|
action = null;
|
||||||
|
}
|
||||||
|
if (exec_cmd) {
|
||||||
|
let res = await fs.exec(exec_cmd, exec_arg);
|
||||||
|
if (res.code != 0) {
|
||||||
|
throw Error('res.code = ' + res.code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
errmsg = null;
|
||||||
|
await this.handleServiceAction(this.appName, action, throwed);
|
||||||
|
} catch(e) {
|
||||||
|
if (throwed) {
|
||||||
|
throw e;
|
||||||
|
} else {
|
||||||
|
let msg = errmsg ? errmsg : _('Unable to run service action') + ' "' + action + '".';
|
||||||
|
ui.addNotification(null, E('p', msg + ' Error: ' + e.message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
baseLoad: function(callback, cbarg)
|
||||||
|
{
|
||||||
|
return Promise.all([
|
||||||
|
this.getSvcInfo(), // svc_info
|
||||||
|
uci.load(this.appName),
|
||||||
|
])
|
||||||
|
.then( ([svcInfo, uci_data]) => {
|
||||||
|
let svc_info = this.decode_svc_info(true, svcInfo, [ ], null);
|
||||||
|
let ret = { svc_info, uci_data };
|
||||||
|
if (typeof callback === 'function') {
|
||||||
|
const res = callback(cbarg, ret);
|
||||||
|
if (res && typeof res.then === 'function') {
|
||||||
|
return res.then(() => ret);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
ui.addNotification(null, E('p', _('Unable to read the contents') + ' (baseLoad): %s '.format(e.message) ));
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
checkAndRestartSvc: function(svcInfo)
|
||||||
|
{
|
||||||
|
let svc_info = null;
|
||||||
|
if (svcInfo?.autorun !== undefined && svcInfo?.dmn !== undefined) {
|
||||||
|
svc_info = svcInfo;
|
||||||
|
} else
|
||||||
|
if (typeof(svcInfo) == 'object') {
|
||||||
|
svc_info = this.decode_svc_info(true, svcInfo, [ ], null);
|
||||||
|
}
|
||||||
|
//console.log('checkAndRestartSvc: svc_info = '+JSON.stringify(svc_info));
|
||||||
|
let need_restart = localStorage.getItem(this.skey_need_restart);
|
||||||
|
if (need_restart) {
|
||||||
|
localStorage.removeItem(this.skey_need_restart);
|
||||||
|
if (svcInfo?.dmn !== undefined && svc_info.dmn.inited) {
|
||||||
|
this.serviceActionEx('restart');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
checkUnsavedChanges: function()
|
checkUnsavedChanges: function()
|
||||||
{
|
{
|
||||||
if (!ui.changes) return false;
|
if (!ui.changes) return false;
|
||||||
@@ -246,13 +331,15 @@ return baseclass.extend({
|
|||||||
},
|
},
|
||||||
"status": this.statusDict.error,
|
"status": this.statusDict.error,
|
||||||
};
|
};
|
||||||
if (proc_list.code != 0) {
|
let plist = proc_list;
|
||||||
return -2;
|
if (proc_list?.code !== undefined) {
|
||||||
}
|
if (proc_list.code != 0) {
|
||||||
let plist = this.get_pid_list(proc_list.stdout);
|
return -2;
|
||||||
|
}
|
||||||
if (plist.length < 4) {
|
plist = this.get_pid_list(proc_list.stdout);
|
||||||
return -3;
|
if (plist.length < 4) {
|
||||||
|
return -3;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (typeof(svc_info) !== 'object') {
|
if (typeof(svc_info) !== 'object') {
|
||||||
return -4;
|
return -4;
|
||||||
|
|||||||
Reference in New Issue
Block a user