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) || ($ts + 86400 < 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 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
98 unless ($ts + 86400 > time)
100 my $gmt = gmtime($ts);
101 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
102 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
110 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
111 "System $animal is unknown\n";
120 my $calc_sig = sha1_hex($content,$secret);
121 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
123 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
126 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
127 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
132 # undo escape-proofing of base64 data and decode it
133 map {tr/$@/+=/; $_ = decode_base64($_); }
134 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive);
136 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
137 $year += 1900; $mon +=1;
139 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
143 my $dirname = "../buildlogs/tmp.$$.unpacklogs";
148 my $archname = "../buildlogs/tmp.$$.tgz";
149 open($log_handle,">$archname");
151 print $log_handle $log_archive;
154 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
155 map {s/\s+//g; } @log_file_names;
156 my @qnames = @log_file_names;
157 map { $_ = qq("$_"); } @qnames;
158 $log_file_names = '{' . join(',',@qnames) . '}';
163 my $container = new Safe;
165 unless ($sconf =~ s/.*(\$Script_Config)/$1/ms )
167 $sconf = '$Script_Config={};';
169 my $client_conf = $container->reval("$sconf;");
171 my @config_flags = @{ $client_conf->{config_opts} || [] };
174 @config_flags = grep {! m/=/ } @config_flags;
175 map {s/\s+//g; $_=qq("$_"); } @config_flags;
176 $config_flags = '{' . join(',',@config_flags) . '}' ;
181 insert into build_status
182 (sysname, snapshot,status, stage, log,conf_sum, branch,
183 changed_this_run, changed_since_success,
184 log_archive_filenames , log_archive, build_flags)
185 values(?,?,?,?,?,?,?,?,?,?,?,?)
188 $sth=$db->prepare($logst);
190 $sth->bind_param(1,$animal);
191 $sth->bind_param(2,$dbdate);
192 $sth->bind_param(3,$res);
193 $sth->bind_param(4,$stage);
194 $sth->bind_param(5,$log);
195 $sth->bind_param(6,$conf);
196 $sth->bind_param(7,$branch);
197 $sth->bind_param(8,$changed_this_run);
198 $sth->bind_param(9,$changed_since_success);
199 $sth->bind_param(10,$log_file_names);
200 $sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
201 $sth->bind_param(12,$config_flags);
206 my $logst2 = <<EOSQL;
208 insert into build_status_log
209 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
210 values (?, ?, ?, ?, ?, ?)
215 $sth = $db->prepare($logst2);
219 my $stage_start = $ts;
221 foreach my $log_file( @log_file_names )
224 open($handle,"$dirname/$log_file");
225 my $mtime = (stat $handle)[9];
226 my $stage_interval = $mtime - $stage_start;
227 $stage_start = $mtime;
228 my $ltext = <$handle>;
230 $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
231 "$stage_interval seconds");
237 my $prevst = <<EOSQL;
239 select coalesce((select distinct on (snapshot) stage
241 where sysname = ? and branch = ? and snapshot < ?
242 order by snapshot desc
243 limit 1), 'NEW') as prev_status
247 $sth=$db->prepare($prevst);
248 $sth->execute($animal,$branch,$dbdate);
249 my $row=$sth->fetchrow_arrayref;
250 my $prev_stat=$row->[0];
255 select operating_system|| ' / ' || os_version as os ,
256 compiler || ' / ' || compiler_version as compiler,
259 where status = 'approved'
264 $sth=$db->prepare($det_st);
265 $sth->execute($animal);
266 $row=$sth->fetchrow_arrayref;
267 my ($os, $compiler,$arch) = @$row;
272 print "Content-Type: text/plain\n\n";
273 print "request was on:\n";
274 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
276 my $client_events = $client_conf->{mail_events};
280 open(TX,">>../buildlogs/$animal.$date");
281 print TX "\n",Dumper(\$client_conf),"\n";
287 if (ref $client_events)
289 my $cbcc = $client_events->{all};
292 push @$bcc_stat, @$cbcc;
294 elsif (defined $cbcc)
296 push @$bcc_stat, $cbcc;
300 $cbcc = $client_events->{all};
303 push @$bcc_stat, @$cbcc;
305 elsif (defined $cbcc)
307 push @$bcc_stat, $cbcc;
310 $cbcc = $client_events->{change};
313 push @$bcc_chg, @$cbcc;
315 elsif (defined $cbcc)
317 push @$bcc_chg, $cbcc;
319 if ($stage eq 'OK' || $prev_stat eq 'OK')
321 $cbcc = $client_events->{green};
324 push @$bcc_chg, @$cbcc;
326 elsif (defined $cbcc)
328 push @$bcc_chg, $cbcc;
334 my $url = $query->url(-base => 1);
337 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
339 my $mailto = [@$all_stat];
340 push(@$mailto,@$fail_stat) if $stage ne 'OK';
342 my $me = `id -un`; chomp $me;
344 my $host = `hostname`; chomp $host;
346 my $msg = new Mail::Send;
348 $msg->set('From',"PG Build Farm <$me\@$host>");
351 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
352 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
357 The PGBuildfarm member $animal had the following event on branch $branch:
361 The snapshot timestamp for the build that triggered this notification is: $dbdate
363 The specs of this machine are:
368 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
374 exit if ($stage eq $prev_stat);
376 $mailto = [@$change_stat];
377 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
379 $msg = new Mail::Send;
381 $msg->set('From',"PG Build Farm <$me\@$host>");
384 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
386 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
387 "changed from OK to $stage";
388 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
389 $stat_type .= " failure" if $stage ne 'OK';
391 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
395 The PGBuildfarm member $animal had the following event on branch $branch:
399 The snapshot timestamp for the build that triggered this notification is: $dbdate
401 The specs of this machine are:
406 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch