diff --git a/apps/emqx_config_helper/.formatter.exs b/apps/emqx_config_helper/.formatter.exs new file mode 100644 index 000000000..d2cda26ed --- /dev/null +++ b/apps/emqx_config_helper/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/apps/emqx_config_helper/.gitignore b/apps/emqx_config_helper/.gitignore new file mode 100644 index 000000000..f2013c094 --- /dev/null +++ b/apps/emqx_config_helper/.gitignore @@ -0,0 +1,26 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +emqx_config_helper-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/apps/emqx_config_helper/README.md b/apps/emqx_config_helper/README.md new file mode 100644 index 000000000..d4b3b9723 --- /dev/null +++ b/apps/emqx_config_helper/README.md @@ -0,0 +1,21 @@ +# EmqxConfigHelper + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `emqx_config_helper` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:emqx_config_helper, "~> 0.1.0"} + ] +end +``` + +Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) +and published on [HexDocs](https://hexdocs.pm). Once published, the docs can +be found at [https://hexdocs.pm/emqx_config_helper](https://hexdocs.pm/emqx_config_helper). + diff --git a/apps/emqx_config_helper/lib/emqx_config_helper.ex b/apps/emqx_config_helper/lib/emqx_config_helper.ex new file mode 100644 index 000000000..1f2e0781d --- /dev/null +++ b/apps/emqx_config_helper/lib/emqx_config_helper.ex @@ -0,0 +1,2 @@ +defmodule EmqxConfigHelper do +end diff --git a/apps/emqx_config_helper/lib/emqx_config_helper/hocon.ex b/apps/emqx_config_helper/lib/emqx_config_helper/hocon.ex new file mode 100644 index 000000000..2d8965153 --- /dev/null +++ b/apps/emqx_config_helper/lib/emqx_config_helper/hocon.ex @@ -0,0 +1,35 @@ +defmodule EmqxConfigHelper.Hocon do + def hocon(_application, options) do + {:ok, config} = + options + |> Keyword.fetch!(:config_file) + |> List.wrap() + |> Enum.map(&to_charlist/1) + |> :hocon.files(%{format: :richmap}) + + options + |> Keyword.fetch!(:schema_module) + |> :hocon_schema.check(config, %{atom_key: true, return_plain: true}) + |> IO.inspect + |> Enum.each(fn {application, envs} -> + Config.config(application, Enum.to_list(envs)) + end) + end +end + +defmodule EmqxConfigHelper.Cuttlefish do + def cuttlefish(application, options) do + config = + options + |> Keyword.fetch!(:config_file) + |> :cuttlefish_conf.file() + + application + |> :code.priv_dir() + |> Path.join("#{application}.schema") + |> List.wrap() + |> :cuttlefish_schema.files() + |> :cuttlefish_generator.map(config) + |> Enum.each(fn {application, envs} -> Config.config(application, envs) end) + end +end diff --git a/apps/emqx_config_helper/mix.exs b/apps/emqx_config_helper/mix.exs new file mode 100644 index 000000000..90ac774e9 --- /dev/null +++ b/apps/emqx_config_helper/mix.exs @@ -0,0 +1,30 @@ +defmodule EmqxConfigHelper.MixProject do + use Mix.Project + + def project do + [ + app: :emqx_config_helper, + version: "0.1.0", + build_path: "../../_build", + config_path: "../../config/config.exs", + deps_path: "../../deps", + lockfile: "../../mix.lock", + elixir: "~> 1.12", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + def application do + [ + extra_applications: [:logger] + ] + end + + defp deps do + [ + {:cuttlefish, github: "emqx/cuttlefish", tag: "v4.0.1"}, + {:hocon, github: "emqx/hocon"} + ] + end +end diff --git a/apps/emqx_config_helper/test/emqx_config_helper_test.exs b/apps/emqx_config_helper/test/emqx_config_helper_test.exs new file mode 100644 index 000000000..0aa7a37b6 --- /dev/null +++ b/apps/emqx_config_helper/test/emqx_config_helper_test.exs @@ -0,0 +1,8 @@ +defmodule EmqxConfigHelperTest do + use ExUnit.Case + doctest EmqxConfigHelper + + test "greets the world" do + assert EmqxConfigHelper.hello() == :world + end +end diff --git a/apps/emqx_config_helper/test/test_helper.exs b/apps/emqx_config_helper/test/test_helper.exs new file mode 100644 index 000000000..869559e70 --- /dev/null +++ b/apps/emqx_config_helper/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() diff --git a/apps/emqx_release_helper/lib/emqx_release_helper/applications.ex b/apps/emqx_release_helper/lib/emqx_release_helper/applications.ex index 6fef24b82..bbee9dcd0 100644 --- a/apps/emqx_release_helper/lib/emqx_release_helper/applications.ex +++ b/apps/emqx_release_helper/lib/emqx_release_helper/applications.ex @@ -100,6 +100,10 @@ defmodule EmqxReleaseHelper.Applications do overlay :plugin end + application :emqx_config_helper do + start_type :load + end + application :emqx_exhook, %{release_type: :cloud} do start_type :load overlay :plugin diff --git a/config/runtime.exs b/config/runtime.exs index ce6c2eb46..7811aefb7 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -1,5 +1,13 @@ import Config +import EmqxConfigHelper.Hocon +import EmqxConfigHelper.Cuttlefish -File.cwd! |> IO.inspect +hocon :emqx_prometheus, + schema_module: :emqx_prometheus_schema, + config_file: "etc/plugins/emqx_prometheus.conf" + +cuttlefish :emqx_sn, + schema_file: "emqx_sn.schema", + config_file: "etc/plugins/emqx_sn.conf" config :mnesia, dir: '/tmp/mnesia'