From 4242b52be4c043837f8555bc1ab497d4267dc7b1 Mon Sep 17 00:00:00 2001 From: ruslandoga Date: Tue, 19 Mar 2024 19:02:52 +0800 Subject: [PATCH] Allow importing extra config (#3906) * allow importing extra config * changelog * fix typo * add test --- CHANGELOG.md | 1 + mix.exs | 6 +++-- rel/overlays/import_extra_config.exs | 8 ++++++ test/plausible/config_test.exs | 38 ++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 rel/overlays/import_extra_config.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c776df3a..5e0c99d44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ All notable changes to this project will be documented in this file. - Add 'browser_versions.csv' to CSV export - Add `CLICKHOUSE_MAX_BUFFER_SIZE_BYTES` env var which defaults to `100000` (100KB) - Add alternative SMTP adapter plausible/analytics#3654 +- Add `EXTRA_CONFIG_PATH` env var to specify extra Elixir config plausible/analytics#3906 ### Removed - Removed the nested custom event property breakdown UI when filtering by a goal in Goal Conversions diff --git a/mix.exs b/mix.exs index 1700af5da..b41c57dfb 100644 --- a/mix.exs +++ b/mix.exs @@ -19,8 +19,10 @@ defmodule Plausible.MixProject do releases: [ plausible: [ include_executables_for: [:unix], - applications: [plausible: :permanent], - steps: [:assemble, :tar] + config_providers: [ + {Config.Reader, + path: {:system, "RELEASE_ROOT", "/import_extra_config.exs"}, imports: []} + ] ] ], dialyzer: [ diff --git a/rel/overlays/import_extra_config.exs b/rel/overlays/import_extra_config.exs new file mode 100644 index 000000000..5e22e47f9 --- /dev/null +++ b/rel/overlays/import_extra_config.exs @@ -0,0 +1,8 @@ +import Config +import Plausible.ConfigHelpers + +config_dir = System.get_env("CONFIG_DIR", "/run/secrets") + +if extra_config_path = get_var_from_path_or_env(config_dir, "EXTRA_CONFIG_PATH") do + import_config extra_config_path +end diff --git a/test/plausible/config_test.exs b/test/plausible/config_test.exs index 5ba0005bf..1adb30a6d 100644 --- a/test/plausible/config_test.exs +++ b/test/plausible/config_test.exs @@ -272,6 +272,44 @@ defmodule Plausible.ConfigTest do end end + describe "extra config" do + test "no-op when no extra path is set" do + put_system_env_undo({"EXTRA_CONFIG_PATH", nil}) + + assert Config.Reader.read!("rel/overlays/import_extra_config.exs") == [] + end + + test "raises if path is invalid" do + put_system_env_undo({"EXTRA_CONFIG_PATH", "no-such-file"}) + + assert_raise File.Error, ~r/could not read file/, fn -> + Config.Reader.read!("rel/overlays/import_extra_config.exs") + end + end + + @tag :tmp_dir + test "reads extra config", %{tmp_dir: tmp_dir} do + extra_config_path = Path.join(tmp_dir, "config.exs") + + File.write!(extra_config_path, """ + import Config + + config :plausible, Plausible.Repo, + after_connect: {Postgrex, :query!, ["SET search_path TO global_prefix", []]} + """) + + put_system_env_undo({"EXTRA_CONFIG_PATH", extra_config_path}) + + assert Config.Reader.read!("rel/overlays/import_extra_config.exs") == [ + {:plausible, + [ + {Plausible.Repo, + [after_connect: {Postgrex, :query!, ["SET search_path TO global_prefix", []]}]} + ]} + ] + end + end + defp runtime_config(env) do put_system_env_undo(env) Config.Reader.read!("config/runtime.exs", env: :prod)