| 191 | 
           rodolico | 
           1 | 
           #! /usr/bin/env perl
  | 
        
        
            | 
            | 
           2 | 
              | 
        
        
            | 
            | 
           3 | 
           use warnings;
  | 
        
        
            | 
            | 
           4 | 
           use strict;
  | 
        
        
            | 
            | 
           5 | 
              | 
        
        
           | 192 | 
           rodolico | 
           6 | 
           use LWP::Simple; # apt install libwww-perl
  | 
        
        
           | 191 | 
           rodolico | 
           7 | 
              | 
        
        
           | 196 | 
           rodolico | 
           8 | 
           # Must be passed full path to config file
  | 
        
        
            | 
            | 
           9 | 
           #
  | 
        
        
            | 
            | 
           10 | 
           # This is a simple script to use the postrun. To enable it, store it in the same directory as sysinf-client, then add the following
  | 
        
        
            | 
            | 
           11 | 
           # to your configuration file
  | 
        
        
            | 
            | 
           12 | 
           # postRunScript:
  | 
        
        
            | 
            | 
           13 | 
           #   'script name': postrunscript.pl
  | 
        
        
            | 
            | 
           14 | 
           #
  | 
        
        
            | 
            | 
           15 | 
           # you will also need to add the some additional values to the above
  | 
        
        
            | 
            | 
           16 | 
           #
  | 
        
        
            | 
            | 
           17 | 
           # Script is designed to download an executable from some web server defined in $configuration{'postRunScript'}{'URL'}
  | 
        
        
            | 
            | 
           18 | 
           # It will first download a checksum file from there. If that checksum is different from the one in this directory, it will 
  | 
        
        
            | 
            | 
           19 | 
           # then download the executable  and run it.
  | 
        
        
            | 
            | 
           20 | 
           # 
  | 
        
        
            | 
            | 
           21 | 
           # values to look for in the configuration file:
  | 
        
        
            | 
            | 
           22 | 
           # $configuration{'postRunScript'}{'URL'};
  | 
        
        
            | 
            | 
           23 | 
           # $configuration{'postRunScript'}{'checksum'};
  | 
        
        
            | 
            | 
           24 | 
           # $configuration{'postRunScript'}{'update script'};
  | 
        
        
            | 
            | 
           25 | 
           #
  | 
        
        
            | 
            | 
           26 | 
           # The YAML equivilent is
  | 
        
        
            | 
            | 
           27 | 
           # postRunScript:
  | 
        
        
            | 
            | 
           28 | 
           #   'script name': postrunscript.pl
  | 
        
        
            | 
            | 
           29 | 
           #    URL: https://example.com/
  | 
        
        
            | 
            | 
           30 | 
           #    checksum: sysinfo_update.cksum
  | 
        
        
            | 
            | 
           31 | 
           #    'update script': sysinfo_update
  | 
        
        
            | 
            | 
           32 | 
           #
  | 
        
        
            | 
            | 
           33 | 
           # this script will try to download https://example.com/sysinfo_update.cksum. If it exists it will compare the contents
  | 
        
        
            | 
            | 
           34 | 
           # to sysinfo_update.cksum in the directory this script is in. If they are different (or there is no file in the script directory)
  | 
        
        
            | 
            | 
           35 | 
           # the contents will be written, then https://example.com/sysinfo_update will be downloaded. sysinfo_update will be changed to mod
  | 
        
        
            | 
            | 
           36 | 
           # 0500, then executed the file
  | 
        
        
            | 
            | 
           37 | 
           #
  | 
        
        
            | 
            | 
           38 | 
           # NOTE: the checksum file doesn't really need a checksum (no checking is made). It just have different contents than what is 
  | 
        
        
            | 
            | 
           39 | 
           # on disk
  | 
        
        
           | 191 | 
           rodolico | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
           # find our location and use it for searching for libraries
  | 
        
        
            | 
            | 
           43 | 
           BEGIN {
  | 
        
        
            | 
            | 
           44 | 
              use FindBin;
  | 
        
        
            | 
            | 
           45 | 
              use File::Spec;
  | 
        
        
            | 
            | 
           46 | 
              use lib File::Spec->catdir($FindBin::Bin);
  | 
        
        
            | 
            | 
           47 | 
              eval( 'use YAML::Tiny;' );
  | 
        
        
            | 
            | 
           48 | 
              eval( 'use Data::Dumper;' );
  | 
        
        
            | 
            | 
           49 | 
           }
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
           # contains the directory our script is in
  | 
        
        
           | 192 | 
           rodolico | 
           52 | 
           my $scriptDir = File::Spec->catdir($FindBin::Bin);
  | 
        
        
            | 
            | 
           53 | 
           $scriptDir .= '/' unless substr( $scriptDir, -1 ) eq '/';
  | 
        
        
           | 191 | 
           rodolico | 
           54 | 
              | 
        
        
           | 194 | 
           rodolico | 
           55 | 
           #######################################################
  | 
        
        
            | 
            | 
           56 | 
           #
  | 
        
        
            | 
            | 
           57 | 
           # loadConfigurationFile($confFile)
  | 
        
        
            | 
            | 
           58 | 
           #
  | 
        
        
           | 196 | 
           rodolico | 
           59 | 
           # I just wrote is simple instead of using the library since it just does a YAML read
  | 
        
        
            | 
            | 
           60 | 
           #
  | 
        
        
           | 194 | 
           rodolico | 
           61 | 
           # Parameters:
  | 
        
        
            | 
            | 
           62 | 
           #    $fileName - name of file to look for
  | 
        
        
           | 196 | 
           rodolico | 
           63 | 
           # Returns
  | 
        
        
            | 
            | 
           64 | 
           # reference to hash of configuration information
  | 
        
        
           | 194 | 
           rodolico | 
           65 | 
           #
  | 
        
        
            | 
            | 
           66 | 
           #######################################################
  | 
        
        
           | 191 | 
           rodolico | 
           67 | 
              | 
        
        
           | 194 | 
           rodolico | 
           68 | 
           sub loadConfigurationFile {   
  | 
        
        
            | 
            | 
           69 | 
              my ( $fileName ) = @_;
  | 
        
        
            | 
            | 
           70 | 
              my $yaml = YAML::Tiny->read( $fileName );
  | 
        
        
            | 
            | 
           71 | 
              return $yaml->[0];
  | 
        
        
            | 
            | 
           72 | 
              die "Can not find $fileName\n";
  | 
        
        
            | 
            | 
           73 | 
           }
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
           | 196 | 
           rodolico | 
           75 | 
           #######################################################
  | 
        
        
            | 
            | 
           76 | 
           # getWriteFile
  | 
        
        
            | 
            | 
           77 | 
           # 
  | 
        
        
            | 
            | 
           78 | 
           # Probably doesn't even need to be a sub it is so simple. Download $filename from $url, 
  | 
        
        
            | 
            | 
           79 | 
           # and store it in $dir/$filename
  | 
        
        
            | 
            | 
           80 | 
           #
  | 
        
        
            | 
            | 
           81 | 
           # Parameters:
  | 
        
        
            | 
            | 
           82 | 
           #   $dir - the local directory to store the downloaded file
  | 
        
        
            | 
            | 
           83 | 
           #   $url - the URL (withou the file name) to download from
  | 
        
        
            | 
            | 
           84 | 
           #   $filename - The name of the file, bot at URL and stored in $dir
  | 
        
        
            | 
            | 
           85 | 
           #
  | 
        
        
            | 
            | 
           86 | 
           #######################################################
  | 
        
        
           | 191 | 
           rodolico | 
           87 | 
           sub getWriteFile {
  | 
        
        
            | 
            | 
           88 | 
              my ($dir, $url, $filename) = @_;
  | 
        
        
           | 192 | 
           rodolico | 
           89 | 
              return getstore("$url$filename", "$dir$filename");
  | 
        
        
           | 191 | 
           rodolico | 
           90 | 
           }
  | 
        
        
            | 
            | 
           91 | 
              | 
        
        
           | 196 | 
           rodolico | 
           92 | 
           ### Main ###
  | 
        
        
            | 
            | 
           93 | 
           # get the config file name from the command line
  | 
        
        
           | 194 | 
           rodolico | 
           94 | 
           my $configFile = $ARGV[0];
  | 
        
        
            | 
            | 
           95 | 
           die "Usage: $0 configFileName\n" unless $configFile;
  | 
        
        
           | 196 | 
           rodolico | 
           96 | 
           # load the configuration file
  | 
        
        
           | 194 | 
           rodolico | 
           97 | 
           my %configuration = %{ &loadConfigurationFile( $configFile) };
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
           die "Configuration does not contain postRunScript URL\n" unless defined $configuration{'postRunScript'}{'URL'};
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
           | 196 | 
           rodolico | 
           101 | 
           # get the values we need
  | 
        
        
           | 194 | 
           rodolico | 
           102 | 
           my $URL = $configuration{'postRunScript'}{'URL'};
  | 
        
        
            | 
            | 
           103 | 
           my $checksum = $configuration{'postRunScript'}{'checksum'};
  | 
        
        
            | 
            | 
           104 | 
           my $update = $configuration{'postRunScript'}{'update script'};
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
           | 196 | 
           rodolico | 
           106 | 
           # check for a current checksum file and get the value if it exists (one line only)
  | 
        
        
           | 191 | 
           rodolico | 
           107 | 
           my $currentChecksum = '';
  | 
        
        
           | 192 | 
           rodolico | 
           108 | 
           if ( -f "$scriptDir$checksum" ) {
  | 
        
        
            | 
            | 
           109 | 
              open CK, "<$scriptDir$checksum" or warn "could not read $scriptDir$checksum: $!\n";
  | 
        
        
           | 191 | 
           rodolico | 
           110 | 
              $currentChecksum = <CK>;
  | 
        
        
            | 
            | 
           111 | 
              close CK;
  | 
        
        
            | 
            | 
           112 | 
           }
  | 
        
        
            | 
            | 
           113 | 
              | 
        
        
           | 196 | 
           rodolico | 
           114 | 
           # download checksum from URL, storing in $data
  | 
        
        
           | 191 | 
           rodolico | 
           115 | 
           my $data = get( $URL . $checksum );
  | 
        
        
           | 196 | 
           rodolico | 
           116 | 
           if ( $data && $data ne $currentChecksum ) { # checksums different, so we have to do something
  | 
        
        
            | 
            | 
           117 | 
              # grab it again, but save it this time
  | 
        
        
           | 192 | 
           rodolico | 
           118 | 
              my $response = getstore( "$URL$checksum", "$scriptDir$checksum" );
  | 
        
        
           | 196 | 
           rodolico | 
           119 | 
              # if we got something (should always happen, so the if is likely useless)
  | 
        
        
           | 192 | 
           rodolico | 
           120 | 
              if ( is_success( $response ) ) {
  | 
        
        
           | 196 | 
           rodolico | 
           121 | 
                 # get the executable
  | 
        
        
           | 192 | 
           rodolico | 
           122 | 
                 $response = getstore( "$URL$update", "$scriptDir$update" );
  | 
        
        
           | 196 | 
           rodolico | 
           123 | 
                 if ( is_success( $response ) ) { # success
  | 
        
        
            | 
            | 
           124 | 
                    # Make executable and execute it
  | 
        
        
            | 
            | 
           125 | 
                    chmod 0500, $scriptDir . $update;
  | 
        
        
            | 
            | 
           126 | 
                    exec( "$scriptDir$update" ) or print STDERR "Could not execute $scriptDir$update: $!\n";
  | 
        
        
           | 192 | 
           rodolico | 
           127 | 
                 }
  | 
        
        
           | 191 | 
           rodolico | 
           128 | 
              }
  | 
        
        
            | 
            | 
           129 | 
           }
  | 
        
        
            | 
            | 
           130 | 
              | 
        
        
            | 
            | 
           131 | 
           1;
  |