5 Copyright (c) 2003-2010, Andrew Dunstan
7 See accompanying License file for license details
14 use vars qw($dbhost $dbname $dbuser $dbpass $dbport
15 $all_stat $fail_stat $change_stat $green_stat
17 $min_script_version $min_web_script_version
18 $default_host $local_git_clone
21 # force this before we do anything - even load modules
22 BEGIN { $server_time = time; }
25 use Digest::SHA1 qw(sha1_hex);
32 use Storable qw(thaw);
34 use FindBin qw($RealBin);
35 require "$RealBin/../BuildFarmWeb.pl";
37 my $buildlogs = "$RealBin/../buildlogs";
39 die "no dbname" unless $dbname;
40 die "no dbuser" unless $dbuser;
42 my $dsn="dbi:Pg:dbname=$dbname";
43 $dsn .= ";host=$dbhost" if $dbhost;
44 $dsn .= ";port=$dbport" if $dbport;
48 my $sig = $query->path_info;
51 my $stage = $query->param('stage');
52 my $ts = $query->param('ts');
53 my $animal = $query->param('animal');
54 my $log = $query->param('log');
55 my $res = $query->param('res');
56 my $conf = $query->param('conf');
57 my $branch = $query->param('branch');
58 my $changed_since_success = $query->param('changed_since_success');
59 my $changed_this_run = $query->param('changed_files');
60 my $log_archive = $query->param('logtar');
61 my $frozen_sconf = $query->param('frozen_sconf') || '';
64 if (open($brhandle,"../htdocs/branches_of_interest.txt"))
66 my @branches_of_interest = <$brhandle>;
68 chomp(@branches_of_interest);
69 unless (grep {$_ eq $branch} @branches_of_interest)
72 "Status: 492 bad branch parameter $branch\nContent-Type: text/plain\n\n",
73 "bad branch parameter $branch\n";
80 'branch=' . uri_escape($branch) . "&res=$res&stage=$stage&animal=$animal&".
81 "ts=$ts&log=$log&conf=$conf";
84 "changed_files=$changed_this_run&".
85 "changed_since_success=$changed_since_success&";
87 unless ($animal && $ts && $stage && $sig)
90 "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
91 "bad parameters for request\n";
96 # Want to allow all kinds of named branches
97 #unless ($branch =~ /^(HEAD|REL\d+_\d+_STABLE)$/)
100 # "Status: 492 bad branch parameter $branch\nContent-Type: text/plain\n\n",
101 # "bad branch parameter $branch\n";
107 my $db = DBI->connect($dsn,$dbuser,$dbpass);
109 die $DBI::errstr unless $db;
112 "select secret from buildsystems where name = ? and status = 'approved'";
113 my $sth = $db->prepare($gethost);
114 $sth->execute($animal);
115 my ($secret)=$sth->fetchrow_array();
118 my $tsdiff = time - $ts;
120 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
121 $year += 1900; $mon +=1;
123 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
125 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
127 open(TX,">$buildlogs/$animal.$date");
128 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
129 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
131 "changed_this_run:\n$changed_this_run\n",
132 "changed_since_success:\n$changed_since_success\n",
133 "frozen_sconf:$frozen_sconf\n",
135 # $query->save(\*TX);
139 unless ($ts < time + 120)
141 my $gmt = gmtime($ts);
142 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
143 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
148 unless ($ts + 86400 > time)
150 my $gmt = gmtime($ts);
151 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
152 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
160 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
161 "System $animal is unknown\n";
170 my $calc_sig = sha1_hex($content,$secret);
171 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
173 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
176 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
177 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
182 # undo escape-proofing of base64 data and decode it
183 map {tr/$@/+=/; $_ = decode_base64($_); }
184 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive, $frozen_sconf);
186 if ($log =~/Last file mtime in snapshot: (.*)/)
188 my $snaptime = parsedate($1);
189 my $brch = $branch eq 'HEAD' ? 'master' : $branch;
190 my $last_branch_time = time - (30 * 86400);
191 $last_branch_time = `TZ=UTC GIT_DIR=$local_git_clone git log -1 --pretty=format:\%ct $brch`;
192 if ($snaptime < ($last_branch_time - 86400))
194 print "Status: 493 snapshot too old: $1\nContent-Type: text/plain\n\n";
195 print "snapshot to old: $1\n";
201 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
202 $year += 1900; $mon +=1;
204 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
208 my $dirname = "$buildlogs/tmp.$$.unpacklogs";
215 my $archname = "$buildlogs/tmp.$$.tgz";
216 open($log_handle,">$archname");
218 print $log_handle $log_archive;
221 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
222 map {s/\s+//g; } @log_file_names;
223 my @qnames = grep { $_ ne 'githead.log' } @log_file_names;
224 map { $_ = qq("$_"); } @qnames;
225 $log_file_names = '{' . join(',',@qnames) . '}';
226 if (-e "$dirname/githead.log" )
228 open(my $githead,"$dirname/githead.log");
229 $githeadref = <$githead>;
240 $client_conf = thaw $frozen_sconf;
243 if ($min_script_version)
245 $client_conf->{script_version} ||= '0.0';
246 my $cli_ver = $client_conf->{script_version} ;
247 $cli_ver =~ s/^REL_//;
248 my ($minmajor,$minminor) = split(/\./,$min_script_version);
249 my ($smajor,$sminor) = split(/\./,$cli_ver);
250 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
252 print "Status: 460 script version too low\nContent-Type: text/plain\n\n";
254 "Script version is below minimum required\n",
255 "Reported version: $client_conf->{script_version},",
256 "Minumum version required: $min_script_version\n";
262 if ($min_web_script_version)
264 $client_conf->{web_script_version} ||= '0.0';
265 my $cli_ver = $client_conf->{web_script_version} ;
266 $cli_ver =~ s/^REL_//;
267 my ($minmajor,$minminor) = split(/\./,$min_web_script_version);
268 my ($smajor,$sminor) = split(/\./,$cli_ver);
269 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
271 print "Status: 461 web script version too low\nContent-Type: text/plain\n\n";
273 "Web Script version is below minimum required\n",
274 "Reported version: $client_conf->{web_script_version}, ",
275 "Minumum version required: $min_web_script_version\n"
283 if (not exists $client_conf->{config_opts} )
287 elsif (ref $client_conf->{config_opts} eq 'HASH')
289 # leave out keys with false values
290 @config_flags = grep { $client_conf->{config_opts}->{$_} }
291 keys %{$client_conf->{config_opts}};
293 elsif (ref $client_conf->{config_opts} eq 'ARRAY' )
295 @config_flags = @{$client_conf->{config_opts}};
300 @config_flags = grep {! m/=/ } @config_flags;
301 map {s/\s+//g; $_=qq("$_"); } @config_flags;
302 push @config_flags,'git' if $client_conf->{scm} eq 'git';
303 push(@config_flags, 'doc')
304 if (defined $client_conf->{'optional_steps'}->{'make-doc'});
305 push(@config_flags, 'test')
306 if (defined $client_conf->{'optional_steps'}->{'test'});
307 $config_flags = '{' . join(',',@config_flags) . '}' ;
310 my $scm = $client_conf->{scm} || 'cvs';
311 my $scmurl = $client_conf->{scm_url};
314 insert into build_status
315 (sysname, snapshot,status, stage, log,conf_sum, branch,
316 changed_this_run, changed_since_success,
317 log_archive_filenames , log_archive, build_flags, scm, scmurl,
318 git_head_ref,frozen_conf)
319 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
324 # this transaction lets us set log_error_verbosity to terse
325 # just for the duration of the transaction. That turns off logging the
326 # bind params, so all the logs don't get stuffed on the postgres logs
331 #$db->do("select set_local_error_terse()");
334 $sth=$db->prepare($logst);
336 $sth->bind_param(1,$animal);
337 $sth->bind_param(2,$dbdate);
338 $sth->bind_param(3,$res & 0x8fffffff); # in case we get a 64 bit int status!
339 $sth->bind_param(4,$stage);
340 $sth->bind_param(5,$log);
341 $sth->bind_param(6,$conf);
342 $sth->bind_param(7,$branch);
343 $sth->bind_param(8,$changed_this_run);
344 $sth->bind_param(9,$changed_since_success);
345 $sth->bind_param(10,$log_file_names);
346 #$sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
347 $sth->bind_param(11,undef,{ pg_type => DBD::Pg::PG_BYTEA });
348 $sth->bind_param(12,$config_flags);
349 $sth->bind_param(13,$scm);
350 $sth->bind_param(14,$scmurl);
351 $sth->bind_param(15,$githeadref);
352 $sth->bind_param(16,$frozen_sconf,{ pg_type => DBD::Pg::PG_BYTEA });
354 $sqlres = $sth->execute;
363 insert into build_status_log
364 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
365 values (?, ?, ?, ?, ?, ?)
369 $sth = $db->prepare($logst2);
373 my $stage_start = $ts;
375 foreach my $log_file( @log_file_names )
377 next if $log_file =~ /^githead/;
379 open($handle,"$dirname/$log_file");
380 my $mtime = (stat $handle)[9];
381 my $stage_interval = $mtime - $stage_start;
382 $stage_start = $mtime;
383 my $ltext = <$handle>;
385 $ltext =~ s/\x00/\\0/g;
386 $sqlres = $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
387 "$stage_interval seconds");
391 $sth->finish unless $sqlres;
398 print "Status: 462 database failure\nContent-Type: text/plain\n\n";
399 print "Your report generated a database failure:\n",
410 my $prevst = <<EOSQL;
412 select coalesce((select distinct on (snapshot) stage
414 where sysname = ? and branch = ? and snapshot < ?
415 order by snapshot desc
416 limit 1), 'NEW') as prev_status
420 $sth=$db->prepare($prevst);
421 $sth->execute($animal,$branch,$dbdate);
422 my $row=$sth->fetchrow_arrayref;
423 my $prev_stat=$row->[0];
428 select operating_system|| ' / ' || os_version as os ,
429 compiler || ' / ' || compiler_version as compiler,
432 where status = 'approved'
437 $sth=$db->prepare($det_st);
438 $sth->execute($animal);
439 $row=$sth->fetchrow_arrayref;
440 my ($os, $compiler,$arch) = @$row;
444 # prevent occasional duplication by forcing serialization of this operation
445 $db->do("lock table dashboard_mat in share row exclusive mode");
446 $db->do("delete from dashboard_mat");
447 $db->do("insert into dashboard_mat select * from dashboard_mat_data");
453 # prevent occasional duplication by forcing serialization of this operation
454 $db->do("lock table nrecent_failures in share row exclusive mode");
455 $db->do("delete from nrecent_failures");
456 $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'");
462 print "Content-Type: text/plain\n\n";
463 print "request was on:\n";
464 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
466 my $client_events = $client_conf->{mail_events};
470 my $client_time = $client_conf->{current_ts};
471 open(TX,">>$buildlogs/$animal.$date");
472 print TX "\n",Dumper(\$client_conf),"\n";
473 print TX "server time: $server_time, client time: $client_time\n" if $client_time;
479 if (ref $client_events)
481 my $cbcc = $client_events->{all};
484 push @$bcc_stat, @$cbcc;
486 elsif (defined $cbcc)
488 push @$bcc_stat, $cbcc;
492 $cbcc = $client_events->{all};
495 push @$bcc_stat, @$cbcc;
497 elsif (defined $cbcc)
499 push @$bcc_stat, $cbcc;
502 $cbcc = $client_events->{change};
505 push @$bcc_chg, @$cbcc;
507 elsif (defined $cbcc)
509 push @$bcc_chg, $cbcc;
511 if ($stage eq 'OK' || $prev_stat eq 'OK')
513 $cbcc = $client_events->{green};
516 push @$bcc_chg, @$cbcc;
518 elsif (defined $cbcc)
520 push @$bcc_chg, $cbcc;
526 my $url = $query->url(-base => 1);
529 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
531 my $mailto = [@$all_stat];
532 push(@$mailto,@$fail_stat) if $stage ne 'OK';
534 my $me = `id -un`; chomp($me);
536 my $host = `hostname`; chomp ($host);
537 $host = $default_host unless ($host =~ m/[.]/ || !defined($default_host));
539 my $from_addr = "Exim Build Farm <$me\@$host>";
540 $from_addr =~ tr /\r\n//d;
542 my $msg = new Mail::Send;
546 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
547 $msg->subject("Exim BuildFarm member $animal Branch $branch $stat_type $stage");
548 $msg->set('From',$from_addr);
553 The Exim BuildFarm member $animal had the following event on branch $branch:
557 The snapshot timestamp for the build that triggered this notification is: $dbdate
559 The specs of this machine are:
564 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
570 exit if ($stage eq $prev_stat);
572 $mailto = [@$change_stat];
573 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
575 $msg = new Mail::Send;
579 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
581 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
582 "changed from OK to $stage";
583 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
584 $stat_type .= " failure" if $stage ne 'OK';
586 $msg->subject("Exim BuildFarm member $animal Branch $branch Status $stat_type");
587 $msg->set('From',$from_addr);
591 The Exim BuildFarm member $animal had the following event on branch $branch:
595 The snapshot timestamp for the build that triggered this notification is: $dbdate
597 The specs of this machine are:
602 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch