From 5acbd2efadffc1bd8566b0ae55cf03221186c6b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 14 Apr 2022 15:52:28 +0200 Subject: [PATCH] Add LIVEBOOK_SHUTDOWN_ENABLED (#1111) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add LIVEBOOK_SHUTDOWN_ENABLED Previously we relied on code:get_mode(), but since releases are starting in interactive mode to use less memory, we need a clearer mechanism. * Move shutdown feature check to Livebook.Config Co-authored-by: Jonatan Kłosko --- README.md | 3 +++ config/config.exs | 15 ++++++--------- config/dev.exs | 1 + lib/livebook.ex | 6 +++++- lib/livebook/config.ex | 15 +++++++++++++-- lib/livebook_web/live/sidebar_helpers.ex | 20 ++++++++++++-------- rel/app/env.bat.eex | 1 + rel/app/env.sh.eex | 1 + 8 files changed, 42 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ed9a7eb25..c4b782418 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,9 @@ The following environment variables configure Livebook: long and it can be generated by commands such as: 'openssl rand -base64 48'. Defaults to a random secret on every boot. + * LIVEBOOK_SHUTDOWN_ENABLED - controls if a shutdown button should be shown + in the homepage. Set it to "true" to enable it. + * LIVEBOOK_TOKEN_ENABLED - controls whether token authentication is enabled. Enabled by default unless `LIVEBOOK_PASSWORD` is set. Set it to "false" to disable it. diff --git a/config/config.exs b/config/config.exs index a52a6c2f6..a0cf0b4a4 100644 --- a/config/config.exs +++ b/config/config.exs @@ -19,15 +19,12 @@ config :mime, :types, %{ "text/plain" => ["livemd"] } -# Sets the default storage backend -config :livebook, :storage, Livebook.Storage.Ets - -# Sets the default authentication mode to token -config :livebook, :authentication_mode, :token - -config :livebook, :plugs, [] - -config :livebook, :explore_notebooks, [] +config :livebook, + authentication_mode: :token, + explore_notebooks: [], + plugs: [], + shutdown_enabled: false, + storage: Livebook.Storage.Ets # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. diff --git a/config/dev.exs b/config/dev.exs index 7ee06c0c4..ea5971f65 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -24,6 +24,7 @@ config :livebook, LivebookWeb.Endpoint, ] config :livebook, :iframe_port, 4001 +config :livebook, :shutdown_enabled, true # ## SSL Support # diff --git a/lib/livebook.ex b/lib/livebook.ex index 19bcf27f7..d370255f1 100644 --- a/lib/livebook.ex +++ b/lib/livebook.ex @@ -102,7 +102,7 @@ defmodule Livebook do password = Livebook.Config.password!("LIVEBOOK_PASSWORD") -> config :livebook, authentication_mode: :password, password: password - Livebook.Config.token_enabled!("LIVEBOOK_TOKEN_ENABLED") -> + Livebook.Config.boolean!("LIVEBOOK_TOKEN_ENABLED", true) -> config :livebook, token: Livebook.Utils.random_id() true -> @@ -113,6 +113,10 @@ defmodule Livebook do config :livebook, :iframe_port, port end + if Livebook.Config.boolean!("LIVEBOOK_SHUTDOWN_ENABLED", false) do + config :livebook, :shutdown_enabled, true + end + config :livebook, :default_runtime, Livebook.Config.default_runtime!("LIVEBOOK_DEFAULT_RUNTIME") || diff --git a/lib/livebook/config.ex b/lib/livebook/config.ex index 2d8b34cad..15e802f18 100644 --- a/lib/livebook/config.ex +++ b/lib/livebook/config.ex @@ -98,6 +98,14 @@ defmodule Livebook.Config do end end + @doc """ + Returns whether the shutdown feature is enabled. + """ + @spec shutdown_enabled?() :: boolean() + def shutdown_enabled?() do + Application.fetch_env!(:livebook, :shutdown_enabled) + end + ## Parsing @doc """ @@ -202,8 +210,11 @@ defmodule Livebook.Config do @doc """ Parses token auth setting from env. """ - def token_enabled!(env) do - System.get_env(env, "1") in ~w(true 1) + def boolean!(env, default \\ false) do + case System.get_env(env) do + nil -> default + var -> var in ~w(true 1) + end end @doc """ diff --git a/lib/livebook_web/live/sidebar_helpers.ex b/lib/livebook_web/live/sidebar_helpers.ex index 653157bd8..e37f3f6fb 100644 --- a/lib/livebook_web/live/sidebar_helpers.ex +++ b/lib/livebook_web/live/sidebar_helpers.ex @@ -58,7 +58,7 @@ defmodule LivebookWeb.SidebarHelpers do end def shutdown_item(assigns) do - if :code.get_mode() == :interactive do + if Livebook.Config.shutdown_enabled?() do ~H"""