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);
23 use Storable qw(thaw);
25 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
26 my $buildlogs = "$ENV{BFConfDir}/buildlogs";
28 die "no dbname" unless $dbname;
29 die "no dbuser" unless $dbuser;
31 my $dsn="dbi:Pg:dbname=$dbname";
32 $dsn .= ";host=$dbhost" if $dbhost;
33 $dsn .= ";port=$dbport" if $dbport;
37 my $sig = $query->path_info;
40 my $stage = $query->param('stage');
41 my $ts = $query->param('ts');
42 my $animal = $query->param('animal');
43 my $log = $query->param('log');
44 my $res = $query->param('res');
45 my $conf = $query->param('conf');
46 my $branch = $query->param('branch');
47 my $changed_since_success = $query->param('changed_since_success');
48 my $changed_this_run = $query->param('changed_files');
49 my $log_archive = $query->param('logtar');
50 my $frozen_sconf = $query->param('frozen_sconf') || '';
53 "branch=$branch&res=$res&stage=$stage&animal=$animal&".
54 "ts=$ts&log=$log&conf=$conf";
57 "changed_files=$changed_this_run&".
58 "changed_since_success=$changed_since_success&";
60 unless ($animal && $ts && $stage && $sig)
63 "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
64 "bad parameters for request\n";
69 unless ($branch =~ /^(HEAD|REL\d+_\d+_STABLE)$/)
72 "Status: 492 bad branch parameter $branch\nContent-Type: text/plain\n\n",
73 "bad branch parameter $branch\n";
79 my $db = DBI->connect($dsn,$dbuser,$dbpass);
81 die $DBI::errstr unless $db;
84 "select secret from buildsystems where name = ? and status = 'approved'";
85 my $sth = $db->prepare($gethost);
86 $sth->execute($animal);
87 my ($secret)=$sth->fetchrow_array();
90 my $tsdiff = time - $ts;
92 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
93 $year += 1900; $mon +=1;
95 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
97 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
99 open(TX,">$buildlogs/$animal.$date");
100 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
101 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
103 "changed_this_run:\n$changed_this_run\n",
104 "changed_since_success:\n$changed_since_success\n",
105 "frozen_sconf:$frozen_sconf\n",
107 # $query->save(\*TX);
111 unless ($ts < time + 120)
113 my $gmt = gmtime($ts);
114 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
115 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
120 unless ($ts + 86400 > time)
122 my $gmt = gmtime($ts);
123 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
124 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
132 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
133 "System $animal is unknown\n";
142 my $calc_sig = sha1_hex($content,$secret);
143 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
145 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
148 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
149 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
154 # undo escape-proofing of base64 data and decode it
155 map {tr/$@/+=/; $_ = decode_base64($_); }
156 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive, $frozen_sconf);
158 if ($log =~/Last file mtime in snapshot: (.*)/)
160 my $snaptime = parsedate($1);
161 if ($snaptime < (time - (10 * 86400)))
163 print "Status: 493 snapshot too old: $1\nContent-Type: text/plain\n\n";
164 print "snapshot to old: $1\n";
170 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
171 $year += 1900; $mon +=1;
173 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
177 my $dirname = "$buildlogs/tmp.$$.unpacklogs";
184 my $archname = "$buildlogs/tmp.$$.tgz";
185 open($log_handle,">$archname");
187 print $log_handle $log_archive;
190 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
191 map {s/\s+//g; } @log_file_names;
192 my @qnames = grep { $_ ne 'githead.log' } @log_file_names;
193 map { $_ = qq("$_"); } @qnames;
194 $log_file_names = '{' . join(',',@qnames) . '}';
195 if (-e "$dirname/githead.log" )
197 open(my $githead,"$dirname/githead.log");
198 $githeadref = <$githead>;
209 $client_conf = thaw $frozen_sconf;
212 if ($min_script_version)
214 $client_conf->{script_version} ||= '0.0';
215 my $cli_ver = $client_conf->{script_version} ;
216 $cli_ver =~ s/^REL_//;
217 my ($minmajor,$minminor) = split(/\./,$min_script_version);
218 my ($smajor,$sminor) = split(/\./,$cli_ver);
219 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
221 print "Status: 460 script version too low\nContent-Type: text/plain\n\n";
223 "Script version is below minimum required\n",
224 "Reported version: $client_conf->{script_version},",
225 "Minumum version required: $min_script_version\n";
231 if ($min_web_script_version)
233 $client_conf->{web_script_version} ||= '0.0';
234 my $cli_ver = $client_conf->{web_script_version} ;
235 $cli_ver =~ s/^REL_//;
236 my ($minmajor,$minminor) = split(/\./,$min_web_script_version);
237 my ($smajor,$sminor) = split(/\./,$cli_ver);
238 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
240 print "Status: 461 web script version too low\nContent-Type: text/plain\n\n";
242 "Web Script version is below minimum required\n",
243 "Reported version: $client_conf->{web_script_version}, ",
244 "Minumum version required: $min_web_script_version\n"
252 if (not exists $client_conf->{config_opts} )
256 elsif (ref $client_conf->{config_opts} eq 'HASH')
258 # leave out keys with false values
259 @config_flags = grep { $client_conf->{config_opts}->{$_} }
260 keys %{$client_conf->{config_opts}};
262 elsif (ref $client_conf->{config_opts} eq 'ARRAY' )
264 @config_flags = @{$client_conf->{config_opts}};
269 @config_flags = grep {! m/=/ } @config_flags;
270 map {s/\s+//g; $_=qq("$_"); } @config_flags;
271 push @config_flags,'git' if $client_conf->{scm} eq 'git';
272 $config_flags = '{' . join(',',@config_flags) . '}' ;
275 my $scm = $client_conf->{scm} || 'cvs';
276 my $scmurl = $client_conf->{scm_url};
279 insert into build_status
280 (sysname, snapshot,status, stage, log,conf_sum, branch,
281 changed_this_run, changed_since_success,
282 log_archive_filenames , log_archive, build_flags, scm, scmurl,
283 git_head_ref,frozen_conf)
284 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
289 # this transaction lets us set log_error_verbosity to terse
290 # just for the duration of the transaction. That turns off logging the
291 # bind params, so all the logs don't get stuffed on the postgres logs
294 $db->do("select set_local_error_terse()");
297 $sth=$db->prepare($logst);
299 $sth->bind_param(1,$animal);
300 $sth->bind_param(2,$dbdate);
301 $sth->bind_param(3,$res);
302 $sth->bind_param(4,$stage);
303 $sth->bind_param(5,$log);
304 $sth->bind_param(6,$conf);
305 $sth->bind_param(7,$branch);
306 $sth->bind_param(8,$changed_this_run);
307 $sth->bind_param(9,$changed_since_success);
308 $sth->bind_param(10,$log_file_names);
309 #$sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
310 $sth->bind_param(11,undef,{ pg_type => DBD::Pg::PG_BYTEA });
311 $sth->bind_param(12,$config_flags);
312 $sth->bind_param(13,$scm);
313 $sth->bind_param(14,$scmurl);
314 $sth->bind_param(15,$githeadref);
315 $sth->bind_param(16,$frozen_sconf,{ pg_type => DBD::Pg::PG_BYTEA });
322 my $logst2 = <<EOSQL;
324 insert into build_status_log
325 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
326 values (?, ?, ?, ?, ?, ?)
331 $sth = $db->prepare($logst2);
335 my $stage_start = $ts;
337 foreach my $log_file( @log_file_names )
339 next if $log_file =~ /^githead/;
341 open($handle,"$dirname/$log_file");
342 my $mtime = (stat $handle)[9];
343 my $stage_interval = $mtime - $stage_start;
344 $stage_start = $mtime;
345 my $ltext = <$handle>;
347 $ltext =~ s/\x00/\\0/g;
348 $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
349 "$stage_interval seconds");
356 my $prevst = <<EOSQL;
358 select coalesce((select distinct on (snapshot) stage
360 where sysname = ? and branch = ? and snapshot < ?
361 order by snapshot desc
362 limit 1), 'NEW') as prev_status
366 $sth=$db->prepare($prevst);
367 $sth->execute($animal,$branch,$dbdate);
368 my $row=$sth->fetchrow_arrayref;
369 my $prev_stat=$row->[0];
374 select operating_system|| ' / ' || os_version as os ,
375 compiler || ' / ' || compiler_version as compiler,
378 where status = 'approved'
383 $sth=$db->prepare($det_st);
384 $sth->execute($animal);
385 $row=$sth->fetchrow_arrayref;
386 my ($os, $compiler,$arch) = @$row;
390 # prevent occasional duplication by forcing serialization of this operation
391 $db->do("lock table dashboard_mat in share row exclusive mode");
392 $db->do("delete from dashboard_mat");
393 $db->do("insert into dashboard_mat select * from dashboard_mat_data");
398 print "Content-Type: text/plain\n\n";
399 print "request was on:\n";
400 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
402 my $client_events = $client_conf->{mail_events};
406 my $client_time = $client_conf->{current_ts};
407 open(TX,">>$buildlogs/$animal.$date");
408 print TX "\n",Dumper(\$client_conf),"\n";
409 print TX "server time: $server_time, client time: $client_time\n" if $client_time;
415 if (ref $client_events)
417 my $cbcc = $client_events->{all};
420 push @$bcc_stat, @$cbcc;
422 elsif (defined $cbcc)
424 push @$bcc_stat, $cbcc;
428 $cbcc = $client_events->{all};
431 push @$bcc_stat, @$cbcc;
433 elsif (defined $cbcc)
435 push @$bcc_stat, $cbcc;
438 $cbcc = $client_events->{change};
441 push @$bcc_chg, @$cbcc;
443 elsif (defined $cbcc)
445 push @$bcc_chg, $cbcc;
447 if ($stage eq 'OK' || $prev_stat eq 'OK')
449 $cbcc = $client_events->{green};
452 push @$bcc_chg, @$cbcc;
454 elsif (defined $cbcc)
456 push @$bcc_chg, $cbcc;
462 my $url = $query->url(-base => 1);
465 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
467 my $mailto = [@$all_stat];
468 push(@$mailto,@$fail_stat) if $stage ne 'OK';
470 my $me = `id -un`; chomp($me);
472 my $host = `hostname`; chomp ($host);
473 $host = $default_host unless ($host =~ m/[.]/ || !defined($default_host));
475 my $from_addr = "PG Build Farm <$me\@$host>";
476 $from_addr =~ tr /\r\n//d;
478 my $msg = new Mail::Send;
482 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
483 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
484 $msg->set('From',$from_addr);
489 The PGBuildfarm member $animal had the following event on branch $branch:
493 The snapshot timestamp for the build that triggered this notification is: $dbdate
495 The specs of this machine are:
500 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
506 exit if ($stage eq $prev_stat);
508 $mailto = [@$change_stat];
509 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
511 $msg = new Mail::Send;
515 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
517 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
518 "changed from OK to $stage";
519 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
520 $stat_type .= " failure" if $stage ne 'OK';
522 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
523 $msg->set('From',$from_addr);
527 The PGBuildfarm member $animal had the following event on branch $branch:
531 The snapshot timestamp for the build that triggered this notification is: $dbdate
533 The specs of this machine are:
538 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch