merge-config.escript 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 = lists:foldl(fun(CfgFile, Acc) ->
  14. case filelib:is_regular(CfgFile) of
  15. true ->
  16. {ok, Bin1} = file:read_file(CfgFile),
  17. [Acc, io_lib:nl(), Bin1];
  18. false -> Acc
  19. end
  20. end, BaseConf, Cfgs),
  21. ClusterInc = "include \"cluster-override.conf\"\n",
  22. LocalInc = "include \"local-override.conf\"\n",
  23. ok = file:write_file("apps/emqx_conf/etc/emqx.conf.all", [Conf, ClusterInc, LocalInc]).
  24. get_all_cfgs(Root) ->
  25. Apps = filelib:wildcard("*", Root) -- ["emqx_machine", "emqx_conf"],
  26. Dirs = [filename:join([Root, App]) || App <- Apps],
  27. lists:foldl(fun get_cfgs/2, [], Dirs).
  28. get_all_cfgs(Dir, Cfgs) ->
  29. Fun = fun(E, Acc) ->
  30. Path = filename:join([Dir, E]),
  31. get_cfgs(Path, Acc)
  32. end,
  33. lists:foldl(Fun, Cfgs, filelib:wildcard("*", Dir)).
  34. get_cfgs(Dir, Cfgs) ->
  35. case filelib:is_dir(Dir) of
  36. false ->
  37. Cfgs;
  38. _ ->
  39. Files = filelib:wildcard("*", Dir),
  40. case lists:member("etc", Files) of
  41. false ->
  42. try_enter_child(Dir, Files, Cfgs);
  43. true ->
  44. EtcDir = filename:join([Dir, "etc"]),
  45. %% the conf name must start with emqx
  46. %% because there are some other conf, and these conf don't start with emqx
  47. Confs = filelib:wildcard("emqx*.conf", EtcDir),
  48. NewCfgs = [filename:join([EtcDir, Name]) || Name <- Confs],
  49. try_enter_child(Dir, Files, NewCfgs ++ Cfgs)
  50. end
  51. end.
  52. try_enter_child(Dir, Files, Cfgs) ->
  53. case lists:member("src", Files) of
  54. false ->
  55. Cfgs;
  56. true ->
  57. get_all_cfgs(filename:join([Dir, "src"]), Cfgs)
  58. end.