[WIP RFC luci] luci-mod-network: provide control over /etc/init.d/dnsmasq service

Rafał Miłecki zajec5 at gmail.com
Fri Feb 16 02:22:58 PST 2024


From: Rafał Miłecki <rafal at milecki.pl>

It's easier to control service from the same page that allows
configuring it.

Cc: Jo-Philipp Wich <jo at mein.io>
Signed-off-by: Rafał Miłecki <rafal at milecki.pl>
---
Over years we got a lot of redundant "enabled" (or "disabled") UCI
config options where it'd be perfecly enough to use init.d script for
controlling it.

There are probably two reasons for that:
1. sysupgrade not preserving disabled services state
2. UI being more friendly by allowing disabling on configuration page

The first issue is being handled as sysupgrade improvement.
This change show how we could make UI more friendly for services
management and hopefully avoid redundant "enabled" UCI config options.

 .../resources/view/network/dhcp.js            | 47 ++++++++++++++++++-
 1 file changed, 45 insertions(+), 2 deletions(-)

diff --git a/modules/luci-mod-network/htdocs/luci-static/resources/view/network/dhcp.js b/modules/luci-mod-network/htdocs/luci-static/resources/view/network/dhcp.js
index 7b0ae3c1f9..8b6a849915 100644
--- a/modules/luci-mod-network/htdocs/luci-static/resources/view/network/dhcp.js
+++ b/modules/luci-mod-network/htdocs/luci-static/resources/view/network/dhcp.js
@@ -9,7 +9,7 @@
 'require validation';
 'require tools.widgets as widgets';
 
-var callHostHints, callDUIDHints, callDHCPLeases, CBILeaseStatus, CBILease6Status;
+var callHostHints, callDUIDHints, callDHCPLeases, callRcList, CBIServiceEnabled, CBIServiceRunning, CBILeaseStatus, CBILease6Status;
 
 callHostHints = rpc.declare({
 	object: 'luci-rpc',
@@ -29,6 +29,37 @@ callDHCPLeases = rpc.declare({
 	expect: { '': {} }
 });
 
+callRcList = rpc.declare({
+	object: 'rc',
+	method: 'list',
+	params: ['name'],
+	expect: { '': {} }
+});
+
+CBIServiceEnabled = form.DummyValue.extend({
+	renderWidget: function(section_id, option_id, cfgvalue) {
+		return E([], [
+			E('span', {}, cfgvalue ? _('Enabled') : _('Disabled')),
+			' ',
+			E('button', {
+				'class': 'btn'
+			}, [ cfgvalue ? _('Disable') : _('Enable') ])
+		]);
+	},
+});
+
+CBIServiceRunning = form.DummyValue.extend({
+	renderWidget: function(section_id, option_id, cfgvalue) {
+		return E([], [
+			E('span', {}, cfgvalue ? _('Started') : _('Stopped')),
+			' ',
+			E('button', {
+				'class': 'btn'
+			}, [ cfgvalue ? _('Stop') : _('Start') ])
+		]);
+	},
+});
+
 CBILeaseStatus = form.DummyValue.extend({
 	renderWidget: function(section_id, option_id, cfgvalue) {
 		return E([
@@ -277,7 +308,8 @@ return view.extend({
 			callHostHints(),
 			callDUIDHints(),
 			getDHCPPools(),
-			network.getNetworks()
+			network.getNetworks(),
+			callRcList('dnsmasq')
 		]);
 	},
 
@@ -287,6 +319,7 @@ return view.extend({
 		    duids = hosts_duids_pools[1],
 		    pools = hosts_duids_pools[2],
 		    networks = hosts_duids_pools[3],
+		    service = hosts_duids_pools[4],
 		    m, s, o, ss, so;
 
 		let noi18nstrings = {
@@ -315,6 +348,16 @@ return view.extend({
 		m = new form.Map('dhcp', _('DHCP and DNS'),
 			_('Dnsmasq is a lightweight <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr> server and <abbr title="Domain Name System">DNS</abbr> forwarder.'));
 
+		s = m.section(form.TypedSection);
+		s.anonymous = true;
+		s.cfgsections = function() { return [ '_status' ] };
+
+		o = s.option(CBIServiceEnabled, '_enabled', _('Autostart'));
+		o.cfgvalue = function() { return service.dnsmasq.enabled; };
+
+		o = s.option(CBIServiceRunning, '_running', _('Current status'));
+		o.cfgvalue = function() { return service.dnsmasq.running; };
+
 		s = m.section(form.TypedSection, 'dnsmasq');
 		s.anonymous = false;
 		s.addremove = true;
-- 
2.35.3




More information about the openwrt-devel mailing list