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 $tsdiff = time - $ts;
79 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
80 $year += 1900; $mon +=1;
82 sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
84 if ($ENV{BF_DEBUG} || ($ts > time) || ($ts + 86400 < time ) || (! $secret) )
86 open(TX,">../buildlogs/$animal.$date");
87 print TX "sig=$sig\nlogtar-len=" , length($log_archive),
88 "\nstatus=$res\nstage=$stage\nconf:\n$conf\n",
90 "changed_this_run:\n$changed_this_run\n",
91 "changed_since_success:\n$changed_since_success\n",
97 unless ($ts < time + 120)
99 my $gmt = gmtime($ts);
100 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is in the future.\n",
101 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is in the future\n";
106 unless ($ts + 86400 > time)
108 my $gmt = gmtime($ts);
109 print "Status: 491 bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n",
110 "Content-Type: text/plain\n\n bad ts parameter - $ts ($gmt GMT) is more than 24 hours ago.\n";
118 "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
119 "System $animal is unknown\n";
128 my $calc_sig = sha1_hex($content,$secret);
129 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
131 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
134 print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
135 print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
140 # undo escape-proofing of base64 data and decode it
141 map {tr/$@/+=/; $_ = decode_base64($_); }
142 ($log, $conf,$changed_this_run,$changed_since_success,$log_archive);
144 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
145 $year += 1900; $mon +=1;
147 sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
151 my $dirname = "../buildlogs/tmp.$$.unpacklogs";
156 my $archname = "../buildlogs/tmp.$$.tgz";
157 open($log_handle,">$archname");
159 print $log_handle $log_archive;
162 @log_file_names = `tar -z -C $dirname -xvf $archname 2>/dev/null`;
163 map {s/\s+//g; } @log_file_names;
164 my @qnames = @log_file_names;
165 map { $_ = qq("$_"); } @qnames;
166 $log_file_names = '{' . join(',',@qnames) . '}';
171 my $container = new Safe;
173 unless ($sconf =~ s/.*(\$Script_Config)/$1/ms )
175 $sconf = '$Script_Config={};';
177 my $client_conf = $container->reval("$sconf;");
179 if ($min_script_version)
181 $client_conf->{script_version} ||= '0.0';
182 my ($minmajor,$minminor) = split(/\./,$min_script_version);
183 my ($smajor,$sminor) = split(/\./,$client_conf->{script_version});
184 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
186 print "Status: 460 script version too low\nContent-Type: text/plain\n\n";
188 "Script version is below minimum required\n",
189 "Reported version: $client_conf->{script_version},",
190 "Minumum version required: $min_script_version\n";
196 if ($min_web_script_version)
198 $client_conf->{web_script_version} ||= '0.0';
199 my ($minmajor,$minminor) = split(/\./,$min_script_version);
200 my ($smajor,$sminor) = split(/\./,$client_conf->{script_version});
201 if ($minmajor > $smajor || ($minmajor == $smajor && $minminor > $sminor))
203 print "Status: 461 web script version too low\nContent-Type: text/plain\n\n";
205 "Web Script version is below minimum required\n",
206 "Reported version: $client_conf->{web_script_version},",
207 "Minumum version required: $min_web_script_version\n";
214 if (not exists $client_conf->{config_opts} )
218 elsif (ref $client_conf->{config_opts} eq 'HASH')
220 # leave out keys with false values
221 @config_flags = grep { $client_conf->{config_opts}->{$_} }
222 keys %{$client_conf->{config_opts}};
224 elsif (ref $client_conf->{config_opts} eq 'ARRAY' )
226 @config_flags = @{$client_conf->{config_opts}};
231 @config_flags = grep {! m/=/ } @config_flags;
232 map {s/\s+//g; $_=qq("$_"); } @config_flags;
233 push @config_flags,'git' if $client_conf->{scm} eq 'git';
234 $config_flags = '{' . join(',',@config_flags) . '}' ;
237 my $scm = $client_conf->{scm} || 'cvs';
238 my $scmurl = $client_conf->{scm_url};
241 insert into build_status
242 (sysname, snapshot,status, stage, log,conf_sum, branch,
243 changed_this_run, changed_since_success,
244 log_archive_filenames , log_archive, build_flags, scm, scmurl)
245 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
248 $sth=$db->prepare($logst);
250 $sth->bind_param(1,$animal);
251 $sth->bind_param(2,$dbdate);
252 $sth->bind_param(3,$res);
253 $sth->bind_param(4,$stage);
254 $sth->bind_param(5,$log);
255 $sth->bind_param(6,$conf);
256 $sth->bind_param(7,$branch);
257 $sth->bind_param(8,$changed_this_run);
258 $sth->bind_param(9,$changed_since_success);
259 $sth->bind_param(10,$log_file_names);
260 #$sth->bind_param(11,$log_archive,{ pg_type => DBD::Pg::PG_BYTEA });
261 $sth->bind_param(11,undef,{ pg_type => DBD::Pg::PG_BYTEA });
262 $sth->bind_param(12,$config_flags);
263 $sth->bind_param(13,$scm);
264 $sth->bind_param(14,$scmurl);
269 my $logst2 = <<EOSQL;
271 insert into build_status_log
272 (sysname, snapshot, branch, log_stage, log_text, stage_duration)
273 values (?, ?, ?, ?, ?, ?)
278 $sth = $db->prepare($logst2);
282 my $stage_start = $ts;
284 foreach my $log_file( @log_file_names )
287 open($handle,"$dirname/$log_file");
288 my $mtime = (stat $handle)[9];
289 my $stage_interval = $mtime - $stage_start;
290 $stage_start = $mtime;
291 my $ltext = <$handle>;
293 $ltext =~ s/\x00/\\0/g;
294 $sth->execute($animal,$dbdate,$branch,$log_file,$ltext,
295 "$stage_interval seconds");
301 my $prevst = <<EOSQL;
303 select coalesce((select distinct on (snapshot) stage
305 where sysname = ? and branch = ? and snapshot < ?
306 order by snapshot desc
307 limit 1), 'NEW') as prev_status
311 $sth=$db->prepare($prevst);
312 $sth->execute($animal,$branch,$dbdate);
313 my $row=$sth->fetchrow_arrayref;
314 my $prev_stat=$row->[0];
319 select operating_system|| ' / ' || os_version as os ,
320 compiler || ' / ' || compiler_version as compiler,
323 where status = 'approved'
328 $sth=$db->prepare($det_st);
329 $sth->execute($animal);
330 $row=$sth->fetchrow_arrayref;
331 my ($os, $compiler,$arch) = @$row;
335 my $have_status = $db->selectrow_arrayref( q{
337 from build_status_latest
338 where sysname = ? and branch = ?
340 undef, $animal, $branch);
344 update build_status_latest
345 set latest_snapshot = ?
346 where sysname = ? and branch = ?
348 undef, $dbdate, $animal, $branch);
353 insert into build_status_latest
354 (sysname, branch, latest_snapshot)
357 undef, $animal, $branch, $dbdate);
362 $db->do("delete from dashboard_mat");
363 $db->do("insert into dashboard_mat select * from dashboard_mat_data2");
368 print "Content-Type: text/plain\n\n";
369 print "request was on:\n";
370 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
372 my $client_events = $client_conf->{mail_events};
376 my $client_time = $client_conf->{current_ts};
377 open(TX,">>../buildlogs/$animal.$date");
378 print TX "\n",Dumper(\$client_conf),"\n";
379 print TX "server time: $server_time, client time: $client_time\n" if $client_time;
385 if (ref $client_events)
387 my $cbcc = $client_events->{all};
390 push @$bcc_stat, @$cbcc;
392 elsif (defined $cbcc)
394 push @$bcc_stat, $cbcc;
398 $cbcc = $client_events->{all};
401 push @$bcc_stat, @$cbcc;
403 elsif (defined $cbcc)
405 push @$bcc_stat, $cbcc;
408 $cbcc = $client_events->{change};
411 push @$bcc_chg, @$cbcc;
413 elsif (defined $cbcc)
415 push @$bcc_chg, $cbcc;
417 if ($stage eq 'OK' || $prev_stat eq 'OK')
419 $cbcc = $client_events->{green};
422 push @$bcc_chg, @$cbcc;
424 elsif (defined $cbcc)
426 push @$bcc_chg, $cbcc;
432 my $url = $query->url(-base => 1);
435 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
437 my $mailto = [@$all_stat];
438 push(@$mailto,@$fail_stat) if $stage ne 'OK';
440 my $me = `id -un`; chomp $me;
442 my $host = `hostname`; chomp $host;
444 my $msg = new Mail::Send;
446 $msg->set('From',"PG Build Farm <$me\@$host>");
449 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
450 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
455 The PGBuildfarm member $animal had the following event on branch $branch:
459 The snapshot timestamp for the build that triggered this notification is: $dbdate
461 The specs of this machine are:
466 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
472 exit if ($stage eq $prev_stat);
474 $mailto = [@$change_stat];
475 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
477 $msg = new Mail::Send;
479 $msg->set('From',"PG Build Farm <$me\@$host>");
482 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
484 $stat_type = $prev_stat ne 'OK' ? "changed from $prev_stat failure to $stage" :
485 "changed from OK to $stage";
486 $stat_type = "New member: $stage" if $prev_stat eq 'NEW';
487 $stat_type .= " failure" if $stage ne 'OK';
489 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
493 The PGBuildfarm member $animal had the following event on branch $branch:
497 The snapshot timestamp for the build that triggered this notification is: $dbdate
499 The specs of this machine are:
504 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch