allow embedded runtime by env (#1084)

* allow embedded runtime by env

* fix session live test

* This reverts commit f36931d6ef9b..81ca7d

* allow runtime modules

* Apply suggestions from code review
This commit is contained in:
ByeongUk Choi 2022-04-04 19:20:36 +09:00 committed by GitHub
parent 902c993098
commit e3d9c5fa35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 76 additions and 28 deletions

View file

@ -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

View file

@ -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.
"""

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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),

View file

@ -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
[