Merge pull request #81292 from hercules-ci/fix-service-runner-quotes

nixos/service-runner.nix: Allow quotes in commands + test
This commit is contained in:
Robert Hensing 2020-03-03 14:31:00 +01:00 committed by GitHub
commit 6734e58da3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 8 deletions

View File

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

View File

@ -263,6 +263,7 @@ in
samba = handleTest ./samba.nix {}; samba = handleTest ./samba.nix {};
sanoid = handleTest ./sanoid.nix {}; sanoid = handleTest ./sanoid.nix {};
sddm = handleTest ./sddm.nix {}; sddm = handleTest ./sddm.nix {};
service-runner = handleTest ./service-runner.nix {};
shiori = handleTest ./shiori.nix {}; shiori = handleTest ./shiori.nix {};
signal-desktop = handleTest ./signal-desktop.nix {}; signal-desktop = handleTest ./signal-desktop.nix {};
simple = handleTest ./simple.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)
'';
})