Fix the foreground proxy, add supervisord testcases
This commit is contained in:
parent
7c636c9d14
commit
30e217945a
|
@ -4,6 +4,7 @@
|
|||
let
|
||||
webapp = import ../../webapp;
|
||||
instanceName = "webapp${instanceSuffix}";
|
||||
pidFile = "${tmpDir}/${instanceName}.pid";
|
||||
in
|
||||
createManagedProcess {
|
||||
name = instanceName;
|
||||
|
@ -16,8 +17,11 @@ createManagedProcess {
|
|||
|
||||
environment = {
|
||||
PORT = port;
|
||||
PID_FILE = "${tmpDir}/${instanceName}.pid";
|
||||
PID_FILE = pidFile;
|
||||
};
|
||||
|
||||
inherit pidFile;
|
||||
|
||||
user = instanceName;
|
||||
credentials = {
|
||||
groups = {
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
# TODO: user unused
|
||||
|
||||
let
|
||||
generateForegroundWrapper = import ./generate-foreground-wrapper.nix {
|
||||
generateForegroundProxy = import ./generate-foreground-proxy.nix {
|
||||
inherit stdenv writeTextFile;
|
||||
};
|
||||
in
|
||||
|
@ -43,12 +43,12 @@ createCygrunsrvParams (stdenv.lib.recursiveUpdate ({
|
|||
|
||||
path = if foregroundProcess != null then
|
||||
if initialize == "" then foregroundProcess
|
||||
else generateForegroundWrapper {
|
||||
else generateForegroundProxy {
|
||||
wrapDaemon = false;
|
||||
executable = foregroundProcess;
|
||||
inherit name initialize runtimeDir pidFile stdenv;
|
||||
}
|
||||
else generateForegroundWrapper {
|
||||
else generateForegroundProxy {
|
||||
wrapDaemon = true;
|
||||
executable = daemon;
|
||||
inherit name initialize runtimeDir pidFile stdenv;
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
# nice unsupported
|
||||
|
||||
let
|
||||
generateForegroundWrapper = import ./generate-foreground-wrapper.nix {
|
||||
generateForegroundProxy = import ./generate-foreground-proxy.nix {
|
||||
inherit stdenv writeTextFile;
|
||||
};
|
||||
|
||||
|
@ -35,7 +35,7 @@ let
|
|||
then [ foregroundProcess ] ++ foregroundProcessArgs
|
||||
else
|
||||
let
|
||||
wrapper = generateForegroundWrapper ({
|
||||
wrapper = generateForegroundProxy ({
|
||||
wrapDaemon = false;
|
||||
executable = foregroundProcess;
|
||||
inherit name initialize runtimeDir stdenv;
|
||||
|
@ -48,7 +48,7 @@ let
|
|||
[ wrapper ] ++ foregroundProcessArgs
|
||||
else
|
||||
let
|
||||
wrapper = generateForegroundWrapper ({
|
||||
wrapper = generateForegroundProxy ({
|
||||
wrapDaemon = true;
|
||||
executable = daemon;
|
||||
inherit name initialize runtimeDir stdenv;
|
||||
|
|
|
@ -13,7 +13,7 @@ let
|
|||
_pidFile = if pidFile == null then "${runtimeDir}/$(basename ${executable}).pid" else pidFile;
|
||||
in
|
||||
writeTextFile {
|
||||
name = "${name}-foregroundwrapper.sh";
|
||||
name = "${name}-foregroundproxy.sh";
|
||||
text = ''
|
||||
#! ${stdenv.shell} -e
|
||||
|
||||
|
@ -42,7 +42,7 @@ writeTextFile {
|
|||
${executable} "$@"
|
||||
|
||||
# Wait for the PID file to become available. Useful to work with daemons that don't behave well enough.
|
||||
count=0
|
||||
count=1 # Start with 1, because 0 returns a non-zero exit status when incrementing it
|
||||
|
||||
while [ ! -f "${_pidFile}" ]
|
||||
do
|
||||
|
@ -55,7 +55,7 @@ writeTextFile {
|
|||
echo "Waiting for ${_pidFile} to become available..."
|
||||
sleep 1
|
||||
|
||||
((count=count++))
|
||||
((count++))
|
||||
done
|
||||
|
||||
# Determine the daemon's PID by using the PID file
|
|
@ -26,13 +26,13 @@
|
|||
}:
|
||||
|
||||
let
|
||||
generateForegroundWrapper = import ./generate-foreground-wrapper.nix {
|
||||
generateForegroundProxy = import ./generate-foreground-proxy.nix {
|
||||
inherit stdenv writeTextFile;
|
||||
};
|
||||
|
||||
Program = if foregroundProcess != null then
|
||||
if initialize == "" then foregroundProcess
|
||||
else generateForegroundWrapper ({
|
||||
else generateForegroundProxy ({
|
||||
wrapDaemon = false;
|
||||
executable = foregroundProcess;
|
||||
inherit name initialize runtimeDir stdenv;
|
||||
|
@ -41,7 +41,7 @@ let
|
|||
} // stdenv.lib.optionalAttrs (pidFile != null) {
|
||||
inherit pidFile;
|
||||
})
|
||||
else generateForegroundWrapper ({
|
||||
else generateForegroundProxy ({
|
||||
wrapDaemon = true;
|
||||
executable = daemon;
|
||||
inherit name initialize runtimeDir stdenv;
|
||||
|
|
|
@ -22,14 +22,14 @@
|
|||
}:
|
||||
|
||||
let
|
||||
generateForegroundWrapper = import ./generate-foreground-wrapper.nix {
|
||||
generateForegroundProxy = import ./generate-foreground-proxy.nix {
|
||||
inherit stdenv writeTextFile;
|
||||
};
|
||||
|
||||
command = if foregroundProcess != null then
|
||||
(if initialize == ""
|
||||
then foregroundProcess
|
||||
else generateForegroundWrapper ({
|
||||
else generateForegroundProxy ({
|
||||
wrapDaemon = false;
|
||||
executable = foregroundProcess;
|
||||
inherit name initialize runtimeDir stdenv;
|
||||
|
@ -38,7 +38,7 @@ let
|
|||
} // stdenv.lib.optionalAttrs (pidFile != null) {
|
||||
inherit pidFile;
|
||||
})) + " ${stdenv.lib.escapeShellArgs foregroundProcessArgs}"
|
||||
else (generateForegroundWrapper ({
|
||||
else (generateForegroundProxy ({
|
||||
wrapDaemon = true;
|
||||
executable = daemon;
|
||||
inherit name initialize runtimeDir stdenv;
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
{ pkgs ? import <nixpkgs> { inherit system; }
|
||||
, system ? builtins.currentSystem
|
||||
, stateDir ? "/var"
|
||||
, runtimeDir ? "${stateDir}/run"
|
||||
, logDir ? "${stateDir}/log"
|
||||
, cacheDir ? "${stateDir}/cache"
|
||||
, tmpDir ? (if stateDir == "/var" then "/tmp" else "${stateDir}/tmp")
|
||||
, forceDisableUserChange ? false
|
||||
, processManager ? "sysvinit"
|
||||
}:
|
||||
|
||||
let
|
||||
constructors = import ../examples/services-agnostic/constructors.nix {
|
||||
inherit pkgs stateDir runtimeDir logDir tmpDir cacheDir forceDisableUserChange processManager;
|
||||
};
|
||||
in
|
||||
rec {
|
||||
extendableSupervisord = {
|
||||
pkg = constructors.extendableSupervisord {};
|
||||
};
|
||||
}
|
|
@ -0,0 +1,164 @@
|
|||
{nixpkgs ? <nixpkgs>}:
|
||||
|
||||
with import "${nixpkgs}/nixos/lib/testing-python.nix" { system = builtins.currentSystem; };
|
||||
|
||||
let
|
||||
supervisordProcessEnv = import ../nixproc/create-managed-process/systemd/build-systemd-env.nix {
|
||||
exprFile = ./processes-supervisord.nix;
|
||||
};
|
||||
|
||||
processesEnvForeground = import ../nixproc/create-managed-process/supervisord/build-supervisord-env.nix {
|
||||
exprFile = ../examples/webapps-agnostic/processes.nix;
|
||||
extraParams = {
|
||||
webappMode = "foreground";
|
||||
};
|
||||
};
|
||||
|
||||
processesEnvDaemon = import ../nixproc/create-managed-process/supervisord/build-supervisord-env.nix {
|
||||
exprFile = ../examples/webapps-agnostic/processes.nix;
|
||||
extraParams = {
|
||||
webappMode = "daemon";
|
||||
};
|
||||
};
|
||||
|
||||
processesEnvAuto = import ../nixproc/create-managed-process/supervisord/build-supervisord-env.nix {
|
||||
exprFile = ../examples/webapps-agnostic/processes.nix;
|
||||
};
|
||||
|
||||
tools = import ../tools {};
|
||||
|
||||
nix-processmgmt = ./..;
|
||||
|
||||
env = "NIX_PATH=nixpkgs=${nixpkgs} SYSTEMD_TARGET_DIR=/etc/systemd-mutable/system SUPERVISORD_CONF_DIR=/var/lib/supervisord";
|
||||
in
|
||||
makeTest {
|
||||
machine =
|
||||
{pkgs, ...}:
|
||||
|
||||
{
|
||||
virtualisation.pathsInNixDB = [ pkgs.stdenv ] ++ pkgs.coreutils.all ++ [ supervisordProcessEnv processesEnvForeground processesEnvDaemon processesEnvAuto ];
|
||||
virtualisation.writableStore = true;
|
||||
virtualisation.memorySize = 1024;
|
||||
|
||||
users.extraUsers = {
|
||||
unprivileged = {
|
||||
uid = 1000;
|
||||
group = "users";
|
||||
shell = "/bin/sh";
|
||||
description = "Unprivileged user";
|
||||
home = "/home/unprivileged";
|
||||
createHome = true;
|
||||
};
|
||||
};
|
||||
|
||||
# We can't download any substitutes in a test environment. To make tests
|
||||
# faster, we disable substitutes so that Nix does not waste any time by
|
||||
# attempting to download them.
|
||||
nix.extraOptions = ''
|
||||
substitute = false
|
||||
'';
|
||||
|
||||
environment.systemPackages = [
|
||||
pkgs.stdenv
|
||||
pkgs.pythonPackages.supervisor
|
||||
pkgs.dysnomia
|
||||
tools.build
|
||||
tools.systemd
|
||||
tools.supervisord
|
||||
];
|
||||
};
|
||||
|
||||
testScript = ''
|
||||
def check_nginx_redirection():
|
||||
machine.succeed(
|
||||
"curl --fail -H 'Host: webapp.local' http://localhost:8080 | grep 'listening on port: 5000'"
|
||||
)
|
||||
|
||||
|
||||
def check_system_unavailable():
|
||||
machine.fail("curl --fail http://localhost:8080")
|
||||
machine.fail("pgrep -f '/bin/webapp'")
|
||||
|
||||
|
||||
def check_nginx_multi_instance_redirection():
|
||||
machine.succeed(
|
||||
"curl --fail -H 'Host: webapp1.local' http://localhost:8080 | grep 'listening on port: 5000'"
|
||||
)
|
||||
machine.succeed(
|
||||
"curl --fail -H 'Host: webapp5.local' http://localhost:8081 | grep 'listening on port: 6002'"
|
||||
)
|
||||
|
||||
|
||||
start_all()
|
||||
|
||||
machine.succeed("mkdir -p /etc/systemd-mutable/system")
|
||||
|
||||
# Deploy supervisord as a systemd unit
|
||||
|
||||
machine.succeed(
|
||||
"${env} nixproc-systemd-switch ${nix-processmgmt}/tests/processes-supervisord.nix"
|
||||
)
|
||||
|
||||
# Deploy the system with foreground webapp processes
|
||||
|
||||
machine.succeed(
|
||||
'${env} nixproc-supervisord-switch ${nix-processmgmt}/examples/webapps-agnostic/processes.nix --extra-params \'{ "webappMode" = "foreground"; }\'${""}'
|
||||
)
|
||||
|
||||
machine.succeed("sleep 1")
|
||||
machine.succeed("pgrep -u webapp -f '/bin/webapp$'")
|
||||
|
||||
check_nginx_redirection()
|
||||
|
||||
# Deploy the system with daemon webapp processes
|
||||
|
||||
machine.succeed(
|
||||
'${env} nixproc-supervisord-switch ${nix-processmgmt}/examples/webapps-agnostic/processes.nix --extra-params \'{ "webappMode" = "daemon"; }\'${""}'
|
||||
)
|
||||
|
||||
machine.succeed("sleep 1")
|
||||
machine.succeed("pgrep -u webapp -f '/bin/webapp -D$'")
|
||||
|
||||
check_nginx_redirection()
|
||||
|
||||
# Deploy the entire system in auto mode. Should result in foreground webapp processes
|
||||
|
||||
machine.succeed(
|
||||
"${env} nixproc-supervisord-switch ${nix-processmgmt}/examples/webapps-agnostic/processes.nix"
|
||||
)
|
||||
|
||||
machine.succeed("sleep 1")
|
||||
machine.succeed("pgrep -u webapp -f '/bin/webapp$'")
|
||||
|
||||
check_nginx_redirection()
|
||||
|
||||
# Deploy the advanced example with multiple instances and see if it works
|
||||
|
||||
machine.succeed(
|
||||
"${env} nixproc-supervisord-switch ${nix-processmgmt}/examples/webapps-agnostic/processes-advanced.nix"
|
||||
)
|
||||
|
||||
machine.succeed("sleep 1")
|
||||
|
||||
check_nginx_multi_instance_redirection()
|
||||
|
||||
# Deploy an instance without changing user privileges
|
||||
|
||||
machine.succeed(
|
||||
"${env} nixproc-supervisord-switch ${nix-processmgmt}/examples/webapps-agnostic/processes.nix --force-disable-user-change"
|
||||
)
|
||||
|
||||
machine.succeed("sleep 1")
|
||||
machine.succeed("pgrep -u root -f '/bin/webapp$'")
|
||||
|
||||
check_nginx_redirection()
|
||||
|
||||
# Undeploy the system
|
||||
|
||||
machine.succeed(
|
||||
"${env} nixproc-supervisord-switch ${nix-processmgmt}/examples/webapps-agnostic/processes-empty.nix"
|
||||
)
|
||||
|
||||
check_system_unavailable()
|
||||
'';
|
||||
}
|
Loading…
Reference in New Issue