Subversion Repositories camp_sysinfo_client_3

Rev

Rev 192 | 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 LWT::Simple;

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 $myDir = File::Spec->catdir($FindBin::Bin);
$myDir .= '/' unless substr( $myDir, -1 ) eq '/';

my $URL = 'https://www.example.com/';
my $checksum = 'sysinfo_update.cksum';
my $update = 'sysinfo_update'

sub getWriteFile {
   my ($dir, $url, $filename) = @_;
   my $response = HTTP::Tiny->new->get("$url$filename");
   if ( $response->{success} ) {
      if ( length $response->{content} ) {
         open FH, ">$dir$filename" or return $!;
         print FH $response->{content};
         close FH;
      } else {
         warn "Zero lenght file downloaded from $url$filename\n";
      }
   } else {
      return "Could not download $url$filename: " . $response->{reason};
   }
   return '';
}

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

my $data = get( $URL . $checksum );
if ( $data != $currentChecksum ) {
   my $response;
   unless ( my $response = &getWriteFile( $myDir, $URL, $checksum ) ) {
      $response = &getWriteFile( $myDir, $URL, $update );
      chmod 0755, $checksum unless $response;
   }
   die "$response\n" if $response;
   `$checksum` if -f $checksum;
}

1;