| 191 |
rodolico |
1 |
#! /usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
use warnings;
|
|
|
4 |
use strict;
|
|
|
5 |
|
|
|
6 |
use LWT::Simple;
|
|
|
7 |
|
|
|
8 |
This is a sample script of how to use the postrun
|
|
|
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
|
|
|
21 |
my $myDir = File::Spec->catdir($FindBin::Bin);
|
|
|
22 |
$myDir .= '/' unless substr( $myDir, -1 ) eq '/';
|
|
|
23 |
|
|
|
24 |
my $URL = 'https://www.example.com/';
|
|
|
25 |
my $checksum = 'sysinfo_update.cksum';
|
|
|
26 |
my $update = 'sysinfo_update'
|
|
|
27 |
|
|
|
28 |
sub getWriteFile {
|
|
|
29 |
my ($dir, $url, $filename) = @_;
|
|
|
30 |
my $response = HTTP::Tiny->new->get("$url$filename");
|
|
|
31 |
if ( $response->{success} ) {
|
|
|
32 |
if ( length $response->{content} ) {
|
|
|
33 |
open FH, ">$dir$filename" or return $!;
|
|
|
34 |
print FH $response->{content};
|
|
|
35 |
close FH;
|
|
|
36 |
} else {
|
|
|
37 |
warn "Zero lenght file downloaded from $url$filename\n";
|
|
|
38 |
}
|
|
|
39 |
} else {
|
|
|
40 |
return "Could not download $url$filename: " . $response->{reason};
|
|
|
41 |
}
|
|
|
42 |
return '';
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
my $currentChecksum = '';
|
|
|
46 |
if ( -f "$myDir$checksum" ) {
|
|
|
47 |
open CK, "<$myDir$checksum" or warn "could not read $myDir$checksum: $!\n";
|
|
|
48 |
$currentChecksum = <CK>;
|
|
|
49 |
close CK;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
my $data = get( $URL . $checksum );
|
|
|
53 |
if ( $data != $currentChecksum ) {
|
|
|
54 |
my $response;
|
|
|
55 |
unless ( my $response = &getWriteFile( $myDir, $URL, $checksum ) ) {
|
|
|
56 |
$response = &getWriteFile( $myDir, $URL, $update );
|
|
|
57 |
chmod 0755, $checksum unless $response;
|
|
|
58 |
}
|
|
|
59 |
die "$response\n" if $response;
|
|
|
60 |
`$checksum` if -f $checksum;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
1;
|