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 use FindBin qw($RealBin);
34 require "$RealBin/../BuildFarmWeb.pl";
36 my $buildlogs = "/home/farm/server/buildlogs";
38 die "no dbname" unless $dbname;
39 die "no dbuser" unless $dbuser;
41 my $dsn="dbi:Pg:dbname=$dbname";
42 $dsn .= ";host=$dbhost" if $dbhost;
43 $dsn .= ";port=$dbport" if $dbport;
47 my $sig = $query->path_info;
50 my $stage = $query->param('stage');
51 my $ts = $query->param('ts');
52 my $animal = $query->param('animal');
53 my $log = $query->param('log');
54 my $res = $query->param('res');
55 my $conf = $query->param('conf');
56 my $branch = $query->param('branch');
57 my $changed_since_success = $query->param('changed_since_success');
58 my $changed_this_run = $query->param('changed_files');
59 my $log_archive = $query->param('logtar');
60 my $frozen_sconf = $query->param('frozen_sconf') || '';
63 if (open($brhandle,"../htdocs/branches_of_interest.txt"))
65 my @branches_of_interest = <$brhandle>;
67 chomp(@branches_of_interest);
68 unless (grep {$_ eq $branch} @branches_of_interest)
71 "Status: 492 bad branch parameter $branch\nContent-Type: text/plain\n\n",
72 "bad branch parameter $branch\n";
79 "branch=$branch&res=$res&stage=$stage&animal=$animal&".
80 "ts=$ts&log=$log&conf=$conf";
83 "changed_files=$changed_this_run&".
84 "changed_since_success=$changed_since_success&";
86 unless ($animal && $ts && $stage && $sig)
89 "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
90 "bad parameters for request\n";
95 # Want to allow all kinds of named branches
96 #unless ($branch =~ /^(HEAD|REL\d+_\d+_STABLE)$/)
99 # "Status: 492 bad branch parameter $branch\nContent-Type: text/plain\n\n",
100 # "bad branch parameter $branch\n";
106 my $db = DBI->connect($dsn,$dbuser,$dbpass);
108 die $DBI::errstr unless $db;
111 "select secret from buildsystems where name = ? and status = 'approved'";
112 my $sth = $db->prepare($gethost);
113 $sth->execute($animal);
114 my ($secret)=$sth->fetchrow_array();
117 my $tsdiff = time - $ts;
119 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
120 $year += 1900; $mon +=1;
122 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
124 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
126 open(TX,">$buildlogs/$animal.$date");
127 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
128 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
130 "changed_this_run:\n$changed_this_run\n",
131 "changed_since_success:\n$changed_since_success\n",
132 "frozen_sconf:$frozen_sconf\n",
134 # $query->save(\*TX);
138 unless ($ts < time + 120)
140 my $gmt = gmtime($ts);
141 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
142 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
147 unless ($ts + 86400 > time)
149 my $gmt = gmtime($ts);
150 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
151 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
159 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
160 "System $animal is unknown\n";
169 my $calc_sig = sha1_hex($content,$secret);
170 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
172 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
175 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
176 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
181 # undo escape-proofing of base64 data and decode it
182 map {tr/$@/+=/; $_ = decode_base64($_); }
183 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive, $frozen_sconf);
185 if ($log =~/Last file mtime in snapshot: (.*)/)
187 my $snaptime = parsedate($1);
188 my $brch = $branch eq 'HEAD' ? 'master' : $branch;
189 my $last_branch_time = time - (30 * 86400);
190 $last_branch_time = `TZ=UTC GIT_DIR=$local_git_clone git log -1 --pretty=format:\%ct $brch`;
191 if ($snaptime < ($last_branch_time - 86400))
193 print "Status: 493 snapshot too old: $1\nContent-Type: text/plain\n\n";
194 print "snapshot to old: $1\n";
200 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
201 $year += 1900; $mon +=1;
203 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
207 my $dirname = "$buildlogs/tmp.$$.unpacklogs";
214 my $archname = "$buildlogs/tmp.$$.tgz";
215 open($log_handle,">$archname");
217 print $log_handle $log_archive;
220 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
221 map {s/\s+//g; } @log_file_names;
222 my @qnames = grep { $_ ne 'githead.log' } @log_file_names;
223 map { $_ = qq("$_"); } @qnames;
224 $log_file_names = '{' . join(',',@qnames) . '}';
225 if (-e "$dirname/githead.log" )
227 open(my $githead,"$dirname/githead.log");
228 $githeadref = <$githead>;
239 $client_conf = thaw $frozen_sconf;
242 if ($min_script_version)
244 $client_conf->{script_version} ||= '0.0';
245 my $cli_ver = $client_conf->{script_version} ;
246 $cli_ver =~ s/^REL_//;
247 my ($minmajor,$minminor) = split(/\./,$min_script_version);
248 my ($smajor,$sminor) = split(/\./,$cli_ver);
249 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
251 print "Status: 460 script version too low\nContent-Type: text/plain\n\n";
253 "Script version is below minimum required\n",
254 "Reported version: $client_conf->{script_version},",
255 "Minumum version required: $min_script_version\n";
261 if ($min_web_script_version)
263 $client_conf->{web_script_version} ||= '0.0';
264 my $cli_ver = $client_conf->{web_script_version} ;
265 $cli_ver =~ s/^REL_//;
266 my ($minmajor,$minminor) = split(/\./,$min_web_script_version);
267 my ($smajor,$sminor) = split(/\./,$cli_ver);
268 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
270 print "Status: 461 web script version too low\nContent-Type: text/plain\n\n";
272 "Web Script version is below minimum required\n",
273 "Reported version: $client_conf->{web_script_version}, ",
274 "Minumum version required: $min_web_script_version\n"
282 if (not exists $client_conf->{config_opts} )
286 elsif (ref $client_conf->{config_opts} eq 'HASH')
288 # leave out keys with false values
289 @config_flags = grep { $client_conf->{config_opts}->{$_} }
290 keys %{$client_conf->{config_opts}};
292 elsif (ref $client_conf->{config_opts} eq 'ARRAY' )
294 @config_flags = @{$client_conf->{config_opts}};
299 @config_flags = grep {! m/=/ } @config_flags;
300 map {s/\s+//g; $_=qq("$_"); } @config_flags;
301 push @config_flags,'git' if $client_conf->{scm} eq 'git';
302 push(@config_flags, 'doc')
303 if (defined $client_conf->{'optional_steps'}->{'make-doc'});
304 push(@config_flags, 'test')
305 if (defined $client_conf->{'optional_steps'}->{'test'});
306 $config_flags = '{' . join(',',@config_flags) . '}' ;
309 my $scm = $client_conf->{scm} || 'cvs';
310 my $scmurl = $client_conf->{scm_url};
313 insert into build_status
314 (sysname, snapshot,status, stage, log,conf_sum, branch,
315 changed_this_run, changed_since_success,
316 log_archive_filenames , log_archive, build_flags, scm, scmurl,
317 git_head_ref,frozen_conf)
318 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
323 # this transaction lets us set log_error_verbosity to terse
324 # just for the duration of the transaction. That turns off logging the
325 # bind params, so all the logs don't get stuffed on the postgres logs
330 #$db->do("select set_local_error_terse()");
333 $sth=$db->prepare($logst);
335 $sth->bind_param(1,$animal);
336 $sth->bind_param(2,$dbdate);
337 $sth->bind_param(3,$res & 0x8fffffff); # in case we get a 64 bit int status!
338 $sth->bind_param(4,$stage);
339 $sth->bind_param(5,$log);
340 $sth->bind_param(6,$conf);
341 $sth->bind_param(7,$branch);
342 $sth->bind_param(8,$changed_this_run);
343 $sth->bind_param(9,$changed_since_success);
344 $sth->bind_param(10,$log_file_names);
345 #$sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
346 $sth->bind_param(11,undef,{ pg_type => DBD::Pg::PG_BYTEA });
347 $sth->bind_param(12,$config_flags);
348 $sth->bind_param(13,$scm);
349 $sth->bind_param(14,$scmurl);
350 $sth->bind_param(15,$githeadref);
351 $sth->bind_param(16,$frozen_sconf,{ pg_type => DBD::Pg::PG_BYTEA });
353 $sqlres = $sth->execute;
362 insert into build_status_log
363 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
364 values (?, ?, ?, ?, ?, ?)
368 $sth = $db->prepare($logst2);
372 my $stage_start = $ts;
374 foreach my $log_file( @log_file_names )
376 next if $log_file =~ /^githead/;
378 open($handle,"$dirname/$log_file");
379 my $mtime = (stat $handle)[9];
380 my $stage_interval = $mtime - $stage_start;
381 $stage_start = $mtime;
382 my $ltext = <$handle>;
384 $ltext =~ s/\x00/\\0/g;
385 $sqlres = $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
386 "$stage_interval seconds");
390 $sth->finish unless $sqlres;
397 print "Status: 462 database failure\nContent-Type: text/plain\n\n";
398 print "Your report generated a database failure:\n",
409 my $prevst = <<EOSQL;
411 select coalesce((select distinct on (snapshot) stage
413 where sysname = ? and branch = ? and snapshot < ?
414 order by snapshot desc
415 limit 1), 'NEW') as prev_status
419 $sth=$db->prepare($prevst);
420 $sth->execute($animal,$branch,$dbdate);
421 my $row=$sth->fetchrow_arrayref;
422 my $prev_stat=$row->[0];
427 select operating_system|| ' / ' || os_version as os ,
428 compiler || ' / ' || compiler_version as compiler,
431 where status = 'approved'
436 $sth=$db->prepare($det_st);
437 $sth->execute($animal);
438 $row=$sth->fetchrow_arrayref;
439 my ($os, $compiler,$arch) = @$row;
443 # prevent occasional duplication by forcing serialization of this operation
444 $db->do("lock table dashboard_mat in share row exclusive mode");
445 $db->do("delete from dashboard_mat");
446 $db->do("insert into dashboard_mat select * from dashboard_mat_data");
452 # prevent occasional duplication by forcing serialization of this operation
453 $db->do("lock table nrecent_failures in share row exclusive mode");
454 $db->do("delete from nrecent_failures");
455 $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'");
461 print "Content-Type: text/plain\n\n";
462 print "request was on:\n";
463 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
465 my $client_events = $client_conf->{mail_events};
469 my $client_time = $client_conf->{current_ts};
470 open(TX,">>$buildlogs/$animal.$date");
471 print TX "\n",Dumper(\$client_conf),"\n";
472 print TX "server time: $server_time, client time: $client_time\n" if $client_time;
478 if (ref $client_events)
480 my $cbcc = $client_events->{all};
483 push @$bcc_stat, @$cbcc;
485 elsif (defined $cbcc)
487 push @$bcc_stat, $cbcc;
491 $cbcc = $client_events->{all};
494 push @$bcc_stat, @$cbcc;
496 elsif (defined $cbcc)
498 push @$bcc_stat, $cbcc;
501 $cbcc = $client_events->{change};
504 push @$bcc_chg, @$cbcc;
506 elsif (defined $cbcc)
508 push @$bcc_chg, $cbcc;
510 if ($stage eq 'OK' || $prev_stat eq 'OK')
512 $cbcc = $client_events->{green};
515 push @$bcc_chg, @$cbcc;
517 elsif (defined $cbcc)
519 push @$bcc_chg, $cbcc;
525 my $url = $query->url(-base => 1);
528 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
530 my $mailto = [@$all_stat];
531 push(@$mailto,@$fail_stat) if $stage ne 'OK';
533 my $me = `id -un`; chomp($me);
535 my $host = `hostname`; chomp ($host);
536 $host = $default_host unless ($host =~ m/[.]/ || !defined($default_host));
538 my $from_addr = "Exim Build Farm <$me\@$host>";
539 $from_addr =~ tr /\r\n//d;
541 my $msg = new Mail::Send;
545 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
546 $msg->subject("Exim BuildFarm member $animal Branch $branch $stat_type $stage");
547 $msg->set('From',$from_addr);
552 The Exim BuildFarm member $animal had the following event on branch $branch:
556 The snapshot timestamp for the build that triggered this notification is: $dbdate
558 The specs of this machine are:
563 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
569 exit if ($stage eq $prev_stat);
571 $mailto = [@$change_stat];
572 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
574 $msg = new Mail::Send;
578 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
580 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
581 "changed from OK to $stage";
582 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
583 $stat_type .= " failure" if $stage ne 'OK';
585 $msg->subject("Exim BuildFarm member $animal Branch $branch Status $stat_type");
586 $msg->set('From',$from_addr);
590 The Exim BuildFarm member $animal had the following event on branch $branch:
594 The snapshot timestamp for the build that triggered this notification is: $dbdate
596 The specs of this machine are:
601 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch