6 use Digest::SHA1 qw(sha1_hex);
14 use vars qw($dbhost $dbname $dbuser $dbpass $dbport
15 $all_stat $fail_stat $change_stat $green_stat
18 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
20 die "no dbname" unless $dbname;
21 die "no dbuser" unless $dbuser;
23 my $dsn="dbi:Pg:dbname=$dbname";
24 $dsn .= ";host=$dbhost" if $dbhost;
25 $dsn .= ";port=$dbport" if $dbport;
29 my $sig = $query->path_info;
32 my $stage = $query->param('stage');
33 my $ts = $query->param('ts');
34 my $animal = $query->param('animal');
35 my $log = $query->param('log');
36 my $res = $query->param('res');
37 my $conf = $query->param('conf');
38 my $branch = $query->param('branch');
39 my $changed_since_success = $query->param('changed_since_success');
40 my $changed_this_run = $query->param('changed_files');
41 my $log_archive = $query->param('logtar');
44 "branch=$branch&res=$res&stage=$stage&animal=$animal&".
45 "ts=$ts&log=$log&conf=$conf";
48 "changed_files=$changed_this_run&".
49 "changed_since_success=$changed_since_success&";
51 unless ($animal && $ts && $stage && $sig)
54 "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
55 "bad parameters for request\n";
61 my $db = DBI->connect($dsn,$dbuser,$dbpass);
63 die $DBI::errstr unless $db;
66 "select secret from buildsystems where name = ? and status = 'approved'";
67 my $sth = $db->prepare($gethost);
68 $sth->execute($animal);
69 my ($secret)=$sth->fetchrow_array();
72 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
73 $year += 1900; $mon +=1;
75 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
77 if ($ENV{BF_DEBUG} || ($ts > time) || (! $secret) )
79 open(TX,">../buildlogs/$animal.$date");
80 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
81 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
82 "changed_this_run:\n$changed_this_run\n",
83 "changed_since_success:\n$changed_since_success\n",
91 my $gmt = gmtime($ts);
92 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
93 "Context-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
101 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
102 "System $animal is unknown\n";
111 my $calc_sig = sha1_hex($content,$secret);
112 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
114 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
117 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
118 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
123 # undo escape-proofing of base64 data and decode it
124 map {tr/$@/+=/; $_ = decode_base64($_); }
125 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive);
127 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
128 $year += 1900; $mon +=1;
130 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
134 my $dirname = "../buildlogs/tmp.$$.unpacklogs";
139 my $archname = "../buildlogs/tmp.$$.tgz";
140 open($log_handle,">$archname");
142 print $log_handle $log_archive;
145 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
146 map {s/\s+//g; } @log_file_names;
147 my @qnames = @log_file_names;
148 map { $_ = qq("$_"); } @qnames;
149 $log_file_names = '{' . join(',',@qnames) . '}';
154 my $container = new Safe;
156 unless ($sconf =~ s/.*(\$Script_Config)/$1/ms )
158 $sconf = '$Script_Config={};';
160 my $client_conf = $container->reval("$sconf;");
162 my @config_flags = @{ $client_conf->{config_opts} || [] };
165 @config_flags = grep {! m/=/ } @config_flags;
166 map {s/\s+//g; $_=qq("$_"); } @config_flags;
167 $config_flags = '{' . join(',',@config_flags) . '}' ;
172 insert into build_status
173 (sysname, snapshot,status, stage, log,conf_sum, branch,
174 changed_this_run, changed_since_success,
175 log_archive_filenames , log_archive, build_flags)
176 values(?,?,?,?,?,?,?,?,?,?,?,?)
179 $sth=$db->prepare($logst);
181 $sth->bind_param(1,$animal);
182 $sth->bind_param(2,$dbdate);
183 $sth->bind_param(3,$res);
184 $sth->bind_param(4,$stage);
185 $sth->bind_param(5,$log);
186 $sth->bind_param(6,$conf);
187 $sth->bind_param(7,$branch);
188 $sth->bind_param(8,$changed_this_run);
189 $sth->bind_param(9,$changed_since_success);
190 $sth->bind_param(10,$log_file_names);
191 $sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
192 $sth->bind_param(12,$config_flags);
197 my $logst2 = <<EOSQL;
199 insert into build_status_log
200 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
201 values (?, ?, ?, ?, ?, ?)
206 $sth = $db->prepare($logst2);
210 my $stage_start = $ts;
212 foreach my $log_file( @log_file_names )
215 open($handle,"$dirname/$log_file");
216 my $mtime = (stat $handle)[9];
217 my $stage_interval = $mtime - $stage_start;
218 $stage_start = $mtime;
219 my $ltext = <$handle>;
221 $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
222 "$stage_interval seconds");
228 my $prevst = <<EOSQL;
230 select coalesce((select distinct on (snapshot) stage
232 where sysname = ? and branch = ? and snapshot < ?
233 order by snapshot desc
234 limit 1), 'NEW') as prev_status
238 $sth=$db->prepare($prevst);
239 $sth->execute($animal,$branch,$dbdate);
240 my $row=$sth->fetchrow_arrayref;
241 my $prev_stat=$row->[0];
246 select operating_system|| ' / ' || os_version as os ,
247 compiler || ' / ' || compiler_version as compiler,
250 where status = 'approved'
255 $sth=$db->prepare($det_st);
256 $sth->execute($animal);
257 $row=$sth->fetchrow_arrayref;
258 my ($os, $compiler,$arch) = @$row;
263 print "Content-Type: text/plain\n\n";
264 print "request was on:\n";
265 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
267 my $client_events = $client_conf->{mail_events};
271 open(TX,">>../buildlogs/$animal.$date");
272 print TX "\n",Dumper(\$client_conf),"\n";
278 if (ref $client_events)
280 my $cbcc = $client_events->{all};
283 push @$bcc_stat, @$cbcc;
285 elsif (defined $cbcc)
287 push @$bcc_stat, $cbcc;
291 $cbcc = $client_events->{all};
294 push @$bcc_stat, @$cbcc;
296 elsif (defined $cbcc)
298 push @$bcc_stat, $cbcc;
301 $cbcc = $client_events->{change};
304 push @$bcc_chg, @$cbcc;
306 elsif (defined $cbcc)
308 push @$bcc_chg, $cbcc;
310 if ($stage eq 'OK' || $prev_stat eq 'OK')
312 $cbcc = $client_events->{green};
315 push @$bcc_chg, @$cbcc;
317 elsif (defined $cbcc)
319 push @$bcc_chg, $cbcc;
325 my $url = $query->url(-base => 1);
328 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
330 my $mailto = [@$all_stat];
331 push(@$mailto,@$fail_stat) if $stage ne 'OK';
333 my $me = `id -un`; chomp $me;
335 my $host = `hostname`; chomp $host;
337 my $msg = new Mail::Send;
339 $msg->set('From',"PG Build Farm <$me\@$host>");
342 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
343 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
348 The PGBuildfarm member $animal had the following event on branch $branch:
352 The snapshot timestamp for the build that triggered this notification is: $dbdate
354 The specs of this machine are:
359 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
365 exit if ($stage eq $prev_stat);
367 $mailto = [@$change_stat];
368 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
370 $msg = new Mail::Send;
372 $msg->set('From',"PG Build Farm <$me\@$host>");
375 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
377 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
378 "changed from OK to $stage";
379 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
380 $stat_type .= " failure" if $stage ne 'OK';
382 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
386 The PGBuildfarm member $animal had the following event on branch $branch:
390 The snapshot timestamp for the build that triggered this notification is: $dbdate
392 The specs of this machine are:
397 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch