1
0
Fork 0

allow multiple haproxy servers with options

This commit is contained in:
ibizaman 2022-10-16 23:43:20 -07:00
parent 50bb2da5e7
commit fab296e6dc

View file

@ -3,8 +3,7 @@
, lib , lib
}: }:
{ serviceName { serviceName
, serviceAddress ? null , servers ? []
, serviceSocket ? null
, phpFastcgi ? false , phpFastcgi ? false
, phpDocroot ? null , phpDocroot ? null
, phpIndex ? "index.php" , phpIndex ? "index.php"
@ -13,46 +12,55 @@
, extraBackendOptions ? [] , extraBackendOptions ? []
}: }:
assert lib.assertMsg ( with lib;
(serviceAddress == null && serviceSocket != null) with lib.lists;
|| (serviceAddress != null && serviceSocket == null) with lib.attrsets;
) "set either serviceAddress or serviceSocket";
let let
backendOptions = lib.concatMapStrings (x : "\n " + x) extraBackendOptions; indent = map (x: " " + x);
serviceBind = if serviceAddress != null then serviceAddress else serviceSocket; mkServer = i: s:
let
proto = optional phpFastcgi "proto fcgi";
in
concatStringsSep " " ([
"server ${serviceName}${toString i} ${s.address}"
] ++ proto ++ s.extra);
serverslines = imap1 mkServer servers;
backend = backend =
if !phpFastcgi (
then '' concatStringsSep "\n" (
backend ${serviceName} [
mode http "backend ${serviceName}"
option forwardfor${backendOptions} ]
server ${serviceName}1 ${serviceBind} ++ indent [
'' else '' "mode http"
backend ${serviceName} "option forwardfor"
mode http ]
option forwardfor${backendOptions} ++ indent extraBackendOptions
use-fcgi-app ${serviceName}-php-fpm ++ optional phpFastcgi " use-fcgi-app ${serviceName}-php-fpm"
server ${serviceName}1 ${serviceBind} proto fcgi ++ indent serverslines
++ [""]) # final newline
) +
(if !phpFastcgi then "" else ''
fcgi-app ${serviceName}-php-fpm fcgi-app ${serviceName}-php-fpm
log-stderr global log-stderr global
docroot ${phpDocroot} docroot ${phpDocroot}
index ${phpIndex} index ${phpIndex}
path-info ^(/.+\.php)(/.*)?$ path-info ^(/.+\.php)(/.*)?$
''; '');
extraAclsCondition = lib.concatStrings (lib.attrsets.mapAttrsToList (k: v: "\nacl acl_${serviceName}_${k} ${v}") extraUseBackendConditions); extraAclsCondition = concatStrings (mapAttrsToList (k: v: "\nacl acl_${serviceName}_${k} ${v}") extraUseBackendConditions);
extraAclsOr = lib.concatStrings (lib.attrsets.mapAttrsToList (k: v: " OR acl_${serviceName}_${k}") extraUseBackendConditions); extraAclsOr = concatStrings (mapAttrsToList (k: v: " OR acl_${serviceName}_${k}") extraUseBackendConditions);
in in
{ {
frontend = '' frontend = ''
acl acl_${serviceName} hdr_beg(host) ${serviceName}.${extraAclsCondition} acl acl_${serviceName} hdr_beg(host) ${serviceName}.${extraAclsCondition}
'' ''
+ lib.concatMapStrings (x: x + "\n") extraFrontendOptions + concatMapStrings (x: x + "\n") extraFrontendOptions
+ '' + ''
use_backend ${serviceName} if acl_${serviceName}${extraAclsOr} use_backend ${serviceName} if acl_${serviceName}${extraAclsOr}
''; '';