Rev 120 | Blame | Last modification | View Log | Download | RSS feed
#! /usr/bin/env perl
use strict;
use warnings;
# install.pl
#
# installer for perl script, in this case, sysinfo
#
# Revision history
#
# Version 1.1.7 20161010 RWR
# Added ability to validate required libraries are installed
#
# version 1.2 20170327 RWR
# did some major modifications to correctly work on BSD systems also
#
# version 2.0 20190330 RWR
# changed it so all configs are YAML
our $VERSION = '2.0';
# 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 YAML::Tiny;
use Data::Dumper;
use File::Basename;
use Getopt::Long;
Getopt::Long::Configure ("bundling"); # allow -vd --os='debian'
# $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 @messages; # stores any messages we want to show up at the end
my @feedback; # store feedback when we execute command line actions
my %install = ( 'bindir' => '/opt/camp/sysinfo-client',
'confdir' => '/etc/camp/sysinfo-client',
'application name' => 'sysinfo client',
'default group' => 'root',
'default owner' => 'root',
'default permission' => '0700',
'configuration' => {
'configurator' => '<bindir>/configure.pl',
'configuration file' => '<confdir>/sysinfo-client.yaml',
'configuration seed file' => 'sysinfo-client.seed.yaml',
'old configuration file' => '<confdir>/sysinfo-client.conf',
'permission' => '700',
'owner' => '<default owner>',
'target' => '<confdir>'
},
'files' => {
'configure.pl' => {
'type' => 'file',
'permission' => '700',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>'
},
'sysinfo-client' => {
'type' => 'file',
'permission' => '700',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>'
},
'sysinfoconf.pm' => {
'type' => 'file',
'permission' => '600',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>'
},
'notes' => {
'type' => 'file',
'permission' => '600',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>'
},
'sysinfo-client.conf.template.yaml' => {
'type' => 'file',
'permission' => '600',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>'
},
'getSendEmail.pl' => {
'type' => 'file',
'permission' => '700',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>'
},
'install.pl' => {
'type' => 'file',
'permission' => '700',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>'
},
'sysinfo-client.seed.example.yaml' => {
'type' => 'file',
'permission' => '600',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>'
},
'VERSION' => {
'type' => 'file',
'permission' => '600',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>'
},
'modules' => {
'type' => 'directory',
'permission' => '700',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>',
'action' => 'chmod 700 *'
},
'scripts' => {
'type' => 'directory',
'permission' => '700',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>',
'action' => 'chmod 700 *'
},
}
);
# hash to set up os specific rules
my %operatingSystems = (
'debian' => {
'regex' => '(debian|mx|devuan)',
'bindir' => '/opt/camp/sysinfo-client',
'confdir' => '/etc/camp/sysinfo-client',
'crontab' => 'ln -s <bindir>/sysinfo-client /etc/cron.daily/sysinfo-client',
'modules' => '((linux)|(dpkg)|(unix)|(all))',
},
'ipfire' => {
'regex' => '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)|(all))',
},
'freebsd' => {
'regex' => 'freebsd',
'bindir' => '/usr/local/opt/camp/sysinfo-client',
'confdir' => '/usr/local/etc/camp/sysinfo-client',
'crontab' => 'ln -s <bindir>/sysinfo-client /etc/periodic/daily/sysinfo-client',
'modules' => '((bsd)|(unix)|(all))',
'default group' => 'wheel',
'default owner' => 'root',
},
'opnsense' => {
'fileexists' => '/conf/config.xml',
'bindir' => '/usr/local/opt/camp/sysinfo-client',
'confdir' => '/usr/local/etc/camp/sysinfo-client',
'crontab' => 'ln -s <bindir>/sysinfo-client /etc/periodic/daily/sysinfo-client',
'modules' => '((bsd)|(unix)|(all))',
'default group' => 'wheel',
'default owner' => 'root',
'files' => {
'YAML' => {
'type' => 'directory',
'permission' => '777',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>'
},
'Tiny.pm' => {
'type' => 'file',
'permission' => '444',
'owner' => '<default owner>:<default group>',
'target' => '<bindir>/YAML'
},
'actions_sysinfo.conf' => {
'type' => 'file',
'permission' => '755',
'owner' => '<default owner>:<default group>',
'target' => '/usr/local/opnsense/service/conf/actions.d',
'post action' => 'service configd restart',
'message' => 'No automatic run can be done. Please log in through the webui and enable the sysinfo cron job via System | Settings | Cron'
},
},
},
);
my %configuration;
# list of libraries used by the system. We will offer to install them if
# we know how. NOTE: I have chosen to put the full installation command
# for each library. This allows us to use the package selector OR a different
# piece of code on a per-library basis, but results in something like
# apt-get -y install perl-modules
# apt-get -y install libwww-perl
# instead of
# apt-get -y install libwww-perl perl-modules
# flexibility vs efficiency in this case.
my %libraries = (
'File::Basename' => { 'debian' => 'apt-get -y install perl-modules' },
'Exporter' => { 'debian' => 'apt-get -y install perl-base' },
'LWP' => {
'debian' => 'apt-get -y install libwww-perl',
'freebsd' => 'pkg install -y p5-libwww',
'opnsense' => 'pkg install -y p5-libwww',
},
);
# utilities md5sum
# freebsd isomd5sum
# validates the libraries needed exist
# simply eval's each library. If it doesn't exist, creates a list of
# commands to be executed to install it, and displays missing libraries
# offering to install them if possible.
sub validateLibraries {
my ( $libraries, $os ) = @_;
my @command;
my @missingLibs;
foreach my $lib ( keys %$libraries ) {
print "Checking on library $lib\n";
eval( "use $lib;" );
if ( $@ ) {
print "\tNot found, adding\n";
push @command,$$libraries{$lib}{$os} if $$libraries{$lib}{$os};
push @missingLibs, $lib;
}
}
if ( @missingLibs ) { # we have missing libraries
if ( @command ) {
&runCommand( join( "\n", @command ) )
if &yesno(
'Following libraries need to be installed: ' .
join( ' ', @missingLibs ) . "\n" .
"I can install them with the following command(s)\n" .
join( "\n", @command ) . "\nDo you want me to do this?\n"
);
} else {
die unless &yesno( 'Following libraries need to be installed: ' .
join( ' ', @missingLibs ) .
"\nand I don't know how to do this. Abort?" );
}
} # if
} # validateLibraries
# attempt to locate the operating system.
# if found, will set some defaults for it.
sub setUpOperatingSystemSpecific {
my ( $install, $operatingSystems, $os ) = @_;
print "They passed $os in as the \$os\n" if $verbose > 2;
if ( $os ) {
# We found the OS, set up some defaults
$$install{'os'} = $os;
print "Setting keys for operating system\n" if $verbose > 2;
# merge operatingSystems into install
foreach my $key ( keys %{$operatingSystems->{ $os }} ) {
if ( $key eq 'files' ) {
$install->{'files'} = { %{$install->{'files'}}, %{$operatingSystems->{$os}->{'files'}} }
} else {
$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 Dump( \%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'},
'<default owner>' => $$install{'default owner'},
'<default group>' => $$install{'default group'},
'<default permission>' => $$install{'default permission'}
);
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 Dump( $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'} . '/', $$install{'default permission'}, "$$install{'default owner'}:$$install{'default group'}" );
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'}, $$install{'default permission'}, "$$install{'default owner'}:$$install{'default group'}" );
&runCommand(
"cp $$fileList{$file}{'source'} $$fileList{$file}{'target'}",
"chmod $$fileList{$file}{'permission'} $$fileList{$file}{'target'}",
"chown $$fileList{$file}{'owner'} $$fileList{$file}{'target'}"
);
# if there is a post action, run it and store any return in @feedback
push @feedback, `$fileList->{$file}->{'post action'}` if defined $fileList->{$file}->{'post action'};
# if there is a message to be passed, store it in @messages
push @messages, $fileList->{$file}->{'meesage'} if defined $fileList->{$file}->{'message'};
} # 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 @fileList;
# the order is important here as, if multiple files exist, latter ones can
# overwrite values in the previous. We do a push so the first value pushed
# is processed last, ie has higher priority.
push @fileList, $install->{'configuration'}->{'old configuration file'};
push @fileList, $install->{'configuration'}->{'configuration file'};
my $seedFile = $install->{'configuration'}->{'configuration seed file'};
if ( -e $seedFile && &yesno( 'Add installation seed file? ' ) ) {
push @fileList, $seedFile;
} # if preload seed file
$config = &makeConfig( @fileList );
# if ScriptDirs and moduleDirs not populated, do so from our configuration
if ( ! $$config{'scriptDirs'} || ! scalar @{ $$config{'scriptDirs'} } ) {
# my @temp = ( $$install{'files'}{'scripts'}{'target'} );
# $$config{'scriptDirs'} = \@temp;
$config->{'scriptDirs'} = [ $install->{'files'}->{'scripts'}->{'target'} ];
}
if ( ! $$config{'moduleDirs'} || ! @{ $$config{'moduleDirs'} } ) {
$config->{'moduleDirs'} = [ $install->{'files'}->{'modules'}->{'target'} ];
# $$config{'moduleDirs'} = [ $$install{'files'}{'modules'}{'target'} ];
}
# print Dumper ($config ) ; die;
my $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
if ( @feedback ) {
print "We got some messages while doing the install, please review below\n" . join( "\n", @feedback ) . "\n";
}
if ( @messages ) {
print "\n\nFollowing are some important messages for your installation\n" . join( "\n", @messages ) . "\n";
}
}
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
$os = `$sourceDir/determineOS` unless $os;
# figure out if we know our operating system and set up special stuff for it
$os = &setUpOperatingSystemSpecific( \%install, \%operatingSystems, $os );
#$os = &setUpOperatingSystemSpecific( \%install, \%operatingSystems, 'freebsd' );
#print Dumper( \%install ); die;
&validateLibraries( \%libraries, $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." );
#my $config = &makeConfig( $install{'configuration'}{'configuration seed file'}, $install{'configuration'}{'configuration file'}, $install{'configuration'}{'old configuration file'} );
#print Dumper( $config );
#die;
$status = &doInstall( \%install );
$status = &postInstall( \%install );
# create uninstaller script
# find the last non-space string in the crontab value. This is the target of the link
$install{'crontab'} =~ m/([^ ]+)$/;
my $uninstall = "#! /usr/bin/env sh\n\n# Uninstall syinfo\nrm -fR $install{'bindir'} $1\n";
my $outFileName = $install{'bindir'} . '/uninstall';
open UNINSTALL, ">$outFileName" or die "could not write to $outFileName: $!\n";
print UNINSTALL $uninstall;
close UNINSTALL;
qx ( chmod 700 $outFileName );
print "Uninstall script has been created at $outFileName\n";
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;
# 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.