wizard.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. local disp = require 'gluon.web.dispatcher'
  2. local fs = require "nixio.fs"
  3. local util = require "gluon.web.util"
  4. local nixio_util = require "nixio.util"
  5. local uci = require("simple-uci").cursor()
  6. local wizard_dir = "/lib/gluon/config-mode/wizard/"
  7. local files = nixio_util.consume(fs.dir(wizard_dir) or function() end)
  8. table.sort(files)
  9. local wizard = {}
  10. for _, entry in ipairs(files) do
  11. if entry:sub(1, 1) ~= '.' then
  12. local f = assert(loadfile(wizard_dir .. entry))
  13. setfenv(f, getfenv())
  14. local w = f()
  15. table.insert(wizard, w)
  16. end
  17. end
  18. local f = Form(translate("Welcome!"))
  19. f.submit = translate('Save & restart')
  20. f.reset = false
  21. local s = f:section(Section)
  22. s.template = "gluon/config-mode/welcome"
  23. s.package = "gluon-config-mode-core"
  24. local commit = {'gluon-setup-mode'}
  25. local run = {}
  26. for _, w in ipairs(wizard) do
  27. for _, c in ipairs(w(f, uci) or {}) do
  28. if type(c) == 'string' then
  29. if not util.contains(commit, c) then
  30. table.insert(commit, c)
  31. end
  32. elseif type(c) == 'function' then
  33. table.insert(run, c)
  34. else
  35. error('invalid wizard module return')
  36. end
  37. end
  38. end
  39. function f:write()
  40. local nixio = require "nixio"
  41. uci:set("gluon-setup-mode", uci:get_first("gluon-setup-mode", "setup_mode"), "configured", true)
  42. for _, c in ipairs(commit) do
  43. uci:commit(c)
  44. end
  45. for _, r in ipairs(run) do
  46. r()
  47. end
  48. f.template = "gluon/config-mode/reboot"
  49. f.package = "gluon-config-mode-core"
  50. f.hidenav = true
  51. if nixio.fork() == 0 then
  52. -- Replace stdout with /dev/null
  53. nixio.dup(nixio.open('/dev/null', 'w'), nixio.stdout)
  54. -- Sleep a little so the browser can fetch everything required to
  55. -- display the reboot page, then reboot the device.
  56. nixio.nanosleep(1)
  57. nixio.execp("reboot")
  58. end
  59. end
  60. return f