Subversion Repositories camp_sysinfo_client_3

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
192 rodolico 8
# This is a sample script of how to use the postrun
191 rodolico 9
 
10
 
11
# find our location and use it for searching for libraries
12
BEGIN {
13
   use FindBin;
14
   use File::Spec;
15
   use lib File::Spec->catdir($FindBin::Bin);
16
   eval( 'use YAML::Tiny;' );
17
   eval( 'use Data::Dumper;' );
18
}
19
 
20
# contains the directory our script is in
192 rodolico 21
my $scriptDir = File::Spec->catdir($FindBin::Bin);
22
$scriptDir .= '/' unless substr( $scriptDir, -1 ) eq '/';
191 rodolico 23
 
194 rodolico 24
#######################################################
25
#
26
# loadConfigurationFile($confFile)
27
#
28
# Loads configuration file defined by $configurationFile, and dies if not available
29
# This is a YAML file containing serialized contents of 
30
# Parameters:
31
#    $fileName - name of file to look for
32
#    @searchPath - array of paths to find $filename
33
#
34
#######################################################
191 rodolico 35
 
194 rodolico 36
sub loadConfigurationFile {   
37
   my ( $fileName ) = @_;
38
   my $yaml = YAML::Tiny->read( $fileName );
39
   return $yaml->[0];
40
   die "Can not find $fileName\n";
41
}
42
 
191 rodolico 43
sub getWriteFile {
44
   my ($dir, $url, $filename) = @_;
192 rodolico 45
   return getstore("$url$filename", "$dir$filename");
191 rodolico 46
}
47
 
194 rodolico 48
my $configFile = $ARGV[0];
49
die "Usage: $0 configFileName\n" unless $configFile;
50
my %configuration = %{ &loadConfigurationFile( $configFile) };
51
 
52
die "Configuration does not contain postRunScript URL\n" unless defined $configuration{'postRunScript'}{'URL'};
53
 
54
my $URL = $configuration{'postRunScript'}{'URL'};
55
my $checksum = $configuration{'postRunScript'}{'checksum'};
56
my $update = $configuration{'postRunScript'}{'update script'};
57
 
58
 
59
 
191 rodolico 60
my $currentChecksum = '';
192 rodolico 61
if ( -f "$scriptDir$checksum" ) {
62
   open CK, "<$scriptDir$checksum" or warn "could not read $scriptDir$checksum: $!\n";
191 rodolico 63
   $currentChecksum = <CK>;
64
   close CK;
65
}
66
 
67
my $data = get( $URL . $checksum );
192 rodolico 68
if ( $data && $data ne $currentChecksum ) {
69
   my $response = getstore( "$URL$checksum", "$scriptDir$checksum" );
70
   if ( is_success( $response ) ) {
71
      $response = getstore( "$URL$update", "$scriptDir$update" );
72
      if ( is_success( $response ) ) {
73
         chmod 0700, $scriptDir . $update;
74
         print `$scriptDir$update` if "$scriptDir$update";
75
      }
191 rodolico 76
   }
77
}
78
 
79
1;