76929e5a07065e6c4df4898480f08e90232ab82d
[buildfarm-server.git] / cgi-bin / show_stage_log.pl
1 #!/usr/bin/perl
2
3 =comment
4
5 Copyright (c) 2003-2010, Andrew Dunstan
6
7 See accompanying License file for license details
8
9 =cut 
10
11 use strict;
12 use DBI;
13 use Template;
14 use CGI;
15 use File::Temp qw(tempfile);
16
17 use vars qw($dbhost $dbname $dbuser $dbpass $dbport @log_file_names);
18
19 use FindBin qw($RealBin);
20 require "$RealBin/../BuildFarmWeb.pl";
21
22 die "no dbname" unless $dbname;
23 die "no dbuser" unless $dbuser;
24
25 my $dsn="dbi:Pg:dbname=$dbname";
26 $dsn .= ";host=$dbhost" if $dbhost;
27 $dsn .= ";port=$dbport" if $dbport;
28
29 my $query = new CGI;
30
31 my $system = $query->param('nm'); $system =~ s/[^a-zA-Z0-9_ -]//g;
32 my $logdate = $query->param('dt');$logdate =~ s/[^a-zA-Z0-9_ -]//g;
33 my $stage = $query->param('stg');$stage =~ s/[^a-zA-Z0-9._ -]//g;
34 my $brnch = $query->param('branch') || 'HEAD'; $brnch =~ s/[^a-zA-Z0-9._ -]//g;
35
36 use vars qw($tgz);
37
38 if ($system && $logdate && $stage)
39 {
40     my $db = DBI->connect($dsn,$dbuser,$dbpass);
41
42     die $DBI::errstr unless $db;
43
44     if ($logdate =~ /^latest$/i)
45     {
46         my $find_latest = qq{
47             select max(snapshot) 
48             from build_status_log 
49             where sysname = ? 
50                 and snapshot > now() - interval '30 days' 
51                 and log_stage = ? || '.log'
52                 and branch = ?
53         };
54         my $logs = $db->selectcol_arrayref($find_latest,undef,$system,$stage,$brnch);
55         $logdate = shift(@$logs);
56     }
57
58     my $statement = q(
59
60         select branch, log_text
61         from build_status_log
62         where sysname = ? and snapshot = ? and log_stage = ? || '.log'
63
64         );
65     
66     my $sth=$db->prepare($statement);
67     $sth->execute($system,$logdate,$stage);
68     my $row=$sth->fetchrow_arrayref;
69     my ($branch, $logtext) = ("unknown","no log text found");
70     if ($row)
71     {
72         $branch = $row->[0];
73         $logtext =$row->[1];
74     }
75     $sth->finish;
76     $db->disconnect;
77
78     print "Content-Type: text/plain\n\nSnapshot: $logdate\n\n", $logtext,
79
80     "-------------------------------------------------\n\n",
81     "Hosting for the Exim BuildFarm is generously ",
82     "provided by: Bytemark";
83
84 }
85
86 else 
87 {
88     print "Status: 460 bad parameters\n",
89     "Content-Type: text/plain\n\n";
90 }