5 use vars qw($dbhost $dbname $dbuser $dbpass $dbport
6 $all_stat $fail_stat $change_stat $green_stat
8 $min_script_version $min_web_script_version
12 # force this before we do anything - even load modules
13 BEGIN { $server_time = time; }
16 use Digest::SHA1 qw(sha1_hex);
24 use Storable qw(thaw);
26 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
27 my $buildlogs = "$ENV{BFConfDir}/buildlogs";
29 die "no dbname" unless $dbname;
30 die "no dbuser" unless $dbuser;
32 my $dsn="dbi:Pg:dbname=$dbname";
33 $dsn .= ";host=$dbhost" if $dbhost;
34 $dsn .= ";port=$dbport" if $dbport;
38 my $sig = $query->path_info;
41 my $stage = $query->param('stage');
42 my $ts = $query->param('ts');
43 my $animal = $query->param('animal');
44 my $log = $query->param('log');
45 my $res = $query->param('res');
46 my $conf = $query->param('conf');
47 my $branch = $query->param('branch');
48 my $changed_since_success = $query->param('changed_since_success');
49 my $changed_this_run = $query->param('changed_files');
50 my $log_archive = $query->param('logtar');
51 my $frozen_sconf = $query->param('frozen_sconf') || '';
54 "branch=$branch&res=$res&stage=$stage&animal=$animal&".
55 "ts=$ts&log=$log&conf=$conf";
58 "changed_files=$changed_this_run&".
59 "changed_since_success=$changed_since_success&";
61 unless ($animal && $ts && $stage && $sig)
64 "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
65 "bad parameters for request\n";
70 unless ($branch =~ /^(HEAD|REL\d+_\d+_STABLE)$/)
73 "Status: 492 bad branch parameter $branch\nContent-Type: text/plain\n\n",
74 "bad branch parameter $branch\n";
80 my $db = DBI->connect($dsn,$dbuser,$dbpass);
82 die $DBI::errstr unless $db;
85 "select secret from buildsystems where name = ? and status = 'approved'";
86 my $sth = $db->prepare($gethost);
87 $sth->execute($animal);
88 my ($secret)=$sth->fetchrow_array();
91 my $tsdiff = time - $ts;
93 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
94 $year += 1900; $mon +=1;
96 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
98 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
100 open(TX,">$buildlogs/$animal.$date");
101 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
102 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
104 "changed_this_run:\n$changed_this_run\n",
105 "changed_since_success:\n$changed_since_success\n",
106 "frozen_sconf:$frozen_sconf\n",
108 # $query->save(\*TX);
112 unless ($ts < time + 120)
114 my $gmt = gmtime($ts);
115 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
116 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
121 unless ($ts + 86400 > time)
123 my $gmt = gmtime($ts);
124 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
125 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
133 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
134 "System $animal is unknown\n";
143 my $calc_sig = sha1_hex($content,$secret);
144 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
146 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
149 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
150 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
155 # undo escape-proofing of base64 data and decode it
156 map {tr/$@/+=/; $_ = decode_base64($_); }
157 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive, $frozen_sconf);
159 if ($log =~/Last file mtime in snapshot: (.*)/)
161 my $snaptime = parsedate($1);
162 if ($snaptime < (time - (10 * 86400)))
164 print "Status: 493 snapshot too old: $1\nContent-Type: text/plain\n\n";
165 print "snapshot to old: $1\n";
171 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
172 $year += 1900; $mon +=1;
174 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
178 my $dirname = "$buildlogs/tmp.$$.unpacklogs";
185 my $archname = "$buildlogs/tmp.$$.tgz";
186 open($log_handle,">$archname");
188 print $log_handle $log_archive;
191 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
192 map {s/\s+//g; } @log_file_names;
193 my @qnames = grep { $_ ne 'githead.log' } @log_file_names;
194 map { $_ = qq("$_"); } @qnames;
195 $log_file_names = '{' . join(',',@qnames) . '}';
196 if (-e "$dirname/githead.log" )
198 open(my $githead,"$dirname/githead.log");
199 $githeadref = <$githead>;
210 $client_conf = thaw $frozen_sconf;
214 my $container = new Safe;
216 unless ($sconf =~ s/.*(\$Script_Config)/$1/ms )
218 $sconf = '$Script_Config={};';
220 $client_conf = $container->reval("$sconf;");
223 if ($min_script_version)
225 $client_conf->{script_version} ||= '0.0';
226 my ($minmajor,$minminor) = split(/\./,$min_script_version);
227 my ($smajor,$sminor) = split(/\./,$client_conf->{script_version});
228 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
230 print "Status: 460 script version too low\nContent-Type: text/plain\n\n";
232 "Script version is below minimum required\n",
233 "Reported version: $client_conf->{script_version},",
234 "Minumum version required: $min_script_version\n";
240 if ($min_web_script_version)
242 $client_conf->{web_script_version} ||= '0.0';
243 my ($minmajor,$minminor) = split(/\./,$min_script_version);
244 my ($smajor,$sminor) = split(/\./,$client_conf->{script_version});
245 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
247 print "Status: 461 web script version too low\nContent-Type: text/plain\n\n";
249 "Web Script version is below minimum required\n",
250 "Reported version: $client_conf->{web_script_version},",
251 "Minumum version required: $min_web_script_version\n";
258 if (not exists $client_conf->{config_opts} )
262 elsif (ref $client_conf->{config_opts} eq 'HASH')
264 # leave out keys with false values
265 @config_flags = grep { $client_conf->{config_opts}->{$_} }
266 keys %{$client_conf->{config_opts}};
268 elsif (ref $client_conf->{config_opts} eq 'ARRAY' )
270 @config_flags = @{$client_conf->{config_opts}};
275 @config_flags = grep {! m/=/ } @config_flags;
276 map {s/\s+//g; $_=qq("$_"); } @config_flags;
277 push @config_flags,'git' if $client_conf->{scm} eq 'git';
278 $config_flags = '{' . join(',',@config_flags) . '}' ;
281 my $scm = $client_conf->{scm} || 'cvs';
282 my $scmurl = $client_conf->{scm_url};
285 insert into build_status
286 (sysname, snapshot,status, stage, log,conf_sum, branch,
287 changed_this_run, changed_since_success,
288 log_archive_filenames , log_archive, build_flags, scm, scmurl,
289 git_head_ref,frozen_conf)
290 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
293 $sth=$db->prepare($logst);
295 $sth->bind_param(1,$animal);
296 $sth->bind_param(2,$dbdate);
297 $sth->bind_param(3,$res);
298 $sth->bind_param(4,$stage);
299 $sth->bind_param(5,$log);
300 $sth->bind_param(6,$conf);
301 $sth->bind_param(7,$branch);
302 $sth->bind_param(8,$changed_this_run);
303 $sth->bind_param(9,$changed_since_success);
304 $sth->bind_param(10,$log_file_names);
305 #$sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
306 $sth->bind_param(11,undef,{ pg_type => DBD::Pg::PG_BYTEA });
307 $sth->bind_param(12,$config_flags);
308 $sth->bind_param(13,$scm);
309 $sth->bind_param(14,$scmurl);
310 $sth->bind_param(15,$githeadref);
311 $sth->bind_param(16,$frozen_sconf,{ pg_type => DBD::Pg::PG_BYTEA });
316 my $logst2 = <<EOSQL;
318 insert into build_status_log
319 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
320 values (?, ?, ?, ?, ?, ?)
325 $sth = $db->prepare($logst2);
329 my $stage_start = $ts;
331 foreach my $log_file( @log_file_names )
333 next if $log_file =~ /^githead/;
335 open($handle,"$dirname/$log_file");
336 my $mtime = (stat $handle)[9];
337 my $stage_interval = $mtime - $stage_start;
338 $stage_start = $mtime;
339 my $ltext = <$handle>;
341 $ltext =~ s/\x00/\\0/g;
342 $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
343 "$stage_interval seconds");
349 my $prevst = <<EOSQL;
351 select coalesce((select distinct on (snapshot) stage
353 where sysname = ? and branch = ? and snapshot < ?
354 order by snapshot desc
355 limit 1), 'NEW') as prev_status
359 $sth=$db->prepare($prevst);
360 $sth->execute($animal,$branch,$dbdate);
361 my $row=$sth->fetchrow_arrayref;
362 my $prev_stat=$row->[0];
367 select operating_system|| ' / ' || os_version as os ,
368 compiler || ' / ' || compiler_version as compiler,
371 where status = 'approved'
376 $sth=$db->prepare($det_st);
377 $sth->execute($animal);
378 $row=$sth->fetchrow_arrayref;
379 my ($os, $compiler,$arch) = @$row;
383 # prevent occasional duplication by forcing serialization of this operation
384 $db->do("lock table dashboard_mat in share row exclusive mode");
385 $db->do("delete from dashboard_mat");
386 $db->do("insert into dashboard_mat select * from dashboard_mat_data");
391 print "Content-Type: text/plain\n\n";
392 print "request was on:\n";
393 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
395 my $client_events = $client_conf->{mail_events};
399 my $client_time = $client_conf->{current_ts};
400 open(TX,">>$buildlogs/$animal.$date");
401 print TX "\n",Dumper(\$client_conf),"\n";
402 print TX "server time: $server_time, client time: $client_time\n" if $client_time;
408 if (ref $client_events)
410 my $cbcc = $client_events->{all};
413 push @$bcc_stat, @$cbcc;
415 elsif (defined $cbcc)
417 push @$bcc_stat, $cbcc;
421 $cbcc = $client_events->{all};
424 push @$bcc_stat, @$cbcc;
426 elsif (defined $cbcc)
428 push @$bcc_stat, $cbcc;
431 $cbcc = $client_events->{change};
434 push @$bcc_chg, @$cbcc;
436 elsif (defined $cbcc)
438 push @$bcc_chg, $cbcc;
440 if ($stage eq 'OK' || $prev_stat eq 'OK')
442 $cbcc = $client_events->{green};
445 push @$bcc_chg, @$cbcc;
447 elsif (defined $cbcc)
449 push @$bcc_chg, $cbcc;
455 my $url = $query->url(-base => 1);
458 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
460 my $mailto = [@$all_stat];
461 push(@$mailto,@$fail_stat) if $stage ne 'OK';
463 my $me = `id -un`; chomp($me);
465 my $host = `hostname`; chomp ($host);
466 $host = $default_host unless ($host =~ m/[.]/ || !defined($default_host));
468 my $from_addr = "PG Build Farm <$me\@$host>";
469 $from_addr =~ tr /\r\n//d;
471 my $msg = new Mail::Send;
475 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
476 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
477 $msg->set('From',$from_addr);
482 The PGBuildfarm member $animal had the following event on branch $branch:
486 The snapshot timestamp for the build that triggered this notification is: $dbdate
488 The specs of this machine are:
493 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
499 exit if ($stage eq $prev_stat);
501 $mailto = [@$change_stat];
502 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
504 $msg = new Mail::Send;
508 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
510 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
511 "changed from OK to $stage";
512 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
513 $stat_type .= " failure" if $stage ne 'OK';
515 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
516 $msg->set('From',$from_addr);
520 The PGBuildfarm member $animal had the following event on branch $branch:
524 The snapshot timestamp for the build that triggered this notification is: $dbdate
526 The specs of this machine are:
531 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch