Infer short vs longname from actual node and use longname in example (#284)

This commit is contained in:
José Valim 2021-05-21 13:03:47 +02:00 committed by GitHub
parent 00edceafc9
commit 7416975376
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 16 deletions

View file

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

View file

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

View file

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

View file

@ -30,10 +30,10 @@ defmodule LivebookWeb.SessionLive.AttachedLive do
Make sure to give the node a name and a cookie, for example:
</p>
<div class="text-gray-700 markdown">
<%= if Livebook.Config.shortnames? do %>
<pre><code>iex --sname test --cookie mycookie</code></pre>
<%= if longname = Livebook.Config.longname() do %>
<pre><code>iex --name test@<%= longname %> --cookie mycookie</code></pre>
<% else %>
<pre><code>iex --name test@127.0.0.1 --cookie mycookie</code></pre>
<pre><code>iex --sname test --cookie mycookie</code></pre>
<% end %>
</div>
<p class="text-gray-700">
@ -43,8 +43,7 @@ defmodule LivebookWeb.SessionLive.AttachedLive do
<div class="flex flex-col space-y-4">
<div>
<div class="input-label">Name</div>
<%= 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() %>
</div>
<div>
<div class="input-label">Cookie</div>
@ -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),