nixos/service-runner.nix: Allow quotes in commands + test

This commit is contained in:
Robert Hensing 2020-02-28 12:17:01 +01:00
parent e97dfe73bb
commit 43521ac965
3 changed files with 57 additions and 8 deletions

View file

@ -12,7 +12,10 @@ let
sub run {
my ($cmd) = @_;
my @args = split " ", $cmd;
my @args = ();
while ($cmd =~ /([^ \t\n']+)|(\'([^'])\')\s*/g) {
push @args, $1;
}
my $prog;
if (substr($args[0], 0, 1) eq "@") {
$prog = substr($args[0], 1);
@ -48,15 +51,20 @@ let
'') service.environment)}
# Run the ExecStartPre program. FIXME: this could be a list.
my $preStart = '${service.serviceConfig.ExecStartPre or ""}';
if ($preStart ne "") {
my $preStart = <<END_CMD;
${service.serviceConfig.ExecStartPre or ""}
END_CMD
if (defined $preStart && $preStart ne "\n") {
print STDERR "running ExecStartPre: $preStart\n";
my $res = run_wait $preStart;
die "$0: ExecStartPre failed with status $res\n" if $res;
};
# Run the ExecStart program.
my $cmd = '${service.serviceConfig.ExecStart}';
my $cmd = <<END_CMD;
${service.serviceConfig.ExecStart}
END_CMD
print STDERR "running ExecStart: $cmd\n";
my $mainPid = run $cmd;
$ENV{'MAINPID'} = $mainPid;
@ -70,8 +78,10 @@ let
$SIG{'QUIT'} = \&intHandler;
# Run the ExecStartPost program.
my $postStart = '${service.serviceConfig.ExecStartPost or ""}';
if ($postStart ne "") {
my $postStart = <<END_CMD;
${service.serviceConfig.ExecStartPost or ""}
END_CMD
if (defined $postStart && $postStart ne "\n") {
print STDERR "running ExecStartPost: $postStart\n";
my $res = run_wait $postStart;
die "$0: ExecStartPost failed with status $res\n" if $res;
@ -82,8 +92,10 @@ let
my $mainRes = $?;
# Run the ExecStopPost program.
my $postStop = '${service.serviceConfig.ExecStopPost or ""}';
if ($postStop ne "") {
my $postStop = <<END_CMD;
${service.serviceConfig.ExecStopPost or ""}
END_CMD
if (defined $postStop && $postStop ne "\n") {
print STDERR "running ExecStopPost: $postStop\n";
my $res = run_wait $postStop;
die "$0: ExecStopPost failed with status $res\n" if $res;

View file

@ -262,6 +262,7 @@ in
samba = handleTest ./samba.nix {};
sanoid = handleTest ./sanoid.nix {};
sddm = handleTest ./sddm.nix {};
service-runner = handleTest ./service-runner.nix {};
shiori = handleTest ./shiori.nix {};
signal-desktop = handleTest ./signal-desktop.nix {};
simple = handleTest ./simple.nix {};

View file

@ -0,0 +1,36 @@
import ./make-test-python.nix ({ pkgs, ... }: {
name = "service-runner";
meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ roberth ];
};
nodes = {
machine = { pkgs, lib, ... }: {
services.nginx.enable = true;
services.nginx.virtualHosts.machine.root = pkgs.runCommand "webroot" {} ''
mkdir $out
echo 'yay' >$out/index.html
'';
systemd.services.nginx.enable = false;
};
};
testScript = { nodes, ... }: ''
url = "http://localhost/index.html"
with subtest("check systemd.services.nginx.runner"):
machine.fail(f"curl {url}")
machine.succeed(
"""
mkdir -p /run/nginx /var/spool/nginx/logs
${nodes.machine.config.systemd.services.nginx.runner} &
echo $!>my-nginx.pid
"""
)
machine.wait_for_open_port(80)
machine.succeed(f"curl {url}")
machine.succeed("kill -INT $(cat my-nginx.pid)")
machine.wait_for_closed_port(80)
'';
})