From e3d9c5fa350749e8e6d7c0521338159b2eeae71b Mon Sep 17 00:00:00 2001 From: ByeongUk Choi Date: Mon, 4 Apr 2022 19:20:36 +0900 Subject: [PATCH] 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 --- lib/livebook.ex | 8 +++ lib/livebook/config.ex | 9 +++ .../live/session_live/attached_live.ex | 4 ++ .../session_live/elixir_standalone_live.ex | 4 ++ .../live/session_live/embedded_live.ex | 4 ++ .../live/session_live/mix_standalone_live.ex | 4 ++ .../live/session_live/runtime_component.ex | 64 +++++++++++-------- test/test_helper.exs | 7 ++ 8 files changed, 76 insertions(+), 28 deletions(-) diff --git a/lib/livebook.ex b/lib/livebook.ex index 816b0104e..19bcf27f7 100644 --- a/lib/livebook.ex +++ b/lib/livebook.ex @@ -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 diff --git a/lib/livebook/config.ex b/lib/livebook/config.ex index 36fe53119..e81b154ed 100644 --- a/lib/livebook/config.ex +++ b/lib/livebook/config.ex @@ -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. """ diff --git a/lib/livebook_web/live/session_live/attached_live.ex b/lib/livebook_web/live/session_live/attached_live.ex index 6ad736f30..e008fe66c 100644 --- a/lib/livebook_web/live/session_live/attached_live.ex +++ b/lib/livebook_web/live/session_live/attached_live.ex @@ -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 diff --git a/lib/livebook_web/live/session_live/elixir_standalone_live.ex b/lib/livebook_web/live/session_live/elixir_standalone_live.ex index 0021813b3..0f0e2dc6e 100644 --- a/lib/livebook_web/live/session_live/elixir_standalone_live.ex +++ b/lib/livebook_web/live/session_live/elixir_standalone_live.ex @@ -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 diff --git a/lib/livebook_web/live/session_live/embedded_live.ex b/lib/livebook_web/live/session_live/embedded_live.ex index 13f465c3a..75fbbcebd 100644 --- a/lib/livebook_web/live/session_live/embedded_live.ex +++ b/lib/livebook_web/live/session_live/embedded_live.ex @@ -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 diff --git a/lib/livebook_web/live/session_live/mix_standalone_live.ex b/lib/livebook_web/live/session_live/mix_standalone_live.ex index 9541e0474..08f1d64df 100644 --- a/lib/livebook_web/live/session_live/mix_standalone_live.ex +++ b/lib/livebook_web/live/session_live/mix_standalone_live.ex @@ -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 diff --git a/lib/livebook_web/live/session_live/runtime_component.ex b/lib/livebook_web/live/session_live/runtime_component.ex index 7eb3125c6..1ed281a6b 100644 --- a/lib/livebook_web/live/session_live/runtime_component.ex +++ b/lib/livebook_web/live/session_live/runtime_component.ex @@ -30,34 +30,42 @@ defmodule LivebookWeb.SessionLive.RuntimeComponent do
- <.choice_button - active={@type == "elixir_standalone"} - phx-click="set_runtime_type" - phx-value-type="elixir_standalone" - phx-target={@myself}> - Elixir standalone - - <.choice_button - active={@type == "mix_standalone"} - phx-click="set_runtime_type" - phx-value-type="mix_standalone" - phx-target={@myself}> - Mix standalone - - <.choice_button - active={@type == "attached"} - phx-click="set_runtime_type" - phx-value-type="attached" - phx-target={@myself}> - Attached node - - <.choice_button - active={@type == "embedded"} - phx-click="set_runtime_type" - phx-value-type="embedded" - phx-target={@myself}> - Embedded - + <%= 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 + + <% 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 + + <% 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 + + <% 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 + + <% end %>
<%= live_render @socket, live_view_for_type(@type), diff --git a/test/test_helper.exs b/test/test_helper.exs index 24490641a..ca5f7050c 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -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 [