|
| 1 | +#!/usr/bin/lua |
| 2 | + |
| 3 | +local uci = require('simple-uci').cursor() |
| 4 | +local unistd = require 'posix.unistd' |
| 5 | +local util = require 'gluon.util' |
| 6 | +local site = require 'gluon.site' |
| 7 | + |
| 8 | +-- Returns true if node was offline long enough to perform domain switch |
| 9 | +function switch_after_min_reached() |
| 10 | + if not unistd.access("/tmp/gluon_offline") then |
| 11 | + return false |
| 12 | + end |
| 13 | + |
| 14 | + local switch_after_sec = site.domain_switch.switch_after_offline_mins() * 60 |
| 15 | + |
| 16 | + local current_uptime = util.get_uptime() |
| 17 | + if current_uptime == nil then |
| 18 | + return false |
| 19 | + end |
| 20 | + |
| 21 | + local f = util.readfile("/tmp/gluon_offline") |
| 22 | + if f == nil then |
| 23 | + return false |
| 24 | + end |
| 25 | + local offline_since = tonumber(f) |
| 26 | + |
| 27 | + local offline_time_sec = current_uptime - offline_since |
| 28 | + |
| 29 | + if offline_time_sec > switch_after_sec then |
| 30 | + return true |
| 31 | + end |
| 32 | + return false |
| 33 | +end |
| 34 | + |
| 35 | +-- Returns true in case switch time has passed |
| 36 | +function switch_time_passed() |
| 37 | + local current_time = os.time() |
| 38 | + local switch_time = site.domain_switch.switch_time() |
| 39 | + |
| 40 | + return switch_time < current_time |
| 41 | +end |
| 42 | + |
| 43 | +if site.domain_switch() == nil then |
| 44 | + -- Switch not applicable for current domain |
| 45 | + print("No domain switch defined for the current domain.") |
| 46 | + os.exit(0) |
| 47 | +end |
| 48 | + |
| 49 | +local current_domain = uci:get("gluon", "core", "domain") |
| 50 | +local target_domain = site.domain_switch.target_domain() |
| 51 | + |
| 52 | +if target_domain == current_domain then |
| 53 | + -- Current and target domain are equal |
| 54 | + print("Domain '" .. target_domain .. "' equals current domain.") |
| 55 | + os.exit(1) |
| 56 | +end |
| 57 | + |
| 58 | +if not switch_after_min_reached() and not switch_time_passed() then |
| 59 | + -- Neither switch-time passed nor switch_after_min reached |
| 60 | + os.exit(0) |
| 61 | +end |
| 62 | + |
| 63 | +uci:set("gluon", "core", "domain", target_domain) |
| 64 | +uci:commit("gluon") |
| 65 | + |
| 66 | +os.execute("gluon-reconfigure") |
| 67 | +os.execute("reboot") |
0 commit comments