Infer hostname automatically in CLI for attached mode (#1474)

This commit is contained in:
Abhijit Kar ツ 2022-10-11 16:55:54 +05:30 committed by GitHub
parent 711368bf0c
commit 41347c51be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 20 deletions

View file

@ -336,10 +336,6 @@ defmodule Livebook.Config do
defp parse_connection_config!(config) do
{node, cookie} = split_at_last_occurrence(config, ":")
unless node =~ "@" do
abort!(~s{expected node to include hostname, got: #{inspect(node)}})
end
node = String.to_atom(node)
cookie = String.to_atom(cookie)

View file

@ -32,6 +32,10 @@ defmodule Livebook.Runtime.Attached do
def connect(runtime) do
%{node: node, cookie: cookie} = runtime
# We need to append the hostname on connect because
# net_kernel has not yet started during new/2.
node = append_hostname(node)
# Set cookie for connecting to this specific node
Node.set_cookie(node, cookie)
@ -42,12 +46,21 @@ defmodule Livebook.Runtime.Attached do
node_manager_opts: [parent_node: node(), capture_orphan_logs: false]
)
{:ok, %{runtime | server_pid: server_pid}}
{:ok, %{runtime | node: node, server_pid: server_pid}}
:pang ->
{:error, "node #{inspect(node)} is unreachable"}
end
end
defp append_hostname(node) do
with :nomatch <- :string.find(Atom.to_string(node), "@"),
<<suffix::binary>> <- :string.find(Atom.to_string(:net_kernel.nodename()), "@") do
:"#{node}#{suffix}"
else
_ -> node
end
end
end
defimpl Livebook.Runtime, for: Livebook.Runtime.Attached do

View file

@ -73,19 +73,6 @@ defmodule Livebook.Utils do
end)
end
@doc """
Converts the given name to node identifier.
"""
@spec node_from_name(String.t()) :: atom()
def node_from_name(name) do
if name =~ "@" do
String.to_atom(name)
else
# Default to the same host as the current node
:"#{name}@#{node_host()}"
end
end
@doc """
Returns the host part of a node.
"""

View file

@ -1,7 +1,7 @@
defmodule LivebookWeb.SessionLive.AttachedLive do
use LivebookWeb, :live_view
alias Livebook.{Session, Runtime, Utils}
alias Livebook.{Session, Runtime}
@impl true
def mount(_params, %{"session" => session, "current_runtime" => current_runtime}, socket) do
@ -90,7 +90,7 @@ defmodule LivebookWeb.SessionLive.AttachedLive do
end
def handle_event("init", %{"data" => data}, socket) do
node = Utils.node_from_name(data["name"])
node = String.to_atom(data["name"])
cookie = String.to_atom(data["cookie"])
runtime = Runtime.Attached.new(node, cookie)