diff options
author | Jo-Philipp Wich <jo@mein.io> | 2018-07-12 16:50:40 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2018-07-12 18:10:10 +0200 |
commit | c4dca36795f00fefa7337f66d4b42191350f4a2c (patch) | |
tree | fba6b5c00da5a5f9d81c226b258fdb0a5c0d4c5e /modules/luci-mod-admin-full/luasrc | |
parent | 54c9a77bd34de1b37488431149e98f64f8c01547 (diff) |
luci-mod-admin-full: rework interface overview page
Convert interface enable, disable and delete actions to proper cbi
operations so that we can benefit from the apply/rollback workflow
when performing critical interface operations.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-mod-admin-full/luasrc')
4 files changed, 316 insertions, 286 deletions
diff --git a/modules/luci-mod-admin-full/luasrc/controller/admin/network.lua b/modules/luci-mod-admin-full/luasrc/controller/admin/network.lua index 4605ffaf3..468068788 100644 --- a/modules/luci-mod-admin-full/luasrc/controller/admin/network.lua +++ b/modules/luci-mod-admin-full/luasrc/controller/admin/network.lua @@ -1,5 +1,5 @@ -- Copyright 2008 Steven Barth <steven@midlink.org> --- Copyright 2011-2015 Jo-Philipp Wich <jow@openwrt.org> +-- Copyright 2011-2018 Jo-Philipp Wich <jo@mein.io> -- Licensed to the public under the Apache License 2.0. module("luci.controller.admin.network", package.seeall) @@ -82,18 +82,12 @@ function index() page = entry({"admin", "network", "iface_add"}, form("admin_network/iface_add"), nil) page.leaf = true - page = entry({"admin", "network", "iface_delete"}, post("iface_delete"), nil) - page.leaf = true - page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil) page.leaf = true page = entry({"admin", "network", "iface_reconnect"}, post("iface_reconnect"), nil) page.leaf = true - page = entry({"admin", "network", "iface_shutdown"}, post("iface_shutdown"), nil) - page.leaf = true - page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10) page.leaf = true page.subindex = true @@ -276,34 +270,6 @@ function iface_reconnect(iface) luci.http.status(404, "No such interface") end -function iface_shutdown(iface) - local netmd = require "luci.model.network".init() - local net = netmd:get_network(iface) - if net then - luci.sys.call("env -i /sbin/ifdown %s >/dev/null 2>/dev/null" - % luci.util.shellquote(iface)) - luci.http.status(200, "Shutdown") - return - end - - luci.http.status(404, "No such interface") -end - -function iface_delete(iface) - local netmd = require "luci.model.network".init() - local net = netmd:del_network(iface) - if net then - luci.sys.call("env -i /sbin/ifdown %s >/dev/null 2>/dev/null" - % luci.util.shellquote(iface)) - luci.http.redirect(luci.dispatcher.build_url("admin/network/network")) - netmd:commit("network") - netmd:commit("wireless") - return - end - - luci.http.status(404, "No such interface") -end - function wifi_status(devs) local s = require "luci.tools.status" local rv = { } diff --git a/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/network.lua b/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/network.lua index 2bfe974af..08494644b 100644 --- a/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/network.lua +++ b/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/network.lua @@ -3,11 +3,143 @@ -- Licensed to the public under the Apache License 2.0. local fs = require "nixio.fs" +local tpl = require "luci.template" +local ntm = require "luci.model.network".init() +local fwm = require "luci.model.firewall".init() local json = require "luci.jsonc" m = Map("network", translate("Interfaces")) +m:chain("wireless") +m:chain("firewall") +m:chain("dhcp") m.pageaction = false -m:section(SimpleSection).template = "admin_network/iface_overview" + + +local tpl_networks = tpl.Template(nil, [[ + <div class="cbi-section-node"> + <div class="table"> + <% + for i, net in ipairs(netlist) do + local z = net[3] + local c = z and z:get_color() or "#EEEEEE" + local t = z and translate("Part of zone %q" % z:name()) or translate("No zone assigned") + local disabled = (net[4]:get("auto") == "0") + local dynamic = net[4]:is_dynamic() + %> + <div class="tr cbi-rowstyle-<%=i % 2 + 1%>"> + <div class="td col-3 center middle"> + <div class="ifacebox"> + <div class="ifacebox-head" style="background-color:<%=c%>" title="<%=pcdata(t)%>"> + <strong><%=net[1]:upper()%></strong> + </div> + <div class="ifacebox-body" id="<%=net[1]%>-ifc-devices" data-network="<%=net[1]%>"> + <img src="<%=resource%>/icons/ethernet_disabled.png" style="width:16px; height:16px" /><br /> + <small>?</small> + </div> + </div> + </div> + <div class="td col-5 left" id="<%=net[1]%>-ifc-description"> + <em><%:Collecting data...%></em> + </div> + <div class="td cbi-section-actions"> + <input type="button" class="cbi-button cbi-button-neutral" onclick="iface_reconnect('<%=net[1]%>')" title="<%:Reconnect this interface%>" value="<%:Restart%>"<%=ifattr(disabled or dynamic, "disabled", "disabled")%> /> + + <% if disabled then %> + <input type="hidden" name="cbid.network.<%=net[1]%>.__disable__" value="1" /> + <input type="submit" name="cbi.apply" class="cbi-button cbi-button-neutral" onclick="this.previousElementSibling.value='0'" title="<%:Reconnect this interface%>" value="<%:Connect%>"<%=ifattr(dynamic, "disabled", "disabled")%> /> + <% else %> + <input type="hidden" name="cbid.network.<%=net[1]%>.__disable__" value="0" /> + <input type="submit" name="cbi.apply" class="cbi-button cbi-button-neutral" onclick="this.previousElementSibling.value='1'" title="<%:Shutdown this interface%>" value="<%:Stop%>"<%=ifattr(dynamic, "disabled", "disabled")%> /> + <% end %> + + <input type="button" class="cbi-button cbi-button-action important" onclick="location.href='<%=url("admin/network/network", net[1])%>'" title="<%:Edit this interface%>" value="<%:Edit%>" id="<%=net[1]%>-ifc-edit"<%=ifattr(dynamic, "disabled", "disabled")%> /> + + <input type="hidden" name="cbid.network.<%=net[1]%>.__delete__" value="" /> + <input type="submit" name="cbi.apply" class="cbi-button cbi-button-negative" onclick="iface_delete(event)" value="<%:Delete%>"<%=ifattr(dynamic, "disabled", "disabled")%> /> + </div> + </div> + <% end %> + </div> + </div> + + <input type="button" class="cbi-button cbi-button-add" value="<%:Add new interface...%>" onclick="location.href='<%=url("admin/network/iface_add")%>'" /> +]]) + +local _, net +local ifaces, netlist = { }, { } + +for _, net in ipairs(ntm:get_networks()) do + if net:name() ~= "loopback" then + local zn = net:zonename() + local z = zn and fwm:get_zone(zn) or fwm:get_zone_by_network(net:name()) + + local w = 1 + if net:is_alias() then + w = 2 + elseif net:is_dynamic() then + w = 3 + end + + ifaces[#ifaces+1] = net:name() + netlist[#netlist+1] = { + net:name(), z and z:name() or "-", z, net, w + } + end +end + +table.sort(netlist, + function(a, b) + if a[2] ~= b[2] then + return a[2] < b[2] + elseif a[5] ~= b[5] then + return a[5] < b[5] + else + return a[1] < b[1] + end + end) + +s = m:section(TypedSection, "interface", translate("Interface Overview")) + +function s.sections(self) + local _, net, sl = nil, nil, { } + + for _, net in ipairs(netlist) do + sl[#sl+1] = net[1] + end + + return sl +end + +function s.render(self) + tpl_networks:render({ + netlist = netlist + }) +end + +o = s:option(Value, "__disable__") + +function o.cfgvalue(self, sid) + return (m:get(sid, "auto") == "0") and "1" or "0" +end + +function o.write(self, sid, value) + if value ~= "1" then + m:set(sid, "auto", "") + else + m:set(sid, "auto", "0") + end +end + +o.remove = o.write + +o = s:option(Value, "__delete__") + +function o.write(self, sid, value) + ntm:del_network(sid) +end + + +m:section(SimpleSection).template = "admin_network/iface_overview_status" if fs.access("/etc/init.d/dsl_control") then local ok, boarddata = pcall(json.parse, fs.readfile("/etc/board.json")) diff --git a/modules/luci-mod-admin-full/luasrc/view/admin_network/iface_overview.htm b/modules/luci-mod-admin-full/luasrc/view/admin_network/iface_overview.htm deleted file mode 100644 index 00ef8944c..000000000 --- a/modules/luci-mod-admin-full/luasrc/view/admin_network/iface_overview.htm +++ /dev/null @@ -1,250 +0,0 @@ -<%# - Copyright 2010 Jo-Philipp Wich <jow@openwrt.org> - Licensed to the public under the Apache License 2.0. --%> - -<%- - local ntm = require "luci.model.network".init() - local fwm = require "luci.model.firewall".init() - - local net - local ifaces = { } - local netlist = { } - for _, net in ipairs(ntm:get_networks()) do - if net:name() ~= "loopback" then - local z = fwm:get_zone_by_network(net:name()) - ifaces[#ifaces+1] = net:name() - netlist[#netlist+1] = { - net:name(), z and z:name() or "-", z - } - end - end - - table.sort(netlist, - function(a, b) - if a[2] ~= b[2] then - return a[2] < b[2] - else - return a[1] < b[1] - end - end) --%> - -<script type="text/javascript">//<![CDATA[ - function iface_shutdown(id, reconnect) { - if (!reconnect && !confirm(<%=luci.http.write_json(translate('Really shutdown interface "%s"? You might lose access to this device if you are connected via this interface.'))%>.format(id))) - return; - - var d = document.getElementById(id + '-ifc-description'); - if (d) - d.innerHTML = reconnect - ? '<em><%:Interface is reconnecting...%></em>' - : '<em><%:Interface is shutting down...%></em>'; - - var s = document.getElementById('ifc-rc-status'); - if (s) - { - s.parentNode.style.display = 'block'; - s.innerHTML = '<%:Waiting for changes to be applied...%>'; - } - - (new XHR()).post('<%=url('admin/network')%>/iface_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, { token: '<%=token%>' }, - function(x) - { - if (s) - { - s.innerHTML = reconnect - ? '<%:Interface reconnected%>' - : '<%:Interface shut down%>'; - - window.setTimeout(function() { - s.parentNode.style.display = 'none'; - }, 1000); - } - } - ); - } - - function iface_delete(id) { - if (!confirm(<%=luci.http.write_json(translate('Really delete this interface? The deletion cannot be undone! You might lose access to this device if you are connected via this interface'))%>)) - return; - - (new XHR()).post('<%=url('admin/network/iface_delete')%>/' + id, { token: '<%=token%>' }, - function(x) { - location.href = '<%=url('admin/network/network')%>'; - } - ); - } - - var iwxhr = new XHR(); - var wifidevs = <%=luci.http.write_json(netdevs)%>; - var arptable = <%=luci.http.write_json(arpcache)%>; - - XHR.poll(5, '<%=url('admin/network/iface_status', table.concat(ifaces, ","))%>', null, - function(x, ifcs) - { - if (ifcs) - { - for (var idx = 0; idx < ifcs.length; idx++) - { - var ifc = ifcs[idx]; - var html = ''; - - var s = document.getElementById(ifc.id + '-ifc-devices'); - if (s) - { - var stat = String.format( - '<img src="<%=resource%>/icons/%s%s.png" style="width:16px; height:16px; vertical-align:middle" />', - ifc.type, - ifc.is_up ? '' : '_disabled' - ); - - if (ifc.subdevices && ifc.subdevices.length) - { - stat += ' <strong>('; - - for (var j = 0; j < ifc.subdevices.length; j++) - { - var sif = ifc.subdevices[j]; - - stat += String.format( - '<img src="<%=resource%>/icons/%s%s.png" style="width:16px; height:16px; vertical-align:middle" title="%h" />', - sif.type, - sif.is_up ? '' : '_disabled', - sif.name - ); - } - - stat += ')</strong>'; - } - - stat += String.format( - '<br /><small>%h</small>', - ifc.name - ); - - s.innerHTML = stat; - } - - var d = document.getElementById(ifc.id + '-ifc-description'); - if (d && ifc.proto && ifc.ifname) - { - if (ifc.is_up) - { - html += String.format('<strong><%:Uptime%>:</strong> %t<br />', ifc.uptime); - } - - if (ifc.macaddr) - { - html += String.format('<strong><%:MAC-Address%>:</strong> %s<br />', ifc.macaddr); - } - - html += String.format( - '<strong><%:RX%>:</strong> %.2mB (%d <%:Pkts.%>)<br />' + - '<strong><%:TX%>:</strong> %.2mB (%d <%:Pkts.%>)<br />', - ifc.rx_bytes, ifc.rx_packets, - ifc.tx_bytes, ifc.tx_packets - ); - - if (ifc.ipaddrs && ifc.ipaddrs.length) - { - for (var i = 0; i < ifc.ipaddrs.length; i++) - html += String.format( - '<strong><%:IPv4%>:</strong> %s<br />', - ifc.ipaddrs[i] - ); - } - - if (ifc.ip6addrs && ifc.ip6addrs.length) - { - for (var i = 0; i < ifc.ip6addrs.length; i++) - html += String.format( - '<strong><%:IPv6%>:</strong> %s<br />', - ifc.ip6addrs[i] - ); - } - - if (ifc.ip6prefix) - { - html += String.format('<strong><%:IPv6-PD%>:</strong> %s<br />', ifc.ip6prefix); - } - - d.innerHTML = html; - } - else if (d && !ifc.proto) - { - var e = document.getElementById(ifc.id + '-ifc-edit'); - if (e) - e.disabled = true; - - d.innerHTML = String.format( - '<em><%:Unsupported protocol type.%></em><br />' + - '<a href="%h"><%:Install protocol extensions...%></a>', - '<%=url("admin/system/packages")%>?query=luci-proto&display=available' - ); - } - else if (d && !ifc.ifname) - { - d.innerHTML = String.format( - '<em><%:Network without interfaces.%></em><br />' + - '<a href="<%=url("admin/network/network/%s")%>?tab.network.%s=physical"><%:Assign interfaces...%></a>', - ifc.name, ifc.name - ); - } - else if (d) - { - d.innerHTML = '<em><%:Interface not present or not connected yet.%></em>'; - } - } - } - } - ); -//]]></script> - -<fieldset class="cbi-section" style="display:none"> - <legend><%:Reconnecting interface%></legend> - <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" /> - <span id="ifc-rc-status"><%:Waiting for changes to be applied...%></span> -</fieldset> - -<div class="cbi-map"> - <div class="cbi-section"> - <legend><%:Interface Overview%></legend> - - <div class="cbi-section-node"> - <div class="table"> - <% - for i, net in ipairs(netlist) do - local z = net[3] - local c = z and z:get_color() or "#EEEEEE" - local t = z and translate("Part of zone %q" % z:name()) or translate("No zone assigned") - %> - <div class="tr cbi-rowstyle-<%=i % 2 + 1%>"> - <div class="td col-3 center middle"> - <div class="ifacebox"> - <div class="ifacebox-head" style="background-color:<%=c%>" title="<%=pcdata(t)%>"> - <strong><%=net[1]:upper()%></strong> - </div> - <div class="ifacebox-body" id="<%=net[1]%>-ifc-devices"> - <img src="<%=resource%>/icons/ethernet_disabled.png" style="width:16px; height:16px" /><br /> - <small>?</small> - </div> - </div> - </div> - <div class="td col-5 left" id="<%=net[1]%>-ifc-description"> - <em><%:Collecting data...%></em> - </div> - <div class="td cbi-section-actions"> - <input type="button" class="cbi-button cbi-button-neutral" onclick="iface_shutdown('<%=net[1]%>', true)" title="<%:Reconnect this interface%>" value="<%:Connect%>" /> - <input type="button" class="cbi-button cbi-button-neutral" onclick="iface_shutdown('<%=net[1]%>', false)" title="<%:Shutdown this interface%>" value="<%:Stop%>" /> - <input type="button" class="cbi-button cbi-button-action important" onclick="location.href='<%=url("admin/network/network", net[1])%>'" title="<%:Edit this interface%>" value="<%:Edit%>" id="<%=net[1]%>-ifc-edit" /> - <input type="button" class="cbi-button cbi-button-negative" onclick="iface_delete('<%=net[1]%>')" value="<%:Delete%>" /> - </div> - </div> - <% end %> - </div> - </div> - - <input type="button" class="cbi-button cbi-button-add" value="<%:Add new interface...%>" onclick="location.href='<%=url("admin/network/iface_add")%>'" /> - </div> -</div> diff --git a/modules/luci-mod-admin-full/luasrc/view/admin_network/iface_overview_status.htm b/modules/luci-mod-admin-full/luasrc/view/admin_network/iface_overview_status.htm new file mode 100644 index 000000000..f56b3e0ad --- /dev/null +++ b/modules/luci-mod-admin-full/luasrc/view/admin_network/iface_overview_status.htm @@ -0,0 +1,182 @@ +<%# + Copyright 2010-2018 Jo-Philipp Wich <jo@mein.io> + Licensed to the public under the Apache License 2.0. +-%> + +<script type="text/javascript">//<![CDATA[ + function iface_reconnect(id) { + XHR.halt(); + + var d = document.getElementById(id + '-ifc-description'); + if (d) d.innerHTML = '<em><%:Interface is reconnecting...%></em>'; + + (new XHR()).post('<%=url('admin/network/iface_reconnect')%>/' + id, + { token: '<%=token%>' }, XHR.run); + } + + function iface_delete(ev) { + if (!confirm(<%=luci.http.write_json(translate('Really delete this interface? The deletion cannot be undone! You might lose access to this device if you are connected via this interface'))%>)) { + ev.preventDefault(); + return false; + } + + ev.target.previousElementSibling.value = '1'; + return true; + } + + var networks = []; + + document.querySelectorAll('[data-network]').forEach(function(n) { + networks.push(n.getAttribute('data-network')); + }); + + XHR.poll(5, '<%=url('admin/network/iface_status')%>/' + networks.join(','), null, + function(x, ifcs) + { + if (ifcs) + { + var primary_devices = { }; + + for (var idx = 0; idx < ifcs.length; idx++) + { + var ifc = ifcs[idx]; + + if (!ifc.is_alias && !ifc.is_dynamic) + primary_devices[ifc.name] = ifc; + } + + for (var idx = 0; idx < ifcs.length; idx++) + { + var ifc = ifcs[idx]; + var html = ''; + + var s = document.getElementById(ifc.id + '-ifc-devices'); + if (s) + { + var stat = String.format( + '<img src="<%=resource%>/icons/%s%s.png" style="width:16px; height:16px; vertical-align:middle" />', + (ifc.is_dynamic || ifc.is_alias) ? 'alias' : ifc.type, + ifc.is_up ? '' : '_disabled' + ); + + if (ifc.subdevices && ifc.subdevices.length) + { + stat += ' <strong>('; + + for (var j = 0; j < ifc.subdevices.length; j++) + { + var sif = ifc.subdevices[j]; + + stat += String.format( + '<img src="<%=resource%>/icons/%s%s.png" style="width:16px; height:16px; vertical-align:middle" title="%h" />', + sif.type, + sif.is_up ? '' : '_disabled', + sif.name + ); + } + + stat += ')</strong>'; + } + + stat += String.format( + '<br /><small>%h</small>', + ifc.name + ); + + s.innerHTML = stat; + } + + var d = document.getElementById(ifc.id + '-ifc-description'); + if (d && ifc.proto && ifc.ifname) + { + var desc = null; + + if (ifc.is_dynamic) + desc = '<%:Virtual dynamic interface%>'; + else if (ifc.is_alias) + desc = '<%:Alias interface%>'; + + if (ifc.desc) + desc = desc ? '%s (%s)'.format(desc, ifc.desc) : ifc.desc; + + html += String.format('<strong><%:Protocol%>:</strong> %h<br />', desc || '?'); + + if (ifc.is_up) + { + html += String.format('<strong><%:Uptime%>:</strong> %t<br />', ifc.uptime); + } + + if (!primary_devices[ifc.name] || primary_devices[ifc.name] === ifc) + { + if (ifc.macaddr) + html += String.format('<strong><%:MAC-Address%>:</strong> %s<br />', ifc.macaddr); + + html += String.format( + '<strong><%:RX%>:</strong> %.2mB (%d <%:Pkts.%>)<br />' + + '<strong><%:TX%>:</strong> %.2mB (%d <%:Pkts.%>)<br />', + ifc.rx_bytes, ifc.rx_packets, + ifc.tx_bytes, ifc.tx_packets + ); + } + + if (ifc.ipaddrs && ifc.ipaddrs.length) + { + for (var i = 0; i < ifc.ipaddrs.length; i++) + html += String.format( + '<strong><%:IPv4%>:</strong> %s<br />', + ifc.ipaddrs[i] + ); + } + + if (ifc.ip6addrs && ifc.ip6addrs.length) + { + for (var i = 0; i < ifc.ip6addrs.length; i++) + html += String.format( + '<strong><%:IPv6%>:</strong> %s<br />', + ifc.ip6addrs[i] + ); + } + + if (ifc.ip6prefix) + html += String.format('<strong><%:IPv6-PD%>:</strong> %s<br />', ifc.ip6prefix); + + if (ifc.errors) + { + for (var i = 0; i < ifc.errors.length; i++) + html += String.format( + '<em class="error"><strong><%:Error%>:</strong> %h</em><br />', + ifc.errors[i] + ); + } + + d.innerHTML = html; + } + else if (d && !ifc.proto) + { + var e = document.getElementById(ifc.id + '-ifc-edit'); + if (e) + e.disabled = true; + + d.innerHTML = String.format( + '<em><%:Unsupported protocol type.%></em><br />' + + '<a href="%h"><%:Install protocol extensions...%></a>', + '<%=url("admin/system/packages")%>?query=luci-proto&display=available' + ); + } + else if (d && !ifc.ifname) + { + d.innerHTML = String.format( + '<em><%:Network without interfaces.%></em><br />' + + '<a href="<%=url("admin/network/network/%s")%>?tab.network.%s=physical"><%:Assign interfaces...%></a>', + ifc.name, ifc.name + ); + } + else if (d) + { + d.innerHTML = '<em><%:Interface not present or not connected yet.%></em>'; + } + } + } + } + ); +//]]></script> |