Skip to content

Commit

Permalink
msf 2 makes execution of setup user possible from outside containers (#…
Browse files Browse the repository at this point in the history
…2422)

* msf 1, allow setup_user to build super

* concept for external commands

* typo oops

* wrap in transaction

* update CL

* use migrator with_repo

* fix dialzyer
  • Loading branch information
taylordowns2000 authored Aug 30, 2024
1 parent 079e4cf commit 5256122
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 70 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ and this project adheres to

### Changed

- Allow setup_user command to be execute from outside the container with
`/app/bin/lightning eval Lightning.Setup.setup_user/3`
- Implement a combo-box to make navigating between projects easier
[#241](https://github.com/OpenFn/lightning/pull/2424)

Expand Down
44 changes: 1 addition & 43 deletions lib/lightning/demo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@ defmodule Lightning.Demo do
"""
def reset_demo do
if Application.get_env(:lightning, :is_resettable_demo) do
Lightning.Release.load_app()

children =
[
{Phoenix.PubSub,
name: Lightning.PubSub, adapter: Lightning.Demo.FakePubSub},
{Lightning.Vault, Application.get_env(:lightning, Lightning.Vault, [])}
]
|> Enum.reject(fn {mod, _} -> Process.whereis(mod) end)

Supervisor.start_link(children, strategy: :one_for_one)
{:ok, _pid} = Lightning.Setup.ensure_minimum_setup()

{:ok, _, _} =
Ecto.Migrator.with_repo(Lightning.Repo, fn _repo ->
Expand All @@ -33,36 +23,4 @@ defmodule Lightning.Demo do
IO.puts("I'm sorry, Dave. I'm afraid I can't do that.")
end
end

defmodule FakePubSub do
@moduledoc false

# FakePubSub is a Phoenix.PubSub adapter that does nothing.
# The purpose of this adapter is to allow the demo to run without
# the whole application running.

@behaviour Phoenix.PubSub.Adapter

@impl true
def child_spec(_opts) do
%{id: __MODULE__, start: {__MODULE__, :start_link, []}}
end

def start_link do
{:ok, self()}
end

@impl true
def node_name(_), do: nil

@impl true
def broadcast(_, _, _, _) do
:ok
end

@impl true
def direct_broadcast(_, _, _, _, _) do
:ok
end
end
end
78 changes: 78 additions & 0 deletions lib/lightning/setup.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
defmodule Lightning.Setup do
@moduledoc """
Demo encapsulates logic for setting up a demonstration site.
"""

alias Lightning.SetupUtils

@doc """
This makes it possible to run setup_user as an external command
See: Lightning.SetupUtils.setup_user() for more docs.
## Examples
iex> kubectl exec -it deploy/demo-web -- /app/bin/lightning eval Lightning.Setup.setup_user(%{email: "[email protected]", first_name: "taylor", last_name: "downs", password: "shh12345!"})
:ok
"""
@spec setup_user(map(), String.t() | nil, list(map()) | nil) ::
{:ok, any(), any()} | {:error, any()}
def setup_user(user, token \\ nil, credentials \\ nil) do
{:ok, _pid} = Lightning.Setup.ensure_minimum_setup()

{:ok, _, _} =
Ecto.Migrator.with_repo(Lightning.Repo, fn _repo ->
SetupUtils.setup_user(user, token, credentials)
end)
end

@doc """
Set up the bare minimum so that commands can be executed against the repo.
"""
def ensure_minimum_setup do
Lightning.Release.load_app()

children =
[
{Phoenix.PubSub,
name: Lightning.PubSub, adapter: Lightning.Setup.FakePubSub},
{Lightning.Vault, Application.get_env(:lightning, Lightning.Vault, [])}
]
|> Enum.reject(fn {mod, _} -> Process.whereis(mod) end)

Supervisor.start_link(children, strategy: :one_for_one)
end

defmodule FakePubSub do
@moduledoc false

# FakePubSub is a Phoenix.PubSub adapter that does nothing.
# The purpose of this adapter is to allow the demo to run without
# the whole application running.

@behaviour Phoenix.PubSub.Adapter

@impl true
def child_spec(_opts) do
%{id: __MODULE__, start: {__MODULE__, :start_link, []}}
end

def start_link do
{:ok, self()}
end

@impl true
def node_name(_), do: nil

@impl true
def broadcast(_, _, _, _) do
:ok
end

@impl true
def direct_broadcast(_, _, _, _, _) do
:ok
end
end
end
52 changes: 27 additions & 25 deletions lib/lightning/setup_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -909,32 +909,34 @@ defmodule Lightning.SetupUtils do
def setup_user(user, token \\ nil, credentials \\ nil) do
{role, user} = Map.pop(user, :role)

# create user
{:ok, user} =
if role == :superuser,
do: Accounts.register_superuser(user),
else: Accounts.create_user(user)

# create token
if token,
do:
Repo.insert!(%Lightning.Accounts.UserToken{
user_id: user.id,
context: "api",
token: token
})
Repo.transaction(fn ->
# create user
{:ok, user} =
if role == :superuser,
do: Accounts.register_superuser(user),
else: Accounts.create_user(user)

# create token
if token,
do:
Repo.insert!(%Lightning.Accounts.UserToken{
user_id: user.id,
context: "api",
token: token
})

# create credentials
if credentials,
do:
Enum.each(credentials, fn credential ->
{:ok, _credential} =
Credentials.create_credential(
credential
|> Map.put(:user_id, user.id)
)
end)
# create credentials
if credentials,
do:
Enum.each(credentials, fn credential ->
{:ok, _credential} =
Credentials.create_credential(
credential
|> Map.put(:user_id, user.id)
)
end)

:ok
:ok
end)
end
end
4 changes: 2 additions & 2 deletions test/lightning/setup_utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ defmodule Lightning.SetupUtilsTest do

describe "setup_user/3" do
test "creates a user, an api token, and credentials" do
assert :ok ==
assert {:ok, :ok} ==
Lightning.SetupUtils.setup_user(
%{
first_name: "Taylor",
Expand Down Expand Up @@ -689,7 +689,7 @@ defmodule Lightning.SetupUtilsTest do
end

test "can be used to set up a superuser" do
assert :ok ==
assert {:ok, :ok} ==
Lightning.SetupUtils.setup_user(
%{
role: :superuser,
Expand Down

0 comments on commit 5256122

Please sign in to comment.