From 904cb233f3572b7bf4ff63be402b11a6be48aa46 Mon Sep 17 00:00:00 2001 From: bluepython508 <16466646+bluepython508@users.noreply.github.com> Date: Thu, 26 Sep 2024 09:09:11 +0100 Subject: [PATCH] More principled initial peers --- .envrc | 1 + config/config.exs | 4 ++++ flake.nix | 17 +++++++++++++++-- lib/agent.ex | 7 +++++++ lib/frajtano.ex | 1 + mix.exs | 7 ++++++- 6 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 config/config.exs diff --git a/.envrc b/.envrc index 2854f9b..3b2e73e 100644 --- a/.envrc +++ b/.envrc @@ -2,3 +2,4 @@ use flake export RELEASE_NODE=frajtano-test@$(cat /etc/hostname) export FRAJTANO_DIR=$PWD/.frajtano_state +export FRAJTANO_CONFIG=$PWD/config/config.exs diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..a238fac --- /dev/null +++ b/config/config.exs @@ -0,0 +1,4 @@ +import Config + +config :frajtano, + initial_peers: [] diff --git a/flake.nix b/flake.nix index f371f6e..bec7d31 100644 --- a/flake.nix +++ b/flake.nix @@ -40,10 +40,22 @@ dir = lib.mkOption { description = "directory in which to place the listening socket"; default = "${config.home.homeDirectory}/.ssh/frajtano"; + type = lib.types.path; + }; + initialPeers = lib.mkOption { + description = "initially spawned peers - will be passed to /usr/bin/env"; + type = with lib.types; listOf (listOf str); + default = []; }; }; - config = lib.mkIf cfg.enable { + config = let + peers = lib.strings.concatMapStringsSep ", " (args: ''{:spawn, {"${pkgs.coreutils}/bin/env", [${lib.concatMapStringsSep ", " (s: ''~S{${s}}'') args}]}}'') cfg.initialPeers; + configFile = pkgs.writeText "config.exs" '' + import Config + config :frajtano, initial_peers: [${peers}] + ''; + in lib.mkIf cfg.enable { home.sessionVariables.FRAJTANO_DIR = cfg.dir; home.packages = [self.packages.${pkgs.system}.default]; @@ -51,7 +63,7 @@ Unit.Description = "frajtano"; Unit.After = ["default.target"]; Service.Type = "notify"; - Service.Environment = ["'FRAJTANO_DIR=${cfg.dir}'"]; + Service.Environment = ["'FRAJTANO_DIR=${cfg.dir}'" "'FRAJTANO_CONFIG=${configFile}'"]; Service.ExecSearchPath = ["${self.packages.${pkgs.system}.default}/bin"]; Service.ExecStart = "frajtano start"; Install.WantedBy = ["default.target"]; @@ -62,6 +74,7 @@ config = { EnvironmentVariables = { FRAJTANO_DIR = cfg.dir; + FRAJTANO_CONFIG = configFile; }; ProgramArguments = ["${self.packages.${pkgs.system}.default}/bin/frajtano" "start"]; RunAtLoad = true; diff --git a/lib/agent.ex b/lib/agent.ex index fb89c5d..5fc0ab3 100644 --- a/lib/agent.ex +++ b/lib/agent.ex @@ -1,6 +1,7 @@ defmodule Frajtano.Agent do alias Frajtano.Peer use GenServer + require Logger def start_link(args) do GenServer.start_link(__MODULE__, nil, [{:name, __MODULE__} | args]) @@ -14,6 +15,12 @@ defmodule Frajtano.Agent do } end + def initial_peers() do + initial_peers = Application.fetch_env!(:frajtano, :initial_peers) + |> Enum.map(&GenServer.call(__MODULE__, {:add_peer, &1})) + Logger.info("Started initial peers: #{inspect initial_peers}") + 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 diff --git a/lib/frajtano.ex b/lib/frajtano.ex index c112834..72019bf 100644 --- a/lib/frajtano.ex +++ b/lib/frajtano.ex @@ -22,6 +22,7 @@ defmodule Frajtano.Supervisor do Frajtano.Agent, {Task.Supervisor, name: Frajtano.ClientSupervisor}, {Frajtano.Listener, [Application.fetch_env!(:frajtano, :listen_path)]}, + {Task, &Frajtano.Agent.initial_peers/0}, :systemd.ready(), ] diff --git a/mix.exs b/mix.exs index b63208b..d51553d 100644 --- a/mix.exs +++ b/mix.exs @@ -7,7 +7,12 @@ defmodule Frajtano.MixProject do version: "0.0.1", elixir: "~> 1.16", start_permanent: Mix.env() == :prod, - deps: deps() + deps: deps(), + releases: [ + frajtano: [ + config_providers: [{Config.Reader, {:system, "FRAJTANO_CONFIG", ""}}] + ], + ] ] end