Skip to content

Commit

Permalink
feat: get cookie from ~/.erlang.cookie on host
Browse files Browse the repository at this point in the history
  • Loading branch information
feng19 committed Mar 7, 2024
1 parent 13eee9e commit 0b3d9b5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
26 changes: 19 additions & 7 deletions lib/x_trace_web/controllers/trace_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,11 @@ defmodule XTraceWeb.TraceLive do
case Node.start(node_name, node_type) do
{:ok, _} ->
if cookie != "" do
cookie |> String.to_atom() |> Node.set_cookie()
cookie |> String.to_atom()
else
get_host_cookie()
end
|> Node.set_cookie()

NodeListener.start_monitor()

Expand Down Expand Up @@ -406,10 +409,6 @@ defmodule XTraceWeb.TraceLive do
{:noreply, socket}
end

defp _to_form(params, errors \\ []) do
to_form(params, errors: errors)
end

defp validate_max(params, errors) do
case check_integer(params, "max") do
{:ok, max} when max > 1000 ->
Expand Down Expand Up @@ -449,13 +448,13 @@ defmodule XTraceWeb.TraceLive do
all_loaded: all_loaded,
module_list: all_loaded,
fun_list: @default_fun_list,
add_tspec_disabled: false
add_tspec_disabled: true
}, errors}
end

defp validate_mfa("module", "_", errors, all_loaded, _self?, _node, _code_server_mode) do
errors = Keyword.put(errors, :module, {"module can't defined as '_'", []})
{%{module_list: all_loaded, fun_list: @default_fun_list, add_tspec_disabled: false}, errors}
{%{module_list: all_loaded, fun_list: @default_fun_list, add_tspec_disabled: true}, errors}
end

defp validate_mfa("module", module, errors, all_loaded, self?, node, code_server_mode) do
Expand Down Expand Up @@ -733,6 +732,10 @@ defmodule XTraceWeb.TraceLive do
}
end

defp _to_form(params, errors \\ []) do
to_form(params, errors: errors)
end

defp apply_settings(data, connecting_node) do
settings = data |> Base.decode64!() |> :erlang.binary_to_term()
Logger.info("apply settings: #{inspect(settings, limit: :infinity)}")
Expand Down Expand Up @@ -805,4 +808,13 @@ defmodule XTraceWeb.TraceLive do
do: Exception.format(error, reason, stacktrace)

defp format_calls_return(error), do: inspect(error)

defp get_host_cookie do
Path.expand("~/.erlang.cookie")
|> File.read()
|> case do
{:ok, cookie} -> String.trim(cookie) |> String.to_atom()
_ -> Node.get_cookie()
end
end
end
4 changes: 2 additions & 2 deletions lib/x_trace_web/controllers/trace_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<%= if @node != :"nonode@nohost" do %>
<div class="grid grid-cols-2 gap-4">
<.input field={f[:connect_node]} label="Node to connect" placeholder="name@domain" />
<.input field={f[:connect_cookie]} label="Cookie" />
<.input field={f[:connect_cookie]} label="Cookie" placeholder="if empty will use value of Node.get_cookie()" />
<.button type="button" class="w-full" phx-click="connect-node"
disabled={is_nil(f[:connect_node].value) || f[:connect_node].value == ""}>Connect</.button>
<.button type="button" class="w-full" phx-click="reset-node">Reset node</.button>
Expand All @@ -73,7 +73,7 @@

<div></div>
<.input field={f[:is_long_name]} label="LongName" type="checkbox" />
<.input field={f[:cookie]} label="Cookie" />
<.input field={f[:cookie]} label="Cookie" placeholder="if empty will use value of ~/.erlang.cookie" />
<.button type="button" class="mt-8" phx-click="setup-node">Setup</.button>
</div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule XTrace.MixProject do
def project do
[
app: @app,
version: "0.1.1",
version: "0.1.2",
elixir: "~> 1.14",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down

0 comments on commit 0b3d9b5

Please sign in to comment.