Browse Source

ffho-site-generate: Backport from master

Karsten Böddeker 8 years ago
parent
commit
e7d6de8b38

+ 6 - 2
ffho/ffho-site-generate/Makefile

@@ -4,6 +4,8 @@ PKG_NAME:=ffho-site-generate
 PKG_VERSION:=1
 PKG_RELEASE:=$(GLUON_VERSION).$(GLUON_SITE_CODE)-$(GLUON_RELEASE).$(GLUON_CONFIG_VERSION)
 
+PFG_BUILD_DEPENDS := lua-cjson/host
+
 PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
 
 include $(INCLUDE_DIR)/package.mk
@@ -12,7 +14,7 @@ define Package/ffho-site-generate
   SECTION:=ffho
   CATEGORY:=Gluon
   TITLE:=Scripts for generating a new site.conf
-  DEPENDS:=+gluon-core
+  DEPENDS:=+gluon-core +luci-lib-json
   MAINTAINER:=Freifunk Hochstift <maschinenraum@paderborn.freifunk.net>
   URL:=https://git.c3pb.de/freifunk-pb/ffho-packages
   SOURCE:=git@git.c3pb.de:freifunk-pb/ffho-packages.git
@@ -36,7 +38,9 @@ endef
 
 define Package/ffho-site-generate/install
 	$(CP) ./files/* $(1)/
-	$(CP) $(GLUON_SITEDIR)/extra/* $(1)/lib/gluon/site-select/
+	lua -e 'print(require("cjson").encode(assert(dofile("./scripts/site_config.lua"))))' > $(1)/lib/gluon/site-select/default.json
+	lua -e 'print(require("cjson").encode(assert(dofile("./scripts/sites.lua"))))' >  $(1)/lib/gluon/site-select/sites.json
+	if [ -e $(GLUON_SITEDIR)/extra/groups.conf  ]; then lua -e 'print(require("cjson").encode(assert(dofile("./scripts/groupes.lua"))))' >  $(1)/lib/gluon/site-select/groups.json; fi
 endef
 
 $(eval $(call BuildPackage,ffho-site-generate))

+ 26 - 50
ffho/ffho-site-generate/files/lib/gluon/upgrade/005-set-site-config

@@ -1,63 +1,41 @@
 #!/usr/bin/lua
 
 local uci = require('luci.model.uci').cursor()
-local site = require 'gluon.site_config'
-local config = require 'gluon.sites'
+local json =  require 'luci.json'
+local site_code = require('gluon.site_config').site_code
+local tools = require 'gluon.site_generate'
+local util = require 'luci.util'
 
-function serialize (f,o,d)
-  if type(o) == "number" then
-    f:write(o)
-  elseif type(o) == "string" then
-    f:write(string.format("%q", o))
-  elseif type(o) == "boolean" then
-    f:write(o and 1 or 0)
-  elseif type(o) == "table" then
-    f:write("{\n")
-    for k,v in pairs(o) do
-      f:write(string.rep ("  ", d+1))
-      if type(k) == "string" then
-        f:write(k, " = ")
+function add_var_to_table(table, var)
+  if type(var) == "table" and type(table) == "table" then
+    for name, value in pairs(var) do
+      if table[name] and type(value) == "table" then
+        table[name] = add_var_to_table(table[name], value)
+      else
+        table[name]=value
       end
-      serialize(f,v,d+1)
-      f:write(",\n")
     end
-    f:write(string.rep ("  ", d), "}")
-  else
-    f:write("ERROR: cannot serialize a " .. type(o))
   end
+  return table
 end
 
+local default = tools.get_config('/lib/gluon/site-select/default.json')
+local groups = tools.get_config('/lib/gluon/site-select/groups.json')
+local sites = tools.get_config('/lib/gluon/site-select/sites.json')
 local currentsite = uci:get("currentsite", "current", "name")
 
-if site.site_code ~= currentsite then
+if site_code ~= currentsite then
   local configured = false
-  for index, tmp in pairs(config) do
-    if tmp.site_code == currentsite then
-      local new = {}
-  
-      new.hostname_prefix = site.hostname_prefix
-      new.site_name = tmp.site_name
-      new.site_code = tmp.site_code
-      new.prefix4 = site.prefix4
-      new.prefix6 = site.prefix6
-      new.additional_prefix6 = site.additional_prefix6
-      new.timezone = site.timezone
-      new.ntp_servers = site.ntp_servers
-      new.opkg_repo = site.opkg_repo
-      new.regdom = site.regdom
-      new.wifi24 = site.wifi24
-      new.wifi5 = site.wifi5
-      new.next_node = site.next_node
-      new.fastd_mesh_vpn = site.fastd_mesh_vpn
-      new.autoupdater = site.autoupdater
-      new.simple_tc = site.simple_tc
-      new.debugserver = site.debugserver
-      new.batman_on_wan = site.batman_on_wan
+  for _, site in pairs(sites) do
+    if site.site_code == currentsite then
+      if site.site_select and site.site_select.group and groups and groups[site.site_select.group] then
+        default = add_var_to_table(default, groups[site.site_select.group])
+      end
+      default = add_var_to_table(default, site)
 
-      file = '/lib/gluon/site.conf'
-      f = io.open(file, "w")
-      serialize(f,new,0)
-      f:write('\n')
+      local file = '/lib/gluon/site.conf'
+      local f = io.open(file, "w")
+      f:write(util.serialize_data(default))
       f:close()
 
       configured = true
@@ -66,8 +44,6 @@ if site.site_code ~= currentsite then
   end
 
   if configured == false then
-    uci:set("currentsite", "current", "name", site.site_code)
-    uci:save('currentsite')
-    uci:commit('currentsite')
+    tools.force_site_code(site_code)
   end
 end

+ 54 - 0
ffho/ffho-site-generate/files/usr/lib/lua/gluon/site_generate.lua

@@ -0,0 +1,54 @@
+#!/usr/bin/lua
+
+local tool = {}
+local uci = require('luci.model.uci').cursor()
+local json =  require 'luci.json'
+local sites_json = '/lib/gluon/site-select/sites.json'
+
+module('gluon.site_generate', package.seeall)
+
+function get_config(file)
+  local f = io.open(file)
+  if f then
+    local config = json.decode(f:read('*a'))
+    f:close()
+    return config
+  end
+  return nil
+end
+
+function get_list()
+  local list = {}
+  local sites = get_config(sites_json)
+  for index, site in pairs(sites) do
+    list[site.site_code]=index
+  end
+  return list
+end
+
+local site_list=get_list()
+
+function validate_site(site_code)
+  return site_list[site_code]
+end
+
+function force_site_code(site_code)
+  if site_code then
+    uci:set('currentsite', 'current', 'name', site_code)
+    uci:save('currentsite')
+    uci:commit('currentsite')
+    return true
+  end
+  return false
+end
+
+function set_site_code(site_code)
+  if site_code and validate_site(site_code) then
+    uci:set('currentsite', 'current', 'name', site_code)
+    uci:save('currentsite')
+    uci:commit('currentsite')
+    return true
+  end
+  return false
+end
+

+ 9 - 0
ffho/ffho-site-generate/scripts/groups.lua

@@ -0,0 +1,9 @@
+local config = os.getenv('GLUON_SITEDIR') .. '/extra/groups.conf'
+
+local function loader()
+   coroutine.yield('return ')
+   coroutine.yield(io.open(config):read('*a'))
+end
+
+-- setfenv doesn't work with Lua 5.2 anymore, but we're using 5.1
+return setfenv(assert(load(coroutine.wrap(loader), 'groups.conf')), {})()

+ 9 - 0
ffho/ffho-site-generate/scripts/site_config.lua

@@ -0,0 +1,9 @@
+local config = os.getenv('GLUON_SITE_CONFIG')
+
+local function loader()
+   coroutine.yield('return ')
+   coroutine.yield(io.open(config):read('*a'))
+end
+
+-- setfenv doesn't work with Lua 5.2 anymore, but we're using 5.1
+return setfenv(assert(load(coroutine.wrap(loader), 'site.conf')), {})()

+ 9 - 0
ffho/ffho-site-generate/scripts/sites.lua

@@ -0,0 +1,9 @@
+local config = os.getenv('GLUON_SITEDIR') .. '/extra/sites.conf'
+
+local function loader()
+   coroutine.yield('return ')
+   coroutine.yield(io.open(config):read('*a'))
+end
+
+-- setfenv doesn't work with Lua 5.2 anymore, but we're using 5.1
+return setfenv(assert(load(coroutine.wrap(loader), 'sites.conf')), {})()