Compare commits

..

1 Commits

Author SHA1 Message Date
bluepython508
25260a5eb8 Flake: HM module 2024-09-22 12:50:57 +01:00
4 changed files with 31 additions and 31 deletions

1
.envrc
View File

@@ -2,3 +2,4 @@ use flake
export RELEASE_NODE=frajtano-test@nomos export RELEASE_NODE=frajtano-test@nomos
export FRAJTANO_DIR=$PWD/.frajtano_state export FRAJTANO_DIR=$PWD/.frajtano_state
export SSH_AUTH_SOCK=$FRAJTANO_DIR/agent.sock

View File

@@ -10,7 +10,9 @@ defmodule Frajtano.Agent do
def init(_) do def init(_) do
{ {
:ok, :ok,
%{}, %{
keys: %{}
},
{:continue, :init_peers} {:continue, :init_peers}
} }
end end
@@ -21,38 +23,36 @@ defmodule Frajtano.Agent do
{:noreply, state} {:noreply, state}
end end
# select: list of specs, where specs are a tuple of match, guards, and outputs
# match is {key, pid, value}, :"$1" is a match variable
def peer_paths() do
Registry.select(Frajtano.Peers, [{{:"$1", :_, :_}, [], [:"$1"]}])
end
def peer_pids() do
Registry.select(Frajtano.Peers, [{{:_, :"$1", :_}, [], [:"$1"]}])
end
@impl true @impl true
def handle_call({:identities}, _from, _state) do def handle_call({:identities}, _from, state) do
idents = idents =
Task.async_stream( for(
peer_pids(), {_, peer, :worker, _} <- DynamicSupervisor.which_children(Frajtano.Peer),
&{&1, Peer.identities(&1)}, is_pid(peer),
ordered: false, do: peer
on_timeout: :kill_task
) )
|> Task.async_stream(fn peer ->
with {:ok, idents} <- Peer.identities(peer),
do: {:ok, {idents, peer}}
end)
idents = for {:ok, {peer, {:ok, idents}}} <- idents, do: {idents, peer} # Double :ok-wrapping because of Task.async_stream
idents = for {:ok, {:ok, {idents, peer}}} <- idents, do: {idents, peer}
{ {
:reply, :reply,
{:ok, Enum.flat_map(idents, &elem(&1, 0))}, {:ok, for({idents, _} <- idents, ident <- idents, do: ident)},
%{
state
| keys:
for({idents, peer} <- idents, {key, _comment} <- idents, into: %{}, do: {key, peer}) for({idents, peer} <- idents, {key, _comment} <- idents, into: %{}, do: {key, peer})
} }
}
end end
@impl true @impl true
def handle_call({:sign, {key, _, _} = req}, _from, state) do def handle_call({:sign, {key, _, _} = req}, _from, state) do
{:reply, Peer.sign(state[key], req), state} {:reply, Peer.sign(state.keys[key], req), state}
end end
@impl true @impl true
@@ -64,16 +64,16 @@ defmodule Frajtano.Agent do
end end
end end
def identities() do def identities(agent \\ __MODULE__) do
GenServer.call(__MODULE__, {:identities}) GenServer.call(agent, {:identities})
end end
def sign(request) do def sign(agent \\ __MODULE__, request) do
# Signing can take some time, as a password may need to be entered or similar # Signing can take some time, as a password may need to be entered or similar
GenServer.call(__MODULE__, {:sign, request}, :infinity) GenServer.call(agent, {:sign, request}, :infinity)
end end
def add_peer(path) do def add_peer(agent \\ __MODULE__, path) do
GenServer.call(__MODULE__, {:add_peer, path}) GenServer.call(agent, {:add_peer, path})
end end
end end

View File

@@ -17,8 +17,7 @@ defmodule Frajtano.Supervisor do
@impl true @impl true
def init(:ok) do def init(:ok) do
children = [ children = [
{DynamicSupervisor, name: Frajtano.PeerSupervisor}, {DynamicSupervisor, name: Frajtano.Peer},
{Registry, keys: :unique, name: Frajtano.Peers},
Frajtano.Agent, Frajtano.Agent,
{Task.Supervisor, name: Frajtano.ClientSupervisor}, {Task.Supervisor, name: Frajtano.ClientSupervisor},
{Frajtano.Listener, [Application.fetch_env!(:frajtano, :listen_path)]}, {Frajtano.Listener, [Application.fetch_env!(:frajtano, :listen_path)]},

View File

@@ -4,11 +4,11 @@ defmodule Frajtano.Peer do
use GenServer, restart: :temporary use GenServer, restart: :temporary
def start(path) do def start(path) do
DynamicSupervisor.start_child(Frajtano.PeerSupervisor, {__MODULE__, path}) DynamicSupervisor.start_child(Frajtano.Peer, {__MODULE__, path})
end end
def start_link(path) do def start_link(path) do
GenServer.start_link(__MODULE__, path, name: {:via, Registry, {Frajtano.Peers, path}}) GenServer.start_link(__MODULE__, path)
end end
@impl true @impl true