wizard.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. local commit = {'gluon-setup-mode'}
  24. local run = {}
  25. for _, w in ipairs(wizard) do
  26. for _, c in ipairs(w(f, uci) or {}) do
  27. if type(c) == 'string' then
  28. if not util.contains(commit, c) then
  29. table.insert(commit, c)
  30. end
  31. elseif type(c) == 'function' then
  32. table.insert(run, c)
  33. else
  34. error('invalid wizard module return')
  35. end
  36. end
  37. end
  38. function f:write()
  39. local nixio = require "nixio"
  40. uci:set("gluon-setup-mode", uci:get_first("gluon-setup-mode", "setup_mode"), "configured", true)
  41. for _, c in ipairs(commit) do
  42. uci:commit(c)
  43. end
  44. for _, r in ipairs(run) do
  45. r()
  46. end
  47. f.template = "gluon/config-mode/reboot"
  48. f.hidenav = true
  49. if nixio.fork() == 0 then
  50. -- Replace stdout with /dev/null
  51. nixio.dup(nixio.open('/dev/null', 'w'), nixio.stdout)
  52. -- Sleep a little so the browser can fetch everything required to
  53. -- display the reboot page, then reboot the device.
  54. nixio.nanosleep(1)
  55. nixio.execp("reboot")
  56. end
  57. end
  58. return f