merge-config.escript 2.9 KB

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