VBTK.pm untested

filter: Statementstmt 0.0% 0 / 191 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 328 CC 42 SCAR 52.8
CC
42
Cov
0%
install
380.0
runAll
272.0
catchSignal
20.0
CRAP 195.8
1 #! /bin/perl
2 #############################################################################
3 #
4 # NOTE: This file under revision control using RCS
5 # Any changes made without RCS will be lost
6 #
7 # $Source: /usr/local/cvsroot/vbtk/VBTK.pm,v $
8 # $Revision: 1.29 $
9 # $Date: 2002/03/04 20:54:25 $
10 # $Author: bhenry $
11 # $Locker: $
12 # $State: Exp $
13 #
14 # Purpose: This module is a placeholder for documentation for
15 # the VB Toolkit
16 #
17 # Copyright (C) 1996-2002 Brent Henry
18 #
19 # This program is free software; you can redistribute it and/or
20 # modify it under the terms of version 2 of the GNU General Public
21 # License as published by the Free Software Foundation available at:
22 # http://www.gnu.org/copyleft/gpl.html
23 #
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 #
29 #############################################################################
30 #
31 #
32 # REVISION HISTORY:
33 #
34 # $Log: VBTK.pm,v $
35 # Revision 1.29 2002/03/04 20:54:25 bhenry
36 # *** empty log message ***
37 #
38 # Revision 1.28 2002/03/04 20:53:06 bhenry
39 # *** empty log message ***
40 #
41 # Revision 1.27 2002/03/04 16:49:08 bhenry
42 # Changed requirement back to perl 5.6.0
43 #
44 # Revision 1.26 2002/03/04 16:46:38 bhenry
45 # *** empty log message ***
46 #
47 # Revision 1.25 2002/03/02 01:01:22 bhenry
48 # Documentation Updates
49 #
50 #
51  
52 package VBTK;
53  
54 use 5.6.0;
55 use strict;
56 use warnings;
57 # I like using undef as a value so I'm turning off the uninitialized warnings
58 no warnings qw(uninitialized);
59  
60 use VBTK::Common;
61 use VBTK::File;
62 use Exporter;
63 use ExtUtils::MakeMaker;
64 use File::Find;
65 use VBTK::Controller;
66  
67 # Setup things to be exported
68 our $VERSION = '0.20';
69  
70 our @ALL_VBTK_OBJECTS = ();
71 our $SIGNAL_CAUGHT = '';
72 our $VERBOSE=$ENV{VERBOSE};
73  
74 our @ISA = qw(Exporter);
75 our @EXPORT = qw(install);
76  
77 &VBTK::Common::checkDateManipTZ();
78  
79 #-------------------------------------------------------------------------------
80 # Function: runAll
81 # Description: Loop through all registered VBTK objects, calling their 'run' method.
82 # Continuously loop through the registered objects until either they
83 # all return 'FINISHED' or we exceed the passed time limit. Not that
84 # some VBTK objects will never return 'FINISHED'.
85 # Input Parms: None
86 # Output Parms: None
87 #-------------------------------------------------------------------------------
88 sub runAll
89 {
90 my $self = shift;
91 my ($obj,$lowestSleepTime,$sleepfor,$retval,$returnAfter,@passedObjList);
92 my ($highestExitValue,$sleepTime,$allFinished,$timeLeft,@objList);
93 my $minSleepTime = 1;
94  
95 # If the first argument is a Wrapper object, then put that on the list.
96 # Otherwise, the first argument is the 'returnAfter' time.
97 if (ref($self) =~ /^VBTK::/)
98 {
99 @passedObjList = ($self);
100 $returnAfter = shift;
101 }
102 else
103 {
104 $returnAfter = $self;
105 }
106  
107 my $returnTime = time + $returnAfter;
108  
109 # Setup signal handlers to exit gracefully.
110 $SIG{'TERM'} = $SIG{'INT'} = \&catchSignal;
111  
112 # Loop until all commands are finished executing.
113 for(;;)
114 {
115 $timeLeft = $returnTime - time if (defined $returnAfter);
116 $allFinished = 1;
117 $lowestSleepTime = 600;
118
119 # Have to keep re-loading the object list, since objects may have
120 # unregistered themselves during the last pass.
121 @objList = (@passedObjList > 0) ? @passedObjList : @ALL_VBTK_OBJECTS;
122  
123 foreach $obj (@objList)
124 {
125 ($sleepTime,$retval) = $obj->run();
126  
127 # If retval is $::NOT_FINISHED, then the command is not complete and we should
128 # look at the sleep time to determine when the command needs to be run again.
129 if($retval == $::NOT_FINISHED)
130 {
131 $allFinished = 0;
132 $lowestSleepTime = $sleepTime if ($sleepTime < $lowestSleepTime);
133 }
134 # Otherwise, the command is complete and we should examine the exit value.
135 else
136 {
137 $highestExitValue = $retval if ($retval > $highestExitValue);
138 }
139  
140 &catchSignal($SIGNAL_CAUGHT) if ($SIGNAL_CAUGHT ne '');
141 }
142  
143 # If all processes have finished, then return.
144 if ($allFinished)
145 {
146 $lowestSleepTime = -1;
147 last;
148 }
149  
150 # If we've exceeded our time limit, then return
151 last if((defined $returnAfter)&&(time >= $returnTime));
152  
153 # Adjust lowest sleep time using the timeLeft variable
154 if(defined $returnAfter)
155 {
156 $timeLeft = $returnTime - time;
157 $lowestSleepTime = $timeLeft if ($timeLeft < $lowestSleepTime);
158 }
159  
160 # Otherwise, just sleep until we need to run again
161 &log("Sleeping for $lowestSleepTime") if ($VERBOSE > 1);
162 $lowestSleepTime = $minSleepTime if ($lowestSleepTime < $minSleepTime);
163 sleep $lowestSleepTime if ($lowestSleepTime > 0);
164 }
165  
166 # If the calling subroutine wants an array then also return the highest exit
167 # value
168 (wantarray) ? ($lowestSleepTime,$highestExitValue) : ($lowestSleepTime);
169 }
170  
171 #-------------------------------------------------------------------------------
172 # Function: register
173 # Description: Register the passed object so that it will be executed later when
174 # the runAll method is executed.
175 # Input Parms: Object
176 # Output Parms: None
177 #-------------------------------------------------------------------------------
178 sub register
179 {
180 my $obj = shift;
181  
182 push(@ALL_VBTK_OBJECTS,$obj) if ($obj);
183  
184 (1);
185 }
186  
187 #-------------------------------------------------------------------------------
188 # Function: unRegister
189 # Description: Remove the passed object from the registration list, so that it
190 # won't be executed by the runAll method anymore.
191 # Input Parms: Object
192 # Output Parms: None
193 #-------------------------------------------------------------------------------
194 sub unRegister
195 {
196 my $delObj = shift;
197 my @tempObjects = @ALL_VBTK_OBJECTS;
198  
199 # Clear out the list
200 @ALL_VBTK_OBJECTS = ();
201
202 &log("Unregistering '$delObj' from process queue") if ($VERBOSE);
203  
204 # Now add back in any objects which don't match the passed one.
205 foreach my $obj (@tempObjects)
206 {
207 push(@ALL_VBTK_OBJECTS,$obj) unless ($obj eq $delObj);
208 }
209  
210 (1);
211 }
212  
213 #-------------------------------------------------------------------------------
214 # Function: unRegisterAll
215 # Description: Remove all objects from the registered list.
216 # Input Parms: Object
217 # Output Parms: None
218 #-------------------------------------------------------------------------------
219 sub unRegisterAll
220 {
221 # Clear out the list
222 @ALL_VBTK_OBJECTS = ();
223  
224 (0);
225 }
226  
227 #-------------------------------------------------------------------------------
228 # Function id: catchSignal
229 # Purpose: Capture signals sent to the program and re-route them.
230 # Input: Exit code
231 # Output: None
232 #-------------------------------------------------------------------------------
233 sub catchSignal
234 {
235 my($signal) = @_;
236 my $status = $::FAILED;
237 my($obj,$pgrp);
238  
239 # Ignore all but TERM and INT signals
240 if($signal =~ /^TERM|^INT/)
241 {
242 # If signal was caught once already, then kill all objects and
243 # exit immediately
244 if($SIGNAL_CAUGHT eq $signal)
245 {
246 foreach $obj (@ALL_VBTK_OBJECTS)
247 {
248 $obj->handleSignal($signal);
249 }
250  
251 # Now kill anything else in the process group before we exit
252 $SIG{'TERM'} = 'IGNORE';
253  
254 # Run 'getpgrp' in an eval, just in case it's not supported
255 eval { $pgrp = getpgrp; };
256  
257 if ($pgrp > 0)
258 {
259 print STDOUT "Killing pgrp $pgrp\n";
260 kill 15, -$pgrp;
261 }
262  
263 exit(0);
264 }
265 # Otherwise, just mark the signal as caught, so that the main program
266 # can exit when it's convenient.
267 else
268 {
269 $SIGNAL_CAUGHT = $signal;
270 }
271 }
272 (0);
273 }
274  
275 #-------------------------------------------------------------------------------
276 # Function: appendToPath
277 # Description: Add the paths path names to ENV{PATH} after checking to see if
278 # they exist.
279 # Input Parms: Array of path names
280 # Output Parms: New Path
281 #-------------------------------------------------------------------------------
282 sub appendToPath
283 {
284 my(@searchPath) = @_;
285
286 foreach my $dir (@searchPath)
287 {
288 if(-d $dir) { $ENV{PATH} .= ":$dir"; }
289 else { &log("Can't find searchpath dir '$dir', ignoring"); }
290 }
291  
292 $ENV{PATH};
293 }
294  
295 #-------------------------------------------------------------------------------
296 # Function: prependToPath
297 # Description: Add the paths path names to the beginning of ENV{PATH} after
298 # checking to see if they exist.
299 # Input Parms: Array of path names
300 # Output Parms: New Path
301 #-------------------------------------------------------------------------------
302 sub prependToPath
303 {
304 my(@searchPath) = @_;
305
306 foreach my $dir (@searchPath)
307 {
308 if(-d $dir) { $ENV{PATH} = "$dir:$ENV{PATH}"; }
309 else { &log("Can't find searchpath dir '$dir', ignoring"); }
310 }
311  
312 $ENV{PATH};
313 }
314  
315 #-------------------------------------------------------------------------------
316 # Function: install
317 # Description: Use prompts to walk the user through the installation process.
318 # Input Parms: None
319 # Output Parms: None
320 #-------------------------------------------------------------------------------
321 sub install
322 {
323 my($resp,$dir,$installDir,$vbcObj,$vbcText,$mode,$result);
324  
325 # Check to see if VBHOME is created and writeable.
326 if(! -d $::VBHOME)
327 {
328 print STDOUT "Can't find '$::VBHOME', please create it or set the " .
329 "Environment variable \$VBHOME to an alternate location\n";
330 exit 1;
331 }
332
333 # See if we're running as the userid 'vbtk', and warn if not.
334 my ($uname) = getpwuid($<);
335 if($uname ne 'vbtk')
336 {
337 print STDOUT "\n" .
338 "It is recommended that you run all VB processes under a separate userid,\n" .
339 "such as 'vbtk'. You are currently running with the userid '$uname'.\n";
340 $resp = prompt("Do you want to continue under this userid? ","n");
341 exit 1 if($resp !~ /^y/i);
342 }
343
344 # Create appropriate directories under VBHOME, die if error
345 foreach $dir ('bin','conf','etc','examples','logs','perf','web')
346 {
347 if ((! -d "$::VBHOME/$dir")&&(! mkdir "$::VBHOME/$dir"))
348 {
349 print STDOUT "Can't create dir '$::VBHOME/$dir'";
350 exit 1;
351 }
352 }
353
354 # Ask user if this will run the (M)aster VBServer, a (S)lave VBServer,
355 # or just (C)lient VB processes?
356 print STDOUT "\nWhich VBTK processes will you be running on this host?\n";
357 $mode = prompt("(M)aster VBServer, (S)lave VBServer, or (C)lients only?","C");
358
359 # If Master, then look for the original VBTK install directory and copy
360 # files from bin, examples, etc, and web over from it. Preconfigure the
361 # vbc file in conf. Ask user for a list of slave servers and clients on
362 # which this will be run. Once configured, give the user instructions
363 # to install the perl libraries on each of those hosts and run the
364 # install process there.
365 if($mode =~ /^m/i)
366 {
367 print STDOUT "\n" .
368 "Since this is to be a Master VBServer, I'll need to copy some files\n" .
369 "from the original VBTK install directory. Please enter it's location.\n";
370
371 $installDir = prompt("Install dir location:","");
372
373 if((! -d "$installDir")||(! -f "$installDir/Makefile.PL"))
374 {
375 print STDOUT "\nError: Can't find VBTK install files in '$installDir', try again\n";
376 exit 1;
377 }
378
379 if($installDir eq $::VBHOME)
380 {
381 print STDOUT "\nError: Install dir can't be the same as \$VBHOME\n";
382 exit 1;
383 }
384
385 # Check for all the appropriate directories
386 my @copyDirs = qw(bin etc web examples);
387 grep(s/^/$installDir\//,@copyDirs);
388 foreach (@copyDirs)
389 {
390 if(! -d $_)
391 {
392 print STDOUT "\nError: Can't find '$_'\n";
393 exit 1;
394 }
395 }
396
397 # Copy specific files from install dir into new VBHOME. If destination
398 # file already exists, then prompt user what to do.
399 my ($fileObj,$targetFile);
400 my $filter = sub {
401 return if (! -f or -l or m:/CVS:);
402
403 $fileObj = new VBTK::File($_);
404 $targetFile = $_;
405 $targetFile =~ s/^$installDir/$::VBHOME/;
406  
407 # If there's no difference between source and destination files, then
408 # just return
409 return unless ($fileObj->hasChanged($targetFile));
410
411 # If the file already exists, then prompt to override
412 if(-f $targetFile)
413 {
414 print STDOUT "\nFile '$targetFile' already exists. ";
415 $resp = prompt("Overwrite?","y");
416 return unless ($resp =~ /^y/i);
417 }
418
419 # Now try to sync the file
420 if(! $fileObj->sync($targetFile))
421 {
422 print STDOUT "\nError: Can't copy '$_' to '$targetFile'\n";
423 exit 1;
424 }
425 };
426
427 print STDOUT "Copying files from '$installDir' to '$::VBHOME'\n";
428 &find({ wanted => $filter, no_chdir => 1},@copyDirs);
429  
430 # If the conf/vbc file doesn't yet exist, then copy one over and make
431 # a rough attempt to configure it.
432 if (! -f "$::VBCONF/vbc")
433 {
434 print STDOUT "Copying 'examples/vbc' to 'conf/vbc\n";
435 $vbcObj = new VBTK::File("$::VBHOME/examples/vbc");
436 $vbcText = $vbcObj->get;
437
438 # Make a few changes to the vbc file
439 $vbcText =~ s/myhost1/$::HOST/mg;
440
441 $vbcObj = new VBTK::File("$::VBCONF/vbc");
442 $vbcObj->put($vbcText);
443 }
444
445 &checkPermsAndLinks();
446  
447 print STDOUT "\n" .
448 "You should now proceed with the installation by editing the\n" .
449 "'conf/vbc' file. Just read the comments for instructions.\n";
450 }
451 # If Slave or Client, prompt user for VBURI of VBServer, connect, and
452 # download files. Inspect vbc to see if it has been configured properly
453 # and warn user if not.
454 elsif($mode =~ /^s|^c/i)
455 {
456 # Prompt user for VBServerURI
457 print STDOUT "\n" .
458 "I need to pull some files from a configured VBServer.\n";
459 $resp = prompt("Please enter the URI: ","$::VBURI") ||
460 die ("Must specify VBServerURI");
461
462 # Run the 'doSync' method on the controller object
463 print STDOUT "Attempting to sync files from '$resp'\n";
464 $result = &VBTK::Controller::doSync($resp);
465 exit 1 if (! defined $result);
466 print STDOUT "Sync was successful!\n";
467
468 # Make sure our host is mentioned somewhere in the vbc file
469 $vbcObj = new VBTK::File("$::VBCONF/vbc");
470 $vbcText = $vbcObj->get;
471
472 if (! $vbcText =~ /\b$::HOST\b/m)
473 {
474 print STDOUT "\n" .
475 "Warning: It doesn't look like there's an entry for '$::HOST' in\n" .
476 "the config file '$::VBCONF/vbc'. Please go back to\n" .
477 "the master VBServer and edit the 'vbc' config file to include\n" .
478 "an entry for '$::HOST'\n";
479 exit 1;
480 }
481  
482 # If we're in slave mode, then remind the user to make sure that this
483 # host is specified in the vbc file.
484 if($mode =~ /^s/i)
485 {
486 print STDOUT "\n" .
487 "You specified that this is to be a slave VBServer, so please\n" .
488 "make sure that it's specified in the 'SlaveVBServers' parm in\n" .
489 "the '$::VBCONF/vbc' file.\n";
490 }
491
492 &checkPermsAndLinks();
493  
494 print STDOUT "\n" .
495 "Configuration was successful!! You can now startup the monitoring\n".
496 "processes on this machine with the command '$::VBCONF/vbc start'\n";
497 }
498 else
499 {
500 die("Invalid response, must select 'M','S', or 'C'");
501 }
502 }
503  
504 #-------------------------------------------------------------------------------
505 # Function: checkPermsAndLinks
506 # Description: Turn on the executable bit on 'vbc' and setup the 'start', 'stop'
507 # 'restart', and 'sync' links.
508 # Input Parms: None
509 # Output Parms: None
510 #-------------------------------------------------------------------------------
511 sub checkPermsAndLinks
512 {
513 my($dir,$link);
514
515 # Turn on the executable bit on the vbc script
516 chmod 0755, "$::VBCONF/vbc";
517
518 # Create symbolic links in both bin and conf
519 foreach $dir ($::VBCONF,$::VBBIN)
520 {
521 foreach $link ('start','stop','restart','sync')
522 {
523 symlink "../conf/vbc","$dir/$link";
524 }
525 }
526 }
527  
528 1;
529 __END__