1
0
Fork 0

merge config with unit for haproxy

This commit is contained in:
ibizaman 2022-12-31 15:18:51 -08:00
parent e41918a1a7
commit a670d691bc
7 changed files with 82 additions and 146 deletions

View file

@ -12,10 +12,7 @@ let
PostgresDB = callPackage ./postgresdb {}; PostgresDB = callPackage ./postgresdb {};
mkPostgresDB = callPackage ./postgresdb/mkdefault.nix {inherit PostgresDB;}; mkPostgresDB = callPackage ./postgresdb/mkdefault.nix {inherit PostgresDB;};
HaproxyConfig = callPackage ./haproxy/config.nix {inherit utils;}; mkHaproxyService = callPackage ./haproxy/unit.nix {inherit utils;};
mkHaproxyConfig = callPackage ./haproxy/mkconfig.nix {inherit HaproxyConfig;};
HaproxyService = callPackage ./haproxy/unit.nix {inherit utils;};
mkHaproxyService = callPackage ./haproxy/mkunit.nix {inherit HaproxyService;};
CaddyConfig = callPackage ./caddy/config.nix {inherit utils;}; CaddyConfig = callPackage ./caddy/config.nix {inherit utils;};
CaddyService = callPackage ./caddy/unit.nix {inherit utils;}; CaddyService = callPackage ./caddy/unit.nix {inherit utils;};

View file

@ -1,28 +0,0 @@
{ stdenv
, pkgs
, lib
, utils
}:
{ configDir ? "/etc/haproxy"
, configFile ? "haproxy.cfg"
, user
, group
, config
}:
dependsOn:
with builtins;
with lib.attrsets;
with lib.lists;
with lib.strings;
let
configcreator = pkgs.callPackage ./configcreator.nix {inherit utils;};
in
utils.mkConfigFile {
name = configFile;
dir = configDir;
content = configcreator.render (configcreator.default (config dependsOn // {inherit user group;}));
}

View file

@ -1,23 +0,0 @@
{ HaproxyConfig
}:
{ name
, configDir
, configFile
, user
, group
, config
, dependsOn ? {}
}:
{
inherit name configDir configFile;
inherit user group;
pkg = HaproxyConfig {
inherit configDir configFile;
inherit config;
inherit user group;
};
inherit dependsOn;
type = "fileset";
}

View file

@ -1,17 +0,0 @@
{ HaproxyService
}:
{ name
, configDir
, configFile
, dependsOn ? {}
}:
{
inherit name configDir configFile;
pkg = HaproxyService {
inherit configDir configFile;
};
inherit dependsOn;
type = "systemd-unit";
}

View file

@ -1,17 +1,27 @@
{ stdenv { pkgs
, pkgs
, utils , utils
}: }:
{ configDir ? "/etc/haproxy" { name
, configFile ? "haproxy.cfg" , user
, group
, config
, pidfile ? "/run/haproxy/haproxy.pid" , pidfile ? "/run/haproxy/haproxy.pid"
, socket ? "/run/haproxy/haproxy.sock" , socket ? "/run/haproxy/haproxy.sock"
, dependsOn ? {}
}: }:
{...}:
# User and group are set in config.nix let
configcreator = pkgs.callPackage ./configcreator.nix {inherit utils;};
utils.systemd.mkService rec { content = configcreator.render (configcreator.default (config dependsOn // {inherit user group;}));
configfile = pkgs.writeText "haproxy.cfg" content;
in
{
inherit name;
inherit user group;
pkg = dependsOn: utils.systemd.mkService {
name = "haproxy"; name = "haproxy";
content = '' content = ''
@ -20,12 +30,14 @@ utils.systemd.mkService rec {
Documentation=https://www.haproxy.com/documentation/hapee/latest/onepage/ Documentation=https://www.haproxy.com/documentation/hapee/latest/onepage/
After=network.target network-online.target After=network.target network-online.target
Wants=network-online.target systemd-networkd-wait-online.target Wants=network-online.target systemd-networkd-wait-online.target
${utils.unitDepends "After" dependsOn}
${utils.unitDepends "Wants" dependsOn}
StartLimitInterval=14400 StartLimitInterval=14400
StartLimitBurst=10 StartLimitBurst=10
[Service] [Service]
Environment="CONFIG=${configDir}/${configFile}" "PIDFILE=${pidfile}" "EXTRAOPTS=-S ${socket}" Environment="CONFIG=${configfile}" "PIDFILE=${pidfile}" "EXTRAOPTS=-S ${socket}"
ExecStart=${pkgs.haproxy}/bin/haproxy -Ws -f $CONFIG -p $PIDFILE $EXTRAOPTS ExecStart=${pkgs.haproxy}/bin/haproxy -Ws -f $CONFIG -p $PIDFILE $EXTRAOPTS
ExecReload=${pkgs.haproxy}/bin/haproxy -Ws -f $CONFIG -c -q $EXTRAOPTS ExecReload=${pkgs.haproxy}/bin/haproxy -Ws -f $CONFIG -c -q $EXTRAOPTS
ExecReload=${pkgs.coreutils}/bin/kill -USR2 $MAINPID ExecReload=${pkgs.coreutils}/bin/kill -USR2 $MAINPID
@ -72,4 +84,8 @@ utils.systemd.mkService rec {
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target
''; '';
};
inherit dependsOn;
type = "systemd-unit";
} }

View file

@ -7,7 +7,6 @@
, realms ? [] , realms ? []
, every ? "10m" , every ? "10m"
, HaproxyService
, KeycloakService , KeycloakService
}: }:
@ -16,6 +15,7 @@ rec {
stateDir = "keycloak-public-keys"; stateDir = "keycloak-public-keys";
downloadDir = "/var/lib/keycloak-public-keys"; downloadDir = "/var/lib/keycloak-public-keys";
systemdUnitFile = "keycloak-haproxy.service";
pkg = pkg =
with pkgs.lib; with pkgs.lib;
@ -34,8 +34,8 @@ rec {
''; '';
}; };
in in
{ HaproxyService { KeycloakService
, KeycloakService , ...
}: utils.systemd.mkService rec { }: utils.systemd.mkService rec {
name = "keycloak-haproxy"; name = "keycloak-haproxy";
@ -89,7 +89,6 @@ rec {
}; };
dependsOn = { dependsOn = {
inherit HaproxyService;
inherit KeycloakService; inherit KeycloakService;
}; };
type = "systemd-unit"; type = "systemd-unit";

View file

@ -24,13 +24,6 @@
, distribution ? {} , distribution ? {}
}: }:
let let
addressOrLocalhost = distHaproxy: service:
if (builtins.head distHaproxy).properties.hostname == service.target.properties.hostname then
"127.0.0.1"
else
service.target.properties.hostname;
mkVaultwardenWeb = pkgs.callPackage ./web.nix {inherit utils;}; mkVaultwardenWeb = pkgs.callPackage ./web.nix {inherit utils;};
in in
rec { rec {
@ -162,12 +155,11 @@ rec {
use_backend = "if acl_vaultwarden"; use_backend = "if acl_vaultwarden";
}; };
backend = { backend = {
servers = [ # TODO: instead, we should generate target specific service https://hydra.nixos.org/build/203347995/download/2/manual/#idm140737322273072
{ servers = map (dist: {
name = "ttrss1"; name = "ttrss_${dist.properties.hostname}_1";
address = "${addressOrLocalhost distribution.HaproxyConfig service}:${builtins.toString ingress}"; address = "${dist.properties.hostname}:${builtins.toString ingress}";
} }) service;
];
}; };
}; };