Fix stopping daemons, write preliminary testcases for s6-rc

This commit is contained in:
Sander van der Burg 2021-01-19 22:07:25 +01:00 committed by Sander van der Burg
parent e123cbdaa3
commit b773dfe295
6 changed files with 219 additions and 2 deletions

View File

@ -32,6 +32,8 @@ Currently, the following process managers are supported:
* `systemd`: [systemd](https://www.freedesktop.org/wiki/Software/systemd)
* `launchd`: [launchd](https://www.launchd.info)
* `cygrunsrv`: Cygwin's [cygrunsrv](http://web.mit.edu/cygwin/cygwin_v1.3.2/usr/doc/Cygwin/cygrunsrv.README)
* `s6-rc`: the [s6-rc](https://skarnet.org/software/s6-rc) tool that manages
services, using [s6](https://skarnet.org/software/s6) as a supervising process
It can also work with the following solutions that are technically not
categorized as process managers (but can still be used as such):

View File

@ -109,4 +109,9 @@ in
inherit createManagedProcess;
inherit (pkgs) docker kmod;
};
s6-svscan = import ./s6-svscan.nix {
inherit createManagedProcess runtimeDir;
inherit (pkgs) s6;
};
}

View File

@ -0,0 +1,12 @@
{createManagedProcess, s6, runtimeDir}:
{instanceSuffix ? "", instanceName ? "s6-svscan${instanceSuffix}", scanDir ? "${runtimeDir}/service${instanceSuffix}"}:
createManagedProcess {
name = instanceName;
path = [ s6 ];
foregroundProcess = "${s6}/bin/s6-svscan";
args = [ scanDir ];
initialize = ''
mkdir -p ${scanDir}
'';
}

View File

@ -127,8 +127,9 @@ else if daemon != null then
text = ''
#!${execline}/bin/execlineb -P
backtick pid { cat ${pidFile} }
kill $pid
backtick PID { cat ${pidFile} }
importas -i PID PID
kill $PID
'';
};
};

View File

@ -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 {
s6-svscan = {
pkg = constructors.s6-svscan {};
};
}

View File

@ -0,0 +1,176 @@
{nixpkgs ? <nixpkgs>}:
with import "${nixpkgs}/nixos/lib/testing-python.nix" { system = builtins.currentSystem; };
let
s6-svscanProcessEnv = import ../nixproc/create-managed-process/systemd/build-systemd-env.nix {
exprFile = ./processes-s6-svscan.nix;
};
processesEnvForeground = import ../nixproc/create-managed-process/s6-rc/build-s6-rc-env.nix {
exprFile = ../examples/webapps-agnostic/processes.nix;
extraParams = {
webappMode = "foreground";
};
};
processesEnvDaemon = import ../nixproc/create-managed-process/s6-rc/build-s6-rc-env.nix {
exprFile = ../examples/webapps-agnostic/processes.nix;
extraParams = {
webappMode = "daemon";
};
};
processesEnvAuto = import ../nixproc/create-managed-process/s6-rc/build-s6-rc-env.nix {
exprFile = ../examples/webapps-agnostic/processes.nix;
};
processesEnvAutoUnprivileged = import ../nixproc/create-managed-process/s6-rc/build-s6-rc-env.nix {
exprFile = ../examples/webapps-agnostic/processes.nix;
forceDisableUserChange = true;
};
processesEnvAdvanced = import ../nixproc/create-managed-process/s6-rc/build-s6-rc-env.nix {
exprFile = ../examples/webapps-agnostic/processes-advanced.nix;
};
processesEnvEmpty = import ../nixproc/create-managed-process/s6-rc/build-s6-rc-env.nix {
exprFile = null;
};
tools = import ../tools {};
nix-processmgmt = ./..;
env = "NIX_PATH=nixpkgs=${nixpkgs} SYSTEMD_TARGET_DIR=/etc/systemd-mutable/system";
in
makeTest {
machine =
{pkgs, ...}:
{
virtualisation.pathsInNixDB = [ pkgs.stdenv ] ++ pkgs.coreutils.all ++ [
s6-svscanProcessEnv
processesEnvForeground
processesEnvDaemon
processesEnvAuto
processesEnvAutoUnprivileged
processesEnvAdvanced
processesEnvEmpty
];
virtualisation.writableStore = true;
virtualisation.memorySize = 1024;
# 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.s6
pkgs.s6-rc
pkgs.dysnomia
tools.common
tools.s6-rc
tools.systemd
];
};
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: 5001'"
)
machine.succeed(
"curl --fail -H 'Host: webapp5.local' http://localhost:8081 | grep 'listening on port: 5005'"
)
start_all()
machine.succeed("mkdir -p /etc/systemd-mutable/system")
# Deploy s6-svscan as a systemd unit
machine.succeed(
"${env} nixproc-systemd-switch ${nix-processmgmt}/tests/processes-s6-svscan.nix"
)
# Deploy the system with foreground webapp processes
machine.succeed(
'${env} nixproc-s6-rc-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-s6-rc-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-s6-rc-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-s6-rc-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-s6-rc-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-s6-rc-switch --undeploy"
)
check_system_unavailable()
'';
}