gluon/package/gluon-scheduled-domain-switch/luasrc/usr/bin/gluon-check-connection
David Bauer c1b9ea2d9c gluon-scheduled-domain-switch: add package (#1555)
This package allows to automatically switch to another domain, either
at a given point in time or after the node was offline long enough.
2019-02-12 11:00:29 +01:00

37 lines
840 B
Lua
Executable File

#!/usr/bin/lua
local unistd = require 'posix.unistd'
local util = require 'gluon.util'
local site = require 'gluon.site'
local offline_flag_file = "/tmp/gluon_offline"
local is_offline = true
-- Check if domain-switch is scheduled
if site.domain_switch() == nil then
-- Switch not applicable for current domain
os.exit(0)
end
-- Check reachability of pre-defined targets
for _, ip in ipairs(site.domain_switch.connection_check_targets()) do
local exit_code = os.execute("ping -c 1 -w 10 " .. ip)
if exit_code == 0 then
is_offline = false
break
end
end
if is_offline then
-- Check if we were previously offline
if unistd.access(offline_flag_file) then
os.exit(0)
end
-- Create offline flag
local f = io.open(offline_flag_file, "w")
f:write(tostring(util.get_uptime()))
f:close()
else
os.remove(offline_flag_file)
end