5 Copyright (c) 2003-2010, Andrew Dunstan
7 See accompanying License file for license details
13 use vars qw($dbhost $dbname $dbuser $dbpass $dbport
14 $all_stat $fail_stat $change_stat $green_stat
16 $min_script_version $min_web_script_version
20 # force this before we do anything - even load modules
21 BEGIN { $server_time = time; }
24 use Digest::SHA1 qw(sha1_hex);
31 use Storable qw(thaw);
33 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
34 my $buildlogs = "$ENV{BFConfDir}/buildlogs";
36 die "no dbname" unless $dbname;
37 die "no dbuser" unless $dbuser;
39 my $dsn="dbi:Pg:dbname=$dbname";
40 $dsn .= ";host=$dbhost" if $dbhost;
41 $dsn .= ";port=$dbport" if $dbport;
45 my $sig = $query->path_info;
48 my $stage = $query->param('stage');
49 my $ts = $query->param('ts');
50 my $animal = $query->param('animal');
51 my $log = $query->param('log');
52 my $res = $query->param('res');
53 my $conf = $query->param('conf');
54 my $branch = $query->param('branch');
55 my $changed_since_success = $query->param('changed_since_success');
56 my $changed_this_run = $query->param('changed_files');
57 my $log_archive = $query->param('logtar');
58 my $frozen_sconf = $query->param('frozen_sconf') || '';
61 "branch=$branch&res=$res&stage=$stage&animal=$animal&".
62 "ts=$ts&log=$log&conf=$conf";
65 "changed_files=$changed_this_run&".
66 "changed_since_success=$changed_since_success&";
68 unless ($animal && $ts && $stage && $sig)
71 "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
72 "bad parameters for request\n";
77 unless ($branch =~ /^(HEAD|REL\d+_\d+_STABLE)$/)
80 "Status: 492 bad branch parameter $branch\nContent-Type: text/plain\n\n",
81 "bad branch parameter $branch\n";
87 my $db = DBI->connect($dsn,$dbuser,$dbpass);
89 die $DBI::errstr unless $db;
92 "select secret from buildsystems where name = ? and status = 'approved'";
93 my $sth = $db->prepare($gethost);
94 $sth->execute($animal);
95 my ($secret)=$sth->fetchrow_array();
98 my $tsdiff = time - $ts;
100 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
101 $year += 1900; $mon +=1;
103 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
105 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
107 open(TX,">$buildlogs/$animal.$date");
108 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
109 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
111 "changed_this_run:\n$changed_this_run\n",
112 "changed_since_success:\n$changed_since_success\n",
113 "frozen_sconf:$frozen_sconf\n",
115 # $query->save(\*TX);
119 unless ($ts < time + 120)
121 my $gmt = gmtime($ts);
122 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
123 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
128 unless ($ts + 86400 > time)
130 my $gmt = gmtime($ts);
131 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
132 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
140 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
141 "System $animal is unknown\n";
150 my $calc_sig = sha1_hex($content,$secret);
151 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
153 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
156 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
157 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
162 # undo escape-proofing of base64 data and decode it
163 map {tr/$@/+=/; $_ = decode_base64($_); }
164 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive, $frozen_sconf);
166 if ($log =~/Last file mtime in snapshot: (.*)/)
168 my $snaptime = parsedate($1);
169 if ($snaptime < (time - (10 * 86400)))
171 print "Status: 493 snapshot too old: $1\nContent-Type: text/plain\n\n";
172 print "snapshot to old: $1\n";
178 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
179 $year += 1900; $mon +=1;
181 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
185 my $dirname = "$buildlogs/tmp.$$.unpacklogs";
192 my $archname = "$buildlogs/tmp.$$.tgz";
193 open($log_handle,">$archname");
195 print $log_handle $log_archive;
198 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
199 map {s/\s+//g; } @log_file_names;
200 my @qnames = grep { $_ ne 'githead.log' } @log_file_names;
201 map { $_ = qq("$_"); } @qnames;
202 $log_file_names = '{' . join(',',@qnames) . '}';
203 if (-e "$dirname/githead.log" )
205 open(my $githead,"$dirname/githead.log");
206 $githeadref = <$githead>;
217 $client_conf = thaw $frozen_sconf;
220 if ($min_script_version)
222 $client_conf->{script_version} ||= '0.0';
223 my $cli_ver = $client_conf->{script_version} ;
224 $cli_ver =~ s/^REL_//;
225 my ($minmajor,$minminor) = split(/\./,$min_script_version);
226 my ($smajor,$sminor) = split(/\./,$cli_ver);
227 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
229 print "Status: 460 script version too low\nContent-Type: text/plain\n\n";
231 "Script version is below minimum required\n",
232 "Reported version: $client_conf->{script_version},",
233 "Minumum version required: $min_script_version\n";
239 if ($min_web_script_version)
241 $client_conf->{web_script_version} ||= '0.0';
242 my $cli_ver = $client_conf->{web_script_version} ;
243 $cli_ver =~ s/^REL_//;
244 my ($minmajor,$minminor) = split(/\./,$min_web_script_version);
245 my ($smajor,$sminor) = split(/\./,$cli_ver);
246 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
248 print "Status: 461 web script version too low\nContent-Type: text/plain\n\n";
250 "Web Script version is below minimum required\n",
251 "Reported version: $client_conf->{web_script_version}, ",
252 "Minumum version required: $min_web_script_version\n"
260 if (not exists $client_conf->{config_opts} )
264 elsif (ref $client_conf->{config_opts} eq 'HASH')
266 # leave out keys with false values
267 @config_flags = grep { $client_conf->{config_opts}->{$_} }
268 keys %{$client_conf->{config_opts}};
270 elsif (ref $client_conf->{config_opts} eq 'ARRAY' )
272 @config_flags = @{$client_conf->{config_opts}};
277 @config_flags = grep {! m/=/ } @config_flags;
278 map {s/\s+//g; $_=qq("$_"); } @config_flags;
279 push @config_flags,'git' if $client_conf->{scm} eq 'git';
280 $config_flags = '{' . join(',',@config_flags) . '}' ;
283 my $scm = $client_conf->{scm} || 'cvs';
284 my $scmurl = $client_conf->{scm_url};
287 insert into build_status
288 (sysname, snapshot,status, stage, log,conf_sum, branch,
289 changed_this_run, changed_since_success,
290 log_archive_filenames , log_archive, build_flags, scm, scmurl,
291 git_head_ref,frozen_conf)
292 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
297 # this transaction lets us set log_error_verbosity to terse
298 # just for the duration of the transaction. That turns off logging the
299 # bind params, so all the logs don't get stuffed on the postgres logs
302 $db->do("select set_local_error_terse()");
305 $sth=$db->prepare($logst);
307 $sth->bind_param(1,$animal);
308 $sth->bind_param(2,$dbdate);
309 $sth->bind_param(3,$res);
310 $sth->bind_param(4,$stage);
311 $sth->bind_param(5,$log);
312 $sth->bind_param(6,$conf);
313 $sth->bind_param(7,$branch);
314 $sth->bind_param(8,$changed_this_run);
315 $sth->bind_param(9,$changed_since_success);
316 $sth->bind_param(10,$log_file_names);
317 #$sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
318 $sth->bind_param(11,undef,{ pg_type => DBD::Pg::PG_BYTEA });
319 $sth->bind_param(12,$config_flags);
320 $sth->bind_param(13,$scm);
321 $sth->bind_param(14,$scmurl);
322 $sth->bind_param(15,$githeadref);
323 $sth->bind_param(16,$frozen_sconf,{ pg_type => DBD::Pg::PG_BYTEA });
330 my $logst2 = <<EOSQL;
332 insert into build_status_log
333 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
334 values (?, ?, ?, ?, ?, ?)
339 $sth = $db->prepare($logst2);
343 my $stage_start = $ts;
345 foreach my $log_file( @log_file_names )
347 next if $log_file =~ /^githead/;
349 open($handle,"$dirname/$log_file");
350 my $mtime = (stat $handle)[9];
351 my $stage_interval = $mtime - $stage_start;
352 $stage_start = $mtime;
353 my $ltext = <$handle>;
355 $ltext =~ s/\x00/\\0/g;
356 $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
357 "$stage_interval seconds");
364 my $prevst = <<EOSQL;
366 select coalesce((select distinct on (snapshot) stage
368 where sysname = ? and branch = ? and snapshot < ?
369 order by snapshot desc
370 limit 1), 'NEW') as prev_status
374 $sth=$db->prepare($prevst);
375 $sth->execute($animal,$branch,$dbdate);
376 my $row=$sth->fetchrow_arrayref;
377 my $prev_stat=$row->[0];
382 select operating_system|| ' / ' || os_version as os ,
383 compiler || ' / ' || compiler_version as compiler,
386 where status = 'approved'
391 $sth=$db->prepare($det_st);
392 $sth->execute($animal);
393 $row=$sth->fetchrow_arrayref;
394 my ($os, $compiler,$arch) = @$row;
398 # prevent occasional duplication by forcing serialization of this operation
399 $db->do("lock table dashboard_mat in share row exclusive mode");
400 $db->do("delete from dashboard_mat");
401 $db->do("insert into dashboard_mat select * from dashboard_mat_data");
407 # prevent occasional duplication by forcing serialization of this operation
408 $db->do("lock table nrecent_failures in share row exclusive mode");
409 $db->do("delete from nrecent_failures");
410 $db->do("insert into nrecent_failures select bs.sysname, bs.snapshot, bs.branch from build_status bs where bs.stage <> 'OK' and bs.snapshot > now() - interval '90 days'");
416 print "Content-Type: text/plain\n\n";
417 print "request was on:\n";
418 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
420 my $client_events = $client_conf->{mail_events};
424 my $client_time = $client_conf->{current_ts};
425 open(TX,">>$buildlogs/$animal.$date");
426 print TX "\n",Dumper(\$client_conf),"\n";
427 print TX "server time: $server_time, client time: $client_time\n" if $client_time;
433 if (ref $client_events)
435 my $cbcc = $client_events->{all};
438 push @$bcc_stat, @$cbcc;
440 elsif (defined $cbcc)
442 push @$bcc_stat, $cbcc;
446 $cbcc = $client_events->{all};
449 push @$bcc_stat, @$cbcc;
451 elsif (defined $cbcc)
453 push @$bcc_stat, $cbcc;
456 $cbcc = $client_events->{change};
459 push @$bcc_chg, @$cbcc;
461 elsif (defined $cbcc)
463 push @$bcc_chg, $cbcc;
465 if ($stage eq 'OK' || $prev_stat eq 'OK')
467 $cbcc = $client_events->{green};
470 push @$bcc_chg, @$cbcc;
472 elsif (defined $cbcc)
474 push @$bcc_chg, $cbcc;
480 my $url = $query->url(-base => 1);
483 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
485 my $mailto = [@$all_stat];
486 push(@$mailto,@$fail_stat) if $stage ne 'OK';
488 my $me = `id -un`; chomp($me);
490 my $host = `hostname`; chomp ($host);
491 $host = $default_host unless ($host =~ m/[.]/ || !defined($default_host));
493 my $from_addr = "PG Build Farm <$me\@$host>";
494 $from_addr =~ tr /\r\n//d;
496 my $msg = new Mail::Send;
500 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
501 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
502 $msg->set('From',$from_addr);
507 The PGBuildfarm member $animal had the following event on branch $branch:
511 The snapshot timestamp for the build that triggered this notification is: $dbdate
513 The specs of this machine are:
518 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
524 exit if ($stage eq $prev_stat);
526 $mailto = [@$change_stat];
527 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
529 $msg = new Mail::Send;
533 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
535 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
536 "changed from OK to $stage";
537 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
538 $stat_type .= " failure" if $stage ne 'OK';
540 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
541 $msg->set('From',$from_addr);
545 The PGBuildfarm member $animal had the following event on branch $branch:
549 The snapshot timestamp for the build that triggered this notification is: $dbdate
551 The specs of this machine are:
556 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch