mix.exs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. defmodule EMQXUmbrella.MixProject do
  2. use Mix.Project
  3. @moduledoc """
  4. The purpose of this file is to configure the release of EMQX under
  5. Mix. Since EMQX uses its own configuration conventions and startup
  6. procedures, one cannot simply use `iex -S mix`. Instead, it's
  7. recommended to build and use the release.
  8. ## Profiles
  9. To control the profile and edition to build, we case split on the
  10. MIX_ENV value.
  11. The following profiles are valid:
  12. * `emqx`
  13. * `emqx-enterprise`
  14. * `emqx-pkg`
  15. * `emqx-enterprise-pkg`
  16. * `dev` -> same as `emqx`, for convenience
  17. ## Release Environment Variables
  18. The release build is controlled by a few environment variables.
  19. * `ELIXIR_MAKE_TAR` - If set to `yes`, will produce a `.tar.gz`
  20. tarball along with the release.
  21. """
  22. # TODO: remove once we switch to the new mix build
  23. def new_mix_build?() do
  24. System.get_env("NEW_MIX_BUILD") == "1"
  25. end
  26. def project() do
  27. profile_info = check_profile!()
  28. version = pkg_vsn()
  29. if new_mix_build?() do
  30. [
  31. # TODO: these lines will be uncommented when we switch to using mix as the manager
  32. # for all umbrella apps.
  33. apps_path: "apps",
  34. apps:
  35. applications(profile_info.release_type, profile_info.edition_type) |> Keyword.keys(),
  36. erlc_options: erlc_options(profile_info, version),
  37. version: version,
  38. deps: deps(profile_info, version),
  39. releases: releases(),
  40. aliases: aliases()
  41. ]
  42. else
  43. # TODO: this check and clause will be removed when we switch to using mix as the
  44. # manager for all umbrella apps.
  45. [
  46. app: :emqx_mix,
  47. erlc_options: erlc_options(profile_info, version),
  48. version: version,
  49. deps: deps(profile_info, version),
  50. releases: releases(),
  51. aliases: aliases()
  52. ]
  53. end
  54. end
  55. @doc """
  56. Please try to add dependencies that used by a single umbrella application in the
  57. application's own `mix.exs` file, if possible. If it's shared by more than one
  58. application, or if the dependency requires an `override: true` option, add a new clause
  59. to `common_dep/1` so that we centralize versions in this root `mix.exs` file as much as
  60. possible.
  61. Here, transitive dependencies from our app dependencies should be placed when there's a
  62. need to override them. For example, since `jsone` is a dependency to `rocketmq` and to
  63. `erlavro`, which are both dependencies and not umbrella apps, we need to add the
  64. override here. Also, there are cases where adding `override: true` to the umbrella
  65. application dependency simply won't satisfy mix. In such cases, it's fine to add it
  66. here.
  67. """
  68. def deps(profile_info, version) do
  69. # we need several overrides here because dependencies specify
  70. # other exact versions, and not ranges.
  71. if new_mix_build?() do
  72. new_deps()
  73. else
  74. old_deps(profile_info, version)
  75. end
  76. end
  77. def new_deps() do
  78. quicer_dep() ++
  79. jq_dep() ++
  80. extra_release_apps() ++
  81. overridden_deps()
  82. end
  83. ## TODO: this should be removed once we migrate the release build to mix
  84. defp old_deps(profile_info, version) do
  85. rebar3_umbrella_apps = emqx_apps(profile_info, version) ++ enterprise_deps(profile_info)
  86. common_deps() ++
  87. extra_release_apps() ++
  88. overridden_deps() ++
  89. jq_dep() ++
  90. quicer_dep() ++ rebar3_umbrella_apps
  91. end
  92. def overridden_deps() do
  93. [
  94. common_dep(:lc),
  95. common_dep(:covertool),
  96. common_dep(:typerefl),
  97. common_dep(:ehttpc),
  98. common_dep(:gproc),
  99. common_dep(:jiffy),
  100. common_dep(:cowboy),
  101. common_dep(:esockd),
  102. common_dep(:rocksdb),
  103. common_dep(:ekka),
  104. common_dep(:gen_rpc),
  105. common_dep(:grpc),
  106. common_dep(:minirest),
  107. common_dep(:ecpool),
  108. common_dep(:replayq),
  109. common_dep(:pbkdf2),
  110. # maybe forbid to fetch quicer
  111. common_dep(:emqtt),
  112. common_dep(:rulesql),
  113. common_dep(:telemetry),
  114. # in conflict by emqtt and hocon
  115. common_dep(:getopt),
  116. common_dep(:snabbkaffe),
  117. common_dep(:hocon),
  118. common_dep(:emqx_http_lib),
  119. common_dep(:esasl),
  120. common_dep(:jose),
  121. # in conflict by ehttpc and emqtt
  122. common_dep(:gun),
  123. # in conflict by emqx_connector and system_monitor
  124. common_dep(:epgsql),
  125. # in conflict by emqx and observer_cli
  126. {:recon, github: "ferd/recon", tag: "2.5.1", override: true},
  127. common_dep(:jsx),
  128. # in conflict by erlavro and rocketmq
  129. common_dep(:jsone),
  130. # dependencies of dependencies; we choose specific refs to match
  131. # what rebar3 chooses.
  132. # in conflict by gun and emqtt
  133. common_dep(:cowlib),
  134. # in conflict by cowboy_swagger and cowboy
  135. common_dep(:ranch),
  136. # in conflict by grpc and eetcd
  137. common_dep(:gpb),
  138. {:hackney, github: "emqx/hackney", tag: "1.18.1-1", override: true},
  139. # set by hackney (dependency)
  140. {:ssl_verify_fun, "1.1.7", override: true},
  141. common_dep(:rfc3339),
  142. common_dep(:bcrypt),
  143. {:uuid, github: "okeuday/uuid", tag: "v2.0.6", override: true},
  144. {:quickrand, github: "okeuday/quickrand", tag: "v2.0.6", override: true},
  145. {:ra, "2.7.3", override: true},
  146. {:mimerl, "1.2.0", override: true}
  147. ]
  148. end
  149. def extra_release_apps() do
  150. [
  151. {:redbug, github: "emqx/redbug", tag: "2.0.10"},
  152. {:observer_cli, "1.7.1"},
  153. {:system_monitor, github: "ieQu1/system_monitor", tag: "3.0.5"}
  154. ]
  155. end
  156. def common_dep(:ekka), do: {:ekka, github: "emqx/ekka", tag: "0.19.5", override: true}
  157. def common_dep(:esockd), do: {:esockd, github: "emqx/esockd", tag: "5.11.3", override: true}
  158. def common_dep(:gproc), do: {:gproc, github: "emqx/gproc", tag: "0.9.0.1", override: true}
  159. def common_dep(:hocon), do: {:hocon, github: "emqx/hocon", tag: "0.43.1", override: true}
  160. def common_dep(:lc), do: {:lc, github: "emqx/lc", tag: "0.3.2", override: true}
  161. # in conflict by ehttpc and emqtt
  162. def common_dep(:gun), do: {:gun, github: "emqx/gun", tag: "1.3.11", override: true}
  163. # in conflict by cowboy_swagger and cowboy
  164. def common_dep(:ranch), do: {:ranch, github: "emqx/ranch", tag: "1.8.1-emqx", override: true}
  165. def common_dep(:ehttpc), do: {:ehttpc, github: "emqx/ehttpc", tag: "0.4.14", override: true}
  166. def common_dep(:jiffy), do: {:jiffy, github: "emqx/jiffy", tag: "1.0.6", override: true}
  167. def common_dep(:grpc), do: {:grpc, github: "emqx/grpc-erl", tag: "0.6.12", override: true}
  168. def common_dep(:cowboy), do: {:cowboy, github: "emqx/cowboy", tag: "2.9.2", override: true}
  169. def common_dep(:jsone), do: {:jsone, github: "emqx/jsone", tag: "1.7.1", override: true}
  170. def common_dep(:ecpool), do: {:ecpool, github: "emqx/ecpool", tag: "0.5.7", override: true}
  171. def common_dep(:replayq), do: {:replayq, github: "emqx/replayq", tag: "0.3.8", override: true}
  172. def common_dep(:jsx), do: {:jsx, github: "talentdeficit/jsx", tag: "v3.1.0", override: true}
  173. # in conflict by emqtt and hocon
  174. def common_dep(:getopt), do: {:getopt, "1.0.2", override: true}
  175. def common_dep(:telemetry), do: {:telemetry, "1.1.0", override: true}
  176. # in conflict by grpc and eetcd
  177. def common_dep(:gpb), do: {:gpb, "4.19.9", override: true, runtime: false}
  178. def common_dep(:covertool),
  179. do: {:covertool, github: "zmstone/covertool", tag: "2.0.4.1", override: true}
  180. # in conflict by emqx_connector and system_monitor
  181. def common_dep(:epgsql), do: {:epgsql, github: "emqx/epgsql", tag: "4.7.1.2", override: true}
  182. def common_dep(:esasl), do: {:esasl, github: "emqx/esasl", tag: "0.2.1"}
  183. def common_dep(:gen_rpc), do: {:gen_rpc, github: "emqx/gen_rpc", tag: "3.3.1", override: true}
  184. def common_dep(:jose),
  185. do: {:jose, github: "potatosalad/erlang-jose", tag: "1.11.2", override: true}
  186. def common_dep(:rulesql), do: {:rulesql, github: "emqx/rulesql", tag: "0.2.1"}
  187. def common_dep(:pbkdf2),
  188. do: {:pbkdf2, github: "emqx/erlang-pbkdf2", tag: "2.0.4", override: true}
  189. def common_dep(:bcrypt),
  190. do: {:bcrypt, github: "emqx/erlang-bcrypt", tag: "0.6.2", override: true}
  191. # hex version 0.2.2 used by `jesse` has buggy mix.exs
  192. def common_dep(:rfc3339), do: {:rfc3339, github: "emqx/rfc3339", tag: "0.2.3", override: true}
  193. def common_dep(:minirest),
  194. do: {:minirest, github: "emqx/minirest", tag: "1.4.3", override: true}
  195. # maybe forbid to fetch quicer
  196. def common_dep(:emqtt),
  197. do:
  198. {:emqtt,
  199. github: "emqx/emqtt", tag: "1.10.1", override: true, system_env: maybe_no_quic_env()}
  200. def common_dep(:typerefl),
  201. do: {:typerefl, github: "ieQu1/typerefl", tag: "0.9.1", override: true}
  202. def common_dep(:rocksdb),
  203. do: {:rocksdb, github: "emqx/erlang-rocksdb", tag: "1.8.0-emqx-5", override: true}
  204. def common_dep(:emqx_http_lib),
  205. do: {:emqx_http_lib, github: "emqx/emqx_http_lib", tag: "0.5.3", override: true}
  206. def common_dep(:cowlib),
  207. do:
  208. {:cowlib,
  209. github: "ninenines/cowlib", ref: "c6553f8308a2ca5dcd69d845f0a7d098c40c3363", override: true}
  210. def common_dep(:snabbkaffe),
  211. do: {
  212. :snabbkaffe,
  213. ## without this, snabbkaffe is compiled with `-define(snk_kind, '$kind')`, which
  214. ## will basically make events in tests never match any predicates.
  215. github: "kafka4beam/snabbkaffe",
  216. tag: "1.0.10",
  217. override: true,
  218. system_env: emqx_app_system_env(profile_info(), pkg_vsn())
  219. }
  220. ###############################################################################################
  221. # BEGIN DEPRECATED FOR MIX BLOCK
  222. # These should be removed once we fully migrate to mix
  223. ###############################################################################################
  224. defp emqx_apps(profile_info, version) do
  225. apps = umbrella_apps(profile_info) ++ enterprise_apps(profile_info)
  226. set_emqx_app_system_env(apps, profile_info, version)
  227. end
  228. defp umbrella_apps(profile_info = %{release_type: release_type}) do
  229. enterprise_apps = enterprise_umbrella_apps(release_type)
  230. excluded_apps = excluded_apps(release_type)
  231. "apps/*"
  232. |> Path.wildcard()
  233. |> Enum.map(fn path ->
  234. app =
  235. path
  236. |> Path.basename()
  237. |> String.to_atom()
  238. {app, path: path, manager: :rebar3, override: true}
  239. end)
  240. |> Enum.reject(fn dep_spec ->
  241. dep_spec
  242. |> elem(0)
  243. |> then(&MapSet.member?(enterprise_apps, &1))
  244. end)
  245. |> Enum.reject(fn {app, _} ->
  246. case profile_info do
  247. %{edition_type: :enterprise} ->
  248. app == :emqx_telemetry
  249. _ ->
  250. false
  251. end
  252. end)
  253. |> Enum.reject(fn {app, _} -> app in excluded_apps end)
  254. end
  255. defp enterprise_apps(_profile_info = %{release_type: release_type, edition_type: :enterprise}) do
  256. Enum.map(enterprise_umbrella_apps(release_type), fn app_name ->
  257. path = "apps/#{app_name}"
  258. {app_name, path: path, manager: :rebar3, override: true}
  259. end)
  260. end
  261. defp enterprise_apps(_profile_info) do
  262. []
  263. end
  264. # need to remove those when listing `/apps/`...
  265. defp enterprise_umbrella_apps(_release_type) do
  266. MapSet.new([
  267. :emqx_connector_aggregator,
  268. :emqx_bridge_kafka,
  269. :emqx_bridge_confluent,
  270. :emqx_bridge_gcp_pubsub,
  271. :emqx_bridge_cassandra,
  272. :emqx_bridge_opents,
  273. :emqx_bridge_dynamo,
  274. :emqx_bridge_greptimedb,
  275. :emqx_bridge_hstreamdb,
  276. :emqx_bridge_influxdb,
  277. :emqx_bridge_iotdb,
  278. :emqx_bridge_es,
  279. :emqx_bridge_matrix,
  280. :emqx_bridge_mongodb,
  281. :emqx_bridge_mysql,
  282. :emqx_bridge_pgsql,
  283. :emqx_bridge_redis,
  284. :emqx_bridge_rocketmq,
  285. :emqx_bridge_tdengine,
  286. :emqx_bridge_timescale,
  287. :emqx_bridge_sqlserver,
  288. :emqx_bridge_pulsar,
  289. :emqx_oracle,
  290. :emqx_bridge_oracle,
  291. :emqx_bridge_rabbitmq,
  292. :emqx_bridge_clickhouse,
  293. :emqx_ft,
  294. :emqx_license,
  295. :emqx_s3,
  296. :emqx_bridge_s3,
  297. :emqx_schema_registry,
  298. :emqx_schema_validation,
  299. :emqx_message_transformation,
  300. :emqx_enterprise,
  301. :emqx_bridge_kinesis,
  302. :emqx_bridge_azure_event_hub,
  303. :emqx_gcp_device,
  304. :emqx_dashboard_rbac,
  305. :emqx_dashboard_sso,
  306. :emqx_audit,
  307. :emqx_gateway_gbt32960,
  308. :emqx_gateway_ocpp,
  309. :emqx_gateway_jt808,
  310. :emqx_bridge_syskeeper,
  311. :emqx_auth_ext
  312. ])
  313. end
  314. defp enterprise_deps(_profile_info = %{edition_type: :enterprise}) do
  315. [
  316. {:hstreamdb_erl,
  317. github: "hstreamdb/hstreamdb_erl", tag: "0.5.18+v0.18.1+ezstd-v1.0.5-emqx1"},
  318. {:influxdb, github: "emqx/influxdb-client-erl", tag: "1.1.13", override: true},
  319. {:wolff, github: "kafka4beam/wolff", tag: "2.0.0"},
  320. {:kafka_protocol, github: "kafka4beam/kafka_protocol", tag: "4.1.5", override: true},
  321. {:brod_gssapi, github: "kafka4beam/brod_gssapi", tag: "v0.1.1"},
  322. {:brod, github: "kafka4beam/brod", tag: "3.18.0"},
  323. {:snappyer, "1.2.9", override: true},
  324. {:crc32cer, "0.1.8", override: true},
  325. {:opentsdb, github: "emqx/opentsdb-client-erl", tag: "v0.5.1", override: true},
  326. {:greptimedb,
  327. github: "GreptimeTeam/greptimedb-ingester-erl", tag: "v0.1.8", override: true},
  328. # The following two are dependencies of rabbit_common. They are needed here to
  329. # make mix not complain about conflicting versions
  330. {:thoas, github: "emqx/thoas", tag: "v1.0.0", override: true},
  331. {:credentials_obfuscation,
  332. github: "emqx/credentials-obfuscation", tag: "v3.2.0", override: true},
  333. {:rabbit_common,
  334. github: "emqx/rabbitmq-server",
  335. tag: "v3.11.13.2",
  336. sparse: "deps/rabbit_common",
  337. override: true},
  338. {:amqp_client,
  339. github: "emqx/rabbitmq-server",
  340. tag: "v3.11.13.2",
  341. sparse: "deps/amqp_client",
  342. override: true}
  343. ]
  344. end
  345. defp enterprise_deps(_profile_info) do
  346. []
  347. end
  348. defp set_emqx_app_system_env(apps, profile_info, version) do
  349. system_env = emqx_app_system_env(profile_info, version) ++ maybe_no_quic_env()
  350. Enum.map(
  351. apps,
  352. fn {app, opts} ->
  353. {app,
  354. Keyword.update(
  355. opts,
  356. :system_env,
  357. system_env,
  358. &Keyword.merge(&1, system_env)
  359. )}
  360. end
  361. )
  362. end
  363. ###############################################################################################
  364. # END DEPRECATED FOR MIX BLOCK
  365. ###############################################################################################
  366. def emqx_app_system_env(profile_info, version) do
  367. erlc_options(profile_info, version)
  368. |> dump_as_erl()
  369. |> then(&[{"ERL_COMPILER_OPTIONS", &1}])
  370. end
  371. defp erlc_options(%{edition_type: edition_type}, version) do
  372. [
  373. :debug_info,
  374. {:compile_info, [{:emqx_vsn, String.to_charlist(version)}]},
  375. {:d, :EMQX_RELEASE_EDITION, erlang_edition(edition_type)},
  376. {:d, :EMQX_ELIXIR},
  377. {:d, :snk_kind, :msg}
  378. ] ++
  379. singleton(test_env?(), {:d, :TEST}) ++
  380. singleton(not enable_quicer?(), {:d, :BUILD_WITHOUT_QUIC})
  381. end
  382. defp singleton(false, _value), do: []
  383. defp singleton(true, value), do: [value]
  384. def profile_info() do
  385. k = {__MODULE__, :profile_info}
  386. get_memoized(k, &check_profile!/0)
  387. end
  388. def pkg_vsn() do
  389. k = {__MODULE__, :pkg_vsn}
  390. get_memoized(k, &do_pkg_vsn/0)
  391. end
  392. def common_deps() do
  393. if test_env?() do
  394. [
  395. {:bbmustache, "1.10.0"},
  396. {:cth_readable, "1.5.1"},
  397. {:proper, "1.4.0"},
  398. {:meck, "0.9.2"}
  399. ]
  400. else
  401. []
  402. end
  403. end
  404. def extra_applications() do
  405. k = {__MODULE__, :extra_applications}
  406. get_memoized(k, fn ->
  407. if test_env?() do
  408. [:eunit, :common_test, :dialyzer, :mnesia]
  409. else
  410. []
  411. end
  412. end)
  413. end
  414. def erlc_paths() do
  415. k = {__MODULE__, :erlc_paths}
  416. get_memoized(k, fn ->
  417. if test_env?() do
  418. ["src", "test"]
  419. else
  420. ["src"]
  421. end
  422. end)
  423. end
  424. def erlc_options() do
  425. k = {__MODULE__, :erlc_options}
  426. get_memoized(k, fn ->
  427. profile_info = profile_info()
  428. version = pkg_vsn()
  429. erlc_options(profile_info, version)
  430. end)
  431. end
  432. def test_env?() do
  433. k = {__MODULE__, :test_env?}
  434. get_memoized(k, fn ->
  435. env = to_string(Mix.env())
  436. System.get_env("TEST") == "1" || env =~ ~r/-test$/
  437. end)
  438. end
  439. defp set_test_env!(test_env?) do
  440. k = {__MODULE__, :test_env?}
  441. :persistent_term.put(k, test_env?)
  442. end
  443. defp get_memoized(k, compute_fn) do
  444. case :persistent_term.get(k, :undefined) do
  445. :undefined ->
  446. res = compute_fn.()
  447. :persistent_term.put(k, res)
  448. res
  449. res ->
  450. res
  451. end
  452. end
  453. def maybe_no_quic_env() do
  454. if not enable_quicer?() do
  455. [{"BUILD_WITHOUT_QUIC", "true"}]
  456. else
  457. []
  458. end
  459. end
  460. defp releases() do
  461. [
  462. emqx: fn ->
  463. %{
  464. release_type: release_type,
  465. package_type: package_type,
  466. edition_type: edition_type
  467. } = check_profile!()
  468. base_steps = [
  469. &make_docs/1,
  470. :assemble,
  471. &create_RELEASES/1,
  472. &copy_files(&1, release_type, package_type, edition_type),
  473. &copy_escript(&1, "nodetool"),
  474. &copy_escript(&1, "install_upgrade.escript")
  475. ]
  476. steps =
  477. if System.get_env("ELIXIR_MAKE_TAR") == "yes" do
  478. base_steps ++ [&prepare_tar_overlays/1, :tar]
  479. else
  480. base_steps
  481. end
  482. [
  483. applications: applications(release_type, edition_type),
  484. skip_mode_validation_for: [
  485. :emqx_mix,
  486. :emqx_machine,
  487. :emqx_gateway,
  488. :emqx_gateway_stomp,
  489. :emqx_gateway_mqttsn,
  490. :emqx_gateway_coap,
  491. :emqx_gateway_lwm2m,
  492. :emqx_gateway_exproto,
  493. :emqx_dashboard,
  494. :emqx_dashboard_sso,
  495. :emqx_audit,
  496. :emqx_resource,
  497. :emqx_connector,
  498. :emqx_exhook,
  499. :emqx_bridge,
  500. :emqx_bridge_mqtt,
  501. :emqx_modules,
  502. :emqx_management,
  503. :emqx_retainer,
  504. :emqx_prometheus,
  505. :emqx_rule_engine,
  506. :emqx_auto_subscribe,
  507. :emqx_slow_subs,
  508. :emqx_plugins,
  509. :emqx_ft,
  510. :emqx_s3,
  511. :emqx_opentelemetry,
  512. :emqx_durable_storage,
  513. :rabbit_common,
  514. :emqx_eviction_agent,
  515. :emqx_node_rebalance
  516. ],
  517. steps: steps,
  518. strip_beams: false
  519. ]
  520. end
  521. ]
  522. end
  523. def applications(release_type, edition_type) do
  524. {:ok,
  525. [
  526. %{
  527. db_apps: db_apps,
  528. system_apps: system_apps,
  529. common_business_apps: common_business_apps,
  530. ee_business_apps: ee_business_apps,
  531. ce_business_apps: ce_business_apps
  532. }
  533. ]} = :file.consult("apps/emqx_machine/priv/reboot_lists.eterm")
  534. edition_specific_apps =
  535. if edition_type == :enterprise do
  536. ee_business_apps
  537. else
  538. ce_business_apps
  539. end
  540. business_apps = common_business_apps ++ edition_specific_apps
  541. excluded_apps = excluded_apps(release_type)
  542. system_apps =
  543. Enum.map(system_apps, fn app ->
  544. if is_atom(app), do: {app, :permanent}, else: app
  545. end)
  546. db_apps = Enum.map(db_apps, &{&1, :load})
  547. business_apps = Enum.map(business_apps, &{&1, :load})
  548. [system_apps, db_apps, [emqx_ctl: :permanent, emqx_machine: :permanent], business_apps]
  549. |> List.flatten()
  550. |> Keyword.reject(fn {app, _type} ->
  551. app in excluded_apps ||
  552. (edition_type == :enterprise && app == :emqx_telemetry)
  553. end)
  554. end
  555. defp excluded_apps(_release_type) do
  556. %{
  557. mnesia_rocksdb: enable_rocksdb?(),
  558. quicer: enable_quicer?(),
  559. jq: enable_jq?(),
  560. observer: is_app?(:observer)
  561. }
  562. |> Enum.reject(&elem(&1, 1))
  563. |> Enum.map(&elem(&1, 0))
  564. end
  565. defp is_app?(name) do
  566. case Application.load(name) do
  567. :ok ->
  568. true
  569. {:error, {:already_loaded, _}} ->
  570. true
  571. _ ->
  572. false
  573. end
  574. end
  575. def check_profile!() do
  576. valid_envs = [
  577. :emqx,
  578. :"emqx-test",
  579. :"emqx-pkg",
  580. :"emqx-enterprise",
  581. :"emqx-enterprise-test",
  582. :"emqx-enterprise-pkg"
  583. ]
  584. if Mix.env() == :dev do
  585. env_profile = System.get_env("PROFILE")
  586. if env_profile do
  587. # copy from PROFILE env var
  588. System.get_env("PROFILE")
  589. |> String.to_atom()
  590. |> Mix.env()
  591. else
  592. Mix.shell().info([
  593. :yellow,
  594. "Warning: env var PROFILE is unset; defaulting to emqx"
  595. ])
  596. Mix.env(:emqx)
  597. end
  598. end
  599. if Mix.env() not in valid_envs do
  600. formatted_envs =
  601. valid_envs
  602. |> Enum.map(&" * #{&1}")
  603. |> Enum.join("\n")
  604. Mix.raise("""
  605. Invalid env #{Mix.env()}. Valid options are:
  606. #{formatted_envs}
  607. """)
  608. end
  609. mix_env = Mix.env()
  610. {
  611. release_type,
  612. package_type,
  613. edition_type
  614. } =
  615. case mix_env do
  616. :dev ->
  617. {:standard, :bin, :community}
  618. :emqx ->
  619. {:standard, :bin, :community}
  620. :"emqx-test" ->
  621. {:standard, :bin, :community}
  622. :"emqx-enterprise" ->
  623. {:standard, :bin, :enterprise}
  624. :"emqx-enterprise-test" ->
  625. {:standard, :bin, :enterprise}
  626. :"emqx-pkg" ->
  627. {:standard, :pkg, :community}
  628. :"emqx-enterprise-pkg" ->
  629. {:standard, :pkg, :enterprise}
  630. end
  631. test? = to_string(mix_env) =~ ~r/-test$/ || test_env?()
  632. normalize_env!(test?)
  633. # Mix.debug(true)
  634. if Mix.debug?() do
  635. Mix.shell().info([
  636. :blue,
  637. "mix_env: #{Mix.env()}",
  638. "; release type: #{release_type}",
  639. "; package type: #{package_type}",
  640. "; edition type: #{edition_type}",
  641. "; test env?: #{test?}"
  642. ])
  643. end
  644. test? = to_string(mix_env) =~ ~r/-test$/ || test_env?()
  645. normalize_env!(test?)
  646. # Mix.debug(true)
  647. if Mix.debug?() do
  648. Mix.shell().info([
  649. :blue,
  650. "mix_env: #{Mix.env()}",
  651. "; release type: #{release_type}",
  652. "; package type: #{package_type}",
  653. "; edition type: #{edition_type}",
  654. "; test env?: #{test?}"
  655. ])
  656. end
  657. %{
  658. release_type: release_type,
  659. package_type: package_type,
  660. edition_type: edition_type,
  661. test?: test?
  662. }
  663. end
  664. #############################################################################
  665. # Custom Steps
  666. #############################################################################
  667. defp make_docs(release) do
  668. profile = System.get_env("MIX_ENV")
  669. os_cmd("build", [profile, "docs"])
  670. release
  671. end
  672. defp copy_files(release, release_type, package_type, edition_type) do
  673. overwrite? = Keyword.get(release.options, :overwrite, false)
  674. bin = Path.join(release.path, "bin")
  675. etc = Path.join(release.path, "etc")
  676. log = Path.join(release.path, "log")
  677. plugins = Path.join(release.path, "plugins")
  678. Mix.Generator.create_directory(bin)
  679. Mix.Generator.create_directory(etc)
  680. Mix.Generator.create_directory(log)
  681. Mix.Generator.create_directory(plugins)
  682. Mix.Generator.create_directory(Path.join(etc, "certs"))
  683. Enum.each(
  684. ["mnesia", "configs", "patches", "scripts"],
  685. fn dir ->
  686. path = Path.join([release.path, "data", dir])
  687. Mix.Generator.create_directory(path)
  688. end
  689. )
  690. Mix.Generator.copy_file(
  691. "apps/emqx_auth/etc/acl.conf",
  692. Path.join(etc, "acl.conf"),
  693. force: overwrite?
  694. )
  695. # required by emqx_auth
  696. File.cp_r!(
  697. "apps/emqx/etc/certs",
  698. Path.join(etc, "certs")
  699. )
  700. profile = System.get_env("MIX_ENV")
  701. File.cp_r!(
  702. "rel/config/examples",
  703. Path.join(etc, "examples"),
  704. force: overwrite?
  705. )
  706. # copy /rel/config/ee-examples if profile is enterprise
  707. case profile do
  708. "emqx-enterprise" ->
  709. File.cp_r!(
  710. "rel/config/ee-examples",
  711. Path.join(etc, "examples"),
  712. force: overwrite?
  713. )
  714. _ ->
  715. :ok
  716. end
  717. # this is required by the produced escript / nodetool
  718. Mix.Generator.copy_file(
  719. Path.join(release.version_path, "start_clean.boot"),
  720. Path.join(bin, "no_dot_erlang.boot"),
  721. force: overwrite?
  722. )
  723. assigns = template_vars(release, release_type, package_type, edition_type)
  724. # This is generated by `scripts/merge-config.escript` or `make merge-config`
  725. # So, this should be run before the release.
  726. # TODO: run as a "compiler" step???
  727. render_template(
  728. "apps/emqx_conf/etc/emqx.conf.all",
  729. assigns,
  730. Path.join(etc, "emqx.conf")
  731. )
  732. render_template(
  733. "rel/emqx_vars",
  734. assigns,
  735. Path.join([release.path, "releases", "emqx_vars"])
  736. )
  737. vm_args_template_path =
  738. case release_type do
  739. _ ->
  740. "apps/emqx/etc/vm.args.cloud"
  741. end
  742. render_template(
  743. vm_args_template_path,
  744. assigns,
  745. [
  746. Path.join(etc, "vm.args"),
  747. Path.join(release.version_path, "vm.args")
  748. ]
  749. )
  750. for name <- [
  751. "emqx",
  752. "emqx_ctl"
  753. ] do
  754. Mix.Generator.copy_file(
  755. "bin/#{name}",
  756. Path.join(bin, name),
  757. force: overwrite?
  758. )
  759. # Files with the version appended are expected by the release
  760. # upgrade script `install_upgrade.escript`
  761. Mix.Generator.copy_file(
  762. Path.join(bin, name),
  763. Path.join(bin, name <> "-#{release.version}"),
  764. force: overwrite?
  765. )
  766. end
  767. for base_name <- ["emqx", "emqx_ctl"],
  768. suffix <- ["", "-#{release.version}"] do
  769. name = base_name <> suffix
  770. File.chmod!(Path.join(bin, name), 0o755)
  771. end
  772. Mix.Generator.copy_file(
  773. "bin/node_dump",
  774. Path.join(bin, "node_dump"),
  775. force: overwrite?
  776. )
  777. File.chmod!(Path.join(bin, "node_dump"), 0o755)
  778. Mix.Generator.copy_file(
  779. "bin/emqx_cluster_rescue",
  780. Path.join(bin, "emqx_cluster_rescue"),
  781. force: overwrite?
  782. )
  783. File.chmod!(Path.join(bin, "emqx_cluster_rescue"), 0o755)
  784. render_template(
  785. "rel/BUILD_INFO",
  786. assigns,
  787. Path.join(release.version_path, "BUILD_INFO")
  788. )
  789. release
  790. end
  791. defp render_template(template, assigns, target) when is_binary(target) do
  792. render_template(template, assigns, [target])
  793. end
  794. defp render_template(template, assigns, tartgets) when is_list(tartgets) do
  795. rendered =
  796. File.read!(template)
  797. |> from_rebar_to_eex_template()
  798. |> EEx.eval_string(assigns)
  799. for target <- tartgets do
  800. File.write!(target, rendered)
  801. end
  802. end
  803. # needed by nodetool and by release_handler
  804. defp create_RELEASES(release) do
  805. apps =
  806. Enum.map(release.applications, fn {app_name, app_props} ->
  807. app_vsn = Keyword.fetch!(app_props, :vsn)
  808. app_path =
  809. "./lib"
  810. |> Path.join("#{app_name}-#{app_vsn}")
  811. |> to_charlist()
  812. {app_name, app_vsn, app_path}
  813. end)
  814. release_entry = [
  815. {
  816. :release,
  817. to_charlist(release.name),
  818. to_charlist(release.version),
  819. release.erts_version,
  820. apps,
  821. :permanent
  822. }
  823. ]
  824. release.path
  825. |> Path.join("releases")
  826. |> Path.join("RELEASES")
  827. |> File.open!([:write, :utf8], fn handle ->
  828. IO.puts(handle, "%% coding: utf-8")
  829. :io.format(handle, ~c"~tp.~n", [release_entry])
  830. end)
  831. release
  832. end
  833. defp copy_escript(release, escript_name) do
  834. [shebang, rest] =
  835. "bin/#{escript_name}"
  836. |> File.read!()
  837. |> String.split("\n", parts: 2)
  838. # the elixir version of escript + start.boot required the boot_var
  839. # RELEASE_LIB to be defined.
  840. # enable-feature is not required when 1.6.x
  841. boot_var = "%%!-boot_var RELEASE_LIB $RUNNER_ROOT_DIR/lib -enable-feature maybe_expr"
  842. # Files with the version appended are expected by the release
  843. # upgrade script `install_upgrade.escript`
  844. Enum.each(
  845. [escript_name, escript_name <> "-" <> release.version],
  846. fn name ->
  847. path = Path.join([release.path, "bin", name])
  848. File.write!(path, [shebang, "\n", boot_var, "\n", rest])
  849. end
  850. )
  851. release
  852. end
  853. # The `:tar` built-in step in Mix Release does not currently add the
  854. # `etc` directory into the resulting tarball. The workaround is to
  855. # add those to the `:overlays` key before running `:tar`.
  856. # See: https://hexdocs.pm/mix/1.13.4/Mix.Release.html#__struct__/0
  857. defp prepare_tar_overlays(release) do
  858. Map.update!(
  859. release,
  860. :overlays,
  861. &[
  862. "etc",
  863. "data",
  864. "plugins",
  865. "bin/node_dump"
  866. | &1
  867. ]
  868. )
  869. end
  870. #############################################################################
  871. # Helper functions
  872. #############################################################################
  873. defp template_vars(release, release_type, :bin = _package_type, edition_type) do
  874. [
  875. emqx_default_erlang_cookie: default_cookie(),
  876. emqx_configuration_doc: emqx_configuration_doc(edition_type, :root),
  877. emqx_configuration_doc_log: emqx_configuration_doc(edition_type, :log),
  878. platform_data_dir: "data",
  879. platform_etc_dir: "etc",
  880. platform_plugins_dir: "plugins",
  881. runner_bin_dir: "$RUNNER_ROOT_DIR/bin",
  882. emqx_etc_dir: "$RUNNER_ROOT_DIR/etc",
  883. runner_lib_dir: "$RUNNER_ROOT_DIR/lib",
  884. runner_log_dir: "$RUNNER_ROOT_DIR/log",
  885. runner_user: "",
  886. release_version: release.version,
  887. erts_vsn: release.erts_version,
  888. # FIXME: this is empty in `make emqx` ???
  889. erl_opts: "",
  890. emqx_description: emqx_description(release_type, edition_type),
  891. emqx_schema_mod: emqx_schema_mod(edition_type),
  892. is_elixir: "yes",
  893. is_enterprise: if(edition_type == :enterprise, do: "yes", else: "no")
  894. ] ++ build_info()
  895. end
  896. defp template_vars(release, release_type, :pkg = _package_type, edition_type) do
  897. [
  898. emqx_default_erlang_cookie: default_cookie(),
  899. emqx_configuration_doc: emqx_configuration_doc(edition_type, :root),
  900. emqx_configuration_doc_log: emqx_configuration_doc(edition_type, :log),
  901. platform_data_dir: "/var/lib/emqx",
  902. platform_etc_dir: "/etc/emqx",
  903. platform_plugins_dir: "/var/lib/emqx/plugins",
  904. runner_bin_dir: "/usr/bin",
  905. emqx_etc_dir: "/etc/emqx",
  906. runner_lib_dir: "$RUNNER_ROOT_DIR/lib",
  907. runner_log_dir: "/var/log/emqx",
  908. runner_user: "emqx",
  909. release_version: release.version,
  910. erts_vsn: release.erts_version,
  911. # FIXME: this is empty in `make emqx` ???
  912. erl_opts: "",
  913. emqx_description: emqx_description(release_type, edition_type),
  914. emqx_schema_mod: emqx_schema_mod(edition_type),
  915. is_elixir: "yes",
  916. is_enterprise: if(edition_type == :enterprise, do: "yes", else: "no")
  917. ] ++ build_info()
  918. end
  919. defp default_cookie() do
  920. "emqx50elixir"
  921. end
  922. defp emqx_description(release_type, edition_type) do
  923. case {release_type, edition_type} do
  924. {_, :enterprise} ->
  925. "EMQX Enterprise"
  926. {_, :community} ->
  927. "EMQX"
  928. end
  929. end
  930. defp emqx_configuration_doc(:enterprise, :root),
  931. do: "https://docs.emqx.com/en/enterprise/latest/configuration/configuration.html"
  932. defp emqx_configuration_doc(:enterprise, :log),
  933. do: "https://docs.emqx.com/en/enterprise/latest/configuration/logs.html"
  934. defp emqx_configuration_doc(:community, :root),
  935. do: "https://www.emqx.io/docs/en/latest/configuration/configuration.html"
  936. defp emqx_configuration_doc(:community, :log),
  937. do: "https://www.emqx.io/docs/en/latest/configuration/logs.html"
  938. defp emqx_schema_mod(:enterprise), do: :emqx_enterprise_schema
  939. defp emqx_schema_mod(:community), do: :emqx_conf_schema
  940. defp jq_dep() do
  941. if enable_jq?(),
  942. do: [{:jq, github: "emqx/jq", tag: "v0.3.12", override: true}],
  943. else: []
  944. end
  945. def quicer_dep() do
  946. if enable_quicer?(),
  947. # in conflict with emqx and emqtt
  948. do: [{:quicer, github: "emqx/quic", tag: "0.0.313", override: true}],
  949. else: []
  950. end
  951. defp enable_jq?() do
  952. not Enum.any?([
  953. build_without_jq?()
  954. ])
  955. end
  956. def enable_quicer?() do
  957. "1" == System.get_env("BUILD_WITH_QUIC") or
  958. not Enum.any?([
  959. macos?(),
  960. build_without_quic?()
  961. ])
  962. end
  963. defp enable_rocksdb?() do
  964. not Enum.any?([
  965. raspbian?(),
  966. build_without_rocksdb?()
  967. ])
  968. end
  969. defp do_pkg_vsn() do
  970. %{edition_type: edition_type} = check_profile!()
  971. basedir = Path.dirname(__ENV__.file)
  972. script = Path.join(basedir, "pkg-vsn.sh")
  973. os_cmd(script, [Atom.to_string(edition_type)])
  974. end
  975. defp os_cmd(script, args) do
  976. {str, 0} = System.cmd("bash", [script | args])
  977. String.trim(str)
  978. end
  979. def macos?() do
  980. {:unix, :darwin} == :os.type()
  981. end
  982. defp raspbian?() do
  983. os_cmd("./scripts/get-distro.sh", []) =~ "raspbian"
  984. end
  985. defp build_without_jq?() do
  986. opt = System.get_env("BUILD_WITHOUT_JQ", "false")
  987. String.downcase(opt) != "false"
  988. end
  989. def build_without_quic?() do
  990. opt = System.get_env("BUILD_WITHOUT_QUIC", "false")
  991. String.downcase(opt) != "false"
  992. end
  993. defp build_without_rocksdb?() do
  994. opt = System.get_env("BUILD_WITHOUT_ROCKSDB", "false")
  995. String.downcase(opt) != "false"
  996. end
  997. defp from_rebar_to_eex_template(str) do
  998. # we must not consider surrounding space in the template var name
  999. # because some help strings contain informative variables that
  1000. # should not be interpolated, and those have no spaces.
  1001. Regex.replace(
  1002. ~r/\{\{ ([a-zA-Z0-9_]+) \}\}/,
  1003. str,
  1004. "<%= \\g{1} %>"
  1005. )
  1006. end
  1007. defp build_info() do
  1008. [
  1009. build_info_arch: to_string(:erlang.system_info(:system_architecture)),
  1010. build_info_wordsize: wordsize(),
  1011. build_info_os: os_cmd("./scripts/get-distro.sh", []),
  1012. build_info_erlang: otp_release(),
  1013. build_info_elixir: System.version(),
  1014. build_info_relform: System.get_env("EMQX_REL_FORM", "tgz")
  1015. ]
  1016. end
  1017. # https://github.com/erlang/rebar3/blob/e3108ac187b88fff01eca6001a856283a3e0ec87/src/rebar_utils.erl#L142
  1018. defp wordsize() do
  1019. size =
  1020. try do
  1021. :erlang.system_info({:wordsize, :external})
  1022. rescue
  1023. ErlangError ->
  1024. :erlang.system_info(:wordsize)
  1025. end
  1026. to_string(8 * size)
  1027. end
  1028. defp normalize_env!(test_env?) do
  1029. env =
  1030. case Mix.env() do
  1031. :dev ->
  1032. :emqx
  1033. env ->
  1034. env
  1035. end
  1036. if test_env? do
  1037. ensure_test_mix_env!()
  1038. end
  1039. Mix.env(env)
  1040. end
  1041. # As from Erlang/OTP 17, the OTP release number corresponds to the
  1042. # major OTP version number. No erlang:system_info() argument gives
  1043. # the exact OTP version.
  1044. # https://www.erlang.org/doc/man/erlang.html#system_info_otp_release
  1045. # https://github.com/erlang/rebar3/blob/e3108ac187b88fff01eca6001a856283a3e0ec87/src/rebar_utils.erl#L572-L577
  1046. defp otp_release() do
  1047. major_version = System.otp_release()
  1048. root_dir = to_string(:code.root_dir())
  1049. [root_dir, "releases", major_version, "OTP_VERSION"]
  1050. |> Path.join()
  1051. |> File.read()
  1052. |> case do
  1053. {:error, _} ->
  1054. major_version
  1055. {:ok, version} ->
  1056. version
  1057. |> String.trim()
  1058. |> String.split("**")
  1059. |> List.first()
  1060. end
  1061. end
  1062. defp dump_as_erl(term) do
  1063. term
  1064. |> then(&:io_lib.format("~0p", [&1]))
  1065. |> :erlang.iolist_to_binary()
  1066. end
  1067. defp erlang_edition(:community), do: :ce
  1068. defp erlang_edition(:enterprise), do: :ee
  1069. defp aliases() do
  1070. [
  1071. ct: &do_ct/1,
  1072. eunit: &do_eunit/1,
  1073. proper: &do_proper/1
  1074. ]
  1075. end
  1076. defp do_ct(args) do
  1077. IO.inspect(args)
  1078. Mix.shell().info("testing")
  1079. ensure_test_mix_env!()
  1080. set_test_env!(true)
  1081. Code.require_file("lib/mix/tasks/emqx.ct.ex")
  1082. Mix.Task.run("emqx.ct", args)
  1083. end
  1084. defp do_eunit(args) do
  1085. ensure_test_mix_env!()
  1086. set_test_env!(true)
  1087. Code.require_file("lib/mix/tasks/emqx.eunit.ex")
  1088. Mix.Task.run("emqx.eunit", args)
  1089. end
  1090. defp do_proper(args) do
  1091. ensure_test_mix_env!()
  1092. set_test_env!(true)
  1093. Code.require_file("lib/mix/tasks/emqx.proper.ex")
  1094. Mix.Task.run("emqx.proper", args)
  1095. end
  1096. defp ensure_test_mix_env!() do
  1097. Mix.env()
  1098. |> to_string()
  1099. |> then(fn env ->
  1100. if String.ends_with?(env, "-test") do
  1101. env
  1102. else
  1103. env <> "-test"
  1104. end
  1105. end)
  1106. |> String.to_atom()
  1107. |> Mix.env()
  1108. end
  1109. end