diff --git a/docs/package/gluon-config-mode-site-select.rst b/docs/package/gluon-config-mode-site-select.rst new file mode 100644 index 00000000..fdac4545 --- /dev/null +++ b/docs/package/gluon-config-mode-site-select.rst @@ -0,0 +1,25 @@ +gluon-config-mode-site-select +============================= + +This Package provides a drop-down list for the config mode, to select the site/region +the node will be placed in. If the selection has changed the upgrade scripts in +`/lib/gluon/upgrade/` are triggered. + +extra/sites.conf +---------------- + +site_select.hidden \: optional + - `0`, show this site in drop-down list (default) + - `1`, hide this site within config mode + +Example:: + +{ + { + site_select: = { + hidden = 1, + }, + ... + }, + ... +}, diff --git a/docs/package/gluon-site-generate.rst b/docs/package/gluon-site-generate.rst new file mode 100644 index 00000000..805045c5 --- /dev/null +++ b/docs/package/gluon-site-generate.rst @@ -0,0 +1,75 @@ +gluon-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` will be replaced by +`extra/template.conf` which is patly replacte by values defined in +`extra/sites.conf` and `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`. + +extra/sites.conf +---------------- + +Array of possible sites, containing site specific configuration different to the +original site.conf and group specific config. Same configuration as in the site.conf +can be done here. + +site_select.group \: optional + specify a group out of `extra/groups.conf` the site belongs to + +Example:: + +{ + { + site_name = 'Freifunk Alpha Centauri - North', + site_code = 'ffxx_north', + subst = { + ['%%ID'] = 1, + ['%%CD'] = 'north', + }, + site_select = { + group = 'ffxx_23', + }, + }, + ... +} + + +extra/groups.conf +----------------- + +Array of groups, containing group specific configuration different to the original +site.conf. Same configuration as in the site.conf can be done here. + +Example:: + +{ + ffac_23 = { + subst = { + ['%%V4'] = '10.xxx.0.0/21', + ['%%V6'] = 'fdxx:xxxx:xxxx::/64', + ... + }, + }, + ... +} + + +extra/default.conf +------------------ + +An array, containing the default configuration, to create site.conf out of template.conf before build. + +Example:: + +subst = { + ['%%SN'] = 'Freifunk Alpha Centauri - Legacy', + ['%%SC'] = 'ffxx', + ['%%SS'] = 'a-centauri.freifunk.net/legacy', + ... +} + diff --git a/package/gluon-config-mode-site-select/Makefile b/package/gluon-config-mode-site-select/Makefile new file mode 100644 index 00000000..246147b2 --- /dev/null +++ b/package/gluon-config-mode-site-select/Makefile @@ -0,0 +1,37 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=gluon-config-mode-site-select +PKG_VERSION:=0.1 +PKG_RELEASE:=1 + +PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) + +include $(GLUONDIR)/include/package.mk + +PKG_CONFIG_DEPENDS += $(GLUON_I18N_CONFIG) + +define Package/gluon-config-mode-site-select + SECTION:=gluon + CATEGORY:=GLUON + TITLE:=UI for changing the node-config + DEPENDS:=+gluon-config-mode-core-virtual +gluon-site-generate +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) +endef + +define Build/Configure +endef + +define Build/Compile + $(call GluonBuildI18N,gluon-config-mode-site-select,i18n) + $(call GluonSrcDiet,./luasrc,$(PKG_BUILD_DIR)/luadest/) +endef + +define Package/gluon-config-mode-site-select/install + $(CP) $(PKG_BUILD_DIR)/luadest/* $(1)/ + $(call GluonInstallI18N,gluon-config-mode-site-select,$(1)) +endef + +$(eval $(call BuildPackage,gluon-config-mode-site-select)) diff --git a/package/gluon-config-mode-site-select/i18n/de.po b/package/gluon-config-mode-site-select/i18n/de.po new file mode 100644 index 00000000..d00b921c --- /dev/null +++ b/package/gluon-config-mode-site-select/i18n/de.po @@ -0,0 +1,14 @@ +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"PO-Revision-Date: 2015-11-02 13:15+0100\n" +"Last-Translator: Freifunk Hochstift \n" +"Language-Team: German\n" +"Language: de\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +msgid "Region" +msgstr "Region" diff --git a/package/gluon-config-mode-site-select/i18n/ffho-config-mode-site-select.pot b/package/gluon-config-mode-site-select/i18n/ffho-config-mode-site-select.pot new file mode 100644 index 00000000..c526e939 --- /dev/null +++ b/package/gluon-config-mode-site-select/i18n/ffho-config-mode-site-select.pot @@ -0,0 +1,5 @@ +msgid "" +msgstr "Content-Type: text/plain; charset=UTF-8" + +msgid "Region" +msgstr "" diff --git a/package/gluon-config-mode-site-select/luasrc/lib/gluon/config-mode/wizard/0200-site-select.lua b/package/gluon-config-mode-site-select/luasrc/lib/gluon/config-mode/wizard/0200-site-select.lua new file mode 100644 index 00000000..e99bb991 --- /dev/null +++ b/package/gluon-config-mode-site-select/luasrc/lib/gluon/config-mode/wizard/0200-site-select.lua @@ -0,0 +1,42 @@ +local cbi = require "luci.cbi" +local i18n = require "luci.i18n" +local uci = require('luci.model.uci').cursor() +local default = require 'gluon.site_config' +local tools = require 'gluon.site_generate' + +local M = {} + +function M.section(form) + local sites = tools.get_config('/lib/gluon/site-select/sites.json') + + local msg = i18n.translate('gluon-config-mode:site-select') + local s = form:section(cbi.SimpleSection, nil, msg) + + local o = s:option(cbi.ListValue, "community", i18n.translate("Region")) + o.rmempty = false + o.optional = false + + if uci:get_first("gluon-setup-mode", "setup_mode", "configured") == "0" then + o:value("") + else + o:value(default.site_code, default.site_name) + end + + for _, site in pairs(sites) do + if site.site_select == nil or site.site_select.hidden ~= 1 then + o:value(site.site_code, site.site_name) + end + end +end + +function M.handle(data) + if data.community ~= uci:get('currentsite', 'current', 'name') then + tools.set_site_code(data.community) + end + + if data.community ~= default.site_code then + os.execute('sh "/lib/gluon/site-select/site-upgrade"') + end +end + +return M diff --git a/package/gluon-site-generate/Makefile b/package/gluon-site-generate/Makefile new file mode 100644 index 00000000..9dedf6f5 --- /dev/null +++ b/package/gluon-site-generate/Makefile @@ -0,0 +1,42 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=gluon-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 $(GLUONDIR)/include/package.mk + +define Package/gluon-site-generate + SECTION:=gluon + CATEGORY:=GLUON + TITLE:=Scripts for generating a new site.json + DEPENDS:=+gluon-core +luci-lib-json +endef + +define Package/gluon-site-generate/description +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) +endef + +define Build/Configure +endef + +define Build/Compile + $(call GluonSrcDiet,./luasrc,$(PKG_BUILD_DIR)/luadest/) +endef + +define Package/gluon-site-generate/install + $(CP) ./files/* $(1)/ + $(CP) $(PKG_BUILD_DIR)/luadest/* $(1)/ + 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/groups.lua"))))' > $(1)/lib/gluon/site-select/groups.json; fi +endef + +$(eval $(call BuildPackage,gluon-site-generate)) diff --git a/package/gluon-site-generate/files/etc/config/currentsite b/package/gluon-site-generate/files/etc/config/currentsite new file mode 100644 index 00000000..eb832440 --- /dev/null +++ b/package/gluon-site-generate/files/etc/config/currentsite @@ -0,0 +1,2 @@ +config site 'current' + option name 'default' diff --git a/package/gluon-site-generate/files/lib/gluon/site-select/site-upgrade b/package/gluon-site-generate/files/lib/gluon/site-select/site-upgrade new file mode 100755 index 00000000..1b6684a8 --- /dev/null +++ b/package/gluon-site-generate/files/lib/gluon/site-select/site-upgrade @@ -0,0 +1,6 @@ +#!/bin/sh +#this script is the same as zzz-gluon-upgrade + +for script in /lib/gluon/upgrade/*; do + "$script" +done diff --git a/package/gluon-site-generate/luasrc/lib/gluon/upgrade/005-set-site-config b/package/gluon-site-generate/luasrc/lib/gluon/upgrade/005-set-site-config new file mode 100755 index 00000000..a99e5808 --- /dev/null +++ b/package/gluon-site-generate/luasrc/lib/gluon/upgrade/005-set-site-config @@ -0,0 +1,48 @@ +#!/usr/bin/lua + +local uci = require('luci.model.uci').cursor() +local json = require 'luci.json' +local site_code = require('gluon.site_config').site_code +local tools = require 'gluon.site_generate' + +local config = io.open('/lib/gluon/site-select/template.json'):read('*a') +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_code ~= currentsite then + local configured = false + for _, site in pairs(sites) do + if site.site_code == currentsite then + local subst = {} + subst['%%SN'] = site.site_name + subst['%%SC'] = site.site_code + + 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] + subst['%%GN'] = site.site_select.group + if group.subst then + config = tools.replace_patterns(config, group.subst) + end + end + + config = tools.replace_patterns(config, subst) + + local file = '/lib/gluon/site.json' + local f = io.open(file, "w") + f:write(config) + f:close() + + configured = true + break + end + end + + if configured == false then + tools.force_site_code(site_code) + end +end diff --git a/package/gluon-site-generate/luasrc/usr/lib/lua/gluon/site_generate.lua b/package/gluon-site-generate/luasrc/usr/lib/lua/gluon/site_generate.lua new file mode 100644 index 00000000..4ccf38c5 --- /dev/null +++ b/package/gluon-site-generate/luasrc/usr/lib/lua/gluon/site_generate.lua @@ -0,0 +1,60 @@ +#!/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 + +function replace_patterns(value, subst) + for k, v in pairs(subst) do + value = value:gsub(k, v) + end + return value +end diff --git a/package/gluon-site-generate/scripts/gen-site-conf.lua b/package/gluon-site-generate/scripts/gen-site-conf.lua new file mode 100755 index 00000000..2da2549b --- /dev/null +++ b/package/gluon-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) diff --git a/package/gluon-site-generate/scripts/groups.lua b/package/gluon-site-generate/scripts/groups.lua new file mode 100644 index 00000000..eaadb6aa --- /dev/null +++ b/package/gluon-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')), {})() diff --git a/package/gluon-site-generate/scripts/sites.lua b/package/gluon-site-generate/scripts/sites.lua new file mode 100644 index 00000000..07c5d45e --- /dev/null +++ b/package/gluon-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')), {})() diff --git a/package/gluon-site-generate/scripts/template.lua b/package/gluon-site-generate/scripts/template.lua new file mode 100644 index 00000000..a7ed2888 --- /dev/null +++ b/package/gluon-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')), {})()