2023-08-23 22:21:20 +03:00
|
|
|
{ cluster, config, depot, lib, ... }:
|
2022-08-05 00:57:54 +03:00
|
|
|
|
|
|
|
let
|
|
|
|
inherit (cluster.config) vars;
|
2024-07-08 21:19:45 +03:00
|
|
|
inherit (cluster.config.services.patroni) secrets;
|
2023-08-31 01:55:45 +03:00
|
|
|
inherit (config.networking) hostName;
|
2022-08-05 00:57:54 +03:00
|
|
|
|
|
|
|
getMeshIp = name: vars.mesh.${name}.meshIp;
|
|
|
|
|
|
|
|
net = vars.meshNet.cidr;
|
|
|
|
|
2023-08-23 22:21:20 +03:00
|
|
|
pg = depot.packages.postgresql;
|
2022-08-05 00:57:54 +03:00
|
|
|
|
|
|
|
baseDir = "/srv/storage/database/postgres-ha";
|
2023-08-23 01:59:53 +03:00
|
|
|
|
|
|
|
walDir = "/var/lib/postgres-ha/${pg.psqlSchema}/wal";
|
2022-08-05 00:57:54 +03:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
imports = [
|
2023-01-31 23:59:53 +02:00
|
|
|
depot.nixosModules.patroni
|
2022-08-05 00:57:54 +03:00
|
|
|
];
|
|
|
|
|
2023-08-23 01:59:53 +03:00
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${baseDir}' 0700 patroni patroni - -"
|
|
|
|
"d '${walDir}' 0700 patroni patroni - -"
|
|
|
|
];
|
2024-08-14 13:46:30 +03:00
|
|
|
systemd.services.patroni = {
|
|
|
|
requires = [ "consul-ready.service" ];
|
|
|
|
after = [ "consul-ready.service" ];
|
|
|
|
};
|
2022-08-05 00:57:54 +03:00
|
|
|
services.patroni = {
|
|
|
|
enable = true;
|
2023-08-31 01:55:45 +03:00
|
|
|
name = hostName;
|
2022-08-05 00:57:54 +03:00
|
|
|
postgresqlPackage = pg;
|
|
|
|
postgresqlDataDir ="${baseDir}/${pg.psqlSchema}";
|
|
|
|
postgresqlPort = cluster.config.links.patroni-pg-internal.port;
|
|
|
|
restApiPort = cluster.config.links.patroni-api.port;
|
|
|
|
scope = "poseidon";
|
|
|
|
namespace = "/patroni";
|
|
|
|
|
2023-08-31 01:55:45 +03:00
|
|
|
nodeIp = getMeshIp hostName;
|
|
|
|
otherNodesIps = map getMeshIp (cluster.config.services.patroni.otherNodes.worker hostName);
|
2022-08-05 00:57:54 +03:00
|
|
|
raft = false;
|
|
|
|
softwareWatchdog = true;
|
|
|
|
settings = {
|
2023-03-24 18:52:48 +02:00
|
|
|
consul = {
|
2024-07-17 03:53:36 +03:00
|
|
|
host = config.links.consulAgent.tuple;
|
2023-03-24 18:52:48 +02:00
|
|
|
register_service = true;
|
|
|
|
};
|
2022-08-05 00:57:54 +03:00
|
|
|
bootstrap.dcs = {
|
|
|
|
ttl = 30;
|
|
|
|
loop_wait = 10;
|
|
|
|
retry_timeout = 10;
|
|
|
|
maximum_lag_on_failover = 1024 * 1024;
|
|
|
|
};
|
2023-03-24 19:10:13 +02:00
|
|
|
failsafe_mode = true;
|
2022-08-05 00:57:54 +03:00
|
|
|
postgresql = {
|
2023-08-23 01:59:53 +03:00
|
|
|
basebackup = {
|
|
|
|
waldir = walDir;
|
|
|
|
};
|
2022-08-05 00:57:54 +03:00
|
|
|
use_pg_rewind = true;
|
|
|
|
use_slots = true;
|
2024-08-14 15:23:17 +03:00
|
|
|
synchronous_mode = true;
|
2022-08-05 00:57:54 +03:00
|
|
|
authentication = {
|
|
|
|
replication.username = "patronirep";
|
|
|
|
rewind.username = "patronirew";
|
|
|
|
superuser.username = "postgres";
|
|
|
|
};
|
|
|
|
parameters = {
|
2023-08-31 01:55:45 +03:00
|
|
|
listen_addresses = getMeshIp hostName;
|
2022-08-05 00:57:54 +03:00
|
|
|
wal_level = "replica";
|
|
|
|
hot_standby_feedback = "on";
|
|
|
|
unix_socket_directories = "/tmp";
|
2024-08-14 15:23:17 +03:00
|
|
|
synchronous_commit = "on";
|
2022-08-05 00:57:54 +03:00
|
|
|
};
|
|
|
|
pg_hba = [
|
|
|
|
"host replication patronirep ${net} scram-sha-256"
|
|
|
|
"host all patronirew ${net} scram-sha-256"
|
|
|
|
"host all postgres ${net} scram-sha-256"
|
2022-08-07 20:57:35 +03:00
|
|
|
"host all all ${net} scram-sha-256"
|
2022-08-05 00:57:54 +03:00
|
|
|
"host all all 127.0.0.1/32 scram-sha-256"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
2024-07-08 21:19:45 +03:00
|
|
|
environmentFiles = lib.mapAttrs (_: secret: secret.path) (lib.filterAttrs (name: _: lib.hasPrefix "PATRONI_" name) secrets);
|
2022-08-05 00:57:54 +03:00
|
|
|
};
|
|
|
|
}
|