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);
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');
52 "branch=$branch&res=$res&stage=$stage&animal=$animal&".
53 "ts=$ts&log=$log&conf=$conf";
56 "changed_files=$changed_this_run&".
57 "changed_since_success=$changed_since_success&";
59 unless ($animal && $ts && $stage && $sig)
62 "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
63 "bad parameters for request\n";
68 unless ($branch =~ /^(HEAD|REL\d+_\d+_STABLE)$/)
71 "Status: 492 bad branch parameter $branch\nContent-Type: text/plain\n\n",
72 "bad branch parameter $branch\n";
78 my $db = DBI->connect($dsn,$dbuser,$dbpass);
80 die $DBI::errstr unless $db;
83 "select secret from buildsystems where name = ? and status = 'approved'";
84 my $sth = $db->prepare($gethost);
85 $sth->execute($animal);
86 my ($secret)=$sth->fetchrow_array();
89 my $tsdiff = time - $ts;
91 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
92 $year += 1900; $mon +=1;
94 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
96 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
98 open(TX,">$buildlogs/$animal.$date");
99 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
100 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
102 "changed_this_run:\n$changed_this_run\n",
103 "changed_since_success:\n$changed_since_success\n",
105 # $query->save(\*TX);
109 unless ($ts < time + 120)
111 my $gmt = gmtime($ts);
112 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
113 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
118 unless ($ts + 86400 > time)
120 my $gmt = gmtime($ts);
121 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
122 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
130 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
131 "System $animal is unknown\n";
140 my $calc_sig = sha1_hex($content,$secret);
141 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
143 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
146 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
147 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
152 # undo escape-proofing of base64 data and decode it
153 map {tr/$@/+=/; $_ = decode_base64($_); }
154 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive);
157 if ($log =~/Last file mtime in snapshot: (.*)/)
159 my $snaptime = parsedate($1);
160 if ($snaptime < (time - (10 * 86400)))
162 print "Status: 493 snapshot too old: $1\nContent-Type: text/plain\n\n";
163 print "snapshot to old: $1\n";
169 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
170 $year += 1900; $mon +=1;
172 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
176 my $dirname = "$buildlogs/tmp.$$.unpacklogs";
181 my $archname = "$buildlogs/tmp.$$.tgz";
182 open($log_handle,">$archname");
184 print $log_handle $log_archive;
187 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
188 map {s/\s+//g; } @log_file_names;
189 my @qnames = @log_file_names;
190 map { $_ = qq("$_"); } @qnames;
191 $log_file_names = '{' . join(',',@qnames) . '}';
196 my $container = new Safe;
198 unless ($sconf =~ s/.*(\$Script_Config)/$1/ms )
200 $sconf = '$Script_Config={};';
202 my $client_conf = $container->reval("$sconf;");
204 if ($min_script_version)
206 $client_conf->{script_version} ||= '0.0';
207 my ($minmajor,$minminor) = split(/\./,$min_script_version);
208 my ($smajor,$sminor) = split(/\./,$client_conf->{script_version});
209 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
211 print "Status: 460 script version too low\nContent-Type: text/plain\n\n";
213 "Script version is below minimum required\n",
214 "Reported version: $client_conf->{script_version},",
215 "Minumum version required: $min_script_version\n";
221 if ($min_web_script_version)
223 $client_conf->{web_script_version} ||= '0.0';
224 my ($minmajor,$minminor) = split(/\./,$min_script_version);
225 my ($smajor,$sminor) = split(/\./,$client_conf->{script_version});
226 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
228 print "Status: 461 web script version too low\nContent-Type: text/plain\n\n";
230 "Web Script version is below minimum required\n",
231 "Reported version: $client_conf->{web_script_version},",
232 "Minumum version required: $min_web_script_version\n";
239 if (not exists $client_conf->{config_opts} )
243 elsif (ref $client_conf->{config_opts} eq 'HASH')
245 # leave out keys with false values
246 @config_flags = grep { $client_conf->{config_opts}->{$_} }
247 keys %{$client_conf->{config_opts}};
249 elsif (ref $client_conf->{config_opts} eq 'ARRAY' )
251 @config_flags = @{$client_conf->{config_opts}};
256 @config_flags = grep {! m/=/ } @config_flags;
257 map {s/\s+//g; $_=qq("$_"); } @config_flags;
258 push @config_flags,'git' if $client_conf->{scm} eq 'git';
259 $config_flags = '{' . join(',',@config_flags) . '}' ;
262 my $scm = $client_conf->{scm} || 'cvs';
263 my $scmurl = $client_conf->{scm_url};
266 insert into build_status
267 (sysname, snapshot,status, stage, log,conf_sum, branch,
268 changed_this_run, changed_since_success,
269 log_archive_filenames , log_archive, build_flags, scm, scmurl)
270 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
273 $sth=$db->prepare($logst);
275 $sth->bind_param(1,$animal);
276 $sth->bind_param(2,$dbdate);
277 $sth->bind_param(3,$res);
278 $sth->bind_param(4,$stage);
279 $sth->bind_param(5,$log);
280 $sth->bind_param(6,$conf);
281 $sth->bind_param(7,$branch);
282 $sth->bind_param(8,$changed_this_run);
283 $sth->bind_param(9,$changed_since_success);
284 $sth->bind_param(10,$log_file_names);
285 #$sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
286 $sth->bind_param(11,undef,{ pg_type => DBD::Pg::PG_BYTEA });
287 $sth->bind_param(12,$config_flags);
288 $sth->bind_param(13,$scm);
289 $sth->bind_param(14,$scmurl);
294 my $logst2 = <<EOSQL;
296 insert into build_status_log
297 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
298 values (?, ?, ?, ?, ?, ?)
303 $sth = $db->prepare($logst2);
307 my $stage_start = $ts;
309 foreach my $log_file( @log_file_names )
312 open($handle,"$dirname/$log_file");
313 my $mtime = (stat $handle)[9];
314 my $stage_interval = $mtime - $stage_start;
315 $stage_start = $mtime;
316 my $ltext = <$handle>;
318 $ltext =~ s/\x00/\\0/g;
319 $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
320 "$stage_interval seconds");
326 my $prevst = <<EOSQL;
328 select coalesce((select distinct on (snapshot) stage
330 where sysname = ? and branch = ? and snapshot < ?
331 order by snapshot desc
332 limit 1), 'NEW') as prev_status
336 $sth=$db->prepare($prevst);
337 $sth->execute($animal,$branch,$dbdate);
338 my $row=$sth->fetchrow_arrayref;
339 my $prev_stat=$row->[0];
344 select operating_system|| ' / ' || os_version as os ,
345 compiler || ' / ' || compiler_version as compiler,
348 where status = 'approved'
353 $sth=$db->prepare($det_st);
354 $sth->execute($animal);
355 $row=$sth->fetchrow_arrayref;
356 my ($os, $compiler,$arch) = @$row;
360 # prevent occasional duplication by forcing serialization of this operation
361 $db->do("lock table dashboard_mat in share row exclusive mode");
362 $db->do("delete from dashboard_mat");
363 $db->do("insert into dashboard_mat select * from dashboard_mat_data");
368 print "Content-Type: text/plain\n\n";
369 print "request was on:\n";
370 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
372 my $client_events = $client_conf->{mail_events};
376 my $client_time = $client_conf->{current_ts};
377 open(TX,">>$buildlogs/$animal.$date");
378 print TX "\n",Dumper(\$client_conf),"\n";
379 print TX "server time: $server_time, client time: $client_time\n" if $client_time;
385 if (ref $client_events)
387 my $cbcc = $client_events->{all};
390 push @$bcc_stat, @$cbcc;
392 elsif (defined $cbcc)
394 push @$bcc_stat, $cbcc;
398 $cbcc = $client_events->{all};
401 push @$bcc_stat, @$cbcc;
403 elsif (defined $cbcc)
405 push @$bcc_stat, $cbcc;
408 $cbcc = $client_events->{change};
411 push @$bcc_chg, @$cbcc;
413 elsif (defined $cbcc)
415 push @$bcc_chg, $cbcc;
417 if ($stage eq 'OK' || $prev_stat eq 'OK')
419 $cbcc = $client_events->{green};
422 push @$bcc_chg, @$cbcc;
424 elsif (defined $cbcc)
426 push @$bcc_chg, $cbcc;
432 my $url = $query->url(-base => 1);
435 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
437 my $mailto = [@$all_stat];
438 push(@$mailto,@$fail_stat) if $stage ne 'OK';
440 my $me = `id -un`; chomp($me);
442 my $host = `hostname`; chomp ($host);
443 $host = $default_host unless ($host =~ m/[.]/ || !defined($default_host));
445 my $from_addr = "PG Build Farm <$me\@$host>";
446 $from_addr =~ tr /\r\n//d;
448 my $msg = new Mail::Send;
452 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
453 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
454 $msg->set('From',$from_addr);
459 The PGBuildfarm member $animal had the following event on branch $branch:
463 The snapshot timestamp for the build that triggered this notification is: $dbdate
465 The specs of this machine are:
470 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
476 exit if ($stage eq $prev_stat);
478 $mailto = [@$change_stat];
479 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
481 $msg = new Mail::Send;
485 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
487 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
488 "changed from OK to $stage";
489 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
490 $stat_type .= " failure" if $stage ne 'OK';
492 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
493 $msg->set('From',$from_addr);
497 The PGBuildfarm member $animal had the following event on branch $branch:
501 The snapshot timestamp for the build that triggered this notification is: $dbdate
503 The specs of this machine are:
508 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch