summaryrefslogtreecommitdiffstats
path: root/controller/bin/dtf-controller.in
blob: e8fc5c8c432ac6958d45c7c30284337f72f19001 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/perl

use strict;
use warnings;
use utf8;

use Data::Dumper;
use Encode 'encode_utf8';
use File::Temp qw/ :mktemp /;
use Getopt::Long        qw(:config auto_help);

use POSIX ":sys_wait_h";
use POSIX qw(strftime);

# yaml (quick) parser
use YAML::Syck;

our @children;

our $opt_runfile = "runfile.yml";

sub load_runfile
{
    open my $fd, '<', $opt_runfile
            or die "can't open yaml file '$opt_runfile'";
    my $config = YAML::Syck::LoadFile($fd)
            or die "can't parse '$opt_runfile'";

    # print Dumper $config;
    return $config;
}

sub subcommand
{
    my $cmd = $_[0];
    my $out_file = $_[1] . ".stdout";
    my $err_file = $_[1] . ".stderr";

    my $pid = fork();
    if ($pid eq 0) {
        open (STDOUT, ">", "$out_file") or die "can not open out log";
        open (STDERR, ">", "$err_file") or die "can not open err log";
        exec @$cmd or die "can't exec";
    } else {
        $SIG{INT} = 'IGNORE';
        wait;
        return $? >> 8;
    }
}

sub child_task
{
    my $run = $_[0];
    my $dir = $_[1];
    my $config = $_[2];

    my $task = "$run->{distro}-$run->{distro_version}";

    my $rc = subcommand [
        '@bindir@/dtf-run-remote',
        '--taskdir',
        $run->{taskdir},
        '--workdir',
        $dir,
    ], "$dir/dtf-run-remote";

    # Note that the 'dtf-run-remote' must return EXIT_SUCCESS even if some of
    # its tests failed.
    if ($rc ne 0) {
        print STDERR "$task: failed dtf-run-remote";
        exit (1);
    }

    my $resultdir = "$config->{db}/$run->{distro}/"
                  . "$run->{distro_version}/$run->{arch}/"
                  . "results_$config->{starttime}-$run->{stamp}";

    $rc = subcommand [
        '@libexecdir@/dtf-commit-results',
        "$dir",
        "$config->{db}/$run->{distro}/$run->{distro_version}/$run->{arch}/",
    ], "$resultdir";
    if ($rc ne 0) {
        print STDERR "$task: failed dtf-commit-results";
        exit (1);
    }
}

sub main
{
    my $config = load_runfile;

    print $config->{description}. "\n";

    $config->{starttime} = strftime "%Y%m%d_%H%M%S", gmtime;

    for my $run (@{$config->{runs}}) {
        # dies on error - which is OK
        my $dir = File::Temp->newdir("/tmp/dtf-controller-XXXXXX", CLEANUP => 0);

        ($run->{stamp} = $dir) =~ s/.*-//;

        my $pid = fork();
        if ($pid == 0) {

            child_task($run, $dir, $config);
            ## child ##
            exit (0);
        }
        else {
            push @children, $pid;
        }
    }

    $SIG{INT} = sub {
        print ".. waiting for children SIGINTing\n";
    };

    # wait for all pseudo sub-processes
    my $child;
    do {
        sleep (0.5);
        $child = waitpid (-1, 0);
    } while ($child > 0);

    print "Finished..";
}

GetOptions(
    "runfile=s",
) || exit (1);

main;