500-autoupdater 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/lua
  2. local fs = require 'nixio.fs'
  3. local site = require 'gluon.site_config'
  4. local uci = require 'luci.model.uci'
  5. local util = require 'luci.util'
  6. local c = uci.cursor()
  7. local subst = {}
  8. subst['%%v'] = util.trim(fs.readfile('/etc/openwrt_version'))
  9. subst['%%n'], subst['%%S'] = util.exec('. /etc/openwrt_release; echo $DISTRIB_CODENAME; echo $DISTRIB_TARGET'):match('([^\n]*)\n([^\n]*)')
  10. subst['%%GS'] = site.site_code
  11. subst['%%GV'] = util.trim(fs.readfile('/lib/gluon/gluon-version'))
  12. subst['%%GR'] = util.trim(fs.readfile('/lib/gluon/release'))
  13. function replace_patterns(url)
  14. for k, v in pairs(subst) do
  15. url = url:gsub(k, v)
  16. end
  17. return url
  18. end
  19. for name, config in pairs(site.autoupdater.branches) do
  20. c:delete('autoupdater', name)
  21. mirrors = {}
  22. for i, v in ipairs(config.mirrors) do
  23. mirrors[i]=replace_patterns(v)
  24. end
  25. c:section('autoupdater', 'branch', name,
  26. {
  27. name = config.name,
  28. mirror = mirrors,
  29. good_signatures = config.good_signatures,
  30. pubkey = config.pubkeys,
  31. }
  32. )
  33. end
  34. if not c:get('autoupdater', 'settings') then
  35. local enabled = 0
  36. local branch = site.autoupdater.branch
  37. local f = io.open('/lib/gluon/autoupdater/default_branch')
  38. if f then
  39. enabled = 1
  40. branch = f:read('*line')
  41. f:close()
  42. end
  43. c:section('autoupdater', 'autoupdater', 'settings',
  44. {
  45. enabled = enabled,
  46. branch = branch,
  47. }
  48. )
  49. end
  50. c:set('autoupdater', 'settings', 'version_file', '/lib/gluon/release')
  51. c:save('autoupdater')
  52. local autoupdater_util = require 'autoupdater.util'
  53. autoupdater_util.randomseed()
  54. -- Perform updates at a random time between 04:00 and 05:00, and once an hour
  55. -- a fallback update (used after the regular updates haven't
  56. local minute = math.random(0, 59)
  57. local f = io.open('/usr/lib/micron.d/autoupdater', 'w')
  58. f:write(string.format('%i 4 * * * /usr/sbin/autoupdater\n', minute))
  59. f:write(string.format('%i 0-3,5-23 * * * /usr/sbin/autoupdater --fallback\n', minute))
  60. f:close()