merge-config.escript 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. -define(APPS, ["emqx", "emqx_dashboard", "emqx_authz"]).
  11. main(_) ->
  12. {ok, BaseConf} = file:read_file("apps/emqx_conf/etc/emqx_conf.conf"),
  13. Cfgs = get_all_cfgs("apps/"),
  14. Conf = [
  15. merge(BaseConf, Cfgs),
  16. io_lib:nl(),
  17. io_lib:nl(),
  18. "include emqx_enterprise.conf",
  19. io_lib:nl()
  20. ],
  21. ok = file:write_file("apps/emqx_conf/etc/emqx.conf.all", Conf),
  22. EnterpriseCfgs = get_all_cfgs("lib-ee/"),
  23. EnterpriseConf = merge("", EnterpriseCfgs),
  24. ok = file:write_file("apps/emqx_conf/etc/emqx_enterprise.conf.all", EnterpriseConf).
  25. merge(BaseConf, Cfgs) ->
  26. lists:foldl(
  27. fun(CfgFile, Acc) ->
  28. case filelib:is_regular(CfgFile) of
  29. true ->
  30. {ok, Bin1} = file:read_file(CfgFile),
  31. case string:trim(Bin1, both) of
  32. <<>> -> Acc;
  33. Bin2 -> [Acc, io_lib:nl(), io_lib:nl(), Bin2]
  34. end;
  35. false ->
  36. Acc
  37. end
  38. end,
  39. BaseConf,
  40. Cfgs
  41. ).
  42. get_all_cfgs(Root) ->
  43. Apps0 = filelib:wildcard("*", Root) -- ["emqx_machine", "emqx_conf"],
  44. Apps1 = (Apps0 -- ?APPS) ++ lists:reverse(?APPS),
  45. Dirs = [filename:join([Root, App]) || App <- Apps1],
  46. lists:foldl(fun get_cfgs/2, [], Dirs).
  47. get_all_cfgs(Dir, Cfgs) ->
  48. Fun = fun(E, Acc) ->
  49. Path = filename:join([Dir, E]),
  50. get_cfgs(Path, Acc)
  51. end,
  52. lists:foldl(Fun, Cfgs, filelib:wildcard("*", Dir)).
  53. get_cfgs(Dir, Cfgs) ->
  54. case filelib:is_dir(Dir) of
  55. false ->
  56. Cfgs;
  57. _ ->
  58. Files = filelib:wildcard("*", Dir),
  59. case lists:member("etc", Files) of
  60. false ->
  61. try_enter_child(Dir, Files, Cfgs);
  62. true ->
  63. EtcDir = filename:join([Dir, "etc"]),
  64. %% the conf name must start with emqx
  65. %% because there are some other conf, and these conf don't start with emqx
  66. Confs = filelib:wildcard("emqx*.conf", EtcDir),
  67. NewCfgs = [filename:join([EtcDir, Name]) || Name <- Confs],
  68. try_enter_child(Dir, Files, NewCfgs ++ Cfgs)
  69. end
  70. end.
  71. try_enter_child(Dir, Files, Cfgs) ->
  72. case lists:member("src", Files) of
  73. false ->
  74. Cfgs;
  75. true ->
  76. get_all_cfgs(filename:join([Dir, "src"]), Cfgs)
  77. end.