fix permissions problem for temp tar file
[buildfarm-server.git] / cgi-bin / show_stage_log.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use DBI;
5 use Template;
6 use CGI;
7 use File::Temp qw(tempfile);
8
9 use vars qw($dbhost $dbname $dbuser $dbpass $dbport @log_file_names);
10
11
12 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
13 #require "BuildFarmWeb.pl";
14
15 die "no dbname" unless $dbname;
16 die "no dbuser" unless $dbuser;
17
18 my $dsn="dbi:Pg:dbname=$dbname";
19 $dsn .= ";host=$dbhost" if $dbhost;
20 $dsn .= ";port=$dbport" if $dbport;
21
22 my $query = new CGI;
23
24 my $system = $query->param('nm'); $system =~ s/[^a-zA-Z0-9_ -]//g;
25 my $logdate = $query->param('dt');$logdate =~ s/[^a-zA-Z0-9_ -]//g;
26 my $stage = $query->param('stg');$stage =~ s/[^a-zA-Z0-9_ -]//g;
27
28 use vars qw($tgz);
29
30 if ($system && $logdate)
31 {
32
33     my $db = DBI->connect($dsn,$dbuser,$dbpass);
34
35     die $DBI::errstr unless $db;
36
37     my $statement = q(
38
39                 select log_archive
40                 from build_status
41                 where sysname = ? and snapshot = ?
42
43                 );
44
45
46     
47     my $sth=$db->prepare($statement);
48     $sth->execute($system,$logdate);
49     my $row=$sth->fetchrow_arrayref;
50     $tgz=$row->[0];
51     $sth->finish;
52     $db->disconnect;
53
54 }
55
56 unless ($stage)
57 {
58
59     print 
60         "Content-Type: application/x-gzip\n", 
61         "Content-Disposition: attachment; filename=buildfarmlog.tgz\n",
62         "\n",
63         $tgz;
64     exit;
65 }
66
67 my $template = "buildlogXXXXXX";
68 my ($fh, $filename) = tempfile($template, 
69                                                            DIR => '/home/community/pgbuildfarm/buildlogs',
70                                                            UNLINK => 1);
71 print $fh $tgz;
72 close($fh);
73
74 my $output = `tar -z -O -xf $filename $stage.log 2>&1`;
75
76 print "Content-Type: text/plain\n\n", $output,
77
78     "-------------------------------------------------\n\n",
79     "Hosting for the PostgreSQL Buildfarm is generously ",
80     "provided by: CommandPrompt, The PostgreSQL Company";
81
82 ; exit;
83
84 # using <pre> like this on huge files can make browsers choke
85
86 print "Content-Type: text/html\n\n";
87
88 print <<EOHTML;
89 <html>
90 <body>
91 <pre>
92 $output
93 </pre>
94 </body>
95 </html>
96
97 EOHTML