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
17 $default_host $local_git_clone
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 if (open($brhandle,"../htdocs/branches_of_interest.txt"))
63 my @branches_of_interest = <$brhandle>;
65 chomp(@branches_of_interest);
66 unless (grep {$_ eq $branch} @branches_of_interest)
69 "Status: 492 bad branch parameter $branch\nContent-Type: text/plain\n\n",
70 "bad branch parameter $branch\n";
77 "branch=$branch&res=$res&stage=$stage&animal=$animal&".
78 "ts=$ts&log=$log&conf=$conf";
81 "changed_files=$changed_this_run&".
82 "changed_since_success=$changed_since_success&";
84 unless ($animal && $ts && $stage && $sig)
87 "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
88 "bad parameters for request\n";
93 unless ($branch =~ /^(HEAD|REL\d+_\d+_STABLE)$/)
96 "Status: 492 bad branch parameter $branch\nContent-Type: text/plain\n\n",
97 "bad branch parameter $branch\n";
103 my $db = DBI->connect($dsn,$dbuser,$dbpass);
105 die $DBI::errstr unless $db;
108 "select secret from buildsystems where name = ? and status = 'approved'";
109 my $sth = $db->prepare($gethost);
110 $sth->execute($animal);
111 my ($secret)=$sth->fetchrow_array();
114 my $tsdiff = time - $ts;
116 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
117 $year += 1900; $mon +=1;
119 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
121 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
123 open(TX,">$buildlogs/$animal.$date");
124 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
125 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
127 "changed_this_run:\n$changed_this_run\n",
128 "changed_since_success:\n$changed_since_success\n",
129 "frozen_sconf:$frozen_sconf\n",
131 # $query->save(\*TX);
135 unless ($ts < time + 120)
137 my $gmt = gmtime($ts);
138 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
139 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
144 unless ($ts + 86400 > time)
146 my $gmt = gmtime($ts);
147 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
148 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
156 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
157 "System $animal is unknown\n";
166 my $calc_sig = sha1_hex($content,$secret);
167 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
169 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
172 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
173 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
178 # undo escape-proofing of base64 data and decode it
179 map {tr/$@/+=/; $_ = decode_base64($_); }
180 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive, $frozen_sconf);
182 if ($log =~/Last file mtime in snapshot: (.*)/)
184 my $snaptime = parsedate($1);
185 my $brch = $branch eq 'HEAD' ? 'master' : $branch;
186 my $last_branch_time = time - (30 * 86400);
187 $last_branch_time = `TZ=UTC GIT_DIR=$local_git_clone git log -1 --pretty=format:\%ct $brch`;
188 if ($snaptime < ($last_branch_time - 86400))
190 print "Status: 493 snapshot too old: $1\nContent-Type: text/plain\n\n";
191 print "snapshot to old: $1\n";
197 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
198 $year += 1900; $mon +=1;
200 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
204 my $dirname = "$buildlogs/tmp.$$.unpacklogs";
211 my $archname = "$buildlogs/tmp.$$.tgz";
212 open($log_handle,">$archname");
214 print $log_handle $log_archive;
217 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
218 map {s/\s+//g; } @log_file_names;
219 my @qnames = grep { $_ ne 'githead.log' } @log_file_names;
220 map { $_ = qq("$_"); } @qnames;
221 $log_file_names = '{' . join(',',@qnames) . '}';
222 if (-e "$dirname/githead.log" )
224 open(my $githead,"$dirname/githead.log");
225 $githeadref = <$githead>;
236 $client_conf = thaw $frozen_sconf;
239 if ($min_script_version)
241 $client_conf->{script_version} ||= '0.0';
242 my $cli_ver = $client_conf->{script_version} ;
243 $cli_ver =~ s/^REL_//;
244 my ($minmajor,$minminor) = split(/\./,$min_script_version);
245 my ($smajor,$sminor) = split(/\./,$cli_ver);
246 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
248 print "Status: 460 script version too low\nContent-Type: text/plain\n\n";
250 "Script version is below minimum required\n",
251 "Reported version: $client_conf->{script_version},",
252 "Minumum version required: $min_script_version\n";
258 if ($min_web_script_version)
260 $client_conf->{web_script_version} ||= '0.0';
261 my $cli_ver = $client_conf->{web_script_version} ;
262 $cli_ver =~ s/^REL_//;
263 my ($minmajor,$minminor) = split(/\./,$min_web_script_version);
264 my ($smajor,$sminor) = split(/\./,$cli_ver);
265 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
267 print "Status: 461 web script version too low\nContent-Type: text/plain\n\n";
269 "Web Script version is below minimum required\n",
270 "Reported version: $client_conf->{web_script_version}, ",
271 "Minumum version required: $min_web_script_version\n"
279 if (not exists $client_conf->{config_opts} )
283 elsif (ref $client_conf->{config_opts} eq 'HASH')
285 # leave out keys with false values
286 @config_flags = grep { $client_conf->{config_opts}->{$_} }
287 keys %{$client_conf->{config_opts}};
289 elsif (ref $client_conf->{config_opts} eq 'ARRAY' )
291 @config_flags = @{$client_conf->{config_opts}};
296 @config_flags = grep {! m/=/ } @config_flags;
297 map {s/\s+//g; $_=qq("$_"); } @config_flags;
298 push @config_flags,'git' if $client_conf->{scm} eq 'git';
299 $config_flags = '{' . join(',',@config_flags) . '}' ;
302 my $scm = $client_conf->{scm} || 'cvs';
303 my $scmurl = $client_conf->{scm_url};
306 insert into build_status
307 (sysname, snapshot,status, stage, log,conf_sum, branch,
308 changed_this_run, changed_since_success,
309 log_archive_filenames , log_archive, build_flags, scm, scmurl,
310 git_head_ref,frozen_conf)
311 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
316 # this transaction lets us set log_error_verbosity to terse
317 # just for the duration of the transaction. That turns off logging the
318 # bind params, so all the logs don't get stuffed on the postgres logs
321 $db->do("select set_local_error_terse()");
324 $sth=$db->prepare($logst);
326 $sth->bind_param(1,$animal);
327 $sth->bind_param(2,$dbdate);
328 $sth->bind_param(3,$res);
329 $sth->bind_param(4,$stage);
330 $sth->bind_param(5,$log);
331 $sth->bind_param(6,$conf);
332 $sth->bind_param(7,$branch);
333 $sth->bind_param(8,$changed_this_run);
334 $sth->bind_param(9,$changed_since_success);
335 $sth->bind_param(10,$log_file_names);
336 #$sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
337 $sth->bind_param(11,undef,{ pg_type => DBD::Pg::PG_BYTEA });
338 $sth->bind_param(12,$config_flags);
339 $sth->bind_param(13,$scm);
340 $sth->bind_param(14,$scmurl);
341 $sth->bind_param(15,$githeadref);
342 $sth->bind_param(16,$frozen_sconf,{ pg_type => DBD::Pg::PG_BYTEA });
349 my $logst2 = <<EOSQL;
351 insert into build_status_log
352 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
353 values (?, ?, ?, ?, ?, ?)
358 $sth = $db->prepare($logst2);
362 my $stage_start = $ts;
364 foreach my $log_file( @log_file_names )
366 next if $log_file =~ /^githead/;
368 open($handle,"$dirname/$log_file");
369 my $mtime = (stat $handle)[9];
370 my $stage_interval = $mtime - $stage_start;
371 $stage_start = $mtime;
372 my $ltext = <$handle>;
374 $ltext =~ s/\x00/\\0/g;
375 $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
376 "$stage_interval seconds");
383 my $prevst = <<EOSQL;
385 select coalesce((select distinct on (snapshot) stage
387 where sysname = ? and branch = ? and snapshot < ?
388 order by snapshot desc
389 limit 1), 'NEW') as prev_status
393 $sth=$db->prepare($prevst);
394 $sth->execute($animal,$branch,$dbdate);
395 my $row=$sth->fetchrow_arrayref;
396 my $prev_stat=$row->[0];
401 select operating_system|| ' / ' || os_version as os ,
402 compiler || ' / ' || compiler_version as compiler,
405 where status = 'approved'
410 $sth=$db->prepare($det_st);
411 $sth->execute($animal);
412 $row=$sth->fetchrow_arrayref;
413 my ($os, $compiler,$arch) = @$row;
417 # prevent occasional duplication by forcing serialization of this operation
418 $db->do("lock table dashboard_mat in share row exclusive mode");
419 $db->do("delete from dashboard_mat");
420 $db->do("insert into dashboard_mat select * from dashboard_mat_data");
426 # prevent occasional duplication by forcing serialization of this operation
427 $db->do("lock table nrecent_failures in share row exclusive mode");
428 $db->do("delete from nrecent_failures");
429 $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'");
435 print "Content-Type: text/plain\n\n";
436 print "request was on:\n";
437 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
439 my $client_events = $client_conf->{mail_events};
443 my $client_time = $client_conf->{current_ts};
444 open(TX,">>$buildlogs/$animal.$date");
445 print TX "\n",Dumper(\$client_conf),"\n";
446 print TX "server time: $server_time, client time: $client_time\n" if $client_time;
452 if (ref $client_events)
454 my $cbcc = $client_events->{all};
457 push @$bcc_stat, @$cbcc;
459 elsif (defined $cbcc)
461 push @$bcc_stat, $cbcc;
465 $cbcc = $client_events->{all};
468 push @$bcc_stat, @$cbcc;
470 elsif (defined $cbcc)
472 push @$bcc_stat, $cbcc;
475 $cbcc = $client_events->{change};
478 push @$bcc_chg, @$cbcc;
480 elsif (defined $cbcc)
482 push @$bcc_chg, $cbcc;
484 if ($stage eq 'OK' || $prev_stat eq 'OK')
486 $cbcc = $client_events->{green};
489 push @$bcc_chg, @$cbcc;
491 elsif (defined $cbcc)
493 push @$bcc_chg, $cbcc;
499 my $url = $query->url(-base => 1);
502 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
504 my $mailto = [@$all_stat];
505 push(@$mailto,@$fail_stat) if $stage ne 'OK';
507 my $me = `id -un`; chomp($me);
509 my $host = `hostname`; chomp ($host);
510 $host = $default_host unless ($host =~ m/[.]/ || !defined($default_host));
512 my $from_addr = "PG Build Farm <$me\@$host>";
513 $from_addr =~ tr /\r\n//d;
515 my $msg = new Mail::Send;
519 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
520 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
521 $msg->set('From',$from_addr);
526 The PGBuildfarm member $animal had the following event on branch $branch:
530 The snapshot timestamp for the build that triggered this notification is: $dbdate
532 The specs of this machine are:
537 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
543 exit if ($stage eq $prev_stat);
545 $mailto = [@$change_stat];
546 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
548 $msg = new Mail::Send;
552 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
554 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
555 "changed from OK to $stage";
556 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
557 $stat_type .= " failure" if $stage ne 'OK';
559 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
560 $msg->set('From',$from_addr);
564 The PGBuildfarm member $animal had the following event on branch $branch:
568 The snapshot timestamp for the build that triggered this notification is: $dbdate
570 The specs of this machine are:
575 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch