]> mj.ucw.cz Git - bex.git/blob - lib/bin/bex-run
bex prun: Squashed uninitialized var warning
[bex.git] / lib / bin / bex-run
1 #!/usr/bin/perl
2 # Batch EXecutor 3.0 -- Run Queued Jobs
3 # (c) 2011-2012 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7 use Getopt::Long;
8 use BEX;
9
10 sub usage() {
11         print <<AMEN ;
12 Usage: bex run [<options>] [[!]<machine-or-class> ...]
13
14 Options:
15 -j, --job=<id>          Run only the specified job
16 -q, --queue=<name>      Select job queue
17     --status-fifo=<f>   Send status updates to the given named pipe
18 AMEN
19         exit 0;
20 }
21
22 my $given_job;
23 my $queue_name;
24 my $status_fifo;
25
26 GetOptions(
27         "j|job=s" => \$given_job,
28         "q|queue=s" => \$queue_name,
29         "s|status-fifo=s" => \$status_fifo,
30         "help" => \&usage,
31 ) or die "Try `bex run --help' for more information.\n";
32
33 # We do not want SIGPIPE on writes to the status FIFO
34 $SIG{'PIPE'} = 'IGNORE';
35
36 my $status_fd;
37 if (defined $status_fifo) {
38         open $status_fd, '>>', $status_fifo or die "Cannot open status FIFO: $!";
39         autoflush $status_fd, 1;
40 }
41
42 sub update_status($$$$;$) {
43         my ($mach, $job, $status, $log_on_queue, $msg) = @_;
44         if ($status_fd) {
45                 print $status_fd "! $mach $job $status\n";
46         }
47         if ($log_on_queue) {
48                 $log_on_queue->update_job_status($mach, $job, $status, $msg);
49         }
50 }
51
52 my %pings;
53
54 sub ping_machine($) {
55         my ($mach) = @_;
56         if (!defined $pings{$mach}) {
57                 if ($BEX::Config::ping_hosts) {
58                         update_status($mach, '-', 'PING', undef);
59                         my $host = BEX::Config::host_name($mach);
60                         `ping -c1 -n $host >/dev/null 2>/dev/null`;
61                         $pings{$mach} = !$?;
62                 } else {
63                         $pings{$mach} = 1;
64                 }
65         }
66         if ($pings{$mach}) {
67                 return ('OK', undef);
68         } else {
69                 return ('NOPING', 'Does not ping');
70         }
71 }
72
73 sub exit_status($) {
74         my ($s) = @_;
75         if ($s >> 8) {
76                 return "with exit code " . ($s >> 8);
77         } else {
78                 return "on fatal signal " . ($s & 127);
79         }
80 }
81
82 sub run_job_prep($$$) {
83         my ($job, $queue, $mach) = @_;
84         my $prep = $job->attr('Prep');
85         defined($prep) && $prep !~ /^\s*$/ or return 'OK';
86
87         my $jid = $job->id;
88         update_status($mach, $jid, 'PREP', $queue);
89         my $lf = $queue->log_file($mach, $jid);
90         $ENV{'HOST'} = BEX::Config::host_name($mach);
91         system 'bash', '-o', 'pipefail', '-c', "( $prep ) 2>&1 | tee -a $lf";
92         delete $ENV{'HOST'};
93         if ($?) {
94                 return ('PREPFAIL', 'Preparatory command failed ' . exit_status($?));
95         } else {
96                 return 'OK';
97         }
98 }
99
100 sub run_job_body($$$) {
101         my ($job, $queue, $mach) = @_;
102
103         if ($job->attr('body') =~ /^\s*$/s) {
104                 # Shortcut if the body is empty
105                 return 'OK'
106         }
107
108         my $host = BEX::Config::host_name($mach);
109         my $jid = $job->id;
110
111         my $tmp = $queue->temp_file($mach, $jid);
112         open T, '>', $tmp or die;
113         if (defined $BEX::Config::job_prolog) {
114                 open P, $BEX::Config::job_prolog or return ('INTERR', "Cannot open prolog: $!");
115                 while (<P>) { print T; }
116                 close P;
117         } else {
118                 print T "#!/bin/sh\n";
119         }
120         print T "# BEX job ", $jid, "\n";
121         print T $job->attr('body');
122         if (defined $BEX::Config::job_epilog) {
123                 open E, $BEX::Config::job_epilog or return ('INTERR', "Cannot open epilog: $!");
124                 while (<E>) { print T; }
125                 close E;
126         }
127         close T;
128
129         update_status($mach, $jid, 'SEND', undef);
130         my $cmd = 't=$(mktemp -t bex-XXXXXXXX) && cat >$t && chmod u+x $t && echo $t';
131         my $rtmp = `$BEX::Config::ssh_command <$tmp $host '$cmd'`;
132         !$? && defined($rtmp) && $rtmp ne '' or return ('NOXFER', 'Transfer failed');
133         chomp $rtmp;
134
135         update_status($mach, $jid, 'RUN', $queue);
136         my $lf = $queue->log_file($mach, $jid);
137         system 'bash', '-o', 'pipefail', '-c', "$BEX::Config::ssh_command -t $host '$rtmp ; e=\$? ; rm -f $rtmp ; exit \$e' 2>&1 | tee -a $lf";
138         if ($?) {
139                 return ('FAILED', 'Job failed ' . exit_status($?));
140         } else {
141                 return 'OK';
142         }
143 }
144
145 sub run_job($$$) {
146         my ($job, $queue, $mach) = @_;
147         my ($stat, $msg);
148
149         ($stat, $msg) = ping_machine($mach);
150         $stat eq 'OK' or return ($stat, $msg);
151
152         ($stat, $msg) = run_job_prep($job, $queue, $mach);
153         $stat eq 'OK' or return ($stat, $msg);
154
155         return run_job_body($job, $queue, $mach);
156 }
157
158 my @machines = BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*');
159 my $queue = BEX::Queue->new($queue_name);
160
161 $queue->lock(undef, undef) or die "The queue is locked by another bex run, cannot continue.\n";
162
163 for my $mach (@machines) {
164         my @q = $queue->scan($mach) or next;
165         if (!$queue->lock($mach, undef)) {
166                 print "### Machine $mach is locked by another bex run, skipping...\n";
167                 update_status($mach, '-', 'LOCKED', undef);
168                 update_status($mach, '-', 'DONE', undef);
169                 next;
170         }
171         update_status($mach, '-', 'INIT', undef);
172         while (my $jid = shift @q) {
173                 if (defined $given_job) {
174                         $jid eq $given_job or next;
175                 }
176                 my $job = BEX::Job->new_from_file($queue->job_file($jid));
177                 update_status($mach, $jid, 'INIT', undef);
178                 if (!$queue->lock($mach, $jid)) {
179                         print "### Skipping locked $jid on $mach ###\n";
180                         update_status($mach, $jid, 'LOCKED', undef);
181                         next;
182                 }
183                 print "### Running ", $job->name, " on $mach ###\n";
184                 my ($s, $msg) = run_job($job, $queue, $mach);
185                 update_status($mach, $jid, $s, $queue, $msg);
186
187                 if ($s eq 'OK') {
188                         print "+++ OK\n";
189                         $queue->remove($mach, $jid);
190                 } else {
191                         print "--- $s: $msg\n";
192                         if ($BEX::Config::skip_on_fail) {
193                                 print "### Skipping other jobs on the same host ###\n" if @q;
194                                 last;
195                         }
196                 }
197         }
198 } continue {
199         update_status($mach, '-', 'DONE', undef);
200 }
201 $queue->unlock;