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
11 # force this before we do anything - even load modules
12 BEGIN { $server_time = time; }
15 use Digest::SHA1 qw(sha1_hex);
23 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
25 die "no dbname" unless $dbname;
26 die "no dbuser" unless $dbuser;
28 my $dsn="dbi:Pg:dbname=$dbname";
29 $dsn .= ";host=$dbhost" if $dbhost;
30 $dsn .= ";port=$dbport" if $dbport;
34 my $sig = $query->path_info;
37 my $stage = $query->param('stage');
38 my $ts = $query->param('ts');
39 my $animal = $query->param('animal');
40 my $log = $query->param('log');
41 my $res = $query->param('res');
42 my $conf = $query->param('conf');
43 my $branch = $query->param('branch');
44 my $changed_since_success = $query->param('changed_since_success');
45 my $changed_this_run = $query->param('changed_files');
46 my $log_archive = $query->param('logtar');
49 "branch=$branch&res=$res&stage=$stage&animal=$animal&".
50 "ts=$ts&log=$log&conf=$conf";
53 "changed_files=$changed_this_run&".
54 "changed_since_success=$changed_since_success&";
56 unless ($animal && $ts && $stage && $sig)
59 "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
60 "bad parameters for request\n";
65 unless ($branch =~ /^(HEAD|REL\d+_\d+_STABLE)$/)
68 "Status: 492 bad branch parameter $branch\nContent-Type: text/plain\n\n",
69 "bad branch parameter $branch\n";
75 my $db = DBI->connect($dsn,$dbuser,$dbpass);
77 die $DBI::errstr unless $db;
80 "select secret from buildsystems where name = ? and status = 'approved'";
81 my $sth = $db->prepare($gethost);
82 $sth->execute($animal);
83 my ($secret)=$sth->fetchrow_array();
86 my $tsdiff = time - $ts;
88 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
89 $year += 1900; $mon +=1;
91 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
93 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
95 open(TX,">../buildlogs/$animal.$date");
96 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
97 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
99 "changed_this_run:\n$changed_this_run\n",
100 "changed_since_success:\n$changed_since_success\n",
102 # $query->save(\*TX);
106 unless ($ts < time + 120)
108 my $gmt = gmtime($ts);
109 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
110 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
115 unless ($ts + 86400 > time)
117 my $gmt = gmtime($ts);
118 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
119 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
127 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
128 "System $animal is unknown\n";
137 my $calc_sig = sha1_hex($content,$secret);
138 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
140 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
143 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
144 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
149 # undo escape-proofing of base64 data and decode it
150 map {tr/$@/+=/; $_ = decode_base64($_); }
151 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive);
153 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
154 $year += 1900; $mon +=1;
156 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
160 my $dirname = "../buildlogs/tmp.$$.unpacklogs";
165 my $archname = "../buildlogs/tmp.$$.tgz";
166 open($log_handle,">$archname");
168 print $log_handle $log_archive;
171 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
172 map {s/\s+//g; } @log_file_names;
173 my @qnames = @log_file_names;
174 map { $_ = qq("$_"); } @qnames;
175 $log_file_names = '{' . join(',',@qnames) . '}';
180 my $container = new Safe;
182 unless ($sconf =~ s/.*(\$Script_Config)/$1/ms )
184 $sconf = '$Script_Config={};';
186 my $client_conf = $container->reval("$sconf;");
188 if ($min_script_version)
190 $client_conf->{script_version} ||= '0.0';
191 my ($minmajor,$minminor) = split(/\./,$min_script_version);
192 my ($smajor,$sminor) = split(/\./,$client_conf->{script_version});
193 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
195 print "Status: 460 script version too low\nContent-Type: text/plain\n\n";
197 "Script version is below minimum required\n",
198 "Reported version: $client_conf->{script_version},",
199 "Minumum version required: $min_script_version\n";
205 if ($min_web_script_version)
207 $client_conf->{web_script_version} ||= '0.0';
208 my ($minmajor,$minminor) = split(/\./,$min_script_version);
209 my ($smajor,$sminor) = split(/\./,$client_conf->{script_version});
210 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
212 print "Status: 461 web script version too low\nContent-Type: text/plain\n\n";
214 "Web Script version is below minimum required\n",
215 "Reported version: $client_conf->{web_script_version},",
216 "Minumum version required: $min_web_script_version\n";
223 if (not exists $client_conf->{config_opts} )
227 elsif (ref $client_conf->{config_opts} eq 'HASH')
229 # leave out keys with false values
230 @config_flags = grep { $client_conf->{config_opts}->{$_} }
231 keys %{$client_conf->{config_opts}};
233 elsif (ref $client_conf->{config_opts} eq 'ARRAY' )
235 @config_flags = @{$client_conf->{config_opts}};
240 @config_flags = grep {! m/=/ } @config_flags;
241 map {s/\s+//g; $_=qq("$_"); } @config_flags;
242 push @config_flags,'git' if $client_conf->{scm} eq 'git';
243 $config_flags = '{' . join(',',@config_flags) . '}' ;
246 my $scm = $client_conf->{scm} || 'cvs';
247 my $scmurl = $client_conf->{scm_url};
250 insert into build_status
251 (sysname, snapshot,status, stage, log,conf_sum, branch,
252 changed_this_run, changed_since_success,
253 log_archive_filenames , log_archive, build_flags, scm, scmurl)
254 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
257 $sth=$db->prepare($logst);
259 $sth->bind_param(1,$animal);
260 $sth->bind_param(2,$dbdate);
261 $sth->bind_param(3,$res);
262 $sth->bind_param(4,$stage);
263 $sth->bind_param(5,$log);
264 $sth->bind_param(6,$conf);
265 $sth->bind_param(7,$branch);
266 $sth->bind_param(8,$changed_this_run);
267 $sth->bind_param(9,$changed_since_success);
268 $sth->bind_param(10,$log_file_names);
269 #$sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
270 $sth->bind_param(11,undef,{ pg_type => DBD::Pg::PG_BYTEA });
271 $sth->bind_param(12,$config_flags);
272 $sth->bind_param(13,$scm);
273 $sth->bind_param(14,$scmurl);
278 my $logst2 = <<EOSQL;
280 insert into build_status_log
281 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
282 values (?, ?, ?, ?, ?, ?)
287 $sth = $db->prepare($logst2);
291 my $stage_start = $ts;
293 foreach my $log_file( @log_file_names )
296 open($handle,"$dirname/$log_file");
297 my $mtime = (stat $handle)[9];
298 my $stage_interval = $mtime - $stage_start;
299 $stage_start = $mtime;
300 my $ltext = <$handle>;
302 $ltext =~ s/\x00/\\0/g;
303 $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
304 "$stage_interval seconds");
310 my $prevst = <<EOSQL;
312 select coalesce((select distinct on (snapshot) stage
314 where sysname = ? and branch = ? and snapshot < ?
315 order by snapshot desc
316 limit 1), 'NEW') as prev_status
320 $sth=$db->prepare($prevst);
321 $sth->execute($animal,$branch,$dbdate);
322 my $row=$sth->fetchrow_arrayref;
323 my $prev_stat=$row->[0];
328 select operating_system|| ' / ' || os_version as os ,
329 compiler || ' / ' || compiler_version as compiler,
332 where status = 'approved'
337 $sth=$db->prepare($det_st);
338 $sth->execute($animal);
339 $row=$sth->fetchrow_arrayref;
340 my ($os, $compiler,$arch) = @$row;
344 my $have_status = $db->selectrow_arrayref( q{
346 from build_status_latest
347 where sysname = ? and branch = ?
349 undef, $animal, $branch);
353 update build_status_latest
354 set latest_snapshot = ?
355 where sysname = ? and branch = ?
357 undef, $dbdate, $animal, $branch);
362 insert into build_status_latest
363 (sysname, branch, latest_snapshot)
366 undef, $animal, $branch, $dbdate);
371 $db->do("delete from dashboard_mat");
372 $db->do("insert into dashboard_mat select * from dashboard_mat_data2");
377 print "Content-Type: text/plain\n\n";
378 print "request was on:\n";
379 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
381 my $client_events = $client_conf->{mail_events};
385 my $client_time = $client_conf->{current_ts};
386 open(TX,">>../buildlogs/$animal.$date");
387 print TX "\n",Dumper(\$client_conf),"\n";
388 print TX "server time: $server_time, client time: $client_time\n" if $client_time;
394 if (ref $client_events)
396 my $cbcc = $client_events->{all};
399 push @$bcc_stat, @$cbcc;
401 elsif (defined $cbcc)
403 push @$bcc_stat, $cbcc;
407 $cbcc = $client_events->{all};
410 push @$bcc_stat, @$cbcc;
412 elsif (defined $cbcc)
414 push @$bcc_stat, $cbcc;
417 $cbcc = $client_events->{change};
420 push @$bcc_chg, @$cbcc;
422 elsif (defined $cbcc)
424 push @$bcc_chg, $cbcc;
426 if ($stage eq 'OK' || $prev_stat eq 'OK')
428 $cbcc = $client_events->{green};
431 push @$bcc_chg, @$cbcc;
433 elsif (defined $cbcc)
435 push @$bcc_chg, $cbcc;
441 my $url = $query->url(-base => 1);
444 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
446 my $mailto = [@$all_stat];
447 push(@$mailto,@$fail_stat) if $stage ne 'OK';
449 my $me = `id -un`; chomp $me;
451 my $host = `hostname`; chomp $host;
453 my $msg = new Mail::Send;
455 $msg->set('From',"PG Build Farm <$me\@$host>");
458 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
459 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
464 The PGBuildfarm member $animal had the following event on branch $branch:
468 The snapshot timestamp for the build that triggered this notification is: $dbdate
470 The specs of this machine are:
475 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
481 exit if ($stage eq $prev_stat);
483 $mailto = [@$change_stat];
484 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
486 $msg = new Mail::Send;
488 $msg->set('From',"PG Build Farm <$me\@$host>");
491 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
493 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
494 "changed from OK to $stage";
495 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
496 $stat_type .= " failure" if $stage ne 'OK';
498 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
502 The PGBuildfarm member $animal had the following event on branch $branch:
506 The snapshot timestamp for the build that triggered this notification is: $dbdate
508 The specs of this machine are:
513 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch