Fix prod admin initialisation #410

Merged
moritz merged 1 commit from fix/admin_init into main 2026-02-04 21:41:40 +01:00

View file

@ -34,6 +34,9 @@ defmodule Mv.Release do
@doc """ @doc """
Ensures an admin user exists from ENV (ADMIN_EMAIL, ADMIN_PASSWORD or ADMIN_PASSWORD_FILE). Ensures an admin user exists from ENV (ADMIN_EMAIL, ADMIN_PASSWORD or ADMIN_PASSWORD_FILE).
Starts the application if not already running (required when called via `bin/mv eval`;
Ash/Telemetry need the running app). Idempotent.
- If ADMIN_EMAIL is unset: no-op (idempotent). - If ADMIN_EMAIL is unset: no-op (idempotent).
- If ADMIN_PASSWORD (and ADMIN_PASSWORD_FILE) are unset and the user does not exist: - If ADMIN_PASSWORD (and ADMIN_PASSWORD_FILE) are unset and the user does not exist:
no user is created (no fallback password in production). no user is created (no fallback password in production).
@ -42,7 +45,11 @@ defmodule Mv.Release do
`bin/mv eval "Mv.Release.seed_admin()"` to change the admin password without redeploying. `bin/mv eval "Mv.Release.seed_admin()"` to change the admin password without redeploying.
""" """
def seed_admin do def seed_admin do
load_app() # Ensure app (and Telemetry/Ash deps) are started when run via bin/mv eval
case Application.ensure_all_started(@app) do
{:ok, _} -> :ok
{:error, {app, reason}} -> raise "Failed to start #{inspect(app)}: #{inspect(reason)}"
end
admin_email = get_env("ADMIN_EMAIL", nil) admin_email = get_env("ADMIN_EMAIL", nil)
admin_password = get_env_or_file("ADMIN_PASSWORD", nil) admin_password = get_env_or_file("ADMIN_PASSWORD", nil)