merge-config.escript 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env escript
  2. %% This script reads up emqx.conf and split the sections
  3. %% and dump sections to separate files.
  4. %% Sections are grouped between CONFIG_SECTION_BGN and
  5. %% CONFIG_SECTION_END pairs
  6. %%
  7. %% NOTE: this feature is so far not used in opensource
  8. %% edition due to backward-compatibility reasons.
  9. -mode(compile).
  10. main(_) ->
  11. {ok, BaseConf} = file:read_file("apps/emqx_conf/etc/emqx_conf.conf"),
  12. Cfgs = get_all_cfgs("apps/"),
  13. Conf = [merge(BaseConf, Cfgs),
  14. io_lib:nl(),
  15. "include emqx_enterprise.conf",
  16. io_lib:nl()],
  17. ok = file:write_file("apps/emqx_conf/etc/emqx.conf.all", Conf),
  18. EnterpriseCfgs = get_all_cfgs("lib-ee/"),
  19. EnterpriseConf = merge("", EnterpriseCfgs),
  20. ok = file:write_file("apps/emqx_conf/etc/emqx_enterprise.conf.all", EnterpriseConf).
  21. merge(BaseConf, Cfgs) ->
  22. lists:foldl(
  23. fun(CfgFile, Acc) ->
  24. case filelib:is_regular(CfgFile) of
  25. true ->
  26. {ok, Bin1} = file:read_file(CfgFile),
  27. [Acc, io_lib:nl(), Bin1];
  28. false -> Acc
  29. end
  30. end, BaseConf, Cfgs).
  31. get_all_cfgs(Root) ->
  32. Apps = filelib:wildcard("*", Root) -- ["emqx_machine", "emqx_conf"],
  33. Dirs = [filename:join([Root, App]) || App <- Apps],
  34. lists:foldl(fun get_cfgs/2, [], Dirs).
  35. get_all_cfgs(Dir, Cfgs) ->
  36. Fun = fun(E, Acc) ->
  37. Path = filename:join([Dir, E]),
  38. get_cfgs(Path, Acc)
  39. end,
  40. lists:foldl(Fun, Cfgs, filelib:wildcard("*", Dir)).
  41. get_cfgs(Dir, Cfgs) ->
  42. case filelib:is_dir(Dir) of
  43. false ->
  44. Cfgs;
  45. _ ->
  46. Files = filelib:wildcard("*", Dir),
  47. case lists:member("etc", Files) of
  48. false ->
  49. try_enter_child(Dir, Files, Cfgs);
  50. true ->
  51. EtcDir = filename:join([Dir, "etc"]),
  52. %% the conf name must start with emqx
  53. %% because there are some other conf, and these conf don't start with emqx
  54. Confs = filelib:wildcard("emqx*.conf", EtcDir),
  55. NewCfgs = [filename:join([EtcDir, Name]) || Name <- Confs],
  56. try_enter_child(Dir, Files, NewCfgs ++ Cfgs)
  57. end
  58. end.
  59. try_enter_child(Dir, Files, Cfgs) ->
  60. case lists:member("src", Files) of
  61. false ->
  62. Cfgs;
  63. true ->
  64. get_all_cfgs(filename:join([Dir, "src"]), Cfgs)
  65. end.