Developing lightweight computation at the DSG edge

Commit a594f149 authored by p4u's avatar p4u
Browse files

Added new bmx6 config section for luci web interface

parent 90ea85bd
config 'bmx6' 'luci'
option ignore '0'
option place 'admin status Bmx6'
#option place 'admin status Bmx6'
option place 'qmp bmx6'
#option json 'http://127.0.0.1/cgi-bin/bmx6-info?'
option json 'exec:/www/cgi-bin/bmx6-info -s'
local bmx6json = require("luci.model.bmx6json")
module("luci.controller.bmx6", package.seeall)
function index()
-- ToDo: Put this code in a function
local ucim = require "luci.model.uci"
local uci = ucim.cursor()
local ucim = require "luci.model.uci"
local uci = ucim.cursor()
local place = {}
-- checking if ignore is on
if uci:get("luci-bmx6","luci","ignore") == "1" then
return nil
end
-- getting value from uci database
local uci_place = uci:get("luci-bmx6","luci","place")
-- default values
if uci_place == nil then
-- checking if ignore is on
if uci:get("luci-bmx6","luci","ignore") == "1" then
return nil
end
-- getting value from uci database
local uci_place = uci:get("luci-bmx6","luci","place")
-- default values
if uci_place == nil then
place = {"bmx6"}
else
local util = require "luci.util"
place = util.split(uci_place," ")
end
---------------------------
-- Starting with the pages
---------------------------
--- status (this is default one)
---------------------------
-- Starting with the pages
---------------------------
--- status (this is default one)
entry(place,call("action_status"),place[#place])
--- interfaces
--- interfaces
table.insert(place,"Interfaces")
entry(place,call("action_interfaces"),"Interfaces")
table.remove(place)
--- neighbours
--- neighbours
table.insert(place,"Neighbours")
entry(place,call("action_neighbours"),"Neighbours")
table.remove(place)
--- wireless links
--- wireless links
table.insert(place,"Wireless")
entry(place,call("action_wireless"),"Wireless")
table.remove(place)
--- configuration (CBI)
table.insert(place,"Configuration")
entry(place, cbi("bmx6/main"), "Configuration").dependent=false
table.insert(place,"Advanced")
entry(place, cbi("bmx6/advanced"), "Advanced")
table.remove(place)
table.insert(place,"Interfaces")
entry(place, cbi("bmx6/interfaces"), "Interfaces")
table.remove(place)
table.insert(place,"Plugins")
entry(place, cbi("bmx6/plugins"), "Plugins")
table.remove(place)
table.insert(place,"HNA")
entry(place, cbi("bmx6/hna"), "HNA")
table.remove(place)
table.remove(place)
end
function action_status()
local data = get_bmx_data("status")
local data = bmx6json.get("status")
if data == nil then return nil end
luci.template.render("bmx6/status", {data=data.status})
end
function action_interfaces()
local data = get_bmx_data("interfaces")
local data = bmx6json.get("interfaces")
if data == nil then return nil end
luci.template.render("bmx6/interfaces", {data=data.interfaces})
end
function action_neighbours()
local data = get_bmx_data("neighbours")
local data = bmx6json.get("neighbours")
if data == nil then return nil end
luci.template.render("bmx6/neighbours", {data=data.neighbours})
end
function action_wireless()
local data = get_bmx_data("wireless")
local data = bmx6json.get("wireless")
if data == nil then return nil end
luci.template.render("bmx6/wireless", {data=data.interfaces})
end
----------------
-- Private
----------------
-- Returns a LUA object from bmx6 JSON daemon
function get_bmx_data(field)
require("luci.ltn12")
require("luci.json")
require("luci.util")
local uci = require "luci.model.uci"
local url = uci.cursor():get("luci-bmx6","luci","json")
if url == nil then
print_error("bmx6 json url not configured, cannot fetch bmx6 daemon data")
return nil
end
local json_url = luci.util.split(url,":")
local raw = ""
if json_url[1] == "http" then
raw = luci.sys.httpget(url..field)
else
if json_url[1] == "exec" then
raw = luci.sys.exec(json_url[2]..' '..field)
else
print_error("bmx6 json url not recognized, cannot fetch bmx6 daemon data. Use http: or exec:")
return nil
end
end
local data = nil
if raw:len() > 10 then
local decoder = luci.json.Decoder()
luci.ltn12.pump.all(luci.ltn12.source.string(raw), decoder:sink())
data = decoder:get()
else
print_error("Cannot get data from bmx6 daemon")
return nil
end
return data
end
function print_error(txt)
luci.util.perror(txt)
luci.template.render("bmx6/error", {txt=txt})
end
m = Map("bmx6", "bmx6")
local bmx6json = require("luci.model.bmx6json")
local util = require("luci.util")
local http = require("luci.http")
local sys = require("luci.sys")
local options = bmx6json.get("options")
if options == nil or options.OPTIONS == nil then
m.message = "bmx6-json plugin is not running or some mistake in luci-bmx6 configuration, check /etc/config/luci-bmx6"
options = {}
else
options = options.OPTIONS
end
local general = m:section(NamedSection,"general","general","General Options")
local name = ""
local help = ""
local value = nil
for _,o in ipairs(options) do
if o.name ~= nil and o.CHILD_OPTIONS == nil then
help = ""
name = o.name
if o.help ~= nil then
help = bmx6json.text2html(o.help)
end
if o.syntax ~= nil then
help = help .. "<br/><strong>Syntax: </strong>" .. bmx6json.text2html(o.syntax)
end
if o.def ~= nil then
help = help .. "<strong> Default: </strong>" .. o.def
end
value = general:option(Value,name,name,help)
end
end
function m.on_commit(self,map)
local err = sys.call('bmx6 -c --configReload > /tmp/bmx6-luci.err.tmp')
if err ~= 0 then
m.message = sys.exec("cat /tmp/bmx6-luci.err.tmp")
end
end
return m
local sys = require("luci.sys")
m = Map("bmx6", "bmx6")
local hna = m:section(TypedSection,"hna","HNA")
hna.addremove = true
hna.anonymous = false
local hna_option = hna:option(Value,"hna", "Host Network Announcement")
function hna_option:validate(value)
local err = sys.call('bmx6 -c --test -a ' .. value)
if err ~= 0 then
return nil
end
return value
end
return m
local sys = require("luci.sys")
local bmx6json = require("luci.model.bmx6json")
local m = Map("bmx6", "bmx6")
local eth_int = sys.net.devices()
local interfaces = m:section(TypedSection,"dev","Devices","")
interfaces.addremove = true
interfaces.anonymous = true
local intlv = interfaces:option(ListValue,"dev","Device")
for _,i in ipairs(eth_int) do
intlv:value(i,i)
end
-- Getting json and looking for device section
local json = bmx6json.get("options")
if json == nil or json.OPTIONS == nil then
m.message = "bmx6-json plugin is not running or some mistake in luci-bmx6 configuration, check /etc/config/luci-bmx6"
json = {}
else
json = json.OPTIONS
end
local dev = {}
for _,j in ipairs(json) do
if j.name == "dev" and j.CHILD_OPTIONS ~= nil then
dev = j.CHILD_OPTIONS
break
end
end
local help = ""
local name = ""
for _,o in ipairs(dev) do
if o.name ~= nil then
help = ""
name = o.name
if o.help ~= nil then
help = bmx6json.text2html(o.help)
end
if o.syntax ~= nil then
help = help .. "<br/><strong>Syntax: </strong>" .. bmx6json.text2html(o.syntax)
end
value = interfaces:option(Value,name,name,help)
value.optional = true
end
end
return m
local sys = require("luci.sys")
local bmx6json = require("luci.model.bmx6json")
m = Map("bmx6", "bmx6")
-- Getting json and Checking if bmx6-json is avaiable
local options = bmx6json.get("options")
if options == nil or options.OPTIONS == nil then
m.message = "bmx6-json plugin is not running or some mistake in luci-bmx6 configuration, check /etc/config/luci-bmx6"
else
options = options.OPTIONS
end
-- Getting a list of interfaces
local eth_int = luci.sys.net.devices()
-- Getting the most important options from general
local general = m:section(NamedSection,"general","general","General")
general.addremove = false
general:option(Value,"globalPrefix","Global ip prefix","Specify global prefix for interfaces: NETADDR/LENGTH")
if m:get("ipVersion","ipVersion") == "6" then
general:option(Value,"niitSource","Ipv4 niit source","Specify niit4to6 source IP address (IP MUST be assigned to niit4to6 interface!)")
end
-- IP section
-- ipVersion section is important, we are allways showing it
local ipV = m:section(NamedSection,"ipVersion","ipVersion","IP options")
ipV.addremove = false
local lipv = ipV:option(ListValue,"ipVersion","IP version")
lipv:value("4","4")
lipv:value("6","6")
lipv.default = "6"
-- rest of ip options are optional, getting them from json
local ipoptions = {}
for _,o in ipairs(options) do
if o.name == "ipVersion" and o.CHILD_OPTIONS ~= nil then
ipoptions = o.CHILD_OPTIONS
break
end
end
local help = ""
local name = ""
local value = nil
for _,o in ipairs(ipoptions) do
if o.name ~= nil then
help = ""
name = o.name
if o.help ~= nil then
help = bmx6json.text2html(o.help)
end
if o.syntax ~= nil then
help = help .. "<br/><strong>Syntax: </strong>" .. bmx6json.text2html(o.syntax)
end
if o.def ~= nil then
help = help .. "<br/><strong> Default: </strong>" .. bmx6json.text2html(o.def)
end
value = ipV:option(Value,name,name,help)
value.optional = true
end
end
-- Interfaces section
local interfaces = m:section(TypedSection,"dev","Devices","")
interfaces.addremove = true
interfaces.anonymous = false
local intlv = interfaces:option(ListValue,"dev","Device")
for _,i in ipairs(eth_int) do
intlv:value(i,i)
end
function m.on_commit(self,map)
local err = sys.call('bmx6 -c --configReload > /tmp/bmx6-luci.err.tmp')
if err ~= 0 then
m.message = sys.exec("cat /tmp/bmx6-luci.err.tmp")
end
end
return m
m = Map("bmx6", "bmx6")
plugins_dir = {"/usr/lib/","/var/lib","/lib"}
plugin = m:section(TypedSection,"plugin","Plugin")
plugin.addremove = true
plugin.anonymous = false
plv = plugin:option(ListValue,"plugin", "Plugin")
for _,d in ipairs(plugins_dir) do
pl = luci.sys.exec("cd "..d..";ls bmx6_*")
if #pl > 6 then
for _,v in ipairs(luci.util.split(pl,"\n")) do
plv:value(v,v)
end
end
end
return m
local ltn12 = require("luci.ltn12")
local json = require("luci.json")
local util = require("luci.util")
local uci = require("luci.model.uci")
local sys = require("luci.sys")
local template = require("luci.template")
local http = require("luci.http")
local string = require "string"
module "luci.model.bmx6json"
-- Returns a LUA object from bmx6 JSON daemon
function get(field)
local url = uci.cursor():get("luci-bmx6","luci","json")
if url == nil then
print_error("bmx6 json url not configured, cannot fetch bmx6 daemon data",true)
return nil
end
local json_url = util.split(url,":")
local raw = ""
if json_url[1] == "http" then
raw = sys.httpget(url..field)
else
if json_url[1] == "exec" then
raw = sys.exec(json_url[2]..' '..field)
else
print_error("bmx6 json url not recognized, cannot fetch bmx6 daemon data. Use http: or exec:",true)
return nil
end
end
local data = nil
if raw:len() > 10 then
local decoder = json.Decoder()
ltn12.pump.all(ltn12.source.string(raw), decoder:sink())
data = decoder:get()
else
print_error("Cannot get data from bmx6 daemon",true)
return nil
end
return data
end
function print_error(txt,popup)
util.perror(txt)
sys.call("logger -t bmx6json " .. txt)
if popup then
http.write('<script type="text/javascript">alert("Some error detected, please check it: '..txt..'");</script>')
else
http.write("<h1>Dammit! some error detected</h1>")
http.write("bmx6-luci: " .. txt)
http.write('<p><FORM><INPUT TYPE="BUTTON" VALUE="Go Back" ONCLICK="history.go(-1)"></FORM></p>')
end
end
function text2html(txt)
txt = string.gsub(txt,"<","{")
txt = string.gsub(txt,">","}")
txt = util.striptags(txt)
return txt
end
......@@ -9,7 +9,7 @@
# Or from web interfae (with cgi enabled): http://host/cgi-bin/bmx6-info?links
# If you ask for a directory you wil get the directory contents in JSON forman
BMX6_DIR="/var/run/bmx6/json"
BMX6_DIR="$(uci get bmx6.general.runtimeDir 2>/dev/null)" || BMX6_DIR="/var/run/bmx6/json"
#Checking if shell mode or cgi-bin mode
if [ "$1" == "-s" ]; then
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment