Initial commit
[buildfarm-server.git] / cgi-bin / pgstatus.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 use CGI;
6 use Digest::SHA1  qw(sha1_hex);
7 use MIME::Base64;
8 use DBI;
9 use DBD::Pg;
10 use Data::Dumper;
11 use Mail::Send;
12 use Safe;
13
14 use vars qw($dbhost $dbname $dbuser $dbpass $dbport
15        $all_stat $fail_stat $change_stat $green_stat
16 );
17
18 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
19
20 die "no dbname" unless $dbname;
21 die "no dbuser" unless $dbuser;
22
23 my $dsn="dbi:Pg:dbname=$dbname";
24 $dsn .= ";host=$dbhost" if $dbhost;
25 $dsn .= ";port=$dbport" if $dbport;
26
27 my $query = new CGI;
28
29 my $sig = $query->path_info;
30 $sig =~ s!^/!!;
31
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');
42
43 my $content = 
44         "branch=$branch&res=$res&stage=$stage&animal=$animal&".
45         "ts=$ts&log=$log&conf=$conf";
46
47 my $extra_content = 
48         "changed_files=$changed_this_run&".
49         "changed_since_success=$changed_since_success&";
50
51 unless ($animal && $ts && $stage && $sig)
52 {
53         print 
54             "Status: 490 bad parameters\nContent-Type: text/plain\n\n",
55             "bad parameters for request\n";
56         exit;
57         
58 }
59
60
61 my $db = DBI->connect($dsn,$dbuser,$dbpass);
62
63 die $DBI::errstr unless $db;
64
65 my $gethost=
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();
70 $sth->finish;
71
72 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
73 $year += 1900; $mon +=1;
74 my $date=
75     sprintf("%d-%.2d-%.2d_%.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
76
77 if ($ENV{BF_DEBUG} || ($ts > time) || (! $secret) )
78 {
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",
84         "log:\n",$log;
85 #    $query->save(\*TX);
86     close(TX);
87 }
88
89 unless ($ts < time)
90 {
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";
94         $db->disconnect;
95     exit;
96 }
97
98 unless ($secret)
99 {
100         print 
101             "Status: 495 Unknown System\nContent-Type: text/plain\n\n",
102             "System $animal is unknown\n";
103         $db->disconnect;
104         exit;
105         
106 }
107
108
109
110
111 my $calc_sig = sha1_hex($content,$secret);
112 my $calc_sig2 = sha1_hex($extra_content,$content,$secret);
113
114 if ($calc_sig ne $sig && $calc_sig2 ne $sig)
115 {
116
117         print "Status: 450 sig mismatch\nContent-Type: text/plain\n\n";
118         print "$sig mismatches $calc_sig($calc_sig2) on content:\n$content";
119         $db->disconnect;
120         exit;
121 }
122
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);
126
127 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ts);
128 $year += 1900; $mon +=1;
129 my $dbdate=
130     sprintf("%d-%.2d-%.2d %.2d:%.2d:%.2d",$year,$mon,$mday,$hour,$min,$sec);
131
132 my $log_file_names;
133 my @log_file_names;
134 my $dirname = "../buildlogs/tmp.$$.unpacklogs";
135
136 if ($log_archive)
137 {
138     my $log_handle;
139     my $archname = "../buildlogs/tmp.$$.tgz";
140     open($log_handle,">$archname");
141     binmode $log_handle;
142     print $log_handle $log_archive;
143     close $log_handle;
144     mkdir $dirname;
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) . '}';
150     # unlink $archname;
151 }
152
153 my $config_flags;
154 my $container = new Safe;
155 my $sconf = $conf; 
156 unless ($sconf =~ s/.*(\$Script_Config)/$1/ms )
157 {
158     $sconf = '$Script_Config={};';
159 }
160 my $client_conf = $container->reval("$sconf;");
161
162 my @config_flags = @{ $client_conf->{config_opts} || [] };
163 if (@config_flags)
164 {
165     @config_flags = grep {! m/=/ } @config_flags;
166     map {s/\s+//g; $_=qq("$_"); } @config_flags;
167     $config_flags = '{' . join(',',@config_flags) . '}' ;
168 }
169
170
171 my $logst = <<EOSQL;
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(?,?,?,?,?,?,?,?,?,?,?,?)
177 EOSQL
178 ;
179 $sth=$db->prepare($logst);
180
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);
193
194 $sth->execute;
195 $sth->finish;
196
197 my $logst2 = <<EOSQL;
198
199   insert into build_status_log 
200     (sysname, snapshot, branch, log_stage, log_text, stage_duration)
201     values (?, ?, ?, ?, ?, ?)
202
203 EOSQL
204     ;
205
206 $sth = $db->prepare($logst2);
207
208 $/=undef;
209
210 my $stage_start = $ts;
211
212 foreach my $log_file( @log_file_names )
213 {
214   my $handle;
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>;
220   close($handle);
221   $sth->execute($animal,$dbdate,$branch,$log_file,$ltext, 
222                 "$stage_interval seconds");
223 }
224
225
226 $sth->finish;
227
228 my $prevst = <<EOSQL;
229
230   select coalesce((select distinct on (snapshot) stage
231                   from build_status
232                   where sysname = ? and branch = ? and snapshot < ?
233                   order by snapshot desc
234                   limit 1), 'NEW') as prev_status
235   
236 EOSQL
237
238 $sth=$db->prepare($prevst);
239 $sth->execute($animal,$branch,$dbdate);
240 my $row=$sth->fetchrow_arrayref;
241 my $prev_stat=$row->[0];
242 $sth->finish;
243
244 my $det_st = <<EOS;
245
246           select operating_system|| ' / ' || os_version as os , 
247                  compiler || ' / ' || compiler_version as compiler, 
248                  architecture as arch
249           from buildsystems 
250           where status = 'approved'
251                 and name = ?
252
253 EOS
254 ;
255 $sth=$db->prepare($det_st);
256 $sth->execute($animal);
257 $row=$sth->fetchrow_arrayref;
258 my ($os, $compiler,$arch) = @$row;
259 $sth->finish;
260
261 $db->disconnect;
262
263 print "Content-Type: text/plain\n\n";
264 print "request was on:\n";
265 print "res=$res&stage=$stage&animal=$animal&ts=$ts";
266
267 my $client_events = $client_conf->{mail_events};
268
269 if ($ENV{BF_DEBUG})
270 {
271     open(TX,">>../buildlogs/$animal.$date");
272     print TX "\n",Dumper(\$client_conf),"\n";
273     close(TX);
274 }
275
276 my $bcc_stat = [];
277 my $bcc_chg=[];
278 if (ref $client_events)
279 {
280     my $cbcc = $client_events->{all};
281     if (ref $cbcc)
282     {
283         push @$bcc_stat, @$cbcc;
284     }
285     elsif (defined $cbcc)
286     {
287         push @$bcc_stat, $cbcc;
288     }
289     if ($stage ne 'OK')
290     {
291         $cbcc = $client_events->{all};
292         if (ref $cbcc)
293         {
294             push @$bcc_stat, @$cbcc;
295         }
296         elsif (defined $cbcc)
297         {
298             push @$bcc_stat, $cbcc;
299         }
300     }
301     $cbcc = $client_events->{change};
302     if (ref $cbcc)
303     {
304         push @$bcc_chg, @$cbcc;
305     }
306     elsif (defined $cbcc)
307     {
308         push @$bcc_chg, $cbcc;
309     }
310     if ($stage eq 'OK' || $prev_stat eq 'OK')
311     {
312         $cbcc = $client_events->{green};
313         if (ref $cbcc)
314         {
315             push @$bcc_chg, @$cbcc;
316         }
317         elsif (defined $cbcc)
318         {
319             push @$bcc_chg, $cbcc;
320         }
321     }
322 }
323
324
325 my $url = $query->url(-base => 1);
326
327
328 my $stat_type = $stage eq 'OK' ? 'Status' : 'Failed at Stage';
329
330 my $mailto = [@$all_stat];
331 push(@$mailto,@$fail_stat) if $stage ne 'OK';
332
333 my $me = `id -un`; chomp $me;
334
335 my $host = `hostname`; chomp $host;
336
337 my $msg = new Mail::Send;
338
339 $msg->set('From',"PG Build Farm <$me\@$host>");
340
341 $msg->to(@$mailto);
342 $msg->bcc(@$bcc_stat) if (@$bcc_stat);
343 $msg->subject("PGBuildfarm member $animal Branch $branch $stat_type $stage");
344 my $fh = $msg->open;
345 print $fh <<EOMAIL; 
346
347
348 The PGBuildfarm member $animal had the following event on branch $branch:
349
350 $stat_type: $stage
351
352 The snapshot timestamp for the build that triggered this notification is: $dbdate
353
354 The specs of this machine are:
355 OS:  $os
356 Arch: $arch
357 Comp: $compiler
358
359 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
360
361 EOMAIL
362
363 $fh->close;
364
365 exit if ($stage eq $prev_stat);
366
367 $mailto = [@$change_stat];
368 push(@$mailto,@$green_stat) if ($stage eq 'OK' || $prev_stat eq 'OK');
369
370 $msg = new Mail::Send;
371
372 $msg->set('From',"PG Build Farm <$me\@$host>");
373
374 $msg->to(@$mailto);
375 $msg->bcc(@$bcc_chg) if (@$bcc_chg);
376
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';
381
382 $msg->subject("PGBuildfarm member $animal Branch $branch Status $stat_type");
383 $fh = $msg->open;
384 print $fh <<EOMAIL;
385
386 The PGBuildfarm member $animal had the following event on branch $branch:
387
388 Status $stat_type
389
390 The snapshot timestamp for the build that triggered this notification is: $dbdate
391
392 The specs of this machine are:
393 OS:  $os
394 Arch: $arch
395 Comp: $compiler
396
397 For more information, see $url/cgi-bin/show_history.pl?nm=$animal&br=$branch
398
399 EOMAIL
400
401 $fh->close;