]> mj.ucw.cz Git - bex.git/blob - brun
Per-job logs
[bex.git] / brun
1 #!/usr/bin/perl
2 # Batch EXecutor 2.0 -- Run Queued Jobs
3 # (c) 2011 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7 use Getopt::Long;
8
9 use lib 'lib';
10 use BEX;
11
12 my $given_job;
13 my $queue_name;
14 my $status_fifo;
15
16 GetOptions(
17         "j|job=s" => \$given_job,
18         "q|queue=s" => \$queue_name,
19         "s|status-fifo=s" => \$status_fifo,
20 ) or die <<AMEN ;
21 Usage: brun [<options>] [[!]<machine-or-class> ...]
22
23 Options:
24 -j, --job=<id>          Run only the specified job
25 -q, --queue=<name>      Select job queue
26     --status-fifo=<f>   Send status updates to the given named pipe
27 AMEN
28
29 my $status_fd;
30 if (defined $status_fifo) {
31         open $status_fd, '>>', $status_fifo or die "Cannot open status FIFO: $!";
32         autoflush $status_fd, 1;
33 }
34
35 sub update_status($$$$;$) {
36         my ($mach, $job, $status, $log_on_queue, $msg) = @_;
37         if ($status_fd) {
38                 print $status_fd "! $mach $job $status\n";
39         }
40         if ($log_on_queue) {
41                 $log_on_queue->log($mach, $job, $status, $msg);
42         }
43 }
44
45 sub ping_machine($) {
46         my ($mach) = @_;
47         update_status($mach, '-', 'PING', undef);
48         `ping -c1 -n $mach >/dev/null 2>/dev/null`;
49         return !$?;
50 }
51
52 sub run_job($$$) {
53         my ($job, $queue, $mach) = @_;
54         my $jid = $job->{'ID'};
55
56         my $tmp = $queue->temp_file($mach, $jid);
57         open T, '>', $tmp or die;
58         if (defined $BEX::Config::job_prolog) {
59                 open P, $BEX::Config::job_prolog or return ('INTERR', "Cannot open prolog: $!");
60                 while (<P>) { print T; }
61                 close P;
62         } else {
63                 print T "#!/bin/sh\n";
64         }
65         print T "# BEX job ", $jid, "\n";
66         print T $job->{'body'};
67         if (defined $BEX::Config::job_epilog) {
68                 open E, $BEX::Config::job_epilog or return ('INTERR', "Cannot open epilog: $!");
69                 while (<E>) { print T; }
70                 close E;
71         }
72         close T;
73
74         update_status($mach, $jid, 'SEND', undef);
75         my $cmd = 't=$(mktemp -t bex-XXXXXXXX) && cat >$t && chmod u+x $t && echo $t';
76         my $rtmp = `ssh <$tmp $mach '$cmd'`;
77         !$? && defined($rtmp) && $rtmp ne '' or return ('NOXFER', 'Transfer failed');
78         chomp $rtmp;
79
80         update_status($mach, $jid, 'RUN', $queue);
81         my $lf = $queue->log_file($mach, $jid);
82         system 'bash', '-o', 'pipefail', '-c', "ssh -t $mach '$rtmp ; e=\$? ; rm -f $rtmp ; exit \$e' 2>&1 | tee -a $lf";
83         if ($?) {
84                 return ('FAILED', 'Job failed');
85         } else {
86                 return ('OK', undef);
87         }
88 }
89
90 my @machines = BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*');
91 my $queue = BEX::Queue->new($queue_name);
92
93 for my $mach (@machines) {
94         my @q = $queue->scan($mach) or next;
95         update_status($mach, '-', 'INIT', undef);
96         my $ping;
97         for my $jid (@q) {
98                 if (defined $given_job) {
99                         $jid eq $given_job or next;
100                 }
101                 my $job = BEX::Job->new_from_file($queue->job_file($jid));
102                 my $stat = {
103                         'Time' => time,
104                 };
105                 print "### Running $jid (", $job->attr('Subject'), ") on $mach ###\n";
106                 $ping //= ping_machine($mach);
107                 my ($s, $msg);
108                 if (!$ping) {
109                         ($s, $msg) = ('NOPING', 'Does not ping');
110                 } else {
111                         ($s, $msg) = run_job($job, $queue, $mach);
112                 }
113                 update_status($mach, $jid, $s, $queue, $msg);
114
115                 if ($s eq 'OK') {
116                         print "+++ OK\n";
117                         $queue->remove($mach, $jid);
118                 } else {
119                         print "--- $s: $msg\n";
120                         $stat->{'Status'} = $s;
121                         $stat->{'Message'} = $msg;
122                         $queue->write_job_status($mach, $jid, $stat);
123                 }
124         }
125         update_status($mach, '-', 'DONE', undef);
126 }