Subversion Repositories camp_sysinfo_client_3

Rev

Rev 193 | Rev 195 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! /usr/bin/env perl

use warnings;
use strict;

use LWP::Simple; # apt install libwww-perl

# This is a sample script of how to use the postrun


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

# contains the directory our script is in
my $scriptDir = File::Spec->catdir($FindBin::Bin);
$scriptDir .= '/' unless substr( $scriptDir, -1 ) eq '/';

#######################################################
#
# loadConfigurationFile($confFile)
#
# Loads configuration file defined by $configurationFile, and dies if not available
# This is a YAML file containing serialized contents of 
# Parameters:
#    $fileName - name of file to look for
#    @searchPath - array of paths to find $filename
#
#######################################################

sub loadConfigurationFile {   
   my ( $fileName ) = @_;
   my $yaml = YAML::Tiny->read( $fileName );
   return $yaml->[0];
   die "Can not find $fileName\n";
}

sub getWriteFile {
   my ($dir, $url, $filename) = @_;
   return getstore("$url$filename", "$dir$filename");
}

my $configFile = $ARGV[0];
die "Usage: $0 configFileName\n" unless $configFile;
my %configuration = %{ &loadConfigurationFile( $configFile) };

die "Configuration does not contain postRunScript URL\n" unless defined $configuration{'postRunScript'}{'URL'};

my $URL = $configuration{'postRunScript'}{'URL'};
my $checksum = $configuration{'postRunScript'}{'checksum'};
my $update = $configuration{'postRunScript'}{'update script'};



my $currentChecksum = '';
if ( -f "$scriptDir$checksum" ) {
   open CK, "<$scriptDir$checksum" or warn "could not read $scriptDir$checksum: $!\n";
   $currentChecksum = <CK>;
   close CK;
}

my $data = get( $URL . $checksum );
if ( $data && $data ne $currentChecksum ) {
   my $response = getstore( "$URL$checksum", "$scriptDir$checksum" );
   if ( is_success( $response ) ) {
      $response = getstore( "$URL$update", "$scriptDir$update" );
      if ( is_success( $response ) ) {
         chmod 0700, $scriptDir . $update;
         print `$scriptDir$update` if "$scriptDir$update";
      }
   }
}

1;