Skip to content
Snippets Groups Projects
Commit 339a6c99 authored by Jan-Philipp Litza's avatar Jan-Philipp Litza
Browse files

Remove dependencies on luci.util (and partly nixio.fs) from announce scripts

parent 7f5c3e43
No related branches found
No related tags found
No related merge requests found
Showing
with 76 additions and 64 deletions
......@@ -2,7 +2,6 @@
local announce = require 'gluon.announce'
local json = require 'luci.jsonc'
local ltn12 = require 'luci.ltn12'
local announce_dir = '/lib/gluon/announce/' .. arg[1] .. '.d'
......
local n = 0
local cpus = util.trim(fs.readfile('/sys/devices/system/cpu/online'))
local cpus = util.readline(io.open('/sys/devices/system/cpu/online'))
for _, entry in ipairs(cpus:split(',')) do
for entry in cpus:gmatch('([^,]+)') do
local x, y = entry:match('(%d+)-(%d+)')
if x then
n = n + tonumber(y) - tonumber(x) + 1
......
return {
base = 'gluon-' .. util.trim(fs.readfile('/lib/gluon/gluon-version')),
release = util.trim(fs.readfile('/lib/gluon/release')),
base = 'gluon-' .. util.readline(io.open('/lib/gluon/gluon-version')),
release = util.readline(io.open('/lib/gluon/release')),
}
local site = require 'gluon.site_config'
return site.site_code
return require('gluon.site_config').site_code
return tonumber(fs.readfile('/proc/uptime'):match('^[^ ]+ ([^ ]+)'))
return tonumber(util.readline(io.open('/proc/uptime')):match('^[^ ]+ ([^ ]+)'))
return tonumber(fs.readfile('/proc/loadavg'):match('^([^ ]+) '))
return tonumber(util.readline(io.open('/proc/loadavg')):match('^([^ ]+) '))
local data = fs.readfile('/proc/meminfo')
local data = io.open('/proc/meminfo'):read('*a')
local fields = {}
for k, v in data:gmatch('([^\n:]+):%s*(%d+) kB') do
......
local running, total = fs.readfile('/proc/loadavg'):match('^[^ ]+ [^ ]+ [^ ]+ (%d+)/(%d+)')
local running, total = util.readline(io.open('/proc/loadavg')):match('^[^ ]+ [^ ]+ [^ ]+ (%d+)/(%d+)')
return { running = tonumber(running), total = tonumber(total) }
return tonumber(fs.readfile('/proc/uptime'):match('^([^ ]+) '))
return tonumber(util.readline(io.open('/proc/uptime')):match('^([^ ]+) '))
......@@ -4,7 +4,7 @@ module('gluon.announce', package.seeall)
fs = require 'nixio.fs'
uci = require('luci.model.uci').cursor()
util = require 'luci.util'
util = require 'gluon.util'
local function collect_entry(entry)
if fs.stat(entry, 'type') == 'dir' then
......
......@@ -38,7 +38,7 @@ local uci = require('luci.model.uci').cursor()
module 'gluon.util'
function exec(...)
return os.execute(escape_args('', ...))
return os.execute(escape_args('', 'exec', ...))
end
-- Removes all lines starting with a prefix from a file, optionally adding a new one
......@@ -52,6 +52,12 @@ function replace_prefix(file, prefix, add)
os.rename(tmp, file)
end
function readline(fd)
local line = fd:read('*l')
fd:close()
return line
end
function lock(file)
exec('lock', file)
end
......
......@@ -5,7 +5,7 @@ function ifname2address(ifname)
if ifname_address_cache[ifname] ~= nil then
ifaddress = ifname_address_cache[ifname]
else
ifaddress = util.trim(fs.readfile("/sys/class/net/" .. ifname .. "/address"))
ifaddress = util.readline(io.open("/sys/class/net/" .. ifname .. "/address"))
ifname_address_cache[ifname] = ifaddress
end
......
......@@ -16,18 +16,17 @@ end
function interfaces()
local interfaces = {}
for _, line in ipairs(util.split(util.exec('batctl if'))) do
ifname = line:match('^(.-): active')
if ifname ~= nil then
pcall(function()
local address = util.trim(fs.readfile('/sys/class/net/' .. ifname .. '/address'))
local wifitype = iwinfo.type(ifname)
if wifitype ~= nil then
interfaces[address] = { ifname = ifname, iw = iwinfo[wifitype] }
end
end)
end
local popen = io.popen('exec batctl if')
for ifname in popen:read('*a'):gmatch('([^%s]+): active') do
pcall(function()
local address = util.readline(io.open('/sys/class/net/' .. ifname .. '/address'))
local wifitype = iwinfo.type(ifname)
if wifitype ~= nil then
interfaces[address] = { ifname = ifname, iw = iwinfo[wifitype] }
end
end)
end
popen:close()
return interfaces
end
......
local list = util.exec('batctl if')
local wireless = {}
local tunnel = {}
local other = {}
......@@ -7,21 +5,27 @@ local other = {}
local function get_address(t, ifname)
pcall(
function()
table.insert(t, util.trim(fs.readfile('/sys/class/net/' .. ifname .. '/address')))
table.insert(t, util.readline(io.open('/sys/class/net/' .. ifname .. '/address')))
end
)
end
local function is_wireless(ifname)
local type = fs.stat('/sys/class/net/' .. ifname .. '/wireless', 'type')
local function file_exists(filename)
local f = io.open(filename)
if f == nil then
return false
else
f:close()
return true
end
end
return type == 'directory'
local function is_wireless(ifname)
return file_exists('/sys/class/net/' .. ifname .. '/wireless')
end
local function is_tuntap(ifname)
local type = fs.stat('/sys/class/net/' .. ifname .. '/tun_flags', 'type')
return type == 'regular'
return file_exists('/sys/class/net/' .. ifname .. '/tun_flags')
end
local function nil_table(t)
......@@ -32,18 +36,17 @@ local function nil_table(t)
end
end
for _, line in ipairs(util.split(list)) do
local ifname = line:match('^(.-):')
if ifname ~= nil then
if is_wireless(ifname) then
get_address(wireless, ifname)
elseif is_tuntap(ifname) then
get_address(tunnel, ifname)
else
get_address(other, ifname)
end
local popen = io.popen('exec batctl if')
for ifname in popen:read('*a'):gmatch('([^%s]+): active') do
if is_wireless(ifname) then
get_address(wireless, ifname)
elseif is_tuntap(ifname) then
get_address(tunnel, ifname)
else
get_address(other, ifname)
end
end
popen:close()
return {
wireless = nil_table(wireless),
......
local list = util.exec('batctl if')
local interfaces = {}
for _, line in ipairs(util.split(list)) do
local ifname = line:match('^(.-):')
if ifname ~= nil then
pcall(
function()
table.insert(interfaces, util.trim(fs.readfile('/sys/class/net/' .. ifname .. '/address')))
end
)
end
local popen = io.popen('exec batctl if')
for ifname in popen:read('*a'):gmatch('([^%s]+): active') do
pcall(
function()
table.insert(interfaces, util.readline(io.open('/sys/class/net/' .. ifname .. '/address')))
end
)
end
popen:close()
return interfaces
return util.trim(fs.readfile('/sys/module/batman_adv/version'))
return util.readline(io.open('/sys/module/batman_adv/version'))
local gateway = util.trim(util.exec("batctl -m bat0 gateways | awk '/^=>/ { print $2 }'"))
local gateway = ''
local popen = io.popen("exec batctl -m bat0 gateways")
for line in popen:lines() do
if line:sub(1, 3) == '=> ' then
gateway = line:sub(4, 20)
break
end
end
popen:close()
if gateway ~= '' then
return gateway
return gateway
end
return {
enabled = uci:get_bool('fastd', 'mesh_vpn', 'enabled'),
version = util.exec('fastd -v'):match('^[^%s]+%s+([^\n]+)'),
local ret = {
enabled = uci:get('fastd', 'mesh_vpn', 'enabled') ~= 0,
version = util.readline(io.popen('exec fastd -v')):match('^[^%s]+%s+(.+)'),
}
return ret
......@@ -2,7 +2,6 @@ local json = require 'luci.jsonc'
local ltn12 = require 'luci.ltn12'
local nixio = require 'nixio'
local site = require 'gluon.site_config'
local uci = require('luci.model.uci').cursor()
local fastd_sock = nixio.socket('unix', 'stream')
local socket_path = uci:get('fastd', 'mesh_vpn', 'status_socket')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment