merge-config.escript 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. Confs = [BaseConf | lists:map(fun read_conf/1, Cfgs)],
  39. infix(lists:filter(fun(I) -> iolist_size(I) > 0 end, Confs), [io_lib:nl(), io_lib:nl()]).
  40. read_conf(CfgFile) ->
  41. case filelib:is_regular(CfgFile) of
  42. true ->
  43. {ok, Bin1} = file:read_file(CfgFile),
  44. string:trim(Bin1, both);
  45. false ->
  46. <<>>
  47. end.
  48. infix([], _With) -> [];
  49. infix([One], _With) -> [One];
  50. infix([H | T], With) -> [H, With, infix(T, With)].
  51. get_all_cfgs(Root) ->
  52. Apps0 = filelib:wildcard("*", Root) -- ["emqx_machine", "emqx_conf"],
  53. Apps1 = (Apps0 -- ?APPS) ++ lists:reverse(?APPS),
  54. Dirs = [filename:join([Root, App]) || App <- Apps1],
  55. lists:foldl(fun get_cfgs/2, [], Dirs).
  56. get_all_cfgs(Dir, Cfgs) ->
  57. Fun = fun(E, Acc) ->
  58. Path = filename:join([Dir, E]),
  59. get_cfgs(Path, Acc)
  60. end,
  61. lists:foldl(Fun, Cfgs, filelib:wildcard("*", Dir)).
  62. get_cfgs(Dir, Cfgs) ->
  63. case filelib:is_dir(Dir) of
  64. false ->
  65. Cfgs;
  66. _ ->
  67. Files = filelib:wildcard("*", Dir),
  68. case lists:member("etc", Files) of
  69. false ->
  70. try_enter_child(Dir, Files, Cfgs);
  71. true ->
  72. EtcDir = filename:join([Dir, "etc"]),
  73. %% the conf name must start with emqx
  74. %% because there are some other conf, and these conf don't start with emqx
  75. Confs = filelib:wildcard("emqx*.conf", EtcDir),
  76. NewCfgs = [filename:join([EtcDir, Name]) || Name <- Confs],
  77. try_enter_child(Dir, Files, NewCfgs ++ Cfgs)
  78. end
  79. end.
  80. try_enter_child(Dir, Files, Cfgs) ->
  81. case lists:member("src", Files) of
  82. false ->
  83. Cfgs;
  84. true ->
  85. get_all_cfgs(filename:join([Dir, "src"]), Cfgs)
  86. end.