Support command-line arguments with spaces, simplify Nginx example, some README improvements

This commit is contained in:
Sander van der Burg 2020-02-11 21:55:37 +01:00 committed by Sander van der Burg
parent 9c6b781308
commit 4dc376a6da
5 changed files with 84 additions and 76 deletions

View File

@ -9,76 +9,66 @@ let
nginxStateDir = "${stateDir}/${instanceName}";
dependencies = webapps ++ (builtins.attrValues interDeps);
generateNginxConf = daemon:
writeTextFile {
name = "nginx.conf";
text = ''
error_log ${nginxStateDir}/logs/error.log;
${stdenv.lib.optionalString (!forceDisableUserChange) ''
user ${user} ${group};
''}
${if daemon then ''
pid ${runtimeDir}/${instanceName}.pid;
'' else ''
daemon off;
''}
events {
worker_connections 190000;
}
http {
${stdenv.lib.concatMapStrings (dependency: ''
upstream webapp${toString dependency.port} {
server localhost:${toString dependency.port};
}
'') webapps}
${stdenv.lib.concatMapStrings (dependencyName:
let
dependency = builtins.getAttr dependencyName interDeps;
in
''
upstream webapp${toString dependency.port} {
server ${dependency.target.properties.hostname}:${toString dependency.port};
}
'') (builtins.attrNames interDeps)}
# Fallback virtual host displaying an error page. This is what users see
# if they connect to a non-deployed web application.
# Without it, nginx redirects to the first available virtual host, giving
# unpredictable results. This could happen while an upgrade is in progress.
server {
listen ${toString port};
server_name aaaa;
root ${./errorpage};
}
${stdenv.lib.concatMapStrings (dependency: ''
server {
listen ${toString port};
server_name ${dependency.dnsName};
location / {
proxy_pass http://webapp${toString dependency.port};
}
}
'') dependencies}
}
'';
};
in
import ./nginx.nix {
inherit createManagedProcess stdenv nginx stateDir forceDisableUserChange;
inherit createManagedProcess stdenv nginx stateDir forceDisableUserChange runtimeDir;
} {
inherit instanceSuffix;
dependencies = map (webapp: webapp.pkg) dependencies;
daemonConfigFile = generateNginxConf true;
foregroundConfigFile = generateNginxConf false;
configFile = writeTextFile {
name = "nginx.conf";
text = ''
error_log ${nginxStateDir}/logs/error.log;
${stdenv.lib.optionalString (!forceDisableUserChange) ''
user ${user} ${group};
''}
events {
worker_connections 190000;
}
http {
${stdenv.lib.concatMapStrings (dependency: ''
upstream webapp${toString dependency.port} {
server localhost:${toString dependency.port};
}
'') webapps}
${stdenv.lib.concatMapStrings (dependencyName:
let
dependency = builtins.getAttr dependencyName interDeps;
in
''
upstream webapp${toString dependency.port} {
server ${dependency.target.properties.hostname}:${toString dependency.port};
}
'') (builtins.attrNames interDeps)}
# Fallback virtual host displaying an error page. This is what users see
# if they connect to a non-deployed web application.
# Without it, nginx redirects to the first available virtual host, giving
# unpredictable results. This could happen while an upgrade is in progress.
server {
listen ${toString port};
server_name aaaa;
root ${./errorpage};
}
${stdenv.lib.concatMapStrings (dependency: ''
server {
listen ${toString port};
server_name ${dependency.dnsName};
location / {
proxy_pass http://webapp${toString dependency.port};
}
}
'') dependencies}
}
'';
};
}

View File

@ -1,5 +1,5 @@
{createManagedProcess, stdenv, nginx, stateDir, forceDisableUserChange}:
{daemonConfigFile, foregroundConfigFile, dependencies ? [], instanceSuffix ? ""}:
{createManagedProcess, stdenv, nginx, stateDir, runtimeDir, forceDisableUserChange}:
{configFile, dependencies ? [], instanceSuffix ? ""}:
let
instanceName = "nginx${instanceSuffix}";
@ -17,9 +17,9 @@ createManagedProcess {
''}
'';
process = "${nginx}/bin/nginx";
args = [ "-p" "${stateDir}/${instanceName}" ];
foregroundProcessExtraArgs = [ "-c" foregroundConfigFile ];
daemonExtraArgs = [ "-c" daemonConfigFile ];
args = [ "-p" "${stateDir}/${instanceName}" "-c" configFile ];
foregroundProcessExtraArgs = [ "-g" "daemon off;" ];
daemonExtraArgs = [ "-g" "pid ${runtimeDir}/${instanceName}.pid;" ];
inherit dependencies instanceName;

View File

@ -18,6 +18,8 @@ let
else if processManager == "sysvinit" then "sysvinit-script"
else if processManager == "systemd" then "systemd-unit"
else if processManager == "supervisord" then "supervisord-program"
else if processManager == "bsdrc" then "bsdrc-script"
else if processManager == "cygrunsrv" then "cygrunsrv-service"
else throw "Unknown process manager: ${processManager}";
in
rec {

View File

@ -8,11 +8,10 @@ let
dependencies = webapps ++ (builtins.attrValues interDeps);
in
import ./nginx.nix {
inherit createSystemVInitScript nginx instanceSuffix;
inherit createSystemVInitScript nginx;
stateDir = nginxStateDir;
dependencies = map (webapp: webapp.pkg) dependencies;
} {
inherit instanceSuffix;
configFile = writeTextFile {
name = "nginx.conf";
text = ''
@ -64,4 +63,5 @@ import ./nginx.nix {
}
'';
};
dependencies = map (webapp: webapp.pkg) dependencies;
}

View File

@ -1,17 +1,33 @@
{createSystemVInitScript, nginx, configFile, stateDir, dependencies ? [], instanceSuffix ? ""}:
{createSystemVInitScript, nginx, stateDir}:
{configFile, dependencies ? [], instanceSuffix ? ""}:
let
instanceName = "nginx${instanceSuffix}";
user = instanceName;
group = instanceName;
nginxLogDir = "${stateDir}/${instanceName}/logs";
in
createSystemVInitScript {
name = instanceName;
description = "Nginx";
initialize = ''
mkdir -p ${stateDir}/logs
mkdir -p ${nginxLogDir}
'';
process = "${nginx}/bin/nginx";
args = [ "-c" configFile "-p" stateDir ];
runlevels = [ 3 4 5 ];
inherit dependencies instanceName;
credentials = {
groups = {
"${group}" = {};
};
users = {
"${user}" = {
inherit group;
description = "Nginx user";
};
};
};
}