ソースを参照

gluon-alfred: split announce.lua into several files and move parts into the appropriate packages

This also fixes the type of a few fields which were strings instead of numbers.
Matthias Schiffer 10 年 前
コミット
48ea4b711f

+ 1 - 1
package/gluon-alfred/Makefile

@@ -11,7 +11,7 @@ include $(INCLUDE_DIR)/package.mk
 define Package/gluon-alfred
   SECTION:=gluon
   CATEGORY:=Gluon
-  DEPENDS:=+gluon-core +gluon-node-info +gluon-cron +alfred +ethtool +luci-lib-json +luci-lib-core
+  DEPENDS:=+gluon-core +gluon-cron +alfred +ethtool +luci-lib-json +luci-lib-core
   TITLE:=Configure alfred
 endef
 

+ 1 - 0
package/gluon-alfred/files/lib/gluon/alfred/announce.d/hardware/model

@@ -0,0 +1 @@
+return require('gluon.model').get_model()

+ 1 - 0
package/gluon-alfred/files/lib/gluon/alfred/announce.d/hostname

@@ -0,0 +1 @@
+return uci:get_first('system', 'system', 'hostname')

+ 1 - 0
package/gluon-alfred/files/lib/gluon/alfred/announce.d/network/mac

@@ -0,0 +1 @@
+return require('gluon.sysconfig').primary_mac

+ 4 - 0
package/gluon-alfred/files/lib/gluon/alfred/announce.d/software/firmware

@@ -0,0 +1,4 @@
+return
+	{ base = 'gluon-' .. util.trim(fs.readfile('/lib/gluon/gluon-version'))
+	, release = util.trim(fs.readfile('/lib/gluon/release'))
+	}

+ 1 - 0
package/gluon-alfred/files/lib/gluon/alfred/announce.d/statistics/loadavg

@@ -0,0 +1 @@
+return tonumber(util.trim(util.exec("cut -d' ' -f1 /proc/loadavg")))

+ 1 - 0
package/gluon-alfred/files/lib/gluon/alfred/announce.d/statistics/uptime

@@ -0,0 +1 @@
+return tonumber(util.trim(util.exec("cut -d' ' -f1 /proc/uptime")))

+ 28 - 101
package/gluon-alfred/files/lib/gluon/alfred/announce.lua

@@ -1,117 +1,44 @@
 #!/usr/bin/lua
 
-local json = require "luci.json"
-local ltn12 = require "luci.ltn12"
-local util = require "luci.util"
+local alfred_data_type = 158
+local announce_dir = '/lib/gluon/alfred/announce.d'
 
-require "luci.model.uci"
-local uci = luci.model.uci.cursor()
 
-local alfred_data_type = tonumber(os.getenv("ALFRED_DATA_TYPE")) or 158
-local net_if = os.getenv("NET_IF") or "br-client"
+fs = require 'luci.fs'
+uci = require('luci.model.uci').cursor()
+util = require 'luci.util'
 
-function readAll(file)
-    local f = io.open(file, "rb")
-    local content = f:read("*all")
-    f:close()
-    return content
-end
-
-function chomp(s)
-  return (s:gsub("^(.-)\n?$", "%1"))
-end
-
-function trim(s)
-  return (s:gsub("^%s*(.-)%s*$", "%1"))
-end
-
-output = {}
 
-output["hostname"] = uci:get_first("system", "system", "hostname")
+local json = require 'luci.json'
+local ltn12 = require 'luci.ltn12'
 
-if uci:get_first("gluon-node-info", "location", "share_location", false) then
-  output["location"] =
-    { latitude = tonumber(uci:get_first("gluon-node-info", "location", "latitude"))
-    , longitude = tonumber(uci:get_first("gluon-node-info", "location", "longitude"))
-    }
-end
 
-local contact = uci:get_first("gluon-node-info", "owner", "contact", "")
-if contact ~= "" then
-  output["owner"] = { contact = contact }
+local function collect_entry(entry)
+	if fs.isdirectory(entry) then
+		return collect_dir(entry)
+	else
+		return dofile(entry)
+	end
 end
 
-output["software"] =
-  { firmware = { base = "gluon-" .. chomp(readAll("/lib/gluon/gluon-version"))
-               , release = chomp(readAll("/lib/gluon/release"))
-               }
-  }
+function collect_dir(dir)
+	local ret = {}
 
-local autoupdater = uci:get_all("autoupdater", "settings")
-if autoupdater then
-  output["software"]["autoupdater"] =
-    { branch = autoupdater["branch"]
-    , enabled = uci:get_bool("autoupdater", "settings", "enabled")
-    }
-end
+	for _, entry in ipairs(fs.dir(dir)) do
+		if entry:sub(1, 1) ~= '.' then
+			err, val = pcall(collect_entry, dir .. '/' .. entry)
+			if err then
+				ret[entry] = val
+			else
+				io.stderr:write(val, '\n')
+			end
+		end
+	end
 
-local fastd = uci:get_all("fastd", "mesh_vpn")
-if fastd then
-  output["software"]["fastd"] =
-    { enabled = uci:get_bool("fastd", "mesh_vpn", "enabled")
-    , version = chomp(util.exec("fastd -v | cut -d' ' -f2"))
-    }
+	return ret
 end
 
-output["hardware"] =
-  { model = chomp(util.exec(". /lib/gluon/functions/model.sh; get_model")) }
-
-
-local addresses = {}
-local tmp = util.exec("ip -o -6 addr show dev \"" .. net_if .. "\" | "
-                   .. "grep -oE 'inet6 [0-9a-fA-F:]+' | cut -d' ' -f2")
-
-for address in tmp:gmatch("[^\n]+") do
-  table.insert(addresses, address)
-end
 
