More principled initial peers

This commit is contained in:
bluepython508
2024-09-26 09:09:11 +01:00
parent 66cb85ec1d
commit 904cb233f3
6 changed files with 34 additions and 3 deletions

1
.envrc
View File

@@ -2,3 +2,4 @@ use flake
export RELEASE_NODE=frajtano-test@$(cat /etc/hostname) export RELEASE_NODE=frajtano-test@$(cat /etc/hostname)
export FRAJTANO_DIR=$PWD/.frajtano_state export FRAJTANO_DIR=$PWD/.frajtano_state
export FRAJTANO_CONFIG=$PWD/config/config.exs

4
config/config.exs Normal file
View File

@@ -0,0 +1,4 @@
import Config
config :frajtano,
initial_peers: []

View File

@@ -40,10 +40,22 @@
dir = lib.mkOption { dir = lib.mkOption {
description = "directory in which to place the listening socket"; description = "directory in which to place the listening socket";
default = "${config.home.homeDirectory}/.ssh/frajtano"; 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.sessionVariables.FRAJTANO_DIR = cfg.dir;
home.packages = [self.packages.${pkgs.system}.default]; home.packages = [self.packages.${pkgs.system}.default];
@@ -51,7 +63,7 @@
Unit.Description = "frajtano"; Unit.Description = "frajtano";
Unit.After = ["default.target"]; Unit.After = ["default.target"];
Service.Type = "notify"; 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.ExecSearchPath = ["${self.packages.${pkgs.system}.default}/bin"];
Service.ExecStart = "frajtano start"; Service.ExecStart = "frajtano start";
Install.WantedBy = ["default.target"]; Install.WantedBy = ["default.target"];
@@ -62,6 +74,7 @@
config = { config = {
EnvironmentVariables = { EnvironmentVariables = {
FRAJTANO_DIR = cfg.dir; FRAJTANO_DIR = cfg.dir;
FRAJTANO_CONFIG = configFile;
}; };
ProgramArguments = ["${self.packages.${pkgs.system}.default}/bin/frajtano" "start"]; ProgramArguments = ["${self.packages.${pkgs.system}.default}/bin/frajtano" "start"];
RunAtLoad = true; RunAtLoad = true;

View File

@@ -1,6 +1,7 @@
defmodule Frajtano.Agent do defmodule Frajtano.Agent do
alias Frajtano.Peer alias Frajtano.Peer
use GenServer use GenServer
require Logger
def start_link(args) do def start_link(args) do
GenServer.start_link(__MODULE__, nil, [{:name, __MODULE__} | args]) GenServer.start_link(__MODULE__, nil, [{:name, __MODULE__} | args])
@@ -14,6 +15,12 @@ defmodule Frajtano.Agent do
} }
end 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 # select: list of specs, where specs are a tuple of match, guards, and outputs
# match is {key, pid, value}, :"$1" is a match variable # match is {key, pid, value}, :"$1" is a match variable
def peer_paths() do def peer_paths() do

View File

@@ -22,6 +22,7 @@ defmodule Frajtano.Supervisor do
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)]},
{Task, &Frajtano.Agent.initial_peers/0},
:systemd.ready(), :systemd.ready(),
] ]

View File

@@ -7,7 +7,12 @@ defmodule Frajtano.MixProject do
version: "0.0.1", version: "0.0.1",
elixir: "~> 1.16", elixir: "~> 1.16",
start_permanent: Mix.env() == :prod, start_permanent: Mix.env() == :prod,
deps: deps() deps: deps(),
releases: [
frajtano: [
config_providers: [{Config.Reader, {:system, "FRAJTANO_CONFIG", ""}}]
],
]
] ]
end end