ffho-site-auto-select 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/lua
  2. local util = require("luci.util")
  3. local json = require("luci.json")
  4. local uci = require('luci.model.uci').cursor()
  5. local site_code = require('gluon.site_config').side_code
  6. local tools = require 'gluon.site_generate'
  7. function neighbours(ifname)
  8. local info = util.exec("gluon-neighbour-info -d ff02::2:1001 -p 1001 -r nodeinfo -t 2 -i " .. ifname)
  9. local macs = {}
  10. for _, line in ipairs(util.split(info)) do
  11. local data = json.decode(line)
  12. if data and data["network"] then
  13. local mac = data["network"]["mac"]
  14. if mac then
  15. macs[mac] = data
  16. end
  17. end
  18. end
  19. return macs
  20. end
  21. function get_neighbour_site()
  22. local interfaces = util.split(util.trim(util.exec("batctl if | grep active | grep -v mesh-vpn | cut -d':' -f1")))
  23. for _, ifname in ipairs(interfaces) do
  24. local macs = neighbours(ifname)
  25. for _, node in pairs(macs) do
  26. if node["system"] then
  27. local node_site = node["system"]["site_code"]
  28. if node_site and tools.validate_site(node_site) then
  29. return node_site
  30. end
  31. end
  32. end
  33. end
  34. return nil
  35. end
  36. local currentsite = uci:get('currentsite', 'current', 'name')
  37. local configured = tools.validate_site(currentsite)
  38. if configured == false then
  39. currentsite = get_neighbour_site()
  40. configured = tools.set_site_code(currentsite)
  41. end
  42. if configured then
  43. os.remove("/lib/gluon/cron/ffho-site-auto-select")
  44. if site_code ~= currentsite then
  45. os.execute("/lib/gluon/site-select/site-upgrade >/dev/null 2>&1")
  46. os.execute("reboot >/dev/null 2>&1")
  47. end
  48. end