cover/Elixir.Agent.Remote.html

1 defmodule Agent.Remote do
2 import Enum, only: [find_value: 3]
3 import String, only: [to_atom: 1]
4 import Utils.Normalize, only: [name: 1]
5
6
:-(
def find(agent) do
7
:-(
:net_adm.names(~c"127.0.0.1") |> list() |> search(agent)
8 catch
9
:-(
_, _ -> :undefined
10 end
11
12
:-(
defp list({:ok, n}), do: n
13
:-(
defp list(_), do: []
14
15 defp search(names, agent) do
16
:-(
find_value(names, :undefined, &match(&1, agent))
17 end
18
19
:-(
defp match({n, _}, agent) do
20
:-(
node = :erlang.list_to_binary(n)
21
:-(
run(node, agent, agent)
22 rescue
23
:-(
_ -> nil
24 end
25
26 defp run(node, agent, a) do
27
:-(
<<a::binary, "-">> |> ok(node) |> exec(agent, node)
28 end
29
30 defp ok(prefix, node) do
31
:-(
take(node, byte_size(prefix)) == prefix
32 end
33
34 defp exec(true, agent, node) do
35
:-(
agent |> to_atom() |> Kernel.to_string() |> name() |> fetch(node)
36 end
37
38
:-(
defp exec(false, _, _), do: nil
39
40
:-(
defp take(node, size) do
41
:-(
binary_part(node, 0, size)
42 rescue
43
:-(
_ -> nil
44 end
45
46
:-(
defp fetch(norm, node) do
47
:-(
addr = "#{node}@127.0.0.1" |> to_atom()
48
:-(
:erpc.call(addr, :global, :whereis_name, [{norm, :puppet}], 5000)
49 catch
50
:-(
_, _ -> nil
51 end
52 end
Line Hits Source