/
relocate.lua
executable file
·57 lines (56 loc) · 2.29 KB
/
relocate.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/lua
local devurandom = io.open("/dev/urandom","rb")
local b1,b2 = devurandom:read(2):byte(1,2)
local autoupdaterlockfile='/var/lock/autoupdater.lock'
local directorurl='http://api.ff3l/move/'
local geoapiurl='http://api.ff3l/getdombycoord/'
seed = b1 + (256 * b2)
math.randomseed(seed)
local delay = math.random(60)
function file_exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
local uci=require('simple-uci').cursor()
local o=uci:get('gluon','core','ignorerelocate')
if (o=='1') then
io.write("Ignoring Domain Change request.. Exiting.\n")
do return end
end
io.write('Sleeping for ' .. delay .. ' Seconds.\n')
os.execute('sleep ' .. delay)
local currentdomain=uci:get("gluon","core","domain")
local nodeid = require('gluon.util').node_id()
local lat = uci:get("gluon-node-info","@location[0]","latitude")
local lon = uci:get("gluon-node-info","@location[0]","longitude")
if (lat ~= nil and lon ~=nil and lat ~= "" and lon ~="") then
local url=geoapiurl .. lat .. "," .. lon
geoseg = io.popen("wget -q -O - '" .. url .. "'"):read('*a')
else
geoseg = ""
end
local url=directorurl .. nodeid
local manseg = io.popen("wget -q -O - '" .. url .. "'"):read('*a')
io.write('Current Domain: ' .. currentdomain .. '\nNodeID: ' .. nodeid .. '\nRequested Domain: ' .. manseg .. '\nDomain by Coordinates: ' .. geoseg .. '\n')
if (manseg ~= "" or currentdomain=="ref" ) then
newseg = manseg
elseif (uci:get('gluon','core','noautodomain') ~= "1") then
newseg = geoseg
end
if (currentdomain==newseg or newseg == "" or newseg == nil or newseg == "noop") then
io.write("Do nothing..\n")
else
if (file_exists('/lib/gluon/domains/' .. newseg .. '.json') == true) then
-- This is not nice, but it works. nixio lacks flock...
local alock = io.popen('lock -n ' .. autoupdaterlockfile .. ' 2>/dev/null && echo unlocked || echo locked'):read('*l')
if (alock ~= 'unlocked') then
io.write('Detected Flock on ' .. autoupdaterlockfile .. '. Exiting.\n')
do return end
end
os.execute('logger -s -t "gluon-segment-mover" -p 5 "Domain Change requested. Moving to "' .. newseg)
os.execute('/usr/bin/gluon-switch-domain "' .. newseg .. '"')
else
io.write('Invalid Domain requested. I don\'t have ' .. newseg .. '.conf')
do return end
end
end