autoupdater: implement new probablity logic
This commit is contained in:
parent
12fa328bca
commit
aae7bf92ed
|
@ -21,16 +21,11 @@ local branch = uci:get_all('autoupdater', settings.branch)
|
||||||
local old_version = util.trim(fs.readfile(settings.version_file) or '')
|
local old_version = util.trim(fs.readfile(settings.version_file) or '')
|
||||||
|
|
||||||
|
|
||||||
if arg[1] ~= '-f' then
|
local force = (arg[1] == '-f')
|
||||||
if settings.enabled ~= '1' then
|
|
||||||
io.stderr:write('autoupdater is disabled.\n')
|
|
||||||
os.exit(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
if math.random() >= tonumber(branch.probability) then
|
if settings.enabled ~= '1' and not force then
|
||||||
io.stderr:write('No autoupdate this time. Use -f to override.\n')
|
io.stderr:write('autoupdater is disabled.\n')
|
||||||
os.exit(0)
|
os.exit(0)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -93,9 +88,15 @@ local function read_manifest(mirror)
|
||||||
branch_ok = true
|
branch_ok = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local date = line:match('^DATE=(.+)$')
|
||||||
|
local priority = line:match('^PRIORITY=([%d%.]+)$')
|
||||||
local model, version, checksum, filename = line:match('^([^ ]+) ([^ ]+) ([^ ]+) ([^ ]+)$')
|
local model, version, checksum, filename = line:match('^([^ ]+) ([^ ]+) ([^ ]+) ([^ ]+)$')
|
||||||
|
|
||||||
if model == platform_info.get_image_name() then
|
if date then
|
||||||
|
ret.date = autoupdater_util.parse_date(date)
|
||||||
|
elseif priority then
|
||||||
|
ret.priority = tonumber(priority)
|
||||||
|
elseif model == platform_info.get_image_name() then
|
||||||
ret.version = version
|
ret.version = version
|
||||||
ret.checksum = checksum
|
ret.checksum = checksum
|
||||||
ret.filename = filename
|
ret.filename = filename
|
||||||
|
@ -113,6 +114,11 @@ local function read_manifest(mirror)
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not ret.date or not ret.priority then
|
||||||
|
io.stderr:write('The manifest downloaded from ' .. mirror .. ' is invalid (DATE or PRIORITY missing)\n')
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
if not branch_ok then
|
if not branch_ok then
|
||||||
io.stderr:write('Wrong branch. We are on ', branch.name, '.\n')
|
io.stderr:write('Wrong branch. We are on ', branch.name, '.\n')
|
||||||
return nil
|
return nil
|
||||||
|
@ -143,6 +149,24 @@ local function fetch_firmware(mirror, filename, output)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- Returns the computed update probability
|
||||||
|
local function get_probability(date, priority)
|
||||||
|
local seconds = priority * 86400
|
||||||
|
local diff = os.difftime(os.time(), date)
|
||||||
|
|
||||||
|
if diff < 0 then
|
||||||
|
return 0
|
||||||
|
elseif diff >= seconds then
|
||||||
|
return 1
|
||||||
|
else
|
||||||
|
local x = diff/seconds
|
||||||
|
-- This is the most simple polynomial with value 0 at 0, 1 at 1, and whose first derivative is 0 at both 0 and 1
|
||||||
|
-- (we all love continuously differentiable functions, right?)
|
||||||
|
return (-2)*x^3 + 3*x^2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Tries to perform an update from a given mirror
|
-- Tries to perform an update from a given mirror
|
||||||
local function autoupdate(mirror)
|
local function autoupdate(mirror)
|
||||||
local manifest = read_manifest(mirror)
|
local manifest = read_manifest(mirror)
|
||||||
|
@ -157,6 +181,13 @@ local function autoupdate(mirror)
|
||||||
|
|
||||||
io.stderr:write('New version available.\n')
|
io.stderr:write('New version available.\n')
|
||||||
|
|
||||||
|
|
||||||
|
if not force and math.random() >= get_probability(manifest.date, manifest.priority) then
|
||||||
|
io.stderr:write('No autoupdate this time. Use -f to override.\n')
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
os.execute('sync; sysctl -w vm.drop_caches=3')
|
os.execute('sync; sysctl -w vm.drop_caches=3')
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue