Add LIVEBOOK_SHUTDOWN_ENABLED (#1111)

* 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 <jonatanklosko@gmail.com>
This commit is contained in:
José Valim 2022-04-14 15:52:28 +02:00 committed by GitHub
parent aa3b58d708
commit 5acbd2efad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 20 deletions

View file

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

View file

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

View file

@ -24,6 +24,7 @@ config :livebook, LivebookWeb.Endpoint,
]
config :livebook, :iframe_port, 4001
config :livebook, :shutdown_enabled, true
# ## SSL Support
#

View file

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

View file

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

View file

@ -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"""
<span class="tooltip right distant" data-tooltip="Shutdown">
<button class="text-2xl text-gray-400 hover:text-gray-50 focus:text-gray-50 rounded-xl h-10 w-10 flex items-center justify-center"
@ -121,13 +121,17 @@ defmodule LivebookWeb.SidebarHelpers do
end
def shared_home_handlers(socket) do
attach_hook(socket, :shutdown, :handle_event, fn
"shutdown", _params, socket ->
System.stop()
{:halt, put_flash(socket, :info, "Livebook is shutting down. You can close this page.")}
if Livebook.Config.shutdown_enabled?() do
attach_hook(socket, :shutdown, :handle_event, fn
"shutdown", _params, socket ->
System.stop()
{:halt, put_flash(socket, :info, "Livebook is shutting down. You can close this page.")}
_event, _params, socket ->
{:cont, socket}
end)
_event, _params, socket ->
{:cont, socket}
end)
else
socket
end
end
end

View file

@ -1,4 +1,5 @@
set RELEASE_MODE=interactive
set LIVEBOOK_SHUTDOWN_ENABLED=true
set cookie_path="!RELEASE_ROOT!\releases\COOKIE"
if not exist %cookie_path% (

View file

@ -1,4 +1,5 @@
export RELEASE_MODE=interactive
export LIVEBOOK_SHUTDOWN_ENABLED=true
cookie_path="${RELEASE_ROOT}/releases/COOKIE"
if [ ! -f $cookie_path ]; then