| 224 | rodolico | 1 | #! /usr/bin/env perl
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | use strict;
 | 
        
           |  |  | 4 | use warnings;
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 | # find our location and use it for searching for libraries
 | 
        
           |  |  | 7 | BEGIN {
 | 
        
           |  |  | 8 |    use FindBin;
 | 
        
           |  |  | 9 |    use File::Spec;
 | 
        
           |  |  | 10 |    # use libraries from the directory this script is in
 | 
        
           |  |  | 11 |    use lib File::Spec->catdir($FindBin::Bin);
 | 
        
           |  |  | 12 |    # and its parent
 | 
        
           |  |  | 13 |    use lib File::Spec->catdir( $FindBin::Bin . '/../' );
 | 
        
           |  |  | 14 |    eval( 'use YAML::Tiny' );
 | 
        
           |  |  | 15 | }
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | use Cwd qw(abs_path);
 | 
        
           |  |  | 18 | use sysinfosetup;
 | 
        
           |  |  | 19 |   | 
        
           |  |  | 20 | my $scriptDir = abs_path(File::Spec->catdir($FindBin::Bin) );
 | 
        
           | 225 | rodolico | 21 | my $binDir = abs_path("$scriptDir/../");
 | 
        
           | 224 | rodolico | 22 | use Digest::MD5 qw(md5_hex);
 | 
        
           |  |  | 23 | use File::Copy;
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | # define the version number
 | 
        
           |  |  | 26 | # see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
 | 
        
           |  |  | 27 | use version;
 | 
        
           |  |  | 28 | our $VERSION = version->declare("v0.001.001");
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | use Data::Dumper;
 | 
        
           |  |  | 32 | use File::Basename;
 | 
        
           |  |  | 33 | use Getopt::Long;
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 | Getopt::Long::Configure ("bundling"); # allow -vd --os='debian'
 | 
        
           |  |  | 36 | our $dryRun;
 | 
        
           |  |  | 37 | our $DEBUG;
 | 
        
           |  |  | 38 | my $os;
 | 
        
           |  |  | 39 | my $help = 0;
 | 
        
           |  |  | 40 | my $version = 0;
 | 
        
           |  |  | 41 | my $quiet = 0;
 | 
        
           |  |  | 42 | my $seedFileName = '';
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 | our %operatingSystems;
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 | # simple display if --help is passed
 | 
        
           |  |  | 47 | sub help {
 | 
        
           |  |  | 48 |    use File::Basename;
 | 
        
           |  |  | 49 |    print basename($0) . " $VERSION\n";
 | 
        
           |  |  | 50 |    print <<END
 | 
        
           |  |  | 51 | $0 [options]
 | 
        
           |  |  | 52 | Options:
 | 
        
           |  |  | 53 |    --os|-o      - lower case name of operating system (debian, freebsd, etc...)
 | 
        
           |  |  | 54 |    --dryrun|-n  - do not actually do anything, just tell you what I'd do
 | 
        
           |  |  | 55 |    --version|-v - display version and exit
 | 
        
           |  |  | 56 |    --quiet|-q   - Do not ask questions, just do stuff
 | 
        
           |  |  | 57 |    --help|-g    - Show this screen
 | 
        
           |  |  | 58 |    --seed|-s    - URL/Path to seed file for config
 | 
        
           |  |  | 59 |    --debug      - Show extra information (--nodebug is default)
 | 
        
           |  |  | 60 | END
 | 
        
           |  |  | 61 | }
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 | ################################################################
 | 
        
           |  |  | 65 | #               Main Code                                      #
 | 
        
           |  |  | 66 | ################################################################
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 | # handle any command line parameters that may have been passed in
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 | GetOptions (
 | 
        
           |  |  | 71 |             "os|o=s"      => \$os,      # pass in the operating system
 | 
        
           |  |  | 72 |             "dryrun|n"    => \$dryRun,  # do NOT actually do anything
 | 
        
           |  |  | 73 |             "seed|s=s"    => \$seedFileName,
 | 
        
           |  |  | 74 |             'help|h'      => \$help,
 | 
        
           |  |  | 75 |             'version|v'   => \$version,
 | 
        
           |  |  | 76 |             'quiet|q'     => \$quiet,
 | 
        
           |  |  | 77 |             'debug!'      => \$DEBUG
 | 
        
           |  |  | 78 |             ) or die "Error parsing command line\n";
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 | if ( $help ) { &help() ; exit; }
 | 
        
           |  |  | 81 | if ( $version ) { use File::Basename; print basename($0) . " $VERSION\n"; exit; }
 | 
        
           |  |  | 82 |   | 
        
           | 249 | rodolico | 83 |   | 
        
           | 225 | rodolico | 84 | # find OS if they did not tell us what it was
 | 
        
           |  |  | 85 | $os = `$scriptDir/determineOS` unless $os;
 | 
        
           |  |  | 86 | print "Installing Conf and Cron for $os\n" unless $quiet;
 | 
        
           |  |  | 87 |   | 
        
           | 224 | rodolico | 88 | my $returnValue = 0;
 | 
        
           | 225 | rodolico | 89 | my $command;
 | 
        
           | 224 | rodolico | 90 |   | 
        
           |  |  | 91 | # load the definitions from installer_config.pl
 | 
        
           |  |  | 92 | do "$scriptDir/installer_config.pl";
 | 
        
           |  |  | 93 |   | 
        
           |  |  | 94 | our %libraries;
 | 
        
           |  |  | 95 | our %install;
 | 
        
           |  |  | 96 | our %binaries;
 | 
        
           |  |  | 97 |   | 
        
           | 249 | rodolico | 98 | if ( $seedFileName =~ m~^(((http)|(ftp))s?://).+$~ ) { # the seed file is a URL
 | 
        
           | 225 | rodolico | 99 |    $command = "wget -O /tmp/seedfile --no-check-certificate $seedFileName";
 | 
        
           |  |  | 100 |    print "Downloading from $seedFileName\n\t$command\n" unless $quiet;
 | 
        
           |  |  | 101 |    `$command` unless $dryRun;
 | 
        
           |  |  | 102 |    $seedFileName = '/tmp/seedfile';
 | 
        
           |  |  | 103 | }
 | 
        
           |  |  | 104 |   | 
        
           | 224 | rodolico | 105 | # Check for configuration file
 | 
        
           |  |  | 106 | my $configDir = $operatingSystems{ $os }{'confdir'};
 | 
        
           |  |  | 107 | unless ( -d $configDir ) {
 | 
        
           | 225 | rodolico | 108 |    $command = "mkdir -p $configDir";
 | 
        
           |  |  | 109 |    print "Creating $configDir\n\t$command\n" unless $quiet;
 | 
        
           |  |  | 110 |    `$command` unless $dryRun;
 | 
        
           | 224 | rodolico | 111 | }
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 | my $configFile = $operatingSystems{'configuration file'};
 | 
        
           |  |  | 114 | $configFile =~ s/<confdir>/$configDir/;
 | 
        
           |  |  | 115 | unless ( -f $configFile ) {
 | 
        
           |  |  | 116 |    $seedFileName = "$scriptDir/sysinfo-client.conf.template.yaml" unless $seedFileName;
 | 
        
           | 225 | rodolico | 117 |    $command = "cp $seedFileName $configFile";
 | 
        
           |  |  | 118 |    print "Creating $configFile\n\t$command\n" unless $quiet;
 | 
        
           | 224 | rodolico | 119 |    `cp $seedFileName $configFile` unless $dryRun;
 | 
        
           |  |  | 120 | }
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 | # check for cron file(s)
 | 
        
           | 225 | rodolico | 123 | if ( defined( $operatingSystems{$os}{'crontab'} ) ) {
 | 
        
           |  |  | 124 |    my $command = $operatingSystems{$os}{'crontab'};
 | 
        
           |  |  | 125 |    $command =~ s/<bindir>/$binDir/;
 | 
        
           |  |  | 126 |    $command =~ s/<scriptdir>/$scriptDir/g;
 | 
        
           |  |  | 127 |    print "Setting up cron\n\t$command\n" unless $quiet;
 | 
        
           |  |  | 128 |    `$command` unless $dryRun;
 | 
        
           |  |  | 129 | }
 |