pgstatus.pl update
[buildfarm-server.git] / cgi-bin / pgstatus.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 use CGI;
6 use Digest::SHA1  qw(sha1_hex);
7 use MIME::Base64;
8 use DBI;
9 use DBD::Pg;
10 use Data::Dumper;
11 use Mail::Send;
12 use Safe;
13
14 use vars qw($dbhost $dbname $dbuser $dbpass $dbport
15        $all_stat $fail_stat $change_stat $green_stat
16 );
17
18 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
19
20 die "no dbname" unless $dbname;
21 die "no dbuser" unless $dbuser;
22
23 my $dsn="dbi:Pg:dbname=$dbname";
24 $dsn .= ";host=$dbhost" if $dbhost;
25 $dsn .= ";port=$dbport" if $dbport;
26
27 my $query = new CGI;
28
29 my $sig = $query->path_info;
30 $sig =~ s!^/!!;
31
32 my $stage = $query->param('stage');
33 my $ts = $query->param('ts');
34 my $animal = $query->param('animal');
35 my $log = $query->param('log');
36 my $res = $query->param('res');
37 my $conf = $query->param('conf');
38 my $branch = $query->param('branch');
39 my $changed_since_success = $query->param('changed_since_success');
40 my $changed_this_run = $query->param('changed_files');
41 my $log_archive = $query->param('logtar');
42
43 my $content = 
44         "branch=$branch&res=$res&stage=$stage&animal=$animal&".
45         "ts=$ts&log=$log&conf=$conf";
46
47 my $extra_content = 
48         "changed_files=$changed_this_run&".
49         "changed_since_success=$changed_since_success&";
50
51 unless ($animal && $ts && $stage && $sig)
52 {
53         print 
54             "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
55             "bad parameters for request\n";
56         exit;
57         
58 }
59
60
61 my $db = DBI->connect($dsn,$dbuser,$dbpass);
62
63 die $DBI::errstr unless $db;
64
65 my $gethost=
66     "select secret from buildsystems where name = ? and status = 'approved'";
67 my $sth = $db->prepare($gethost);
68 $sth->execute($animal);
69 my ($secret)=$sth->fetchrow_array();
70 $sth->finish;
71
72 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
73 $year += 1900; $mon +=1;
74 my $date=
75     sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
76
77 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
78 {
79     open(TX,">../buildlogs/$animal.$date");
80     print TX "sig=$sig\nlogtar-len=" , length($log_archive),
81         "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
82         "changed_this_run:\n$changed_this_run\n",
83         "changed_since_success:\n$changed_since_success\n",
84         "log:\n",$log;
85 #    $query->save(\*TX);
86     close(TX);
87 }
88
89 unless ($ts < time)
90 {
91     my $gmt = gmtime($ts);
92     print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
93     "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
94         $db->disconnect;
95     exit;
96 }
97
98 unless ($ts + 86400 > time)
99 {
100     my $gmt = gmtime($ts);
101     print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
102      "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
103     $db->disconnect;
104     exit;
105 }
106
107 unless ($secret)
108 {
109         print 
110             "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
111             "System $animal is unknown\n";
112         $db->disconnect;
113         exit;
114         
115 }
116
117
118
119
120 my $calc_sig = sha1_hex($content,$secret);
121 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
122
123 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
124 {
125
126         print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
127         print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
128         $db->disconnect;
129         exit;
130 }
131
132 # undo escape-proofing of base64 data and decode it
133 map {tr/$@/+=/; $_ = decode_base64($_); } 
134     ($log, $conf,$changed_this_run,$changed_since_success,$log_archive);
135
136 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
137 $year += 1900; $mon +=1;
138 my $dbdate=
139     sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
140
141 my $log_file_names;
142 my @log_file_names;
143 my $dirname = "../buildlogs/tmp.$$.unpacklogs";
144
145 if ($log_archive)
146 {
147     my $log_handle;
148     my $archname = "../buildlogs/tmp.$$.tgz";
149     open($log_handle,">$archname");
150     binmode $log_handle;
151     print $log_handle $log_archive;
152     close $log_handle;
153     mkdir $dirname;
154     @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
155     map {s/\s+//g; } @log_file_names;
156     my @qnames = @log_file_names;
157     map { $_ = qq("$_"); } @qnames;
158     $log_file_names = '{' . join(',',@qnames) . '}';
159     # unlink $archname;
160 }
161
162 my $config_flags;
163 my $container = new Safe;
164 my $sconf = $conf; 
165 unless ($sconf =~ s/.*(\$Script_Config)/$1/ms )
166 {
167     $sconf = '$Script_Config={};';
168 }
169 my $client_conf = $container->reval("$sconf;");
170
171 my @config_flags = @{ $client_conf->{config_opts} || [] };
172 if (@config_flags)
173 {
174     @config_flags = grep {! m/=/ } @config_flags;
175     map {s/\s+//g; $_=qq("$_"); } @config_flags;
176     $config_flags = '{' . join(',',@config_flags) . '}' ;
177 }
178
179
180 my $logst = <<EOSQL;
181     insert into build_status 
182       (sysname, snapshot,status, stage, log,conf_sum, branch,
183        changed_this_run, changed_since_success, 
184        log_archive_filenames , log_archive, build_flags)
185     values(?,?,?,?,?,?,?,?,?,?,?,?)
186 EOSQL
187 ;
188 $sth=$db->prepare($logst);
189
190 $sth->bind_param(1,$animal);
191 $sth->bind_param(2,$dbdate);
192 $sth->bind_param(3,$res);
193 $sth->bind_param(4,$stage);
194 $sth->bind_param(5,$log);
195 $sth->bind_param(6,$conf);
196 $sth->bind_param(7,$branch);
197 $sth->bind_param(8,$changed_this_run);
198 $sth->bind_param(9,$changed_since_success);
199 $sth->bind_param(10,$log_file_names);
200 $sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
201 $sth->bind_param(12,$config_flags);
202
203 $sth->execute;
204 $sth->finish;
205
206 my $logst2 = <<EOSQL;
207
208   insert into build_status_log 
209     (sysname, snapshot, branch, log_stage, log_text, stage_duration)
210     values (?, ?, ?, ?, ?, ?)
211
212 EOSQL
213     ;
214
215 $sth = $db->prepare($logst2);
216
217 $/=undef;
218
219 my $stage_start = $ts;
220
221 foreach my $log_file( @log_file_names )
222 {
223   my $handle;
224   open($handle,"$dirname/$log_file");
225   my $mtime = (stat $handle)[9];
226   my $stage_interval = $mtime - $stage_start;
227   $stage_start = $mtime;
228   my $ltext = <$handle>;
229   close($handle);
230   $sth->execute($animal,$dbdate,$branch,$log_file,$ltext, 
231                 "$stage_interval seconds");
232 }
233
234
235 $sth->finish;
236
237 my $prevst = <<EOSQL;
238
239   select coalesce((select distinct on (snapshot) stage
240                   from build_status
241                   where sysname = ? and branch = ? and snapshot < ?
242                   order by snapshot desc
243                   limit 1), 'NEW') as prev_status
244   
245 EOSQL
246
247 $sth=$db->prepare($prevst);
248 $sth->execute($animal,$branch,$dbdate);
249 my $row=$sth->fetchrow_arrayref;
250 my $prev_stat=$row->[0];
251 $sth->finish;
252
253 my $det_st = <<EOS;
254
255           select operating_system|| ' / ' || os_version as os , 
256                  compiler || ' / ' || compiler_version as compiler, 
257                  architecture as arch
258           from buildsystems 
259           where status = 'approved'
260                 and name = ?
261
262 EOS
263 ;
264 $sth=$db->prepare($det_st);
265 $sth->execute($animal);
266 $row=$sth->fetchrow_arrayref;
267 my ($os, $compiler,$arch) = @$row;
268 $sth->finish;
269
270 $db->disconnect;
271
272 print "Content-Type: text/plain\n\n";
273 print "request was on:\n";
274 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
275
276 my $client_events = $client_conf->{mail_events};
277
278 if ($ENV{BF_DEBUG})
279 {
280     open(TX,">>../buildlogs/$animal.$date");
281     print TX "\n",Dumper(\$client_conf),"\n";
282     close(TX);
283 }
284
285 my $bcc_stat = [];
286 my $bcc_chg=[];
287 if (ref $client_events)
288 {
289     my $cbcc = $client_events->{all};
290     if (ref $cbcc)
291     {
292         push @$bcc_stat, @$cbcc;
293     }
294     elsif (defined $cbcc)
295     {
296         push @$bcc_stat, $cbcc;
297     }
298     if ($stage ne 'OK')
299     {
300         $cbcc = $client_events->{all};
301         if (ref $cbcc)
302         {
303             push @$bcc_stat, @$cbcc;
304         }
305         elsif (defined $cbcc)
306         {
307             push @$bcc_stat, $cbcc;
308         }
309     }
310     $cbcc = $client_events->{change};
311     if (ref $cbcc)
312     {
313         push @$bcc_chg, @$cbcc;
314     }
315     elsif (defined $cbcc)
316     {
317         push @$bcc_chg, $cbcc;
318     }
319     if ($stage eq 'OK' || $prev_stat eq 'OK')
320     {
321         $cbcc = $client_events->{green};
322         if (ref $cbcc)
323         {
324             push @$bcc_chg, @$cbcc;
325         }
326         elsif (defined $cbcc)
327         {
328             push @$bcc_chg, $cbcc;
329         }
330     }
331 }
332
333
334 my $url = $query->url(-base => 1);
335
336
337 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
338
339 my $mailto = [@$all_stat];
340 push(@$mailto,@$fail_stat) if $stage ne 'OK';
341
342 my $me = `id -un`; chomp $me;
343
344 my $host = `hostname`; chomp $host;
345
346 my $msg = new Mail::Send;
347
348 $msg->set('From',"PG Build Farm <$me\@$host>");
349
350 $msg->to(@$mailto);
351 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
352 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
353 my $fh = $msg->open;
354 print $fh <<EOMAIL; 
355
356
357 The PGBuildfarm member $animal had the following event on branch $branch:
358
359 $stat_type: $stage
360
361 The snapshot timestamp for the build that triggered this notification is: $dbdate
362
363 The specs of this machine are:
364 OS:  $os
365 Arch: $arch
366 Comp: $compiler
367
368 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
369
370 EOMAIL
371
372 $fh->close;
373
374 exit if ($stage eq $prev_stat);
375
376 $mailto = [@$change_stat];
377 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
378
379 $msg = new Mail::Send;
380
381 $msg->set('From',"PG Build Farm <$me\@$host>");
382
383 $msg->to(@$mailto);
384 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
385
386 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
387     "changed from OK to $stage";
388 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
389 $stat_type .= " failure" if $stage ne 'OK';
390
391 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
392 $fh = $msg->open;
393 print $fh <<EOMAIL;
394
395 The PGBuildfarm member $animal had the following event on branch $branch:
396
397 Status $stat_type
398
399 The snapshot timestamp for the build that triggered this notification is: $dbdate
400
401 The specs of this machine are:
402 OS:  $os
403 Arch: $arch
404 Comp: $compiler
405
406 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
407
408 EOMAIL
409
410 $fh->close;