Subversion Repositories camp_sysinfo_client_3

Rev

Rev 39 | Blame | Last modification | View Log | RSS feed

#! /usr/bin/env perl

use strict;
use warnings;

our $VERSION = '1.1.5';

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

use sysinfoconf;
use File::Basename;
use Getopt::Long;
Getopt::Long::Configure ("bundling"); # allow -vd --os='debian'

use Data::Dumper;

# $verbose can have the following values
# 0 - do everything
# 1 - Do everything except the install, display what would be done
# 2 - Be verbose to STDERR
# 3 - Be very verbose
my $verbose = 0; # if test mode, simply show what would be done
my $dryRun = 0;
my $os;
my $help = 0;
my $version = 0;

my $status; # exit status of the processes
my $sourceDir = File::Spec->catdir($FindBin::Bin);
my $installType;

my %install = (  'bindir' => '/opt/camp/sysinfo-client',
                 'confdir' => '/etc/camp/sysinfo-client',
                 'application name' => 'sysinfo client',
                 'configuration' => {
                          'configurator' => '<bindir>/configure.pl',
                          'configuration file' => '<confdir>/sysinfo-client.conf',
                          'configuration seed file' => 'sysinfo-client.seed',
                          'permission' => '700',
                          'owner'      => 'root',
                          'target'     => '<confdir>'
                            },
                 'files' => {
                           'configure.pl' => { 
                                 'type' => 'file',
                                 'permission' => '700', 
                                 'owner' => 'root:root', 
                                 'target' =>  '<bindir>'
                              },
                           'sysinfo-client' => { 
                                 'type' => 'file',
                                 'permission' => '700',
                                 'owner' => 'root:root',
                                 'target' =>  '<bindir>'
                              },
                           'sysinfoconf.pm' => {
                                 'type' => 'file',
                                 'permission' => '600',
                                 'owner' => 'root:root',
                                 'target' =>  '<bindir>'
                              },
                           'notes' => { 
                                 'type' => 'file',
                                 'permission' => '600', 
                                 'owner' => 'root:root', 
                                 'target' =>  '<bindir>'
                              },
                           'sysinfo-client.conf.template' => { 
                                 'type' => 'file',
                                 'permission' => '600', 
                                 'owner' => 'root:root', 
                                 'target' =>  '<bindir>' 
                              },
                           'getSendEmail.pl' => { 
                                 'type' => 'file',
                                 'permission' => '700', 
                                 'owner' => 'root:root', 
                                 'target' =>  '<bindir>' 
                              },
                           'install.pl' => {
                                 'type' => 'file',
                                 'permission' => '700', 
                                 'owner' => 'root:root', 
                                 'target' =>  '<bindir>' 
                              },
                           'MANIFEST' => {
                                 'type' => 'file',
                                 'permission' => '600', 
                                 'owner' => 'root:root', 
                                 'target' =>  '<bindir>' 
                              },
                           'sysinfo-client.seed.example' => { 
                                 'type' => 'file',
                                 'permission' => '600', 
                                 'owner' => 'root:root', 
                                 'target' =>  '<bindir>' 
                              },
                           'VERSION' => { 
                                 'type' => 'file',
                                 'permission' => '600', 
                                 'owner' => 'root:root', 
                                 'target' =>  '<bindir>' 
                              },
                              'modules' => {
                                 'type' => 'directory',
                                 'permission' => '700', 
                                 'owner' => 'root:root', 
                                 'target' =>  '<bindir>',
                                 'action' => 'chmod 700 *'
                              },
                              'scripts' => {
                                 'type' => 'directory',
                                 'permission' => '700', 
                                 'owner' => 'root:root', 
                                 'target' =>  '<bindir>',
                                 'action' => 'chmod 700 *'
                              },
                     }
                  );

