script/es-apply-settings.pl untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 152 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2 # PODNAME: es-apply-settings.pl
3 # ABSTRACT: Run to apply a JSON list of settings to indexes matching a pattern
4 use strict;
5 use warnings;
6  
7 use App::ElasticSearch::Utilities qw(:all);
8 use CLI::Helpers qw(:all);
9 use JSON::MaybeXS;
10 use Getopt::Long qw(:config no_ignore_case no_ignore_case_always);
11 use Pod::Usage;
12 use Test::Deep qw( cmp_details deep_diag subhashof );
13  
14 #------------------------------------------------------------------------#
15 # Argument Collection
16 my %opt;
17 GetOptions(\%opt,
18 'skip-alias=s@',
19 'close',
20 'older',
21 'no-skip|noskip',
22 'no-diff|nodiff',
23 # Basic options
24 'help|h',
25 'manual|m',
26 );
27  
28 #------------------------------------------------------------------------#
29 # Documentations!
30 pod2usage(1) if $opt{help};
31 pod2usage(-exitstatus => 0, -verbose => 2) if $opt{manual};
32  
33 #------------------------------------------------------------------------#
34  
35 # Figure out the skipped aliases
36 my %SKIP = map { $_ => 1 } (
37 qw( .hold .do_not_erase ),
38 $opt{'skip-alias'} ? ( @{ $opt{'skip-alias'} } ) : (),
39 );
40  
41 # Read JSON Settings
42 my $RawJSON = '';
43 $RawJSON .= $_ while <>;
44  
45 my $settings = undef;
46 eval {
47 $settings = decode_json $RawJSON;
48 };
49 if( my $err = $@ ) {
50 output({color=>'red'}, "Invalid JSON structure passed, error was '$err'");
51 debug('JSON Passed was:', $RawJSON);
52 exit 1;
53 }
54 debug("Settings to apply");
55 debug_var($settings);
56  
57 # Delete Indexes older than a certain point
58 my @indices = es_indices(older => $opt{older});
59 if ( !@indices ) {
60 output({color=>"red"}, "No matching indices found.");
61 exit 1;
62 }
63 # Loop through the indices and take appropriate actions;
64 foreach my $index (sort @indices) {
65 verbose("$index: evaluated");
66  
67 my $info = es_request($index);
68 next unless $info->{$index};
69 my $meta = $info->{$index};
70  
71 # Only safety check when not forced
72 if( !$opt{'no-skip'} ) {
73 my $skipped;
74 foreach my $alias ( keys %{ $meta->{aliases} } ) {
75 $skipped = $alias if exists $SKIP{$alias};
76 last if defined $skipped;
77 }
78 if( defined $skipped ) {
79 output({color=>'magenta'}, " ~ Skipped $index due to protected alias '$skipped', use --no-skip to skip this safety check");
80 next;
81 }
82 }
83  
84 # Check for settings first
85 if( !$opt{'no-diff'} ) {
86 # Since both, either, or neither can be flattened, flatten
87 my $current = es_flatten_hash( $meta->{settings} );
88 my $desired = es_flatten_hash( $settings );
89 my ($ok,$stack) = cmp_details($desired, subhashof($current));
90 if( $ok ) {
91 output({color=>'cyan'}, " ~ Skipped $index as it already contains those settings, use --no-diff to apply without this check");
92 next;
93 }
94 else {
95 debug(deep_diag($stack));
96 }
97 }
98  
99 # Close the index first
100 if (exists $opt{close} && $opt{close}) {
101 my $res = es_close_index($index);
102 if ( !defined $res ) {
103 output({color=>"red"}, "Closing index $index failed.");
104 next;
105 }
106 output({color=>'cyan'}, " + Closed $index to apply settings.");
107 }
108  
109 my $result = es_apply_index_settings($index,$settings);
110 if( !defined $result ) {
111 output({color=>'red'}, "Unable to update settings on $index");
112 debug("Current");
113 debug_var($meta->{settings});
114 }
115 else {
116 output({color=>'green'}, " + Settings applied successfully!");
117 }
118 debug({color=>"cyan"},"Result was:");
119 debug_var($result);
120  
121 # Re-open the index
122 if (exists $opt{close} && $opt{close}) {
123 my $result = es_open_index($index);
124 if ( !defined($result) ) {
125 output({color=>"red"}, " + Opening index $index failed.");
126 next;
127 }
128 output({color=>'cyan'}, " + Re-opening $index with new settings.");
129 }
130 }
131  
132 __END__