Explorar o código

ffho-site-auto-select: new package
actualy ffho-site-auto-select only discovers the site of batman-neighbour. For that purpose a cron-job ist added during upgrade.

Karsten Böddeker %!s(int64=8) %!d(string=hai) anos
pai
achega
a122d618b1

+ 42 - 0
ffho/ffho-site-auto-select/Makefile

@@ -0,0 +1,42 @@
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=ffho-site-auto-select
+PKG_VERSION:=1
+PKG_RELEASE:=$(GLUON_VERSION).$(GLUON_SITE_CODE)-$(GLUON_RELEASE).$(GLUON_CONFIG_VERSION)
+
+PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/ffho-site-auto-select
+  SECTION:=ffho
+  CATEGORY:=Gluon
+  TITLE:=Toolset to autodetect the site.
+  DEPENDS:=+gluon-core +micrond +gluon-neighbour-info +ffho-site-generate
+  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
+endef
+
+define Package/ffho-site-auto-select/description
+  Toolset to autodetect the site by geo and gluon-neighbour-info.
+endef
+
+define Build/Prepare
+	mkdir -p $(PKG_BUILD_DIR)
+endef
+
+define Build/Configure
+endef
+
+define Build/Compile
+endef
+
+define Package/ffho-site-auto-select/preinst
+endef
+
+define Package/ffho-site-auto-select/install
+  $(CP) ./files/* $(1)/
+endef
+
+$(eval $(call BuildPackage,ffho-site-auto-select))

+ 58 - 0
ffho/ffho-site-auto-select/files/lib/gluon/upgrade/003-site-auto-select

@@ -0,0 +1,58 @@
+#!/usr/bin/lua
+
+local uci = require('luci.model.uci').cursor()
+local json = require 'luci.json'
+
+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 is_site_valid(site_code)
+  local sites = get_config('/lib/gluon/site-select/sites.json')
+  for _, site in pairs(sites) do
+    if site.site_code == site_code then
+      return true
+    end
+  end
+  return false
+end
+
+function set_currentsite(site_code)
+  if site_code and is_site_valid(site_code) then
+    uci:set('currentsite', 'current', 'name', site_code)
+    uci:save('currentsite')
+    uci:commit('currentsite')
+    return true
+  end
+  return false
+end
+
+function get_site_by_geo(latitude, longitude)
+  return nil
+end
+
+local currentsite = uci:get('currentsite', 'current', 'name')
+local configured = is_site_valid(currentsite)
+
+if configured == false then
+  local latitude = uci:get_first('gluon-node-info', 'location', 'latitude')
+  local longitude = uci:get_first('gluon-node-info', 'location', 'longitude')
+  if latitude and longitude then
+    currentsite = get_site_by_geo(latitude, longitude)
+    configured = set_currentsite(currentsite)
+  end
+end
+
+if configured == false then
+  local minute = math.random(0, 59)
+  local f = io.open('/usr/lib/micron.d/ffho-site-auto-select', 'w')
+  f:write(string.format('%i * * * * /usr/sbin/ffho-site-auto-select\n', minute))
+  f:close()
+end
+

+ 58 - 0
ffho/ffho-site-auto-select/files/usr/sbin/ffho-site-auto-select

@@ -0,0 +1,58 @@
+#!/usr/bin/lua
+
+local util = require("luci.util")
+local json = require("luci.json")
+local config = require 'gluon.sites'
+local uci = require('luci.model.uci').cursor()
+local site_code = require('gluon.site_config').side_code
+local tools = require 'gluon.site_generate'
+
+function neighbours(ifname)
+  local info = util.exec("gluon-neighbour-info -d ff02::2:1001 -p 1001 -r nodeinfo -t 2 -i " .. ifname)
+  local macs = {}
+  for _, line in ipairs(util.split(info)) do
+    local data = json.decode(line)
+    if data and data["network"] then
+      local mac = data["network"]["mac"]
+      if mac then
+        macs[mac] = data
+      end
+    end
+  end
+
+  return macs
+end
+
+function get_neighbour_site()
+  local interfaces = util.split(util.trim(util.exec("batctl if | grep active | grep -v mesh-vpn | cut -d':' -f1")))
+  for _, ifname in ipairs(interfaces) do
+    local macs = neighbours(ifname)
+    for _, node in pairs(macs) do
+      if node["system"] then
+        local node_site = node["system"]["site_code"]
+        if node_site and tools.validate_site(node_site) then
+          return node_site
+        end
+      end
+    end
+  end
+
+  return nil
+end
+
+local currentsite = uci:get('currentsite', 'current', 'name')
+local configured = tools.validate_site(currentsite)
+
+if configured == false then
+  currentsite = get_neighbour_site()
+  configured = tools.set_site_code(currentsite)
+end
+
+if configured then
+  os.remove("/usr/lib/micron.d/ffho-site-auto-select")
+  if site_code ~= currentsite then
+    os.execute("/lib/gluon/site-select/site-upgrade >/dev/null 2>&1")
+    os.execute("reboot >/dev/null 2>&1")
+  end
+end
+