Subversion Repositories camp_sysinfo_client_3

Rev

Rev 229 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/env perl

use warnings;
use strict;

# v1.1.0 20161022 RWR
# use library sysinfoconf.pm for loading, editing and displaying config
#
# v1.2.0 20190108 RWR
# added UUID
#
# v1.3.0 20191107 RWR
# changed the screen to allow the user to see most of the values and decide what to edit
# then write the results.
# also, parameters are passed that allow the import of a file in old conf format
#
# v1.3.1 20191112 RWR
# Fixed showEntry() to set value to empty string if it is null 

use Getopt::Long;
use Data::Dumper;

our $VERSION = '1.3.1';

# find our location and use it for searching for libraries
BEGIN {
   use FindBin;
   use File::Spec;
   use lib File::Spec->catdir($FindBin::Bin);
}

use Cwd qw(abs_path);


use sysinfoconf; # a library of shared routines with install.pl
our %displayOrder;

$binDir = abs_path(File::Spec->catdir($FindBin::Bin) );


my $config;

sub findSendEmail {
   my $currentLocation = shift;
   my @possibles = ( 
            '/usr/local/opt/sendEmail/sendEmail',
            '/usr/local/opt/sendEmail/sendEmail.pl',
            '/opt/sendEmail/sendEmail', 
            '/opt/sendEmail/sendEmail.pl' 
            );
   return $currentLocation if ( $currentLocation && -x $currentLocation );
   # well, we didn't find it, so look around some more
   # Look through the possibles
   foreach my $current ( @possibles ) {
      return $current if -x $current;
   }
   if ( &yesno( "You are asking for sendEmail, but I don't see it on the system\nWould you like me to automatically download and install" ) ) {
      my $path = `perl getSendEmail.pl`;
      chomp $path;
      return $path;
   }
   return '';
}

sub userConfirmation {
   my $config = shift;
   
}

sub setUpTransport {
   my ($priority, $transport, $fields, $type ) = @_;
   $priority = getAnswer( 'Priority', $priority );
   $$transport{'sendScript'} = $$fields{'sendScript'} unless $$transport{'sendScript'};
   $$transport{'name'} = $type unless $$transport{'name'};
   my $allKeys = $$fields{'keys'};
   foreach my $key ( @$allKeys ) {
      if ( $key eq 'sendEmailScriptLocation' && ! -e $$transport{$key} ) {
         my $temp = &findSendEmail( $$transport{$key} );
         $$transport{$key} = $temp if $temp;
      }
      $$transport{$key} = &getAnswer( "$key ", $$transport{$key} ? $$transport{$key} : '' );
   }
   return ( $priority, $transport );
}

sub showAllTransports {
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
      print '='x40 . "\n";
      &showTransports( $priority, $$transports{ $priority } );
      print '='x40 . "\n";
   }
}   

sub showTransports {
   my ( $priority,$thisOne )  = @_;
   print $$thisOne{'name'} . " has priority $priority\n";
   foreach my $key ( sort keys %$thisOne ) {
      next if $key =~ m/^-.*-$/;
      print "$key = $$thisOne{$key}\n";
   }
}

sub doTransports {
   my ( $transports ) = @_;
   
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
      if ( &yesno( $$transports{$priority}{'name'} . " has a priority of $priority, edit it?") ) {
         #print Dumper( $sendTypes{$$transports{$priority}{'name'}} );
         #die;
         my ( $newpriority,$temp ) = &setUpTransport( $priority, $$transports{$priority}, $sendTypes{$$transports{$priority}{'name'}} );
         if ( $newpriority != $priority ) {
            delete $$transports{$priority};
            $priority = $newpriority;
         }
         $$transports{$priority} = $temp;
      } # if
   }

   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
      print '='x40 . "\n";
      &showTransports( $priority, $$transports{ $priority } );
      print '='x40 . "\n";
   }

   while ( &yesno( "Would you like to add any other transport mechanisms?" ) ) {
      my $newType = &getAnswer( 'Type of Transport? ', keys %sendTypes );
      my ( $priority,$temp ) = &setUpTransport( 99, {}, $sendTypes{$newType}, $newType );
      $$transports{$priority} = $temp;
   }

   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
      print '='x40 . "\n";
      &showTransports( $priority, $$transports{ $priority } );
      print '='x40 . "\n";
   }
}

sub showEntry {
   my ( $lineno, $prompt, $value ) = @_;
   $value = '' unless $value;
   my $valueType = ref( $value );
   if ( $valueType eq 'ARRAY' ) {
      $value = join( ', ', @$value );
   } elsif ( $valueType eq  'HASH' ) {
      $value = ( scalar keys %$value ) . " Entries";
   }
   return "$lineno. $prompt: $value\n";
}

sub displayEditScreen {
   my $config = shift;
#   print "\033[2J";    #clear the screen
#   print "\033[0;0H"; #jump to 0,0  
   foreach my $entry ( sort keys %displayOrder ) {
      print &showEntry( 
         $entry, 
         $displayOrder{$entry}{'display'}, 
         $config->{$displayOrder{$entry}{'fieldname'}} 
      );
   }
   print "\nSelect Option to Edit, or 'Done' if finished [Done]: ";
   my $selection = <>;
   chomp $selection;
   $selection = 'd' unless $selection;
   return lc substr( $selection, 0, 1 );
}

