5 use vars qw($dbhost $dbname $dbuser $dbpass $dbport
6 $all_stat $fail_stat $change_stat $green_stat
10 # force this before we do anything - even load modules
11 BEGIN { $server_time = time; }
14 use Digest::SHA1 qw(sha1_hex);
22 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
24 die "no dbname" unless $dbname;
25 die "no dbuser" unless $dbuser;
27 my $dsn="dbi:Pg:dbname=$dbname";
28 $dsn .= ";host=$dbhost" if $dbhost;
29 $dsn .= ";port=$dbport" if $dbport;
33 my $sig = $query->path_info;
36 my $stage = $query->param('stage');
37 my $ts = $query->param('ts');
38 my $animal = $query->param('animal');
39 my $log = $query->param('log');
40 my $res = $query->param('res');
41 my $conf = $query->param('conf');
42 my $branch = $query->param('branch');
43 my $changed_since_success = $query->param('changed_since_success');
44 my $changed_this_run = $query->param('changed_files');
45 my $log_archive = $query->param('logtar');
48 "branch=$branch&res=$res&stage=$stage&animal=$animal&".
49 "ts=$ts&log=$log&conf=$conf";
52 "changed_files=$changed_this_run&".
53 "changed_since_success=$changed_since_success&";
55 unless ($animal && $ts && $stage && $sig)
58 "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
59 "bad parameters for request\n";
65 my $db = DBI->connect($dsn,$dbuser,$dbpass);
67 die $DBI::errstr unless $db;
70 "select secret from buildsystems where name = ? and status = 'approved'";
71 my $sth = $db->prepare($gethost);
72 $sth->execute($animal);
73 my ($secret)=$sth->fetchrow_array();
76 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
77 $year += 1900; $mon +=1;
79 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
81 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
83 open(TX,">../buildlogs/$animal.$date");
84 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
85 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
86 "changed_this_run:\n$changed_this_run\n",
87 "changed_since_success:\n$changed_since_success\n",
95 my $gmt = gmtime($ts);
96 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
97 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
102 unless ($ts + 86400 > time)
104 my $gmt = gmtime($ts);
105 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
106 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
114 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
115 "System $animal is unknown\n";
124 my $calc_sig = sha1_hex($content,$secret);
125 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
127 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
130 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
131 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
136 # undo escape-proofing of base64 data and decode it
137 map {tr/$@/+=/; $_ = decode_base64($_); }
138 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive);
140 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
141 $year += 1900; $mon +=1;
143 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
147 my $dirname = "../buildlogs/tmp.$$.unpacklogs";
152 my $archname = "../buildlogs/tmp.$$.tgz";
153 open($log_handle,">$archname");
155 print $log_handle $log_archive;
158 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
159 map {s/\s+//g; } @log_file_names;
160 my @qnames = @log_file_names;
161 map { $_ = qq("$_"); } @qnames;
162 $log_file_names = '{' . join(',',@qnames) . '}';
167 my $container = new Safe;
169 unless ($sconf =~ s/.*(\$Script_Config)/$1/ms )
171 $sconf = '$Script_Config={};';
173 my $client_conf = $container->reval("$sconf;");
176 if (not exists $client_conf->{config_opts} )
180 elsif (ref $client_conf->{config_opts} eq 'HASH')
182 # leave out keys with false values
183 @config_flags = grep { $client_conf->{config_opts}->{$_} }
184 keys %{$client_conf->{config_opts}};
186 elsif (ref $client_conf->{config_opts} eq 'ARRAY' )
188 @config_flags = @{$client_conf->{config_opts}};
193 @config_flags = grep {! m/=/ } @config_flags;
194 map {s/\s+//g; $_=qq("$_"); } @config_flags;
195 $config_flags = '{' . join(',',@config_flags) . '}' ;
200 insert into build_status
201 (sysname, snapshot,status, stage, log,conf_sum, branch,
202 changed_this_run, changed_since_success,
203 log_archive_filenames , log_archive, build_flags)
204 values(?,?,?,?,?,?,?,?,?,?,?,?)
207 $sth=$db->prepare($logst);
209 $sth->bind_param(1,$animal);
210 $sth->bind_param(2,$dbdate);
211 $sth->bind_param(3,$res);
212 $sth->bind_param(4,$stage);
213 $sth->bind_param(5,$log);
214 $sth->bind_param(6,$conf);
215 $sth->bind_param(7,$branch);
216 $sth->bind_param(8,$changed_this_run);
217 $sth->bind_param(9,$changed_since_success);
218 $sth->bind_param(10,$log_file_names);
219 $sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
220 $sth->bind_param(12,$config_flags);
225 my $logst2 = <<EOSQL;
227 insert into build_status_log
228 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
229 values (?, ?, ?, ?, ?, ?)
234 $sth = $db->prepare($logst2);
238 my $stage_start = $ts;
240 foreach my $log_file( @log_file_names )
243 open($handle,"$dirname/$log_file");
244 my $mtime = (stat $handle)[9];
245 my $stage_interval = $mtime - $stage_start;
246 $stage_start = $mtime;
247 my $ltext = <$handle>;
249 $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
250 "$stage_interval seconds");
256 my $prevst = <<EOSQL;
258 select coalesce((select distinct on (snapshot) stage
260 where sysname = ? and branch = ? and snapshot < ?
261 order by snapshot desc
262 limit 1), 'NEW') as prev_status
266 $sth=$db->prepare($prevst);
267 $sth->execute($animal,$branch,$dbdate);
268 my $row=$sth->fetchrow_arrayref;
269 my $prev_stat=$row->[0];
274 select operating_system|| ' / ' || os_version as os ,
275 compiler || ' / ' || compiler_version as compiler,
278 where status = 'approved'
283 $sth=$db->prepare($det_st);
284 $sth->execute($animal);
285 $row=$sth->fetchrow_arrayref;
286 my ($os, $compiler,$arch) = @$row;
291 print "Content-Type: text/plain\n\n";
292 print "request was on:\n";
293 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
295 my $client_events = $client_conf->{mail_events};
299 my $client_time = $client_conf->{current_ts};
300 open(TX,">>../buildlogs/$animal.$date");
301 print TX "\n",Dumper(\$client_conf),"\n";
302 print TX "server time: $server_time, client time: $client_time\n" if $client_time;
308 if (ref $client_events)
310 my $cbcc = $client_events->{all};
313 push @$bcc_stat, @$cbcc;
315 elsif (defined $cbcc)
317 push @$bcc_stat, $cbcc;
321 $cbcc = $client_events->{all};
324 push @$bcc_stat, @$cbcc;
326 elsif (defined $cbcc)
328 push @$bcc_stat, $cbcc;
331 $cbcc = $client_events->{change};
334 push @$bcc_chg, @$cbcc;
336 elsif (defined $cbcc)
338 push @$bcc_chg, $cbcc;
340 if ($stage eq 'OK' || $prev_stat eq 'OK')
342 $cbcc = $client_events->{green};
345 push @$bcc_chg, @$cbcc;
347 elsif (defined $cbcc)
349 push @$bcc_chg, $cbcc;
355 my $url = $query->url(-base => 1);
358 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
360 my $mailto = [@$all_stat];
361 push(@$mailto,@$fail_stat) if $stage ne 'OK';
363 my $me = `id -un`; chomp $me;
365 my $host = `hostname`; chomp $host;
367 my $msg = new Mail::Send;
369 $msg->set('From',"PG Build Farm <$me\@$host>");
372 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
373 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
378 The PGBuildfarm member $animal had the following event on branch $branch:
382 The snapshot timestamp for the build that triggered this notification is: $dbdate
384 The specs of this machine are:
389 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
395 exit if ($stage eq $prev_stat);
397 $mailto = [@$change_stat];
398 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
400 $msg = new Mail::Send;
402 $msg->set('From',"PG Build Farm <$me\@$host>");
405 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
407 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
408 "changed from OK to $stage";
409 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
410 $stat_type .= " failure" if $stage ne 'OK';
412 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
416 The PGBuildfarm member $animal had the following event on branch $branch:
420 The snapshot timestamp for the build that triggered this notification is: $dbdate
422 The specs of this machine are:
427 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch