#! /usr/bin/env perl use strict; use warnings; # find our location and use it for searching for libraries BEGIN { use FindBin; use File::Spec; # use libraries from the directory this script is in use lib File::Spec->catdir($FindBin::Bin); # and its parent use lib File::Spec->catdir( $FindBin::Bin . '/../' ); eval( 'use YAML::Tiny' ); } use Cwd qw(abs_path); my $scriptDir = abs_path(File::Spec->catdir($FindBin::Bin) ); use Digest::MD5 qw(md5_hex); use File::Copy; # define the version number # see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod use version; our $VERSION = version->declare("v0.001.001"); use Data::Dumper; use File::Basename; use Getopt::Long; # load the definitions from installer_config.pl do "$scriptDir/installer_config.pl"; our %libraries; our %install; our %binaries; use sysinfosetup; Getopt::Long::Configure ("bundling"); # allow -vd --os='debian' our $dryRun = 0; my $os; my $help = 0; my $version = 0; my $quiet = 0; our $DEBUG = 0; my %commands; # we will use this to track commands that are to be run # simple display if --help is passed sub help { use File::Basename; print basename($0) . " $VERSION\n"; print <{$lib}->{$os} ? $libraries->{$lib}->{$os} : 'UNK'; push @{$commands->{$libraries->{$lib}->{$os}->{'command'}}}, $libraries->{$lib}->{$os}->{'parameter'} } } return \%return; } # validateLibraries # check for any required binaries sub validateBinaries { my ( $binaries, $os, $commands ) = @_; &logIt( 'Entering validateBinaries' ); my %return; foreach my $bin ( keys %$binaries ) { unless ( `which $bin` ) { print "Needing $bin\n" if $DEBUG; $return{$bin} = $binaries->{$bin}->{$os} ? $binaries->{$bin}->{$os} : 'UNK'; push @{$commands->{$binaries->{$bin}->{$os}->{'command'}}}, $binaries->{$bin}->{$os}->{'parameter'} } } return \%return; } # validateBinaries ################################################################ # validateCPAN # # some of the systems will need to run cpan to get some perl modules. # this will go through each of them and see if command starts with cpan # and, if so, will check that cpan is installed and configured for root. # # when cpan is installed, it requires one manual run as root from the cli # to determine where to get the files. If that is not done, cpan can not # be controlled by a program. We check to see if cpan is installed, then # verify /root/.cpan has been created by the configuration tool ################################################################ sub validateCPAN { my $libraries = shift; my $needCPAN = 0; my $message = ''; foreach my $app ( keys %$libraries ) { if ( $libraries->{$app}->{'command'} =~ m/^cpan/ ) { $needCPAN = 1; last; } } return $message unless $needCPAN; if ( `which cpan` ) { $message = "****ERROR****\nWe need cpan, and it is installed, but not configured\nrun cpan as root one time to configure it\n" unless -d '/root/.cpan'; } else { $message = 'In order to install on this OS, we need cpan, which should have been installed with perl.' . " Can not continue until cpan is installed and configured\n"; } return $message; } ################################################################ # Main Code # ################################################################ # handle any command line parameters that may have been passed in GetOptions ( "os|o=s" => \$os, # pass in the operating system "dryrun|n" => \$dryRun, # do NOT actually do anything 'help|h' => \$help, 'version|v' => \$version, 'quiet|q' => \$quiet, 'debug!' => \$DEBUG ) or die "Error parsing command line\n"; if ( $help ) { &help() ; exit; } if ( $version ) { use File::Basename; print basename($0) . " $VERSION\n"; exit; } die "Can not continue without operating system parameter (--os=)\n" unless $os; &logIt( "Inside validateDependencies" ); print "Inside validateDependencies\n"; # check libraries necessary are loaded, and if not, track the ones we need $install{'missing libraries'} = &validateLibraries( \%libraries, $os, \%commands ); &logIt( "Missing Libraries\n" . Dumper( $install{'missing libraries'} ) ); # Check that required binaries are installed and create list of the ones we need $install{'missing binaries'} = &validateBinaries( \%binaries, $os, \%commands ); &logIt( "Missing binaries\n" . Dumper( $install{'missing binaries'} ) ); # if we need libraries and the os needs to use CPAN, validate CPAN my $cpanMessage = &validateCPAN( $install{'missing libraries'} ) if ( $install{'missing libraries'} ); if ( %commands ) { print "We need the following libraries\n" . join( "\n", keys( %{ $install{'missing libraries'} } ) ) . "\n" if $install{'missing libraries'}; print "We need the following binaries\n" . join( "\n", keys( %{ $install{'missing binaries'} } ) ) . "\n" if $install{'missing binaries'}; print "I can install them with the following command(s)\n"; my @commandList; foreach my $command ( keys %commands ) { push @commandList, $command . ' ' . join( ' ', @{$commands{$command}} ); } print join( "\n", @commandList ) . "\n"; if ( $cpanMessage ) { print "However, $cpanMessage\nExiting\n"; return 2; } if ( $quiet || &yesno( "Do you want me to run these commands?" ) ) { for ( my $i = 0; $i < @commandList; $i++ ) { &runCommand( $commandList[$i] ) || die "Error executing $commandList[$i]\n"; } } } 1;