Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow importing extra config #3906

Merged
merged 4 commits into from
Mar 19, 2024
Merged

Allow importing extra config #3906

merged 4 commits into from
Mar 19, 2024

Conversation

ruslandoga
Copy link
Contributor

@ruslandoga ruslandoga commented Mar 17, 2024

Changes

This PR allows importing extra config as suggested in #3705 (comment)

It can serve as an easy escape hatch for specifying configurations not covered by env vars. Like #3823

Example:

my_config.exs

import Config

config :plausible, Plausible.Repo,
  after_connect: {Postgrex, :query!, ["SET search_path TO global_prefix", []]}

docker-compose.yml

plausible:
  image: ghcr.io/plausible/community-edition:v2.1.0
+ volumes:
+   - ./my_config.exs:/app/my_config.exs

plausible-conf.env

+ EXTRA_CONFIG_PATH=/app/my_config.exs

Tests

  • Automated tests have been added

Changelog

  • Entry has been added to changelog

Documentation

Dark mode

  • This PR does not change the UI

@@ -19,8 +19,10 @@ defmodule Plausible.MixProject do
releases: [
plausible: [
include_executables_for: [:unix],
applications: [plausible: :permanent],
steps: [:assemble, :tar]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are copying all rel/ files directly, the tar archive ends up being unused.

config_dir = System.get_env("CONFIG_DIR", "/run/secrets")

if extra_config_path = get_var_from_path_or_env(config_dir, "EXTRA_CONIFG_PATH") do
import_config extra_config_path
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This intermediate config file is needed as runtime.exs is forbidden from importing other configs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From https://hexdocs.pm/mix/Mix.Tasks.Release.html#module-runtime-configuration

It MUST NOT import any other configuration file via import_config

@@ -19,8 +19,10 @@ defmodule Plausible.MixProject do
releases: [
plausible: [
include_executables_for: [:unix],
applications: [plausible: :permanent],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is part of the default applications

@ruslandoga ruslandoga marked this pull request as ready for review March 17, 2024 22:48
@ruslandoga ruslandoga requested a review from a team March 17, 2024 22:48
{Plausible.Repo,
[after_connect: {Postgrex, :query!, ["SET search_path TO global_prefix", []]}]}
]}
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also tried it out locally:

docker run --rm -d \
    -v ./my_config.exs:/app/my_config.exs \
    -e EXTRA_CONFIG_PATH=my_config.exs \
    -e BASE_URL=http://localhost:8000 \
    -e SECRET_KEY_BASE=FQ9iAQSkUx4peZfjRT9AIcX2iUupTYIPH90GbRS42NDUYQUcVzaTEARzK8NBRpVO \
    -e TOTP_VAULT_KEY=ykRRyEnKQX8jJ0zrE3rn91U0jluSsnevOGnQO2fTYUg= \
    -e DATABASE_URL=ecto://postgres:postgres@172.17.0.2:5432/plausible_dev \
    -e CLICKHOUSE_DATABASE_URL=http://172.17.0.3:8123/plausible_events_db \
    plausible:local
$ ls
bin                      erts-14.2.1              init-admin.sh            migrate.sh               pending-migrations.sh    rollback.sh
createdb.sh              import_extra_config.exs  lib                      my_config.exs            releases                 seed.sh
$ cat my_config.exs 
import Config

config :plausible, Plausible.Repo,
  after_connect: {Postgrex, :query!, ["SET search_path TO public", []]}

config :plausible, a: :b
$ bin/plausible remote
Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Interactive Elixir (1.16.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(plausible@7f241994598d)1> Application.get_env :plausible, Plausible.Repo
[
  timeout: 300000,
  connect_timeout: 300000,
  handshake_timeout: 300000,
  url: "ecto://postgres:postgres@172.17.0.2:5432/plausible_dev",
  socket_options: [],
  ssl_opts: [
    cacertfile: "/app/lib/castore-1.0.5/priv/cacerts.pem",
    verify: :verify_peer,
    customize_hostname_check: [
      match_fun: #Function<6.117093159/2 in :public_key.pkix_verify_hostname_match_fun/1>
    ]
  ],
  after_connect: {Postgrex, :query!, ["SET search_path TO public", []]}
]
iex(plausible@7f241994598d)2> Application.get_env :plausible, :a
:b

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't come up with a simple test that would check that extra config overrides runtime.exs, but it does.

@ruslandoga ruslandoga added the self-hosting Anything self-hosted label Mar 18, 2024
@zoldar zoldar added the deploy-to-staging Special label that triggers a deploy to a staging environment label Mar 19, 2024
@zoldar zoldar changed the title allow importing extra config Allow importing extra config Mar 19, 2024
@zoldar zoldar merged commit 4242b52 into master Mar 19, 2024
9 checks passed
@zoldar zoldar deleted the allow-extra-config branch March 19, 2024 11:02
@ruslandoga ruslandoga mentioned this pull request Mar 20, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deploy-to-staging Special label that triggers a deploy to a staging environment self-hosting Anything self-hosted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants