Browse Source

ffho-site-genrate: introduce new strategy for config generation

Karsten Böddeker 7 years ago
parent
commit
5183706fd8

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

@@ -1,7 +1,7 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=ffho-site-generate
-PKG_VERSION:=1
+PKG_VERSION:=2
 PKG_RELEASE:=$(GLUON_VERSION).$(GLUON_SITE_CODE)-$(GLUON_RELEASE).$(GLUON_CONFIG_VERSION)
 
 PFG_BUILD_DEPENDS := lua-cjson/host
@@ -36,7 +36,7 @@ endef
 define Package/ffho-site-generate/install
 	$(CP) ./files/* $(1)/
 	$(CP) $(PKG_BUILD_DIR)/luadest/* $(1)/
-	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/template.lua"))))' > $(1)/lib/gluon/site-select/template.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

+ 26 - 45
ffho/ffho-site-generate/ReadMe.md

@@ -3,8 +3,11 @@ ffho-site-generate
 
 This package generates the needed `site.json` directly on the node after firmware
 upgrad has been performed. This can be used, to support different sites/regions
-within one firmware image. The default `site.json` is partially overridden by the
-variables defined in `site/extra/sites.conf` and `site/extra/groups.conf`.
+within one firmware image. The default `site.json` will be replaced by
+`site/extra/template.conf` which is patly replacte by values defined in
+`site/extra/sites.conf` and `site/extra/groups.conf`.
+
+One can generate the `site.conf` before build with: `GLUON_SITEDIR=$PATH ./scripts/gen-site-conf.lua`
 
 This does not belong to the `site.mk`.
 
@@ -36,15 +39,9 @@ can be done here.
   {
     site_name = 'Freifunk Hochstift - Altenbeken',
     site_code = 'ffho_abn',
-    wifi24 = {
-      ap = {
-        ssid = 'hochstift.freifunk.net/altenbkn',
-      },
-    },
-    wifi5 = {
-      ap = {
-        ssid = 'hochstift.freifunk.net/altenbkn',
-      },
+    subst = {
+      ['%%ID'] = 1,
+      ['%%CD'] = 'abn',
     },
     site_select = {
       group = 'ffho_ost',
@@ -64,43 +61,27 @@ site.conf. Same configuration as in the site.conf can be done here.
 ```
 {
   ffho_ost = {
-    prefix4 = '10.132.xxx.0/21',
-    prefix6 = '2a03:2260:2342:2303::/64',
-    wifi24 = {
-      ibss = {
-        ssid = 'ffho-mesh-ost',
-        bssid = '02:ff:03:ff:00:24',
-      },
-    },
-    wifi5 = {
-      ibss = {
-        ssid = 'ffho-mesh-ost',
-        bssid = '02:ff:03:ff:00:05',
-      },
-    },
-    next_node = {
-      ip4 = '10.132.xxx.1',
-      ip6 = '2a03:2260:2342:2303::1',
-      mac = '02:ff:03:ff:00:00',
-    },
-    fastd_mesh_vpn = {
-      groups = {
-        backbone = {
-          peers = {
-            gw01 = {
-              key = 'GW01_KEY',
-              remotes = {
-                '"gw01.ffho.net" port 10003',
-                ...
-              },
-            },
-            ...
-          },
-        },
-      },
+    subst = {
+      ['%%V4'] = '10.132.xxx.0/21',
+      ['%%V6'] = '2a03:2260:2342:2303::/64',
+      ...
     },
   },
   ...
 }
 ```
 
+site/extra/default.conf
+-----------------------
+
+An array, containing the default configuration, to create site.conf out of template.conf before build.
+
+### example
+```
+subst = {
+  ['%%SN'] = 'Bitte wählen',
+  ['%%SC'] = 'ffho',
+  ['%%SS'] = 'paderborn.freifunk.net',
+  ...
+}
+```

+ 16 - 18
ffho/ffho-site-generate/luasrc/lib/gluon/upgrade/005-set-site-config

@@ -5,20 +5,9 @@ local json =  require 'luci.json'
 local site_code = require('gluon.site_config').site_code
 local tools = require 'gluon.site_generate'
 
-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
-  end
-  return table
-end
-
-local default = tools.get_config('/lib/gluon/site-select/default.json')
+local template = io.open('/lib/gluon/site-select/template.json', 'r')
+local config = template:read("*a")
+default:close()
 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")
@@ -27,14 +16,23 @@ if site_code ~= currentsite then
   local configured = false
   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])
+      if site.subst then
+        config = tools.replace_patterns(config, site.subst)
+      end
+      if (site.site_select or {}).group and groups and groups[site.site_select.group] then
+        group = groups[site.site_select.group]
+        if group.subst then
+          config = tools.replace_patterns(config, group.subst)
+        end
       end
-      default = add_var_to_table(default, site)
+      local subst = {}
+      subst['%%SN'] = site.site_name
+      subst['%%SC'] = site.site_code
+      config = tools.replace_patterns(config, subst)
 
       local file = '/lib/gluon/site.json'
       local f = io.open(file, "w")
-      f:write(json.encode(default))
+      f:write(config)
       f:close()
 
       configured = true

+ 6 - 0
ffho/ffho-site-generate/luasrc/usr/lib/lua/gluon/site_generate.lua

@@ -52,3 +52,9 @@ function set_site_code(site_code)
   return false
 end
 
+function replace_patterns(value, subst)
+  for k, v in pairs(subst) do
+    value = value:gsub(k, v)
+  end
+  return value
+end

+ 17 - 0
ffho/ffho-site-generate/scripts/gen-site-conf.lua

@@ -0,0 +1,17 @@
+#!/usr/bin/lua
+
+function replace_patterns(value, subst)
+  for k, v in pairs(subst) do
+    value = value:gsub(k, v)
+  end
+  return value
+end
+
+dofile(os.getenv('GLUON_SITEDIR') ..'/extra/default.conf')
+local template = os.getenv('GLUON_SITEDIR') ..'/extra/template.conf'
+local site = os.getenv('GLUON_SITEDIR') ..'/site.conf'
+
+local config = io.open(template):read('*a')
+config = replace_patterns(config, subst)
+
+io.open(site, 'w'):write(config)

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

@@ -0,0 +1,9 @@
+local config = os.getenv('GLUON_SITEDIR') .. '/extra/template.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), 'template.conf')), {})()