-output["network"] =
-  { mac = chomp(util.exec(". /lib/gluon/functions/sysconfig.sh; sysconfig primary_mac"))
-  , addresses = addresses
-  }
-
-local gateway =
-  chomp(util.exec("batctl -m bat0 gateways | awk '/^=>/ { print $2 }'"))
-
-if gateway ~= "" then
-  output["network"]["gateway"] = gateway
-end
-
-local traffic = {}
-local ethtool = util.exec("ethtool -S bat0")
-for k, v in ethtool:gmatch("([%a_]+): ([0-9]+)") do
-  traffic[k] = v
-end
-
-for _,class in ipairs({"rx", "tx", "forward", "mgmt_rx", "mgmt_tx"}) do
-  traffic[class] =
-    { bytes = traffic[class .. "_bytes"]
-    , packets = traffic[class]
-    }
-
-  if class == "tx" then
-    traffic[class]["dropped"] = traffic[class .. "_dropped"]
-  end
-end
-
-output["statistics"] =
-  { uptime = chomp(util.exec("cut -d' ' -f1 /proc/uptime"))
-  , loadavg = chomp(util.exec("cut -d' ' -f1 /proc/loadavg"))
-  , traffic = traffic
-  }
-
-encoder = json.Encoder(output)
-alfred = io.popen("alfred -s " .. tostring(alfred_data_type), "w")
+encoder = json.Encoder(collect_dir(announce_dir))
+alfred = io.popen('alfred -s ' .. tostring(alfred_data_type), 'w')
 ltn12.pump.all(encoder:source(), ltn12.sink.file(alfred))
-
- 

+ 15 - 10
package/gluon-alfred/files/lib/gluon/upgrade/alfred/invariant/010-enable-alfred

@@ -1,14 +1,19 @@
-#!/bin/sh
+#!/usr/bin/lua
 
-. /lib/functions.sh
+local uci = require 'luci.model.uci'
+local c = uci.cursor()
 
-uci_remove alfred alfred
 
-uci_add alfred alfred alfred
-uci_set alfred alfred interface 'br-client'
-uci_set alfred alfred mode 'slave'
-uci_set alfred alfred batmanif 'bat0'
-uci_set alfred alfred start_vis '1'
-uci_set alfred alfred run_facters '0'
+c:delete('alfred', 'alfred')
+c:section('alfred', 'alfred', 'alfred',
+	  {
+		  interface = 'br-client',
+		  mode = 'slave',
+		  batmanif = 'bat0',
+		  start_vis = '1',
+		  run_facters = '0',
+	  }
+)
 
-uci_commit alfred
+c:save('alfred')
+c:commit('alfred')

+ 7 - 0
package/gluon-autoupdater/files/lib/gluon/alfred/announce.d/software/autoupdater

@@ -0,0 +1,7 @@
+local autoupdater = uci:get_all('autoupdater', 'settings')
+if autoupdater then
+	return
+		{ branch = autoupdater['branch']
+		, enabled = uci:get_bool('autoupdater', 'settings', 'enabled')
+		}
+end

+ 8 - 0
package/gluon-mesh-batman-adv/files/lib/gluon/alfred/announce.d/network/addresses

@@ -0,0 +1,8 @@
+local ip = util.exec('ip -o -6 addr show dev br-client')
+
+local addresses = {}
+for _, line in ipairs(util.split(ip)) do
+	table.insert(addresses, line:match('inet6 ([%x:]+)/'))
+end
+
+return addresses

+ 5 - 0
package/gluon-mesh-batman-adv/files/lib/gluon/alfred/announce.d/network/gateway

@@ -0,0 +1,5 @@
+local gateway = util.trim(util.exec("batctl -m bat0 gateways | awk '/^=>/ { print $2 }'"))
+
+if gateway ~= '' then
+	return gateway
+end

+ 17 - 0
package/gluon-mesh-batman-adv/files/lib/gluon/alfred/announce.d/statistics/traffic

@@ -0,0 +1,17 @@
+local ethtool = util.exec('ethtool -S bat0')
+
+local fields = {}
+for k, v in ethtool:gmatch('([%a_]+): ([0-9]+)') do
+	fields[k] = tonumber(v)
+end
+
+local traffic = {}
+for _, class in ipairs({'rx', 'tx', 'forward', 'mgmt_rx', 'mgmt_tx'}) do
+	traffic[class] =
+		{ bytes = fields[class .. '_bytes']
+		, packets = fields[class]
+		}
+end
+traffic['tx']['dropped'] = fields['tx_dropped']
+
+return traffic

+ 4 - 0
package/gluon-mesh-vpn-fastd/files/lib/gluon/alfred/announce.d/software/fastd

@@ -0,0 +1,4 @@
+return
+	{ enabled = uci:get_bool('fastd', 'mesh_vpn', 'enabled')
+	, version = util.trim(util.exec("fastd -v | cut -d' ' -f2"))
+	}

+ 6 - 0
package/gluon-node-info/files/lib/gluon/alfred/announce.d/location

@@ -0,0 +1,6 @@
+if uci:get_first('gluon-node-info', 'location', 'share_location', false) then
+	return
+		{ latitude = tonumber(uci:get_first('gluon-node-info', 'location', 'latitude'))
+		, longitude = tonumber(uci:get_first('gluon-node-info', 'location', 'longitude'))
+		}
+end

+ 4 - 0
package/gluon-node-info/files/lib/gluon/alfred/announce.d/owner

@@ -0,0 +1,4 @@
+local contact = uci:get_first('gluon-node-info', 'owner', 'contact', '')
+if contact ~= '' then
+	return { contact = contact }
+end