diff --git a/lib/livebook/config.ex b/lib/livebook/config.ex index 34d0ac238..55d3335c3 100644 --- a/lib/livebook/config.ex +++ b/lib/livebook/config.ex @@ -4,21 +4,21 @@ defmodule Livebook.Config do @type auth_mode() :: :token | :password | :disabled @doc """ - Checks if the distribution mode is configured to use short names. + Returns the longname if the distribution mode is configured to use long names. """ - @spec shortnames?() :: boolean() - def shortnames?() do - case Application.get_env(:livebook, :node) do - nil -> true - {:shortnames, _name} -> true - {:longnames, _name} -> false + @spec longname() :: binary() | nil + def longname() do + host = Livebook.Utils.node_host() + + if host =~ "." do + host end end @doc """ Returns the runtime module to be used by default. """ - @spec default_runtime() :: Livebook.Runtime + @spec default_runtime() :: Livebook.Runtime.t() def default_runtime() do Application.fetch_env!(:livebook, :default_runtime) end diff --git a/lib/livebook/runtime/standalone_init.ex b/lib/livebook/runtime/standalone_init.ex index d31e8c1e0..5e5eeb1fc 100644 --- a/lib/livebook/runtime/standalone_init.ex +++ b/lib/livebook/runtime/standalone_init.ex @@ -33,7 +33,7 @@ defmodule Livebook.Runtime.StandaloneInit do @spec elixir_flags(node()) :: list() def elixir_flags(node_name) do [ - if(Livebook.Config.shortnames?(), do: "--sname", else: "--name"), + if(Livebook.Config.longname(), do: "--name", else: "--sname"), to_string(node_name), "--erl", # Minimize schedulers busy wait threshold, diff --git a/lib/livebook/utils.ex b/lib/livebook/utils.ex index c7f227a96..e7d50d128 100644 --- a/lib/livebook/utils.ex +++ b/lib/livebook/utils.ex @@ -36,11 +36,19 @@ defmodule Livebook.Utils do String.to_atom(name) else # Default to the same host as the current node - [_, host] = node() |> Atom.to_string() |> String.split("@") - :"#{name}@#{host}" + :"#{name}@#{node_host()}" end end + @doc """ + Returns the host part of a node. + """ + @spec node_host() :: binary() + def node_host do + [_, host] = node() |> Atom.to_string() |> :binary.split("@") + host + end + @doc """ Registers the given process under `name` for the time of `fun` evaluation. """ diff --git a/lib/livebook_web/live/session_live/attached_live.ex b/lib/livebook_web/live/session_live/attached_live.ex index edc61e614..ce57fb2c8 100644 --- a/lib/livebook_web/live/session_live/attached_live.ex +++ b/lib/livebook_web/live/session_live/attached_live.ex @@ -30,10 +30,10 @@ defmodule LivebookWeb.SessionLive.AttachedLive do Make sure to give the node a name and a cookie, for example:

- <%= if Livebook.Config.shortnames? do %> -
iex --sname test --cookie mycookie
+ <%= if longname = Livebook.Config.longname() do %> +
iex --name test@<%= longname %> --cookie mycookie
<% else %> -
iex --name test@127.0.0.1 --cookie mycookie
+
iex --sname test --cookie mycookie
<% end %>

@@ -43,8 +43,7 @@ defmodule LivebookWeb.SessionLive.AttachedLive do

Name
- <%= text_input f, :name, value: @data["name"], class: "input", - placeholder: if(Livebook.Config.shortnames?, do: "test", else: "test@127.0.0.1") %> + <%= text_input f, :name, value: @data["name"], class: "input", placeholder: name_placeholder() %>
Cookie
@@ -90,6 +89,10 @@ defmodule LivebookWeb.SessionLive.AttachedLive do data["name"] != "" and data["cookie"] != "" end + defp name_placeholder do + if longname = Livebook.Config.longname(), do: "test@#{longname}", else: "test" + end + defp runtime_error_to_message(:unreachable), do: "Node unreachable" defp runtime_error_to_message(:already_in_use),