| 1 | #!/usr/local/bin/perl | ||
| 2 | # PODNAME: es-cluster-settings.pl | ||
| 3 | # ABSTRACT: Get or apply settings to the cluster | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use App::ElasticSearch::Utilities qw(:all); | ||
| 8 | use CLI::Helpers qw(:all); | ||
| 9 | use Const::Fast; | ||
| 10 | use JSON::MaybeXS; | ||
| 11 | use Getopt::Long::Descriptive; | ||
| 12 | use Pod::Usage; | ||
| 13 | |||
| 14 | #------------------------------------------------------------------------# | ||
| 15 | # Argument Collection | ||
| 16 | const my %DEFAULT => ( | ||
| 17 | duration => 'transient', | ||
| 18 | ); | ||
| 19 | my ($opt,$usage) = describe_options('%c %o', | ||
| 20 | ['duration=s', hidden => | ||
| 21 | { | ||
| 22 | default => $DEFAULT{duration}, | ||
| 23 | one_of => [ | ||
| 24 | [ 'transient|t' => "Apply to the transient settings, the default", { implies => { duration => 'transient' } } ], | ||
| 25 | [ 'persistent|p' => "Apply to the persistent settings", { implies => { duration => 'persistent' } } ], | ||
| 26 | ], | ||
| 27 | }, | ||
| 28 | ], | ||
| 29 | ['update|u=s%', "Settings in flat form to set, can be specified more than once, ie: -s search.max_buckets=1000000"], | ||
| 30 | ['delete=s@', "Settings in flat form to delete, can be specified more than once"], | ||
| 31 | [], | ||
| 32 | ['help', 'Display this message', { shortcircuit => 1 }], | ||
| 33 | ['manual', 'Display full manual', { shortcircuit => 1 }], | ||
| 34 | ); | ||
| 35 | |||
| 36 | #------------------------------------------------------------------------# | ||
| 37 | # Documentations! | ||
| 38 | if( $opt->help ) { | ||
| 39 | print $usage->text; | ||
| 40 | exit 0; | ||
| 41 | } | ||
| 42 | pod2usage(-exitstatus => 0, -verbose => 2) if $opt->manual; | ||
| 43 | |||
| 44 | #------------------------------------------------------------------------# | ||
| 45 | my $json = JSON->new->pretty->utf8->canonical; | ||
| 46 | |||
| 47 | my $current = es_request('/_cluster/settings', { uri_param => { flat_settings => 'true' } }); | ||
| 48 | output({color=>'cyan'}, "-- Current Settings --"); | ||
| 49 | output($json->encode($current)); | ||
| 50 | |||
| 51 | if( $opt->update || $opt->delete ) { | ||
| 52 | output({color=>'magenta',clear =>1}, sprintf "-- Updating Settings [%s] --", $opt->duration); | ||
| 53 | |||
| 54 | # Add updates | ||
| 55 | my %settings = $opt->update ? %{ $opt->update } : (); | ||
| 56 | |||
| 57 | # Add deletes | ||
| 58 | if( my $deletes = $opt->delete ) { | ||
| 59 | foreach my $setting ( @{ $deletes } ) { | ||
| 60 | $settings{$setting} = undef; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | # Peform the operation | ||
| 65 | my $data = es_request('/_cluster/settings', | ||
| 66 | { | ||
| 67 | method => 'PUT', | ||
| 68 | uri_param => { flat_settings => 'true' }, | ||
| 69 | }, | ||
| 70 | { | ||
| 71 | $opt->duration => \%settings, | ||
| 72 | } | ||
| 73 | ); | ||
| 74 | |||
| 75 | die "Failed updating settings" unless $data; | ||
| 76 | |||
| 77 | # Report success/failure | ||
| 78 | if( my $ack = delete $data->{acknowledged} ) { | ||
| 79 | output({color=>'green'}, "Successfully applied settings!"); | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | output({color=>'red'}, "FAILED applying settings:"); | ||
| 83 | } | ||
| 84 | output($json->encode($data)); | ||
| 85 | |||
| 86 | # Show resulting settings | ||
| 87 | my $now = es_request('/_cluster/settings', { uri_param => { flat_settings => 'true' } }); | ||
| 88 | output({color=>'cyan'}, "-- Final Settings --"); | ||
| 89 | output($json->encode($now)); | ||
| 90 | } | ||
| 91 | |||
| 92 | __END__ |