002-set-site-config 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/lua
  2. local uci = require 'luci.model.uci'
  3. local site = require 'gluon.site_config'
  4. local fs = require "nixio.fs"
  5. local c = uci.cursor()
  6. local config = require 'gluon.sites'
  7. local currentsite = c:get("currentsite", "current", "name")
  8. local file = "/etc/sysupgrade.conf"
  9. local configured = false
  10. function serialize (f,o)
  11. if type(o) == "number" then
  12. f:write(o)
  13. elseif type(o) == "string" then
  14. f:write(string.format("%q", o))
  15. elseif type(o) == "boolean" then
  16. f:write(o and 1 or 0)
  17. elseif type(o) == "table" then
  18. f:write("{\n")
  19. for k,v in pairs(o) do
  20. if type(k) == "string" then
  21. f:write(" ", k, " = ")
  22. end
  23. serialize(f,v)
  24. f:write(",\n")
  25. end
  26. f:write("}")
  27. else
  28. error("cannot serialize a " .. type(o))
  29. end
  30. end
  31. for line in io.lines(file) do
  32. if line == "/etc/config/currentsite" then
  33. configured = true
  34. end
  35. end
  36. if configured == false then
  37. f = io.open(file,"a")
  38. f:write("/etc/config/currentsite\n")
  39. f:close()
  40. end
  41. if site.site_code ~= currentsite then
  42. if type(config[currentsite]) == "table" then
  43. local new = {}
  44. new.hostname_prefix = site.hostname_prefix
  45. new.site_name = config[currentsite].site_name
  46. new.site_code = config[currentsite].site_code
  47. new.prefix4 = site.prefix4
  48. new.prefix6 = site.prefix6
  49. new.additional_prefix6 = site.additional_prefix6
  50. new.timezone = site.timezone
  51. new.ntp_servers = site.ntp_servers
  52. new.opkg_repo = site.opkg_repo
  53. new.regdom = site.regdom
  54. new.wifi24 = site.wifi24
  55. new.wifi5 = site.wifi5
  56. new.wifi24.ssid = config[currentsite].ssid
  57. new.wifi5.ssid = config[currentsite].ssid
  58. new.next_node = site.next_node
  59. new.fastd_mesh_vpn = site.fastd_mesh_vpn
  60. new.autoupdater = site.autoupdater
  61. new.simple_tc = site.simple_tc
  62. new.debugserver = site.debugserver
  63. new.batman_on_wan = site.batman_on_wan
  64. file = '/lib/gluon/site.conf'
  65. f = io.open(file, "w")
  66. serialize(f,new)
  67. f:write('\n')
  68. f:close()
  69. else
  70. c:set("currentsite", "current", "name", site.site_code)
  71. c:save('currentsite')
  72. c:commit('currentsite')
  73. end
  74. end