12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #!/usr/bin/lua
- local util = require("luci.util")
- local json = require("luci.json")
- local config = require 'gluon.sites'
- local uci = require('luci.model.uci').cursor()
- local site = require 'gluon.site_config'
- function is_site_valid(site)
- for _, tmp in pairs(config) do
- if tmp.site_code == site then
- return true
- end
- end
- return false
- end
- function set_currentsite(site)
- if site and is_site_valid(site) then
- uci:set('currentsite', 'current', 'name', site)
- uci:save('currentsite')
- uci:commit('currentsite')
- return true
- end
- return false
- end
- function neighbours(ifname)
- local info = util.exec("gluon-neighbour-info -d ff02::2:1001 -p 1001 -r nodeinfo -t 2 -i " .. ifname)
- local macs = {}
- for _, line in ipairs(util.split(info)) do
- local data = json.decode(line)
- if data and data["network"] then
- local mac = data["network"]["mac"]
- if mac then
- macs[mac] = data
- end
- end
- end
- return macs
- end
- function get_neighbour_site()
- local interfaces = util.split(util.trim(util.exec("batctl if | grep active | grep -v mesh-vpn | cut -d':' -f1")))
- for _, ifname in ipairs(interfaces) do
- local macs = neighbours(ifname)
- for _, node in pairs(macs) do
- if node["system"] then
- local node_site = node["system"]["site_code"]
- if node_site and is_site_valid(node_site) then
- return node_site
- end
- end
- end
- end
- return nil
- end
- local currentsite = uci:get('currentsite', 'current', 'name')
- local configured = is_site_valid(currentsite)
- if configured == false then
- currentsite = get_neighbour_site()
- configured = set_currentsite(currentsite)
- end
- if configured then
- os.execute("rm -f /lib/gluon/cron/ffho-site-auto-select >/dev/null 2>&1")
- if site.site_code ~= currentsite then
- os.execute("/lib/gluon/site-select/site-upgrade >/dev/null 2>&1")
- os.execute("reboot >/dev/null 2>&1")
- end
- end
|