Code

1ace2b030dfaa8a028bae30ae1dd3eaa47847921
[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";
25 #Default values of cmdline opts
26 my $ReportStatus = 0;  # Do not send results to server
28 # Set these to what makes sense for your system
30 # Set these proper values for your tinderbox server
31 my $Tinderbox_server = 'tinderbox2@tinderbox.altinity.org';
33 # These shouldn't really need to be changed
34 my $BuildTree = 'nagiosplug';
35 my $BuildName = '';
36 my $ConfigureArgs = $ENV{CONFIGURE_ARGS};
38 my $OS = `uname -s`;
39 my $OSVer = `uname -r`;
40     
41 chop($OS, $OSVer);
42     
43 if ( $OS eq 'AIX' ) {
44         $OSVer = `uname -v`;
45         chop($OSVer);
46         $OSVer = $OSVer . "." . `uname -r`;
47         chop($OSVer);
48 }
49         
50 if ( $OS eq 'IRIX64' ) {
51         $OS = 'IRIX';
52 }
53     
54 if ( $OS eq 'SCO_SV' ) {
55         $OS = 'SCOOS';
56         $OSVer = '5.0';
57 }
58     
59 if ( "$host" ne "" ) {
60         $BuildName = $host . ' ';
61 }
62 $BuildName .= $OS . ' ' . $OSVer;
63 $_ = $BuildName;
64 s/ /_/g;
66 my $logfile = "$_.log";
68 sub BuildIt {
69         my ($fe, @felist, $EarlyExit, $LastTime);
71         my $StartDir = getcwd();
72         $LastTime = 0;
74         print "Starting dir is : $StartDir\n";
76         my $EarlyExit = 0;
78         chdir("$StartDir");
80         my $StartTime = time;
81         if (-e (my $file = "nagios-plugins.spec")) {
82                 open F, $file;
83                 while (<F>) {
84                         if (/^Version: HEAD-(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
85                                 $StartTime = timegm(0, $5, $4, $3, ($2 - 1), ($1 - 1900));
86                                 last;
87                         }
88                 }
89         }
91         print "Start time is $StartTime",$/;
93         my $CurrentDir = getcwd();
94         if ( $CurrentDir ne $StartDir ) {
95                 print "startdir: $StartDir, curdir $CurrentDir\n";
96                 die "curdir != startdir";
97         }
99         unlink( "$logfile" );
100         
101         print "opening $logfile\n";
102         open( LOG, ">$logfile" ) || print "can't open $?\n";
103         print LOG "current dir is -- $host:$CurrentDir\n";
104         print LOG "Build Administrator is $BuildAdministrator\n";
105         &PrintEnv;
106         
107         my $BuildStatus;
108         if (&configure) {
109                 if (&make) {
110                         if (&maketest) {
111                                 $BuildStatus = "success";
112                         } else {        
113                                 $BuildStatus = "test_failed";
114                         }
115                 } else {
116                         $BuildStatus = "build_failed";
117                 }
118         } else {
119                 $BuildStatus = "busted";
120         }
122         print LOG "\nBuild Status = $BuildStatus\n";
124         close(LOG);
125         chdir("$StartDir");
126         
127 # TV: Leaving this in, because process mail program probably has some
128 # limitation retained
130 # this fun line added on 2/5/98. do not remove. Translated to english,
131 # that's "take any line longer than 1000 characters, and split it into less
132 # than 1000 char lines.  If any of the resulting lines is
133 # a dot on a line by itself, replace that with a blank line."  
134 # This is to prevent cases where a <cr>.<cr> occurs in the log file.  Sendmail
135 # interprets that as the end of the mail, and truncates the log before
136 # it gets to Tinderbox.  (terry weismann, chris yeh)
138 # This was replaced by a perl 'port' of the above, writen by 
139 # preed@netscape.com; good things: no need for system() call, and now it's
140 # all in perl, so we don't have to do OS checking like before.
142         open(LOG, "$logfile") || die "Couldn't open logfile: $!\n";
143         open(OUTLOG, ">${logfile}.last") || die "Couldn't open logfile: $!\n";
144             
145         print OUTLOG $/;
146         print OUTLOG "tinderbox: tree: $BuildTree\n";
147         print OUTLOG "tinderbox: builddate: $StartTime\n";
148         print OUTLOG "tinderbox: status: $BuildStatus\n";
149         print OUTLOG "tinderbox: build: $BuildName $fe\n";
150         print OUTLOG "tinderbox: errorparser: unix\n";
151         print OUTLOG "tinderbox: buildfamily: unix\n";
152         print OUTLOG "tinderbox: END\n";            
153         print OUTLOG $/;
155         while (<LOG>) {
156             my $q = 0;
157             
158             for (;;) {
159                 my $val = $q * 1000;
160                 my $Output = substr($_, $val, 1000);
161                 
162                 last if $Output eq undef;
163                 
164                 $Output =~ s/^\.$//g;
165                 $Output =~ s/\n//g;
166                 print OUTLOG "$Output\n";
167                 $q++;
168             } #EndFor
169                 
170         } #EndWhile
171             
172         close(LOG);
173         close(OUTLOG);
175         if ($ReportStatus) {
176                 system( "ssh $Tinderbox_server tinderbox_receive < ${logfile}.last" )
177         } else {
178                 print <<"EOF"
179 Not sending logs to http://tinderbox.altinity.org
180 If you have SSH keys setup on the tinderbox server, you can manually send
181 with 'ssh $Tinderbox_server tinderbox_receive < ${logfile}.last'
182 EOF
183         }
184         
185         unlink("$logfile");
186         print "Finished building for tinderbox",$/;
188 } #EndSub-BuildIt
190 sub ParseArgs {
191     my($i);
193     $i = 0;
194     while( $i < @ARGV ) {
195         if ($ARGV[$i] eq '--version' || $ARGV[$i] eq '-v') {
196             die "$0: version $Version\n";
197         } elsif ($ARGV[$i] eq '-y') {
198                 $ReportStatus = 1;
199         } else {
200             &PrintUsage;
201         }
203         $i++;
204     } #EndWhile
206 } #EndSub-ParseArgs
208 sub PrintUsage {
209     die "usage: $0 [-v | --version ] [-t do not send report to tinderbox server]\n";
212 sub PrintEnv {
213     my ($key);
214     foreach $key (keys %ENV) {
215         print LOG "$key = $ENV{$key}\n";
216         print "$key = $ENV{$key}\n";
217     }
219         # Print the NPTest variables
220         if (-e "/var/tmp/NPTest.cache") {
221                 open F, "/var/tmp/NPTest.cache";
222                 print LOG "NPTest variables:\n";
223                 print LOG <F>;
224                 close F;
225         }
226                 
227 } #EndSub-PrintEnv
229 sub SetupPath {
230     my($Path);
231     $Path = $ENV{PATH};
232     print "Path before: $Path\n";
234     if ( $OS eq 'SunOS' ) {
235         $ENV{'PATH'} = '/usr/ccs/bin:' . $ENV{'PATH'};
236     }
238     $Path = $ENV{PATH};
239     print "Path After: $Path\n";
240 } #EndSub-SetupPath
242 sub configure {
243         # Configure
244         print LOG "./configure $ConfigureArgs\n";
245         open (CONFIGURE, "./configure $ConfigureArgs 2>&1 |") || die "../configure: $!\n";
246         while (<CONFIGURE>) {
247             print $_;
248             print LOG $_;
249         }
250         close(CONFIGURE);
251         return ! $?;
253         
254 sub make {
255         # Building
256         print LOG "make 2>&1\n";
257         open( MAKE, "make 2>&1 |");
258         while ( <MAKE> ) {
259                 print $_;
260                 print LOG $_;
261         }
262         close( MAKE);
263         return ! $?;
266 sub maketest {
267         # Tests
268         print LOG "LANG=C make test 2>&1\n";
269         open( MAKE, "LANG=C make test && make install DESTDIR=/tmp/tinderbox_build.$$ && make install-strip DESTDIR=/tmp/tinderbox_build2.$$ 2>&1 |");
270         while ( <MAKE> ) {
271                 print $_;
272                 print LOG $_;
273         }
274         close( MAKE);
275         my $rc = $?;
276         system("rm -fr /tmp/tinderbox_build.$$ /tmp/tinderbox_build2.$$");
277         return ! $rc;
280 # Main function
281 &ParseArgs;
282 &SetupPath;
283 &BuildIt;
285 1;