# hash to set up os specific rules                  
my %operatingSystems = (
                  'debian' => {
                     'bindir' => '/opt/camp/sysinfo-client',
                     'confdir' => '/etc/camp/sysinfo-client',
                     'crontab' => 'ln -s <bindir>/sysinfo-client /etc/cron.daily/sysinfo-client',
                     'modules' => '((dpkg)|(unix)|(ipmi)|(xen))',
                  },
                  'ipfire' => {
                     'bindir' => '/opt/camp/sysinfo-client',
                     'confdir' => '/etc/camp/sysinfo-client',
                     'crontab' => 'ln -s <bindir>sysinfo-client /etc/fcron.daily/sysinfo-client.fcron',
                     'modules' => '((ipfire)|(unix))',
                  }
                  );
                  

# attempt to locate the operating system.
# if found, will set some defaults for it.
sub getOS {
   my ( $install, $operatingSystems, $os ) = @_;
   if ( ! $os ) { # we don't know, so we must try to figure it out
      my $osString = `uname -a`;
      foreach my $osType ( keys %$operatingSystems ) {
         print "Checking if OS is $osType in $osString\n" if $verbose > 2;
         next unless $osString =~ m/$osType/i;
         print "Yes, it is $osType\n" if $verbose > 2;
         # We found the OS, set up some defaults
         $os = $osType;
      } # foreach
   }
   if ( $os ) {
      $$install{'os'} = $os;
      print "Setting keys for operating system\n" if $verbose > 2;
      foreach my $key ( keys $$operatingSystems{ $os } ) {
         $$install{$key} = $operatingSystems{ $os }{$key};
      } # if it is a known OS
   } # if
   return $os;
} # getOperatingSystem


# get some input from the user and decide how to install/upgrade/remove/whatever
sub getInstallActions {
   my $install = shift;
   if ( ! &yesno( "This looks like a $$install{'os'} machine, correct?" ) ) {
      die "User Aborted\n" if &yesno( "If we continue, I will set this up like a $$install{'os'} system. Abort?" );
   }
   if ( -d $$install{'confdir'} ) {
      $$install{'action'} = &getAnswer( "It looks like $$install{'application name'} is already installed, what do you want to do?", 
                            ( "upgrade","remove", "overwrite" )
                          );
   } else {
      if ( &yesno( "This looks like a fresh install, correct?" ) ) {
         $$install{'action'} = 'install';
         $$install{'preseed config'} = &yesno( "Preseed the configuration file?" );
      } else {
         die "Can not continue at this time: Do not understand your system\n";
      }
   }
   $$install{'build config'} = &yesno( "Edit config file when done?" );
   $$install{'setup cron'} = &yesno( "Set up for automatic running via crontab?" );
}

sub showWork { 
   my $install = shift;
   print Dumper( \%install );
}


sub doPlaceholderSubstitution {
   my ($hash, $placeholder) = @_;
   return if ref $hash ne 'HASH';
   foreach my $key ( keys %$hash ) {
      if ( ref( $$hash{$key} ) ) {
         &doPlaceholderSubstitution( $$hash{$key}, $placeholder );
      } else {
         foreach my $place ( keys %$placeholder ) {
            $$hash{$key} =~ s/$place/$$placeholder{$place}/;
         } # foreach
      } # if..else
   } # foreach
   return;
}
   
