Browse Source

ffho-site-generate: rewrite to store data as .json
In actual gluon master the site.conf is converted to site.json during build, so the created site.conf has to be stored as site.json. Additionally the conversion during build is done for the associated config files, too.
Actually the following config files are used by this programm:
- $(GLUON_SITEDIR)/site.conf -> /lib/gluon/site-select/default.json
- the default site-config
- is used as basis for new generated sites
- $(GLUON_SITEDIR)/extra/sites.conf -> /lib/gluon/site-select/sites.json
- contains the individual site-config for each site
- these changes are added to the default site
- $(GLUON_SITEDIR)/extra/groups.conf -> /lib/gluon/site-select/groups.json (if file exists)
- with this file you can define some individual config for a group of sites
- changes are applied to default site, before sites.json
- set site_select.group='group_x' in sites.conf for each site, which should use a group called group_x

Karsten Böddeker 8 years ago
parent
commit
8cd4542171

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

@@ -4,6 +4,8 @@ PKG_NAME:=ffho-site-generate
 PKG_VERSION:=1
 PKG_VERSION:=1
 PKG_RELEASE:=$(GLUON_VERSION).$(GLUON_SITE_CODE)-$(GLUON_RELEASE).$(GLUON_CONFIG_VERSION)
 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)
 PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
 
 
 include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/package.mk
@@ -11,7 +13,7 @@ include $(INCLUDE_DIR)/package.mk
 define Package/ffho-site-generate
 define Package/ffho-site-generate
   SECTION:=ffho
   SECTION:=ffho
   CATEGORY:=Gluon
   CATEGORY:=Gluon
-  TITLE:=Scripts for generating a new site.conf
+  TITLE:=Scripts for generating a new site.json
   DEPENDS:=+gluon-core
   DEPENDS:=+gluon-core
   MAINTAINER:=Freifunk Hochstift <maschinenraum@paderborn.freifunk.net>
   MAINTAINER:=Freifunk Hochstift <maschinenraum@paderborn.freifunk.net>
   URL:=https://git.c3pb.de/freifunk-pb/ffho-packages
   URL:=https://git.c3pb.de/freifunk-pb/ffho-packages
@@ -36,7 +38,11 @@ endef
 
 
 define Package/ffho-site-generate/install
 define Package/ffho-site-generate/install
 	$(CP) ./files/* $(1)/
 	$(CP) ./files/* $(1)/
-	$(CP) $(GLUON_SITEDIR)/extra/* $(1)/lib/gluon/site-select/
+	lua -e 'print(require("cjson").encode(assert(dofile("$(GLUONDIR)/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
 endef
 
 
 $(eval $(call BuildPackage,ffho-site-generate))
 $(eval $(call BuildPackage,ffho-site-generate))

+ 23 - 46
ffho/ffho-site-generate/files/lib/gluon/upgrade/005-set-site-config

@@ -1,62 +1,39 @@
 #!/usr/bin/lua
 #!/usr/bin/lua
 
 
 local uci = require('luci.model.uci').cursor()
 local uci = require('luci.model.uci').cursor()
-local site = require 'gluon.site_config'
-local config = require 'gluon.sites'
-
-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, " = ")
+local json =  require 'luci.jsonc'
+local site_code = require('gluon.site_config').site_code
+local default = json.decode(io.open('/lib/gluon/site-select/default.json'):read('*a'))
+local groups = json.decode(io.open('/lib/gluon/site-select/groups.json'):read('*a'))
+local sites = json.decode(io.open('/lib/gluon/site-select/sites.json'):read('*a'))
+
+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
       end
-      serialize(f,v,d+1)
-      f:write(",\n")
     end
     end
-    f:write(string.rep ("  ", d), "}")
-  else
-    f:write("ERROR: cannot serialize a " .. type(o))
   end
   end
+  return table
 end
 end
 
 
 local currentsite = uci:get("currentsite", "current", "name")
 local currentsite = uci:get("currentsite", "current", "name")
 
 
-if site.site_code ~= currentsite then
+if site_code ~= currentsite then
   local configured = false
   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 = site.opkg
-      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.debugserver = site.debugserver
-      new.batman_on_wan = site.batman_on_wan
+  for index, site in pairs(sites) do
+    if site.site_code == currentsite then
+      if 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'
       file = '/lib/gluon/site.conf'
       f = io.open(file, "w")
       f = io.open(file, "w")
-      serialize(f,new,0)
-      f:write('\n')
+      io.write(f,json.encode(default))
       f:close()
       f:close()
 
 
       configured = true
       configured = true
@@ -65,7 +42,7 @@ if site.site_code ~= currentsite then
   end
   end
 
 
   if configured == false then
   if configured == false then
-    uci:set("currentsite", "current", "name", site.site_code)
+    uci:set("currentsite", "current", "name", site_code)
     uci:save('currentsite')
     uci:save('currentsite')
     uci:commit('currentsite')
     uci:commit('currentsite')
   end
   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/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')), {})()