sub getValue {
   my ( $prompt, $value ) = @_;
   $value = '' unless $value;
   print "$prompt [$value]: ";
   my $t = <>;
   chomp $t;
   return $t ? $t : $value;
}

sub editSelection {
   my $entry = shift;
   my $type = ref( $config->{$displayOrder{$entry}{'fieldname'}} );
   print "Type is $type\n";
   if ( $type eq 'ARRAY' ) {
      print "Don't know how to edit arrays yet\n";
   } elsif ( $type eq 'HASH' ) {
      print "Don't know how to edit transports yet\n";
   } else { # it is a scalar
      $config->{$displayOrder{$entry}{'fieldname'}} = &getValue( 
         $displayOrder{$entry}{'display'}, 
         $config->{$displayOrder{$entry}{'fieldname'}}
         );
   }
}


# will validate the config, adding modules, scripts and transports if necessary
sub testConfig {
   my $config = shift;
   my @return;
   push @return, 'No client name given' unless $config->{'clientName'};
   push @return, 'No host name given' unless $config->{'hostname'};
   $config->{'serialNumber'} = $config->{'UUID'} if $config->{'serialNumber'} eq 'NotSpecified';
   push @return, 'No serial number given' unless $config->{'serialNumber'};
   push @return, 'No UUID given' unless $config->{'UUID'};
   # if scriptDirs not defined, use installdir/scripts
   $config->{'scriptDirs'} = [ $binDir . '/scripts' ] unless @{$config->{'scriptDirs'}};
   # if moduleDirs not defined, use installdir/modules
   $config->{'moduleDirs'} = [ $binDir . '/modules' ] unless defined $config->{'moduleDirs'};

   # ensure we have the default SaveLocal transport defined
   unless ( defined $config->{'transports'}{'99'} ) {
      $config->{'transports'}{'99'} = {
                         'name'=> 'SaveLocal',
                         'output directory' => '/tmp',
                         'sendScript' => 'save_local'
                        };
   }
   
   return @return ? join ( "\n", @return ) . "\n" : 'Ok';
}

sub help {
   print "$0 version $VERSION\n";
   print "Reads one or more sysinfo configuration file and\nallows user to edit the values\n\n";
   print "$0 --verbose --file=/full/path/to/input -output=/full/path/to/sysinfo-client.yaml\n";
   print "   --verbose - may be entered more than one time to increase verbosity\n";
   print "   --file     - may be entered multiple times and may contain a comma delimited group of files\n";
   print "   --output   - file which will be written to\n\n";
   print "   --test     - test the config file and report errors\n";
   print "   --dryrun   - do not save result\n";
   print "   --quiet    - do not ask any questions, automatically check, repair and write to output\n";
   print "   --help     -  display this help screen\n\n";

   print "All parameters are optional\n";
   print "Will search common locations for sysinfo-client.yaml and, if found, append this to the end\n";
   print "of the input file list.\n";
   print "If no --output is passed, will write to the last --file passed or the file which was found\n";
   print "in the search\n";
   print "All parameters accept the short flag, ie -v, -f, -o and -h\n";
   exit;
}


my $confFileName;
my $outputFile;
my $verbose;
my $help;
my $version;
my $test = 0;
my $dryRun = 0;
my $quiet = 0;

# handle any command line parameters that may have been passed in
# verbose is incremental, so for example, -vv would give more info than -v
# there can be multiple files passed, and an entry can have a comma
# separated list. The files will be processed in order, with the last
# one passed in being the one that overrides everything.
GetOptions (
            "verbose|v+"  => \$verbose, # verbosity level, 0-9
            'file|f=s'    => \$confFileName, # file name to use
            'output|o=s'  => \$outputFile, # file to save to
            'test|t'      => \$test, # test the config file
            'dryrun|n'    => \$dryRun, # do not save result
            'quiet|q'     => \$quiet, # do not ask any questions
            'help|h'      => \$help
            ) or die "Error parsing command line\n";
                  
if ( $help ) { &help() ; exit; }

# look for the standard configuration file in the standard place
unless ( $confFileName ) {
   ( $confDir, $sysinfo3 ) = &findConf( $sysinfo3 );
   $outputFile = $confDir . '/' . $sysinfo3 unless $outputFile;
   $confFileName =  $confDir . "/" . $sysinfo3 if ( $confDir );
}
print "Loading defaults from existing config\n";
$config = &makeConfig( $outputFile, $confFileName  );



my $selection;
while ( ! $quiet && ( $selection = &displayEditScreen( $config ) ) ne 'd' ) {
   &editSelection( $selection );
}

#die Dumper($config) . "\n";

my $testResults = &testConfig($config);
print "Error found in configuration file, manually run\n$0\n" if ( $testResults ne 'Ok' );
print $testResults if $test || $testResults ne 'Ok';

if ( $quiet || &yesno( "Ready to write configuration to $outputFile, ok?" ) ) {
   print "Config File written to $outputFile\n";
   &writeConfig( $outputFile, &showConf( $config ) ) unless $dryRun;
}

#use Data::Dumper;
#print Dumper( $config ); die;

#&doTransports( $$config{'transports'} );


1;