Subversion Repositories camp_sysinfo_client_3

Rev

Rev 195 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 195 Rev 196
Line 3... Line 3...
3
use warnings;
3
use warnings;
4
use strict;
4
use strict;
5
 
5
 
6
use LWP::Simple; # apt install libwww-perl
6
use LWP::Simple; # apt install libwww-perl
7
 
7
 
-
 
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
8
# This is a sample script of how to use the postrun
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
9
 
40
 
10
 
41
 
11
# find our location and use it for searching for libraries
42
# find our location and use it for searching for libraries
12
BEGIN {
43
BEGIN {
13
   use FindBin;
44
   use FindBin;
Line 23... Line 54...
23
 
54
 
24
#######################################################
55
#######################################################
25
#
56
#
26
# loadConfigurationFile($confFile)
57
# loadConfigurationFile($confFile)
27
#
58
#
28
# Loads configuration file defined by $configurationFile, and dies if not available
59
# I just wrote is simple instead of using the library since it just does a YAML read
29
# This is a YAML file containing serialized contents of 
-
 
-
 
60
#
30
# Parameters:
61
# Parameters:
31
#    $fileName - name of file to look for
62
#    $fileName - name of file to look for
-
 
63
# Returns
32
#    @searchPath - array of paths to find $filename
64
# reference to hash of configuration information
33
#
65
#
34
#######################################################
66
#######################################################
35
 
67
 
36
sub loadConfigurationFile {   
68
sub loadConfigurationFile {   
37
   my ( $fileName ) = @_;
69
   my ( $fileName ) = @_;
38
   my $yaml = YAML::Tiny->read( $fileName );
70
   my $yaml = YAML::Tiny->read( $fileName );
39
   return $yaml->[0];
71
   return $yaml->[0];
40
   die "Can not find $fileName\n";
72
   die "Can not find $fileName\n";
41
}
73
}
42
 
74
 
-
 
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
#######################################################
43
sub getWriteFile {
87
sub getWriteFile {
44
   my ($dir, $url, $filename) = @_;
88
   my ($dir, $url, $filename) = @_;
45
   return getstore("$url$filename", "$dir$filename");
89
   return getstore("$url$filename", "$dir$filename");
46
}
90
}
47
 
91
 
-
 
92
### Main ###
-
 
93
# get the config file name from the command line
48
my $configFile = $ARGV[0];
94
my $configFile = $ARGV[0];
49
die "Usage: $0 configFileName\n" unless $configFile;
95
die "Usage: $0 configFileName\n" unless $configFile;
-
 
96
# load the configuration file
50
my %configuration = %{ &loadConfigurationFile( $configFile) };
97
my %configuration = %{ &loadConfigurationFile( $configFile) };
51
 
98
 
52
die "Configuration does not contain postRunScript URL\n" unless defined $configuration{'postRunScript'}{'URL'};
99
die "Configuration does not contain postRunScript URL\n" unless defined $configuration{'postRunScript'}{'URL'};
53
 
100
 
-
 
101
# get the values we need
54
my $URL = $configuration{'postRunScript'}{'URL'};
102
my $URL = $configuration{'postRunScript'}{'URL'};
55
my $checksum = $configuration{'postRunScript'}{'checksum'};
103
my $checksum = $configuration{'postRunScript'}{'checksum'};
56
my $update = $configuration{'postRunScript'}{'update script'};
104
my $update = $configuration{'postRunScript'}{'update script'};
57
 
105
 
58
 
-
 
59
 
-
 
-
 
106
# check for a current checksum file and get the value if it exists (one line only)
60
my $currentChecksum = '';
107
my $currentChecksum = '';
61
if ( -f "$scriptDir$checksum" ) {
108
if ( -f "$scriptDir$checksum" ) {
62
   open CK, "<$scriptDir$checksum" or warn "could not read $scriptDir$checksum: $!\n";
109
   open CK, "<$scriptDir$checksum" or warn "could not read $scriptDir$checksum: $!\n";
63
   $currentChecksum = <CK>;
110
   $currentChecksum = <CK>;
64
   close CK;
111
   close CK;
65
}
112
}
66
 
113
 
-
 
114
# download checksum from URL, storing in $data
67
my $data = get( $URL . $checksum );
115
my $data = get( $URL . $checksum );
68
if ( $data && $data ne $currentChecksum ) {
116
if ( $data && $data ne $currentChecksum ) { # checksums different, so we have to do something
-
 
117
   # grab it again, but save it this time
69
   my $response = getstore( "$URL$checksum", "$scriptDir$checksum" );
118
   my $response = getstore( "$URL$checksum", "$scriptDir$checksum" );
-
 
119
   # if we got something (should always happen, so the if is likely useless)
70
   if ( is_success( $response ) ) {
120
   if ( is_success( $response ) ) {
-
 
121
      # get the executable
71
      $response = getstore( "$URL$update", "$scriptDir$update" );
122
      $response = getstore( "$URL$update", "$scriptDir$update" );
72
      if ( is_success( $response ) ) {
123
      if ( is_success( $response ) ) { # success
-
 
124
         # Make executable and execute it
73
         chmod 0700, $scriptDir . $update;
125
         chmod 0500, $scriptDir . $update;
74
         print `$scriptDir$update` if "$scriptDir$update";
126
         exec( "$scriptDir$update" ) or print STDERR "Could not execute $scriptDir$update: $!\n";
75
      }
127
      }
76
   }
128
   }
77
}
129
}
78
 
130
 
79
1;
131
1;