aredn/files/etc/cron.hourly/check-services

131 lines
3.2 KiB
Lua
Executable File

#!/usr/bin/lua
--[[
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2022-2023 Tim Wilkinson
See Contributors file for additional contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
You must not misrepresent the origin of the material contained within.
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version
--]]
require("uci")
require("aredn.services")
local cursor = uci.cursor()
-- Current nameservice state
local ns = cursor:get_all("olsrd", "nameservice")
if not ns then
print("Missing nameservice")
return
end
local cnames = {}
for _, name in ipairs(ns.name)
do
cnames[name] = true
end
local chosts = {}
for _, iphost in ipairs(ns.hosts or {})
do
chosts[iphost] = true
end
local cservices = {}
for _, service in ipairs(ns.service or {})
do
cservices[service] = true
end
-- Work out the differences between the validated state and the current state
local names, hosts, services = aredn.services.get(true)
local nnames = false
for _, name in ipairs(names)
do
if cnames[name] then
cnames[name] = nil
else
nnames = true
end
end
local nhosts = false
for _, host in ipairs(hosts)
do
local iphost = host.ip .. " " .. host.host
if chosts[iphost] then
chosts[iphost] = nil
else
nhosts = true
end
end
local nservices = false
for _, service in ipairs(services)
do
if cservices[service] then
cservices[service] = nil
else
nservices = true
end
end
function not_empty(h)
for _, _ in pairs(h)
do
return true
end
return false
end
local change = false
-- Apply the changes
if nnames or not_empty(cnames) then
cursor:set("olsrd", "nameservice", "name", names)
change = true
end
if nhosts or not_empty(chosts) then
for i, host in ipairs(hosts)
do
hosts[i] = host.ip .. " " .. host.host
end
cursor:set("olsrd", "nameservice", "hosts", hosts)
change = true
end
if nservices or not_empty(cservices) then
cursor:set("olsrd", "nameservice", "service", services)
change = true
end
-- If services have changed we need to restart olsrd
if change then
cursor:commit("olsrd")
print("Change")
os.execute("/usr/local/bin/restart-services.sh --force olsrd")
else
print("Unchange")
end