# This will go through and first, see if anything is a directory, in
# which case, we'll create new entries for all files in there.
# then, it will do keyword substitution of <bindir> and <confdir>
# to populate the target.
# When this is done, each file should have a source and target that is
# a fully qualified path and filename
sub populateSourceDir {
   my ( $install, $sourceDir ) = @_;
   my %placeHolders = 
      ( 
        '<bindir>' => $$install{'bindir'},
        '<confdir>' => $$install{'confdir'}
      );

   my $allFiles = $$install{'files'};

   # find all directory entries and load files in that directory into $$install{'files'}
   foreach my $dir ( keys %$allFiles ) {
      if ( defined( $$allFiles{$dir}{'type'} ) && $$allFiles{$dir}{'type'} eq 'directory' ) {
         print "Found directory $dir\n" if $verbose > 2;
         if ( opendir( my $dh, "$sourceDir/$dir" ) ) {
            my @files = map{ $dir . '/' . $_ } grep { ! /^\./ && -f "$sourceDir/$dir/$_" } readdir( $dh );
            print "\tFound files " . join( ' ', @files ) . "\n" if $verbose > 2;
            foreach my $file ( @files ) {
               $$allFiles{ $file }{'type'} = 'file';
               if ( $dir eq 'modules' ) {
                  $$allFiles{ $file }{'permission'} = ( $file =~ m/$$install{'modules'}/ ) ? '0700' : '0600';
               } else {
                  $$allFiles{ $file }{'permission'} = $$allFiles{ $dir }{'permission'};
               }
               $$allFiles{ $file }{'owner'} = $$allFiles{ $dir }{'owner'};
               $$allFiles{ $file }{'target'} = $$allFiles{ $dir }{'target'};
            } # foreach
            closedir $dh;
         } # if opendir
      } # if it is a directory
   } # foreach
   # find all files, and set the source directory, and add the filename to
   # the target
   foreach my $file ( keys %$allFiles ) {
      $$allFiles{$file}{'source'} = "$sourceDir/$file";
      $$allFiles{$file}{'target'} .= "/$file";
   } # foreach

   # finally, do place holder substitution. This recursively replaces all keys
   # in  %placeHolders with the values.
   &doPlaceholderSubstitution( $install, \%placeHolders );

   print Dumper( $install ) if $verbose > 2;

   return 1;
} # populateSourceDir

# there is a file named VERSIONS. We get the values out of the install
# directory and (if it exists) the target so we can decide what needs
# to be updated.
sub getVersions {
   my $install = shift;
   my $currentVersionFile = $$install{'files'}{'VERSION'}{'target'};
   my $newVersionFile = $$install{'files'}{'VERSION'}{'source'};
   if ( open FILE,"<$currentVersionFile" ) {
      while ( my $line = <FILE> ) {
         chomp $line;
         my ( $filename, $version, $checksum ) = split( "\t", $line );
         $$install{'files'}{$filename}{'installed version'} = $version ? $version : '';
      }
      close FILE;
   }
   if ( open FILE,"<$newVersionFile" ) {
      while ( my $line = <FILE> ) {
         chomp $line;
         my ( $filename, $version, $checksum ) = split( "\t", $line );
         $$install{'files'}{$filename}{'new version'} = $version ? $version : '';
      }
      close FILE;
   }
   foreach my $file ( keys %{$$install{'files'}} ) {
      $$install{'files'}{$file}{'installed version'} = -2 unless defined $$install{'files'}{$file}{'installed version'};
      $$install{'files'}{$file}{'new version'} = -1 unless defined $$install{'files'}{$file}{'new version'};
   }
   return 1;
} # getVersions

# this actually does the installation, except for the configuration
sub doInstall {
   my $install = shift;
   my $fileList = $$install{'files'};
   
   &checkDirectoryExists( $$install{'bindir'} . '/' );
   foreach my $file ( keys %$fileList ) {
      next unless ( $$fileList{$file}{'type'} && $$fileList{$file}{'type'} eq 'file' );
      next if $$install{'action'} eq 'upgrade' && ! defined( $$fileList{$file}{'installed version'} )
              ||
              ( $$fileList{$file}{'new version'} eq $$fileList{$file}{'installed version'} );
      &checkDirectoryExists( $$fileList{$file}{'target'} );
      &runCommand( 
            "cp $$fileList{$file}{'source'} $$fileList{$file}{'target'}",
            "chmod $$fileList{$file}{'permission'} $$fileList{$file}{'target'}",
            "chown  $$fileList{$file}{'owner'} $$fileList{$file}{'target'}"
            );
   } # foreach file
   return 1;
}

