Add runtime support: don't only listen on /tmp/frajtano1, script to add peer

This commit is contained in:
bluepython508
2024-09-19 12:39:19 +01:00
parent 5229bd8d1c
commit 6aaf8ff8e5
6 changed files with 40 additions and 19 deletions

1
.gitignore vendored
View File

@@ -28,3 +28,4 @@ frajtano-*.tar
/.nix-hex /.nix-hex
/.nix-mix /.nix-mix
/result

2
config/runtime.exs Normal file
View File

@@ -0,0 +1,2 @@
import Config
config :frajtano, listen_path: Path.expand(System.get_env("FRAJTANO_DIR")) |> Path.join("agent.sock")

View File

@@ -1,10 +1,12 @@
{ {
lib,
pkgs,
mixRelease, mixRelease,
fetchMixDeps,
elixir, elixir,
}: }: let
mixRelease rec {
pname = "frajtano"; pname = "frajtano";
pkg = mixRelease {
inherit pname;
version = "0.0.1"; version = "0.0.1";
inherit elixir; inherit elixir;
@@ -13,12 +15,26 @@ mixRelease rec {
name = "${pname}-source"; name = "${pname}-source";
filter = path: _type: baseNameOf path != "flake.nix" && baseNameOf path != "flake.lock"; filter = path: _type: baseNameOf path != "flake.nix" && baseNameOf path != "flake.lock";
}; };
mixFodDeps = fetchMixDeps {
pname = "mix-deps-${pname}";
inherit version src;
sha256 = "sha256-lknkq6b1yDOLgTpR/tPEk9UKqpqQT6H1N5g15M60nCA=";
};
ELIXIR_MAKE_CACHE_DIR = "/tmp/.elixir-make-cache"; ELIXIR_MAKE_CACHE_DIR = "/tmp/.elixir-make-cache";
meta.mainProgram = "${pname}"; meta.mainProgram = pname;
} };
script = pkgs.writeShellScriptBin pname ''
set -eu
file="$FRAJTANO_DIR/cookie"
(umask 077; [ -f "$file" ] || ${pkgs.coreutils}/bin/head -c 128 /dev/urandom | ${pkgs.coreutils}/bin/base64 -w0 > "$file")
export RELEASE_COOKIE=$(${pkgs.coreutils}/bin/cat "$file")
run() {
exec ${lib.getExe pkg} "$@"
}
case $1 in
assimilate)
run rpc ":ok = \"$(echo -n "$2" | ${pkgs.coreutils}/bin/base64)\" |> Base.decode64!() |> Frajtano.Agent.add_peer()"
;;
*)
run "$@"
;;
esac
'';
in pkgs.symlinkJoin { name = pname; paths = [ script pkg ]; meta.mainProgram = pname; }

View File

@@ -49,6 +49,7 @@ defmodule Frajtano.Agent do
@impl true @impl true
def handle_call({:add_peer, path}, _from, state) do def handle_call({:add_peer, path}, _from, state) do
# TODO: deduplicate peers by socket path
case Peer.start(path) do case Peer.start(path) do
{:ok, _} -> {:reply, :ok, state} {:ok, _} -> {:reply, :ok, state}
{:error, error} -> {:reply, {:error, error}, state} {:error, error} -> {:reply, {:error, error}, state}

View File

@@ -18,7 +18,7 @@ defmodule Frajtano.Supervisor do
def init(:ok) do def init(:ok) do
children = [ children = [
Frajtano.Agent, Frajtano.Agent,
{Frajtano.Listener, ["/tmp/frajtano1"]}, {Frajtano.Listener, [Application.fetch_env!(:frajtano, :listen_path)]},
{Task.Supervisor, name: Frajtano.ClientSupervisor}, {Task.Supervisor, name: Frajtano.ClientSupervisor},
{DynamicSupervisor, name: Frajtano.Peer} {DynamicSupervisor, name: Frajtano.Peer}
] ]

View File

@@ -14,6 +14,7 @@ defmodule Frajtano.MixProject do
def application do def application do
[ [
extra_applications: [:logger], extra_applications: [:logger],
env: [listen_path: nil],
mod: {Frajtano, []} mod: {Frajtano, []}
] ]
end end