Browse Source

Improves ffho-config-mode-site-select

Create the site.conf during Runtime
Needs $gluondir/site/extra/sites.conf
Karsten Böddeker 8 years ago
parent
commit
e100abf02b

+ 6 - 3
ffpb/ffho-config-mode-site-select/README.md

@@ -1,15 +1,18 @@
 ffho-config-mode-site-select
 ======================
 
-This Repository contains a Gluon package to choose between different site.conf files after flashing the image. <br>
+This Repository contains a Gluon package to choose between different sites after flashing the image. <br>
 This does not apply to the site.mk!
 
 This is a modified Version of gluon-config-mode-site-select
 
 Usage
 -----
-* Rename your site-files to site_code.conf (E.g. ffki.conf, ffhh.conf)
-* copy your renamed site-files to $gluondir/site/extra/
+Create a file called sites.conf in $gluondir/site/extra/.
+This file should include a lua-table with the following informations about the different sites
+* site_name
+* site_code
+* ssid
 
 <br>
 Das ganze sieht dann so aus: <br>

+ 0 - 14
ffpb/ffho-config-mode-site-select/create-config.sh

@@ -1,19 +1,5 @@
 #!/bin/sh
 
-echo "" > "$DIR"/etc/config/siteselect
-
-dir -1 "$GLUON_SITEDIR"/extra/ | while read FILE
-do
-site_name="$(cat "$GLUON_SITEDIR"/extra/"$FILE" | grep "site_name" | sed "s/site_name =//; s/,//")"
-site_code="$(cat "$GLUON_SITEDIR"/extra/"$FILE" | grep "site_code" | sed "s/site_code =//; s/,//")"
-
-echo "config site"$site_code"" >> "$DIR"/etc/config/siteselect
-echo "    option path '/lib/gluon/site-select/"$FILE"'" >> "$DIR"/etc/config/siteselect
-echo "    option sitename"$site_name"" >> "$DIR"/etc/config/siteselect
-echo "" >> "$DIR"/etc/config/siteselect
-
-done
-
 current_site="$(cat "$GLUON_SITEDIR"/site.conf | grep "site_code" | sed "s/site_code =//; s/,//")"
 
 echo "config site 'current'" >> "$DIR"/etc/config/currentsite

+ 6 - 16
ffpb/ffho-config-mode-site-select/files/lib/gluon/config-mode/wizard/0200-site-select.lua

@@ -3,23 +3,16 @@ local i18n = require "luci.i18n"
 local uci = luci.model.uci.cursor()
 local site = require 'gluon.site_config'
 local fs = require "nixio.fs"
+local config = require 'gluon.sites'
 
-local sites = {}
 local M = {}
 
 function M.section(form)
-	
 	local msg = i18n.translate('Here you have the possibility of selecting the region in which ' ..
                                'your node is placed. Please keep in mind that your router ' ..
                                'connects only with the mesh of the selected region')
 	local s = form:section(cbi.SimpleSection, nil, msg)
-	
-	uci:foreach('siteselect', 'site',
-	function(s)
-		table.insert(sites, s['.name'])
-	end
-	)
-	
+
 	local o = s:option(cbi.ListValue, "community", i18n.translate("Region"))
 	o.rmempty = false
 	o.optional = false
@@ -30,20 +23,17 @@ function M.section(form)
 		o:value(site.site_code, site.site_name)
 	end
 
-	for index, site in ipairs(sites) do
-		o:value(site, uci:get('siteselect', site, 'sitename'))
+	for index, tmp in pairs(config) do
+		o:value(tmp.site_code, tmp.site_name))
 	end
-
 end
 
 function M.handle(data)
-
-	if data.community ~= site.site_code then
-		
-		fs.copy(uci:get('siteselect', data.community , 'path'), '/lib/gluon/site.conf')
+	if data.community ~= uci:get('currentsite', 'current', 'name') then
 		uci:set('currentsite', 'current', 'name', data.community)
 		uci:save('currentsite')
 		uci:commit('currentsite')		
+
 		os.execute('sh "/lib/gluon/site-upgrade"')
 	end
 end

+ 53 - 2
ffpb/ffho-config-mode-site-select/files/lib/gluon/upgrade/002-set-site-config

@@ -4,12 +4,35 @@ local uci = require 'luci.model.uci'
 local site = require 'gluon.site_config'
 local fs = require "nixio.fs"
 local c = uci.cursor()
+local config = require 'gluon.sites'
 
 local currentsite = c:get("currentsite", "current", "name")
 
 local file = "/etc/sysupgrade.conf"
 local configured = false
 
+function serialize (o)
+  if type(o) == "number" then
+    io.write(o)
+  elseif type(o) == "string" then
+    io.write(string.format("%q", o))
+  elseif type(o) == "boolean" then
+    io.write(o and 1 or 0)
+  elseif type(o) == "table" then
+    io.write("{\n")
+    for k,v in pairs(o) do
+      if type(k) == "string" then
+        io.write("  ", k, " = ")
+      end
+      serialize(v)
+      io.write(",\n")
+    end
+    io.write("}\n")
+  else
+    error("cannot serialize a " .. type(o))
+  end
+end
+
 for line in io.lines(file) do
   if line == "/etc/config/currentsite" then
     configured = true
@@ -22,6 +45,34 @@ if configured == false then
   f:close()
 end
 
-if site.site_name ~= c:get('siteselect', currentsite, 'sitename') then
-  fs.copy(c:get('siteselect', currentsite , 'path'), '/lib/gluon/site.conf')
+if site.site_code ~= currentsite then
+  local new = {}
+  
+  new.hostname_prefix = site.hostname_prefix
+  new.site_name = config[currentsite].site_name
+  new.site_code = config[currentsite].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.wifi24.ssid = config[currentsite].ssid
+  new.wifi5.ssid = config[currentsite].ssid
+  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
+
+  file = '/lib/gluon/site.conf'
+  io.output(file)
+  f = io.open(file, 'w')
+  serialize(new)
+  f:write('\n')
+  f:close()
 end

+ 13 - 0
ffpb/ffho-config-mode-site-select/files/usr/lib/lua/gluon/sites.lua

@@ -0,0 +1,13 @@
+local config = '/lib/gluon/site-select/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
+local sites = setfenv(assert(load(coroutine.wrap(loader), 'sites.conf')), {})()
+
+module 'gluon.sites'
+
+return sites