forked from hiway/freedive
Add mix release, commands to create user and reset password.
This commit is contained in:
parent
f430ef8a34
commit
a980dca4e5
4 changed files with 165 additions and 3 deletions
|
@ -2,17 +2,22 @@ defmodule Freedive.Application do
|
||||||
# See https://hexdocs.pm/elixir/Application.html
|
# See https://hexdocs.pm/elixir/Application.html
|
||||||
# for more information on OTP Applications
|
# for more information on OTP Applications
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
@app :freedive
|
||||||
|
|
||||||
use Application
|
use Application
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def start(_type, _args) do
|
def start(_type, _args) do
|
||||||
children = [
|
minimal = Application.get_env(@app, :minimal)
|
||||||
|
|
||||||
|
app_minimal = [
|
||||||
FreediveWeb.Telemetry,
|
FreediveWeb.Telemetry,
|
||||||
Freedive.Repo,
|
Freedive.Repo,
|
||||||
{Ecto.Migrator,
|
{Ecto.Migrator,
|
||||||
repos: Application.fetch_env!(:freedive, :ecto_repos),
|
repos: Application.fetch_env!(:freedive, :ecto_repos), skip: skip_migrations?()}
|
||||||
skip: skip_migrations?()},
|
]
|
||||||
|
|
||||||
|
app_features = [
|
||||||
{DNSCluster, query: Application.get_env(:freedive, :dns_cluster_query) || :ignore},
|
{DNSCluster, query: Application.get_env(:freedive, :dns_cluster_query) || :ignore},
|
||||||
{Phoenix.PubSub, name: Freedive.PubSub},
|
{Phoenix.PubSub, name: Freedive.PubSub},
|
||||||
# Start the Finch HTTP client for sending emails
|
# Start the Finch HTTP client for sending emails
|
||||||
|
@ -23,6 +28,8 @@ defmodule Freedive.Application do
|
||||||
FreediveWeb.Endpoint
|
FreediveWeb.Endpoint
|
||||||
]
|
]
|
||||||
|
|
||||||
|
children = if minimal, do: app_minimal, else: app_minimal ++ app_features
|
||||||
|
|
||||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||||
# for other strategies and supported options
|
# for other strategies and supported options
|
||||||
opts = [strategy: :one_for_one, name: Freedive.Supervisor]
|
opts = [strategy: :one_for_one, name: Freedive.Supervisor]
|
||||||
|
|
145
lib/freedive/release.ex
Normal file
145
lib/freedive/release.ex
Normal file
|
@ -0,0 +1,145 @@
|
||||||
|
defmodule Freedive.Release do
|
||||||
|
@moduledoc """
|
||||||
|
Used for executing DB release tasks when run in production without Mix
|
||||||
|
installed.
|
||||||
|
"""
|
||||||
|
@app :freedive
|
||||||
|
require Logger
|
||||||
|
|
||||||
|
def account_create do
|
||||||
|
start_app()
|
||||||
|
|
||||||
|
email = Input.get_text("Enter email:")
|
||||||
|
password = Input.get_password("Enter password:")
|
||||||
|
confirm_password = Input.get_password("Confirm password:")
|
||||||
|
|
||||||
|
if password != confirm_password do
|
||||||
|
Logger.error("Passwords do not match.")
|
||||||
|
exit(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
confirmed_at = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
||||||
|
|
||||||
|
case Freedive.Accounts.register_user(%{
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
confirmed_at: confirmed_at
|
||||||
|
}) do
|
||||||
|
{:ok, _user} ->
|
||||||
|
Logger.warning("User created successfully.")
|
||||||
|
|
||||||
|
{:error, %Ecto.Changeset{errors: errors}} ->
|
||||||
|
Logger.error("Failed to create user")
|
||||||
|
Enum.each(errors, fn {key, message} -> Logger.error("#{key}: #{inspect(message)}") end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def password_reset do
|
||||||
|
start_app()
|
||||||
|
|
||||||
|
email = Input.get_text("Enter email:")
|
||||||
|
user = Freedive.Accounts.get_user_by_email(email)
|
||||||
|
|
||||||
|
case user do
|
||||||
|
nil ->
|
||||||
|
Logger.error("User not found.")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
password = Input.get_password("Enter new password:")
|
||||||
|
confirm_password = Input.get_password("Confirm new password:")
|
||||||
|
|
||||||
|
if password != confirm_password do
|
||||||
|
Logger.error("Passwords do not match.")
|
||||||
|
exit(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
case Freedive.Release.Account.update_user_password(user, password) do
|
||||||
|
{:ok, _user} ->
|
||||||
|
Logger.warning("Password updated successfully.")
|
||||||
|
|
||||||
|
{:error, changeset} ->
|
||||||
|
Logger.error("Failed to update password")
|
||||||
|
|
||||||
|
Enum.each(changeset.errors, fn {key, message} ->
|
||||||
|
Logger.error("#{key}: #{inspect(message)}")
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def migrate do
|
||||||
|
load_app()
|
||||||
|
|
||||||
|
for repo <- repos() do
|
||||||
|
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def rollback(repo, version) do
|
||||||
|
load_app()
|
||||||
|
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp repos do
|
||||||
|
Application.fetch_env!(@app, :ecto_repos)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp load_app do
|
||||||
|
Application.load(@app)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp start_app do
|
||||||
|
load_app()
|
||||||
|
Application.put_env(@app, :minimal, true)
|
||||||
|
Application.ensure_all_started(@app)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defmodule Input do
|
||||||
|
def get_text(prompt) do
|
||||||
|
IO.write("#{prompt} ")
|
||||||
|
IO.gets("") |> String.trim()
|
||||||
|
end
|
||||||
|
|
||||||
|
# Borrowed from Mix.Tasks.Hex.password_get/1
|
||||||
|
def get_password(prompt) do
|
||||||
|
pid = spawn_link(fn -> loop(prompt) end)
|
||||||
|
ref = make_ref()
|
||||||
|
value = IO.gets("#{prompt} ")
|
||||||
|
|
||||||
|
send(pid, {:done, self(), ref})
|
||||||
|
receive do: ({:done, ^pid, ^ref} -> :ok)
|
||||||
|
|
||||||
|
value |> String.trim()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp loop(prompt) do
|
||||||
|
receive do
|
||||||
|
{:done, parent, ref} ->
|
||||||
|
send(parent, {:done, self(), ref})
|
||||||
|
IO.write(:standard_error, "\e[2K\r")
|
||||||
|
after
|
||||||
|
1 ->
|
||||||
|
IO.write(:standard_error, "\e[2K\r#{prompt} ")
|
||||||
|
loop(prompt)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defmodule Freedive.Release.Account do
|
||||||
|
def update_user_password(user, password) do
|
||||||
|
changeset =
|
||||||
|
user
|
||||||
|
|> Freedive.Accounts.User.password_changeset(%{password: password})
|
||||||
|
|
||||||
|
Ecto.Multi.new()
|
||||||
|
|> Ecto.Multi.update(:user, changeset)
|
||||||
|
|> Ecto.Multi.delete_all(:tokens, Freedive.Accounts.UserToken.by_user_and_contexts_query(user, :all))
|
||||||
|
|> Freedive.Repo.transaction()
|
||||||
|
|> case do
|
||||||
|
{:ok, %{user: user}} -> {:ok, user}
|
||||||
|
{:error, :user, changeset, _} -> {:error, changeset}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
5
rel/overlays/bin/migrate
Executable file
5
rel/overlays/bin/migrate
Executable file
|
@ -0,0 +1,5 @@
|
||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
cd -P -- "$(dirname -- "$0")"
|
||||||
|
exec ./freedive eval Freedive.Release.migrate
|
5
rel/overlays/bin/server
Executable file
5
rel/overlays/bin/server
Executable file
|
@ -0,0 +1,5 @@
|
||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
cd -P -- "$(dirname -- "$0")"
|
||||||
|
PHX_SERVER=true exec ./freedive start
|
Loading…
Reference in a new issue