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 embedded runtime by env #1084

Merged
merged 5 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/livebook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ defmodule Livebook do
Livebook.Config.default_runtime!("LIVEBOOK_DEFAULT_RUNTIME") ||
Livebook.Runtime.ElixirStandalone.new()

config :livebook,
:runtime_modules,
[
Livebook.Runtime.ElixirStandalone,
Livebook.Runtime.MixStandalone,
Livebook.Runtime.Attached
]

if home = Livebook.Config.writable_dir!("LIVEBOOK_HOME") do
config :livebook, :home, home
end
Expand Down
9 changes: 9 additions & 0 deletions lib/livebook/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ defmodule Livebook.Config do
Application.fetch_env!(:livebook, :default_runtime)
end

@doc """
Returns if the runtime module is enabled.
"""
@spec runtime_enabled?(module()) :: boolean()
def runtime_enabled?(runtime) do
runtime in Application.fetch_env!(:livebook, :runtime_modules) or
runtime == default_runtime().__struct__
end

@doc """
Returns the authentication mode.
"""
Expand Down
4 changes: 4 additions & 0 deletions lib/livebook_web/live/session_live/attached_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ defmodule LivebookWeb.SessionLive.AttachedLive do

@impl true
def mount(_params, %{"session" => session, "current_runtime" => current_runtime}, socket) do
unless Livebook.Config.runtime_enabled?(Livebook.Runtime.Attached) do
raise "runtime module not allowed"
end

if connected?(socket) do
Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session.id}")
end
Expand Down
4 changes: 4 additions & 0 deletions lib/livebook_web/live/session_live/elixir_standalone_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ defmodule LivebookWeb.SessionLive.ElixirStandaloneLive do

@impl true
def mount(_params, %{"session" => session, "current_runtime" => current_runtime}, socket) do
unless Livebook.Config.runtime_enabled?(Livebook.Runtime.ElixirStandalone) do
raise "runtime module not allowed"
end

if connected?(socket) do
Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session.id}")
end
Expand Down
4 changes: 4 additions & 0 deletions lib/livebook_web/live/session_live/embedded_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ defmodule LivebookWeb.SessionLive.EmbeddedLive do

@impl true
def mount(_params, %{"session" => session, "current_runtime" => current_runtime}, socket) do
unless Livebook.Config.runtime_enabled?(Livebook.Runtime.Embedded) do
raise "runtime module not allowed"
end

if connected?(socket) do
Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session.id}")
end
Expand Down
4 changes: 4 additions & 0 deletions lib/livebook_web/live/session_live/mix_standalone_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ defmodule LivebookWeb.SessionLive.MixStandaloneLive do

@impl true
def mount(_params, %{"session" => session, "current_runtime" => current_runtime}, socket) do
unless Livebook.Config.runtime_enabled?(Livebook.Runtime.MixStandalone) do
raise "runtime module not allowed"
end

if connected?(socket) do
Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session.id}")
end
Expand Down
64 changes: 36 additions & 28 deletions lib/livebook_web/live/session_live/runtime_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,42 @@ defmodule LivebookWeb.SessionLive.RuntimeComponent do
</h3>
<div class="w-full flex-col space-y-5">
<div class="flex space-x-4">
<.choice_button
active={@type == "elixir_standalone"}
phx-click="set_runtime_type"
phx-value-type="elixir_standalone"
phx-target={@myself}>
Elixir standalone
</.choice_button>
<.choice_button
active={@type == "mix_standalone"}
phx-click="set_runtime_type"
phx-value-type="mix_standalone"
phx-target={@myself}>
Mix standalone
</.choice_button>
<.choice_button
active={@type == "attached"}
phx-click="set_runtime_type"
phx-value-type="attached"
phx-target={@myself}>
Attached node
</.choice_button>
<.choice_button
active={@type == "embedded"}
phx-click="set_runtime_type"
phx-value-type="embedded"
phx-target={@myself}>
Embedded
</.choice_button>
<%= if Livebook.Config. runtime_enabled?(Livebook.Runtime.ElixirStandalone) do %>
<.choice_button
active={@type == "elixir_standalone"}
phx-click="set_runtime_type"
phx-value-type="elixir_standalone"
phx-target={@myself}>
Elixir standalone
</.choice_button>
<% end %>
<%= if Livebook.Config. runtime_enabled?(Livebook.Runtime.MixStandalone) do %>
<.choice_button
active={@type == "mix_standalone"}
phx-click="set_runtime_type"
phx-value-type="mix_standalone"
phx-target={@myself}>
Mix standalone
</.choice_button>
<% end %>
<%= if Livebook.Config. runtime_enabled?(Livebook.Runtime.Attached) do %>
<.choice_button
active={@type == "attached"}
phx-click="set_runtime_type"
phx-value-type="attached"
phx-target={@myself}>
Attached node
</.choice_button>
<% end %>
<%= if Livebook.Config. runtime_enabled?(Livebook.Runtime.Embedded) do %>
<.choice_button
active={@type == "embedded"}
phx-click="set_runtime_type"
phx-value-type="embedded"
phx-target={@myself}>
Embedded
</.choice_button>
<% end %>
</div>
<div>
<%= live_render @socket, live_view_for_type(@type),
Expand Down
7 changes: 7 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ Livebook.Runtime.ErlDist.NodeManager.start(
# and setting them explicitly
Application.put_env(:livebook, :default_runtime, Livebook.Runtime.Embedded.new())

Application.put_env(:livebook, :runtime_modules, [
Livebook.Runtime.ElixirStandalone,
Livebook.Runtime.MixStandalone,
Livebook.Runtime.Attached,
Livebook.Runtime.Embedded
])

defmodule Livebook.Runtime.Embedded.Dependencies do
def entries() do
[
Expand Down