merge-config.escript 3.1 KB

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