check_site.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. local cjson = require 'cjson'
  2. local function exit_error(src, ...)
  3. io.stderr:write(string.format('*** %s error: %s\n', src, string.format(...)))
  4. os.exit(1)
  5. end
  6. local has_domains = (os.execute('ls -d "$IPKG_INSTROOT"/lib/gluon/domains/ >/dev/null 2>&1') == 0)
  7. local function load_json(filename)
  8. local f = assert(io.open(filename))
  9. local json = cjson.decode(f:read('*a'))
  10. f:close()
  11. return json
  12. end
  13. local function get_domains()
  14. local domains = {}
  15. local dirs = io.popen("find \"$IPKG_INSTROOT\"/lib/gluon/domains/ -name '*.json'")
  16. for filename in dirs:lines() do
  17. local name = string.match(filename, '([^/]+).json$')
  18. domains[name] = load_json(filename)
  19. end
  20. dirs:close()
  21. if not next(domains) then
  22. exit_error('site', 'no domain configurations found')
  23. end
  24. return domains
  25. end
  26. local site, domain_code, domain, conf
  27. local function merge(a, b)
  28. local function is_array(t)
  29. local n = 0
  30. for k, v in pairs(t) do
  31. n = n + 1
  32. end
  33. return n == #t
  34. end
  35. if not b then return a end
  36. if type(a) ~= type(b) then return b end
  37. if type(b) ~= 'table' then return b end
  38. if not next(b) then return a end
  39. if is_array(a) ~= is_array(b) then return b end
  40. local m = {}
  41. for k, v in pairs(a) do
  42. m[k] = v
  43. end
  44. for k, v in pairs(b) do
  45. m[k] = merge(m[k], v)
  46. end
  47. return m
  48. end
  49. function in_site(var)
  50. return var
  51. end
  52. function in_domain(var)
  53. return var
  54. end
  55. function this_domain()
  56. return domain_code
  57. end
  58. local function path_to_string(path)
  59. return table.concat(path, '/')
  60. end
  61. local function array_to_string(array)
  62. return '[' .. table.concat(array, ', ') .. ']'
  63. end
  64. local loadpath
  65. local function site_src()
  66. return 'site.conf'
  67. end
  68. local function domain_src()
  69. return 'domains/' .. domain_code .. '.conf'
  70. end
  71. local function var_error(path, val, msg)
  72. if type(val) == 'string' then
  73. val = string.format('%q', val)
  74. end
  75. local src
  76. if has_domains then
  77. if loadpath(nil, domain, unpack(path)) ~= nil then
  78. src = domain_src()
  79. elseif loadpath(nil, site, unpack(path)) ~= nil then
  80. src = site_src()
  81. else
  82. src = site_src() .. ' / ' .. domain_src()
  83. end
  84. else
  85. src = site_src()
  86. end
  87. exit_error(src, 'expected %s to %s, but it is %s', path_to_string(path), msg, tostring(val))
  88. end
  89. function extend(path, c)
  90. if not path then return nil end
  91. local p = {unpack(path)}
  92. for _, e in ipairs(c) do
  93. p[#p+1] = e
  94. end
  95. return p
  96. end
  97. function loadpath(path, base, c, ...)
  98. if not c or base == nil then
  99. return base
  100. end
  101. if type(base) ~= 'table' then
  102. if path then
  103. var_error(path, base, 'be a table')
  104. else
  105. return nil
  106. end
  107. end
  108. return loadpath(extend(path, {c}), base[c], ...)
  109. end
  110. local function loadvar(path)
  111. return loadpath({}, conf, unpack(path))
  112. end
  113. local function check_type(t)
  114. return function(val)
  115. return type(val) == t
  116. end
  117. end
  118. local function check_one_of(array)
  119. return function(val)
  120. for _, v in ipairs(array) do
  121. if v == val then
  122. return true
  123. end
  124. end
  125. return false
  126. end
  127. end
  128. function need(path, check, required, msg)
  129. local val = loadvar(path)
  130. if required == false and val == nil then
  131. return nil
  132. end
  133. if not check(val) then
  134. var_error(path, val, msg)
  135. end
  136. return val
  137. end
  138. local function need_type(path, type, required, msg)
  139. return need(path, check_type(type), required, msg)
  140. end
  141. function need_alphanumeric_key(path)
  142. local val = path[#path]
  143. -- We don't use character classes like %w here to be independent of the locale
  144. if not val:match('^[0-9a-zA-Z_]+$') then
  145. var_error(path, val, 'have a key using only alphanumeric characters and underscores')
  146. end
  147. end
  148. function need_string(path, required)
  149. return need_type(path, 'string', required, 'be a string')
  150. end
  151. function need_string_match(path, pat, required)
  152. local val = need_string(path, required)
  153. if not val then
  154. return nil
  155. end
  156. if not val:match(pat) then
  157. var_error(path, val, "match pattern '" .. pat .. "'")
  158. end
  159. return val
  160. end
  161. function need_number(path, required)
  162. return need_type(path, 'number', required, 'be a number')
  163. end
  164. function need_boolean(path, required)
  165. return need_type(path, 'boolean', required, 'be a boolean')
  166. end
  167. function need_array(path, subcheck, required)
  168. local val = need_type(path, 'table', required, 'be an array')
  169. if not val then
  170. return nil
  171. end
  172. if subcheck then
  173. for i = 1, #val do
  174. subcheck(extend(path, {i}))
  175. end
  176. end
  177. return val
  178. end
  179. function need_table(path, subcheck, required)
  180. local val = need_type(path, 'table', required, 'be a table')
  181. if not val then
  182. return nil
  183. end
  184. if subcheck then
  185. for k, _ in pairs(val) do
  186. subcheck(extend(path, {k}))
  187. end
  188. end
  189. return val
  190. end
  191. function need_value(path, value, required)
  192. return need(path, function(v)
  193. return v == value
  194. end, required, 'be ' .. tostring(value))
  195. end
  196. function need_one_of(path, array, required)
  197. return need(path, check_one_of(array), required, 'be one of the given array ' .. array_to_string(array))
  198. end
  199. function need_string_array(path, required)
  200. return need_array(path, need_string, required)
  201. end
  202. function need_string_array_match(path, pat, required)
  203. return need_array(path, function(e) need_string_match(e, pat) end, required)
  204. end
  205. function need_array_of(path, array, required)
  206. return need_array(path, function(e) need_one_of(e, array) end, required)
  207. end
  208. local check = assert(loadfile())
  209. site = load_json(os.getenv('IPKG_INSTROOT') .. '/lib/gluon/site.json')
  210. if has_domains then
  211. for k, v in pairs(get_domains()) do
  212. domain_code = k
  213. domain = v
  214. conf = merge(site, domain)
  215. check()
  216. end
  217. else
  218. conf = site
  219. check()
  220. end