-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcheck_deployment_checksums.pl
executable file
·100 lines (77 loc) · 2.13 KB
/
check_deployment_checksums.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env perl
=head1 NAME
check_checksums.pl
=head1 SYNOPSIS
check_deployment_checksums.pl CHECKSUM_FILE
=head1 DESCRIPTION
This will check that the files listed in the CHECKSUM_FILE
have not been modified.
=head1 CONTACT
=head1 METHODS
=cut
BEGIN { unshift(@INC, './modules') }
use strict;
use warnings;
use Getopt::Long;
use Digest::MD5::File qw(file_md5_hex);
use JSON qw(to_json);
use Data::Dumper;
my $CHECKSUM_PATH = pop @ARGV;
my $OUTPUT_JSON;
GetOptions ('output-json|j' => \$OUTPUT_JSON);
sub die_usage
{
die <<USAGE;
Usage: $0 [OPTIONS] CHECKSUM_PATH
Check that the files listed in CHECKSUM_PATH haven't changed
Options:
--output-json Output to stdout in JSON
USAGE
;
}
sub output_text
{
my ( $differences ) = @_;
foreach my $difference (@$differences) {
my ($path, $old_checksum, $current_checksum) = @$difference;
print "'$path' has been modified: '$old_checksum' => '$current_checksum'\n";
}
}
sub output_json
{
my ( $differences_tuples, $checksum_path ) = @_;
my @differences_hashes = ();
foreach my $difference (@$differences_tuples) {
my ($path, $old_checksum, $current_checksum) = @$difference;
push @differences_hashes, { path => $path,
old_checksum => $old_checksum,
current_checksum => $current_checksum };
}
my $json_output = to_json( { differences => \@differences_hashes,
according_to => $checksum_path } );
print "$json_output\n";
}
$CHECKSUM_PATH or die_usage();
my @differences = ();
open (my $checksum_file, '<', $CHECKSUM_PATH) or die_usage();
while (my $row = <$checksum_file>) {
chomp($row);
my ($old_checksum, $path) = split(' ', $row);
my $current_checksum;
if ( -e $path ) {
$current_checksum = file_md5_hex($path);
} else {
$current_checksum = 'File not found';
}
if ( $old_checksum ne $current_checksum ) {
my @difference = ($path, $old_checksum, $current_checksum);
push @differences, \@difference;
}
}
if ( $OUTPUT_JSON ) {
output_json( \@differences, $CHECKSUM_PATH );
} else {
output_text( \@differences );
}
exit 0;