Code

Run tinderbox builds in a test debug mode using "make test-debug"
[nagiosplug.git] / tools / tinderbox_build
1 #!/usr/bin/perl
2 # tinderbox_build.pl
3 # This script builds the nagiosplugins and then sends
4 # logs back to the master tinderbox server
5 #
6 # This script is based on mozilla-unix.pl which comes with tinderbox2
7 #
8 # See http://tinderbox.altinity.org for more details
10 require 5.000;
12 use strict;
13 use Sys::Hostname;
14 use Cwd;
15 use Time::Local;
17 my $Version = '$Revision$';
19 my $myhost = hostname;
20 chomp($myhost);
21 my ($host, $junk) = split(/\./, $myhost);
22         
23 my $BuildAdministrator = $ENV{TINDERBOX_BUILD_ADMIN} || "$ENV{'USER'}\@$myhost";
24 my $TmpDir = $ENV{TMPDIR} || "/tmp";
26 #Default values of cmdline opts
27 my $ReportStatus = 0;  # Do not send results to server
29 # Set these to what makes sense for your system
31 # Set these proper values for your tinderbox server
32 my $Tinderbox_server = 'tinderbox2@tinderbox.altinity.org';
34 # These shouldn't really need to be changed
35 my $BuildTree = 'nagiosplug';
36 my $BuildName = '';
37 my $ConfigureArgs = $ENV{CONFIGURE_ARGS};
39 my $OS = `uname -s`;
40 my $OSVer = `uname -r`;
41     
42 chop($OS, $OSVer);
43     
44 if ( $OS eq 'AIX' ) {
45         $OSVer = `uname -v`;
46         chop($OSVer);
47         $OSVer = $OSVer . "." . `uname -r`;
48         chop($OSVer);
49 }
50         
51 if ( $OS eq 'IRIX64' ) {
52         $OS = 'IRIX';
53 }
54     
55 if ( $OS eq 'SCO_SV' ) {
56         $OS = 'SCOOS';
57         $OSVer = '5.0';
58 }
59     
60 if ( "$host" ne "" ) {
61         $BuildName = $host . ' ';
62 }
63 $BuildName .= $OS . ' ' . $OSVer;
64 $_ = $BuildName;
65 s/ /_/g;
67 my $logfile = "$_.log";
69 sub BuildIt {
70         my ($fe, @felist, $EarlyExit, $LastTime);
72         my $StartDir = getcwd();
73         $LastTime = 0;
75         print "Starting dir is : $StartDir\n";
77         my $EarlyExit = 0;
79         chdir("$StartDir");
81         my $StartTime = time;
82         if (-e (my $file = "nagios-plugins.spec")) {
83                 open F, $file;
84                 while (<F>) {
85                         if (/^Version: HEAD-(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
86                                 $StartTime = timegm(0, $5, $4, $3, ($2 - 1), ($1 - 1900));
87                                 last;
88                         }
89                 }
90         }
92         print "Start time is $StartTime",$/;
94         my $CurrentDir = getcwd();
95         if ( $CurrentDir ne $StartDir ) {
96                 print "startdir: $StartDir, curdir $CurrentDir\n";
97                 die "curdir != startdir";
98         }
100         unlink( "$logfile" );
101         
102         print "opening $logfile\n";
103         open( LOG, ">$logfile" ) || print "can't open $?\n";
104         print LOG "current dir is -- $host:$CurrentDir\n";
105         print LOG "Build Administrator is $BuildAdministrator\n";
106         &PrintEnv;
107         
108         my $BuildStatus;
109         if (&configure) {
110                 if (&make) {
111                         if (&maketest) {
112                                 $BuildStatus = "success";
113                         } else {        
114                                 $BuildStatus = "test_failed";
115                         }
116                 } else {
117                         $BuildStatus = "build_failed";
118                 }
119         } else {
120                 $BuildStatus = "busted";
121         }
123         print LOG "\nBuild Status = $BuildStatus\n";
125         close(LOG);
126         chdir("$StartDir");
127         
128 # TV: Leaving this in, because process mail program probably has some
129 # limitation retained
131 # this fun line added on 2/5/98. do not remove. Translated to english,
132 # that's "take any line longer than 1000 characters, and split it into less
133 # than 1000 char lines.  If any of the resulting lines is
134 # a dot on a line by itself, replace that with a blank line."  
135 # This is to prevent cases where a <cr>.<cr> occurs in the log file.  Sendmail
136 # interprets that as the end of the mail, and truncates the log before
137 # it gets to Tinderbox.  (terry weismann, chris yeh)
139 # This was replaced by a perl 'port' of the above, writen by 
140 # preed@netscape.com; good things: no need for system() call, and now it's
141 # all in perl, so we don't have to do OS checking like before.
143         open(LOG, "$logfile") || die "Couldn't open logfile: $!\n";
144         open(OUTLOG, ">${logfile}.last") || die "Couldn't open logfile: $!\n";
145             
146         print OUTLOG $/;
147         print OUTLOG "tinderbox: tree: $BuildTree\n";
148         print OUTLOG "tinderbox: builddate: $StartTime\n";
149         print OUTLOG "tinderbox: status: $BuildStatus\n";
150         print OUTLOG "tinderbox: build: $BuildName $fe\n";
151         print OUTLOG "tinderbox: errorparser: unix\n";
152         print OUTLOG "tinderbox: buildfamily: unix\n";
153         print OUTLOG "tinderbox: END\n";            
154         print OUTLOG $/;
156         while (<LOG>) {
157             my $q = 0;
158             
159             for (;;) {
160                 my $val = $q * 1000;
161                 my $Output = substr($_, $val, 1000);
162                 
163                 last if $Output eq undef;
164                 
165                 $Output =~ s/^\.$//g;
166                 $Output =~ s/\n//g;
167                 print OUTLOG "$Output\n";
168                 $q++;
169             } #EndFor
170                 
171         } #EndWhile
172             
173         close(LOG);
174         close(OUTLOG);
176         if ($ReportStatus) {
177                 system( "ssh $Tinderbox_server tinderbox_receive < ${logfile}.last" )
178         } else {
179                 print <<"EOF"
180 Not sending logs to http://tinderbox.altinity.org
181 If you have SSH keys setup on the tinderbox server, you can manually send
182 with 'ssh $Tinderbox_server tinderbox_receive < ${logfile}.last'
183 EOF
184         }
185         
186         unlink("$logfile");
187         print "Finished building for tinderbox",$/;
189 } #EndSub-BuildIt
191 sub ParseArgs {
192     my($i);
194     $i = 0;
195     while( $i < @ARGV ) {
196         if ($ARGV[$i] eq '--version' || $ARGV[$i] eq '-v') {
197             die "$0: version $Version\n";
198         } elsif ($ARGV[$i] eq '-y') {
199                 $ReportStatus = 1;
200         } else {
201             &PrintUsage;
202         }
204         $i++;
205     } #EndWhile
207 } #EndSub-ParseArgs
209 sub PrintUsage {
210     die "usage: $0 [-v | --version ] [-t do not send report to tinderbox server]\n";
213 sub PrintEnv {
214     my ($key);
215     foreach $key (keys %ENV) {
216         print LOG "$key = $ENV{$key}\n";
217         print "$key = $ENV{$key}\n";
218     }
220         # Print the NPTest variables
221         if (-e "/var/tmp/NPTest.cache") {
222                 open F, "/var/tmp/NPTest.cache";
223                 print LOG "NPTest variables:\n";
224                 print LOG <F>;
225                 close F;
226         }
227                 
228 } #EndSub-PrintEnv
230 sub SetupPath {
231     my($Path);
232     $Path = $ENV{PATH};
233     print "Path before: $Path\n";
235     if ( $OS eq 'SunOS' ) {
236         $ENV{'PATH'} = '/usr/ccs/bin:' . $ENV{'PATH'};
237     }
239     $Path = $ENV{PATH};
240     print "Path After: $Path\n";
241 } #EndSub-SetupPath
243 sub configure {
244         # Configure
245         print LOG "./configure $ConfigureArgs\n";
246         open (CONFIGURE, "./configure $ConfigureArgs 2>&1 |") || die "../configure: $!\n";
247         while (<CONFIGURE>) {
248             print $_;
249             print LOG $_;
250         }
251         close(CONFIGURE);
252         return ! $?;
254         
255 sub make {
256         # Building
257         print LOG "make 2>&1\n";
258         open( MAKE, "make 2>&1 |");
259         while ( <MAKE> ) {
260                 print $_;
261                 print LOG $_;
262         }
263         close( MAKE);
264         return ! $?;
267 sub maketest {
268         # Tests
269         print LOG "LANG=C make test-debug 2>&1\n";
270         open( MAKE, "LANG=C make test-debug && make install DESTDIR=$TmpDir/tinderbox_build.$$ && make install-strip DESTDIR=$TmpDir/tinderbox_build2.$$ 2>&1 |");
271         while ( <MAKE> ) {
272                 print $_;
273                 print LOG $_;
274         }
275         close( MAKE);
276         my $rc = $?;
277         system("rm -fr $TmpDir/tinderbox_build.$$ $TmpDir/tinderbox_build2.$$");
278         return ! $rc;
281 # Main function
282 &ParseArgs;
283 &SetupPath;
284 &BuildIt;
286 1;