sub postInstall {
   my $install = shift;

   # set up crontab, if necessary
   &runCommand( $$install{'crontab'} ) if defined ( $$install{'crontab'} );
   
   # seed configuration, if needed
   if ( $$install{'build config'} ) {
      my %config;
      my $seedFile = $$install{'configuration'}{'configuration seed file'};
      my $confFile = $$install{'configuration'}{'configuration file'};
      my $content;

      my $clientName;
      my $serialNumber;
      my $hostname;
      my @moduleDirs;
      my @scriptDirs;
      my $transports = {};

      
      if ( -f $seedFile  && &yesno( 'Add installation seed file? ' ) ) {
         print "Loading seed file $seedFile\n";
         open SEED, "<$seedFile" or die "Could not open $seedFile: $!\n";
         $content = join( '', <SEED> );
         close SEED;
         # now, eval the information we just read.
         # NOTE: we must turn off strict while doing this, and we die if something breaks.
         no strict "vars"; eval( $content ); use strict "vars"; die "Error during eval: $@\n" if $@;
      } # if preload seed file

      if (  -f $confFile  ) {
         print "Loading configuration file $confFile\n";
         open SEED, "<$confFile" or die "Could not open $confFile: $!\n";
         $content = join( '', <SEED> );
         close SEED;
         # now, eval the information we just read.
         # NOTE: we must turn off strict while doing this, and we die if something breaks.
         no strict "vars"; eval( $content ); use strict "vars"; die "Error during eval: $@\n" if $@;
      }
      $config{'clientName'} = $clientName;
      $config{'serialNumber'} = $serialNumber;
      $config{'hostname'} = $hostname;
      $config{'moduleDirs'} = [ @moduleDirs ];
      $config{'scriptDirs'} = [ @scriptDirs ];
      $config{'transports'} = $transports;

      $content = &showConf( \%config );
      print $content;
      print &writeConfig( $$install{'configuration'}{'configuration file'} , $content ) . "\n"
         if ( &yesno( "Write the above configuration to $$install{'configuration'}{'configuration file'}?" ) );
   } # if we are building/merging configuration
   
}

sub help {
   my $oses = join( ' ', keys %operatingSystems );
   print <<END
$0 --verbose=x --os="osname" --dryrun --help --version

--os      - osname is one of [$oses]
--dryrun  - do not actually do anything, just tell you what I'd do
--verbose - x is 0 (normal) to 3 (horrible)
END
}



##########################################
# Main Loop
##########################################

# handle any command line parameters that may have been passed in

GetOptions (
            "verbose|v=i" => \$verbose, # verbosity level, 0-9
            "os|o=s"      => \$os,      # pass in the operating system
            "dryrun|n"    => \$dryRun,  # do NOT actually do anything
            'help|h'      => \$help,
            'version|V'   => \$version
            ) or die "Error parsing command line\n";
                  
if ( $help ) { &help() ; exit; }
if ( $version ) { print "$0 version $VERSION\n"; exit; }
&setDryRun( $dryRun ); # tell the library whether this is a dry run or not

# figure out if we know our operating system
$os = &getOS( \%install, \%operatingSystems, $os );

$installType = &getInstallActions( \%install );

# based on the defaults, flesh out the install hash
$status = &populateSourceDir( \%install, $sourceDir );


$status = &getVersions( \%install );

&showWork( \%install );
die unless &yesno( "Ready to run? Select No to abort." );

$status = &doInstall( \%install );

$status = &postInstall( \%install );

if ( ( -x $install{'configuration'}{'configurator'} ) && $install{'build config'} ) {
   exec( $install{'configuration'}{'configurator'} );
} else {
   print "Done, you should check the files in $install{'bindir'} and $install{'confdir'} before running\n";
   print "If you need help configuring, the helper app at\n$install{'configuration'}{'configurator'}\ncan be used.\n";
}

1;   


# add uninstall, clean to install.pl
# clean will look for any file in bindir which is NOT in the list of available files and remove them
# if files already exist in install, preserve their permissions

Generated by GNU Enscript 1.6.5.90.