gluon/package/gluon-autoupdater/luasrc/lib/gluon/upgrade/500-autoupdater
Matthias Schiffer d82ffb4f81
gluon-autoupdater: make site.conf branch setting optional
Default to the alphabetically smallest branch if none is set in
site.conf.

Also prevent enabling the autoupdater when no branches exist.
2020-08-15 16:36:27 +02:00

64 lines
1.7 KiB
Lua
Executable File

#!/usr/bin/lua
local site = require 'gluon.site'
local uci = require('simple-uci').cursor()
local unistd = require 'posix.unistd'
local min_branch
for name, config in pairs(site.autoupdater.branches()) do
uci:delete('autoupdater', name)
uci:section('autoupdater', 'branch', name, {
name = config.name,
mirror = config.mirrors,
good_signatures = config.good_signatures,
pubkey = config.pubkeys,
})
if not min_branch or (name < min_branch) then
min_branch = name
end
end
if not uci:get('autoupdater', 'settings') then
local enabled = unistd.access('/lib/gluon/autoupdater/default_enabled') ~= nil
local branch = site.autoupdater.branch(min_branch)
local f = io.open('/lib/gluon/autoupdater/default_branch')
if f then
branch = f:read('*line')
f:close()
end
if not branch then
enabled = false
end
uci:section('autoupdater', 'autoupdater', 'settings', {
enabled = enabled,
branch = branch,
})
end
uci:set('autoupdater', 'settings', 'version_file', '/lib/gluon/release')
uci:save('autoupdater')
local urandom = io.open('/dev/urandom', 'r')
local seed1, seed2 = urandom:read(2):byte(1, 2)
math.randomseed(seed1*0x100 + seed2)
urandom:close()
-- Perform updates at a random time between 04:00 and 05:00, and once an hour
-- a fallback update (used after the regular updates haven't worked for
-- (priority+1) days after a firmware release, for example because the node
-- is always offline at night)
local minute = math.random(0, 59)
local f = io.open('/usr/lib/micron.d/autoupdater', 'w')
f:write(string.format('%i 4 * * * /usr/sbin/autoupdater\n', minute))
f:write(string.format('%i 0-3,5-23 * * * /usr/sbin/autoupdater --fallback\n', minute))
f:close()