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";
66 my $db = DBI->connect($dsn,$dbuser,$dbpass);
68 die $DBI::errstr unless $db;
71 "select secret from buildsystems where name = ? and status = 'approved'";
72 my $sth = $db->prepare($gethost);
73 $sth->execute($animal);
74 my ($secret)=$sth->fetchrow_array();
77 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
78 $year += 1900; $mon +=1;
80 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
82 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
84 open(TX,">../buildlogs/$animal.$date");
85 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
86 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
87 "changed_this_run:\n$changed_this_run\n",
88 "changed_since_success:\n$changed_since_success\n",
96 my $gmt = gmtime($ts);
97 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
98 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
103 unless ($ts + 86400 > time)
105 my $gmt = gmtime($ts);
106 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
107 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
115 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
116 "System $animal is unknown\n";
125 my $calc_sig = sha1_hex($content,$secret);
126 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
128 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
131 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
132 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
137 # undo escape-proofing of base64 data and decode it
138 map {tr/$@/+=/; $_ = decode_base64($_); }
139 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive);
141 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
142 $year += 1900; $mon +=1;
144 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
148 my $dirname = "../buildlogs/tmp.$$.unpacklogs";
153 my $archname = "../buildlogs/tmp.$$.tgz";
154 open($log_handle,">$archname");
156 print $log_handle $log_archive;
159 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
160 map {s/\s+//g; } @log_file_names;
161 my @qnames = @log_file_names;
162 map { $_ = qq("$_"); } @qnames;
163 $log_file_names = '{' . join(',',@qnames) . '}';
168 my $container = new Safe;
170 unless ($sconf =~ s/.*(\$Script_Config)/$1/ms )
172 $sconf = '$Script_Config={};';
174 my $client_conf = $container->reval("$sconf;");
176 if ($min_script_version)
178 $client_conf->{script_version} ||= '0.0';
179 my ($minmajor,$minminor) = split(/\./,$min_script_version);
180 my ($smajor,$sminor) = split(/\./,$client_conf->{script_version});
181 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
183 print "Status: 460 script version too low\nContent-Type: text/plain\n\n";
185 "Script version is below minimum required\n",
186 "Reported version: $client_conf->{script_version},",
187 "Minumum version required: $min_script_version\n";
193 if ($min_web_script_version)
195 $client_conf->{web_script_version} ||= '0.0';
196 my ($minmajor,$minminor) = split(/\./,$min_script_version);
197 my ($smajor,$sminor) = split(/\./,$client_conf->{script_version});
198 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
200 print "Status: 461 web script version too low\nContent-Type: text/plain\n\n";
202 "Web Script version is below minimum required\n",
203 "Reported version: $client_conf->{web_script_version},",
204 "Minumum version required: $min_web_script_version\n";
211 if (not exists $client_conf->{config_opts} )
215 elsif (ref $client_conf->{config_opts} eq 'HASH')
217 # leave out keys with false values
218 @config_flags = grep { $client_conf->{config_opts}->{$_} }
219 keys %{$client_conf->{config_opts}};
221 elsif (ref $client_conf->{config_opts} eq 'ARRAY' )
223 @config_flags = @{$client_conf->{config_opts}};
228 @config_flags = grep {! m/=/ } @config_flags;
229 map {s/\s+//g; $_=qq("$_"); } @config_flags;
230 $config_flags = '{' . join(',',@config_flags) . '}' ;
235 insert into build_status
236 (sysname, snapshot,status, stage, log,conf_sum, branch,
237 changed_this_run, changed_since_success,
238 log_archive_filenames , log_archive, build_flags)
239 values(?,?,?,?,?,?,?,?,?,?,?,?)
242 $sth=$db->prepare($logst);
244 $sth->bind_param(1,$animal);
245 $sth->bind_param(2,$dbdate);
246 $sth->bind_param(3,$res);
247 $sth->bind_param(4,$stage);
248 $sth->bind_param(5,$log);
249 $sth->bind_param(6,$conf);
250 $sth->bind_param(7,$branch);
251 $sth->bind_param(8,$changed_this_run);
252 $sth->bind_param(9,$changed_since_success);
253 $sth->bind_param(10,$log_file_names);
254 $sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
255 $sth->bind_param(12,$config_flags);
260 my $logst2 = <<EOSQL;
262 insert into build_status_log
263 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
264 values (?, ?, ?, ?, ?, ?)
269 $sth = $db->prepare($logst2);
273 my $stage_start = $ts;
275 foreach my $log_file( @log_file_names )
278 open($handle,"$dirname/$log_file");
279 my $mtime = (stat $handle)[9];
280 my $stage_interval = $mtime - $stage_start;
281 $stage_start = $mtime;
282 my $ltext = <$handle>;
284 $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
285 "$stage_interval seconds");
291 my $prevst = <<EOSQL;
293 select coalesce((select distinct on (snapshot) stage
295 where sysname = ? and branch = ? and snapshot < ?
296 order by snapshot desc
297 limit 1), 'NEW') as prev_status
301 $sth=$db->prepare($prevst);
302 $sth->execute($animal,$branch,$dbdate);
303 my $row=$sth->fetchrow_arrayref;
304 my $prev_stat=$row->[0];
309 select operating_system|| ' / ' || os_version as os ,
310 compiler || ' / ' || compiler_version as compiler,
313 where status = 'approved'
318 $sth=$db->prepare($det_st);
319 $sth->execute($animal);
320 $row=$sth->fetchrow_arrayref;
321 my ($os, $compiler,$arch) = @$row;
326 print "Content-Type: text/plain\n\n";
327 print "request was on:\n";
328 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
330 my $client_events = $client_conf->{mail_events};
334 my $client_time = $client_conf->{current_ts};
335 open(TX,">>../buildlogs/$animal.$date");
336 print TX "\n",Dumper(\$client_conf),"\n";
337 print TX "server time: $server_time, client time: $client_time\n" if $client_time;
343 if (ref $client_events)
345 my $cbcc = $client_events->{all};
348 push @$bcc_stat, @$cbcc;
350 elsif (defined $cbcc)
352 push @$bcc_stat, $cbcc;
356 $cbcc = $client_events->{all};
359 push @$bcc_stat, @$cbcc;
361 elsif (defined $cbcc)
363 push @$bcc_stat, $cbcc;
366 $cbcc = $client_events->{change};
369 push @$bcc_chg, @$cbcc;
371 elsif (defined $cbcc)
373 push @$bcc_chg, $cbcc;
375 if ($stage eq 'OK' || $prev_stat eq 'OK')
377 $cbcc = $client_events->{green};
380 push @$bcc_chg, @$cbcc;
382 elsif (defined $cbcc)
384 push @$bcc_chg, $cbcc;
390 my $url = $query->url(-base => 1);
393 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
395 my $mailto = [@$all_stat];
396 push(@$mailto,@$fail_stat) if $stage ne 'OK';
398 my $me = `id -un`; chomp $me;
400 my $host = `hostname`; chomp $host;
402 my $msg = new Mail::Send;
404 $msg->set('From',"PG Build Farm <$me\@$host>");
407 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
408 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
413 The PGBuildfarm member $animal had the following event on branch $branch:
417 The snapshot timestamp for the build that triggered this notification is: $dbdate
419 The specs of this machine are:
424 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
430 exit if ($stage eq $prev_stat);
432 $mailto = [@$change_stat];
433 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
435 $msg = new Mail::Send;
437 $msg->set('From',"PG Build Farm <$me\@$host>");
440 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
442 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
443 "changed from OK to $stage";
444 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
445 $stat_type .= " failure" if $stage ne 'OK';
447 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
451 The PGBuildfarm member $animal had the following event on branch $branch:
455 The snapshot timestamp for the build that triggered this notification is: $dbdate
457 The specs of this machine are:
462 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch