merge-config.escript 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. merge_desc_files_per_lang("en"),
  35. %% TODO: remove this when we have zh translation moved to dashboard package
  36. merge_desc_files_per_lang("zh").
  37. is_enterprise() ->
  38. Profile = os:getenv("PROFILE", "emqx"),
  39. nomatch =/= string:find(Profile, "enterprise").
  40. merge(BaseConf, Cfgs) ->
  41. Confs = [BaseConf | lists:map(fun read_conf/1, Cfgs)],
  42. infix(lists:filter(fun(I) -> iolist_size(I) > 0 end, Confs), [io_lib:nl(), io_lib:nl()]).
  43. read_conf(CfgFile) ->
  44. case filelib:is_regular(CfgFile) of
  45. true ->
  46. {ok, Bin1} = file:read_file(CfgFile),
  47. string:trim(Bin1, both);
  48. false ->
  49. <<>>
  50. end.
  51. infix([], _With) -> [];
  52. infix([One], _With) -> [One];
  53. infix([H | T], With) -> [H, With, infix(T, With)].
  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.
  90. %% Desc files merge is for now done locally in emqx.git repo for all languages.
  91. %% When zh and other languages are moved to a separate repo,
  92. %% we will only merge the en files.
  93. %% The file for other languages will be merged in the other repo,
  94. %% the built as a part of the dashboard package,
  95. %% finally got pulled at build time as a part of the dashboard package.
  96. merge_desc_files_per_lang(Lang) ->
  97. BaseConf = <<"">>,
  98. Cfgs0 = get_all_desc_files(Lang),
  99. Conf = do_merge_desc_files_per_lang(BaseConf, Cfgs0),
  100. OutputFile = case Lang of
  101. "en" ->
  102. %% en desc will always be in the priv dir of emqx_dashboard
  103. "apps/emqx_dashboard/priv/desc.en.hocon";
  104. "zh" ->
  105. %% so far we inject zh desc as if it's extracted from dashboard package
  106. %% TODO: remove this when we have zh translation moved to dashboard package
  107. "apps/emqx_dashboard/priv/www/static/desc.zh.hocon"
  108. end,
  109. ok = filelib:ensure_dir(OutputFile),
  110. ok = file:write_file(OutputFile, Conf).
  111. do_merge_desc_files_per_lang(BaseConf, Cfgs) ->
  112. lists:foldl(
  113. fun(CfgFile, Acc) ->
  114. case filelib:is_regular(CfgFile) of
  115. true ->
  116. {ok, Bin1} = file:read_file(CfgFile),
  117. [Acc, io_lib:nl(), Bin1];
  118. false -> Acc
  119. end
  120. end, BaseConf, Cfgs).
  121. get_all_desc_files(Lang) ->
  122. Dir =
  123. case Lang of
  124. "en" ->
  125. filename:join(["rel", "i18n"]);
  126. "zh" ->
  127. %% TODO: remove this when we have zh translation moved to dashboard package
  128. filename:join(["rel", "i18n", "zh"])
  129. end,
  130. Files = filelib:wildcard("*.hocon", Dir),
  131. lists:map(fun(Name) -> filename:join([Dir, Name]) end, Files).