@@ -45,6 +45,14 @@ has arg_list_types => (
45
45
documentation => ' list available index type names' ,
46
46
);
47
47
48
+ has arg_cluster_info => (
49
+ init_arg => ' show_cluster_info' ,
50
+ is => ' ro' ,
51
+ isa => Bool,
52
+ default => 0,
53
+ documentation => ' show basic info about cluster, indices and aliases' ,
54
+ );
55
+
48
56
has arg_create_index => (
49
57
init_arg => ' create_index' ,
50
58
is => ' ro' ,
@@ -121,14 +129,31 @@ has delete_from_type => (
121
129
);
122
130
123
131
sub run {
124
- my $self = shift ;
125
- $self -> create_index if $self -> arg_create_index;
126
- $self -> delete_index if $self -> arg_delete_index;
127
- $self -> update_index if $self -> arg_update_index;
128
- $self -> copy_type if $self -> copy_to_index;
129
- $self -> empty_type if $self -> delete_from_type;
130
- $self -> list_types if $self -> arg_list_types;
131
- $self -> deploy_mapping if $self -> arg_deploy_mapping;
132
+ my $self = shift ;
133
+
134
+ if ( $self -> await ) {
135
+ $self -> delete_index if $self -> arg_delete_index;
136
+ $self -> update_index if $self -> arg_update_index;
137
+ $self -> copy_type if $self -> copy_to_index;
138
+ $self -> empty_type if $self -> delete_from_type;
139
+ $self -> list_types if $self -> arg_list_types;
140
+
141
+ if ( $self -> arg_deploy_mapping ) {
142
+ unless ( $self -> deploy_mapping ) {
143
+ $self -> print_error(" Indices Re-creation has failed!" );
144
+ $self -> exit_code(1);
145
+ }
146
+ }
147
+
148
+ if ( $self -> arg_cluster_info ) {
149
+ $self -> check_health;
150
+ $self -> show_info;
151
+ }
152
+ }
153
+
154
+ # Correctly reaching this point it will end the application
155
+ # with the set MetaCPAN::Role::Script::exit_code
156
+ exit $self -> exit_code;
132
157
}
133
158
134
159
sub _check_index_exists {
@@ -158,6 +183,7 @@ sub delete_index {
158
183
159
184
sub _delete_index {
160
185
my ( $self , $name ) = @_ ;
186
+
161
187
log_info {" Deleting index: $name " };
162
188
$self -> es-> indices-> delete ( index => $name );
163
189
}
@@ -273,7 +299,7 @@ sub copy_type {
273
299
274
300
# else ... do copy by monthly slices
275
301
276
- my $dt = DateTime-> new( year => 1994, month => 1 );
302
+ my $dt = DateTime-> new( year => 1994, month => 1 );
277
303
my $end_time = DateTime-> now()-> add( months => 1 );
278
304
279
305
while ( $dt < $end_time ) {
@@ -369,9 +395,20 @@ sub list_types {
369
395
print " $_ \n " for sort keys %{ $self -> index -> types };
370
396
}
371
397
398
+ sub show_info {
399
+ my $self = $_ [0];
400
+ my $info_rs = {
401
+ ' cluster_info' => \%{ $self -> cluster_info },
402
+ ' indices_info' => \%{ $self -> indices_info },
403
+ ' aliases_info' => \%{ $self -> aliases_info }
404
+ };
405
+ print JSON-> new-> utf8-> pretty-> encode($info_rs );
406
+ }
407
+
372
408
sub deploy_mapping {
373
409
my $self = shift ;
374
410
my $cpan_index = ' cpan_v1_01' ;
411
+ my $imappingok = 0;
375
412
376
413
$self -> are_you_sure(
377
414
' this will delete EVERYTHING and re-create the (empty) indexes' );
@@ -446,25 +483,105 @@ sub deploy_mapping {
446
483
}
447
484
}
448
485
449
- # create alias
486
+ # create aliases
450
487
451
- $es -> indices-> put_alias(
452
- index => $cpan_index ,
453
- name => ' cpan' ,
454
- );
488
+ my %aliases = ( ' cpan' => $cpan_index );
489
+ for my $alias ( sort keys %aliases ) {
490
+ log_info {" Creating alias: '$alias ' -> '$aliases {$alias }'" };
491
+ $es -> indices-> put_alias(
492
+ index => $aliases {$alias },
493
+ name => $alias ,
494
+ );
495
+ }
496
+
497
+ $self -> check_health(1);
498
+ $imappingok = $self -> verify_mapping( \%mappings , \%aliases );
455
499
456
500
# done
457
501
log_info {" Done." };
458
- 1;
502
+
503
+ return $imappingok ;
504
+ }
505
+
506
+ sub verify_mapping {
507
+ my ( $self , $rmappings , $raliases ) = @_ ;
508
+ my $ihealth = 0;
509
+
510
+ if ( defined $rmappings && ref $rmappings eq ' HASH' ) {
511
+ my $rhealth = undef ;
512
+
513
+ $ihealth = 1;
514
+
515
+ for my $idx ( sort keys %$rmappings ) {
516
+ $rhealth = $self -> indices_info-> {$idx };
517
+ if ( defined $rhealth ) {
518
+ if ( $rhealth -> {' health' } eq ' red' ) {
519
+ log_error {
520
+ " Broken index: $idx (state '"
521
+ . $rhealth -> {' health' } . " ')"
522
+ };
523
+ $ihealth = 0;
524
+ }
525
+ else {
526
+ log_info {
527
+ " Healthy index: $idx (state '"
528
+ . $rhealth -> {' health' } . " ')"
529
+ };
530
+ }
531
+ }
532
+ else {
533
+ log_error {" Missing index: $idx " };
534
+ $ihealth = 0;
535
+ }
536
+ }
537
+ }
538
+
539
+ if ( defined $raliases && ref $raliases eq ' HASH' ) {
540
+ my $ralias = undef ;
541
+ for my $name ( sort keys %$raliases ) {
542
+ $ralias = $self -> aliases_info-> {$name };
543
+ if ( defined $ralias ) {
544
+ if ( $ralias -> {' index' } eq $raliases -> {$name } ) {
545
+ log_info {
546
+ " Correct alias: $name (index '"
547
+ . $ralias -> {' index' } . " ')"
548
+ };
549
+ }
550
+ else {
551
+ log_error {
552
+ " Broken alias: $name (index '"
553
+ . $ralias -> {' index' } . " ')"
554
+ };
555
+ $ihealth = 0;
556
+ }
557
+ }
558
+ else {
559
+ log_error {" Missing alias: $name " };
560
+ $ihealth = 0;
561
+ }
562
+ }
563
+ }
564
+ else {
565
+ $ihealth = 0 if ( scalar ( keys %{ $self -> aliases_info } ) == 0 );
566
+ }
567
+
568
+ return $ihealth ;
459
569
}
460
570
461
571
__PACKAGE__ -> meta-> make_immutable;
462
572
1;
463
573
464
574
__END__
465
575
576
+ =pod
577
+
578
+ =head1 NAME
579
+
580
+ MetaCPAN::Script::Mapping - Script to set the index and mapping the types
581
+
466
582
=head1 SYNOPSIS
467
583
584
+ # bin/metacpan mapping --show_cluster_info # show basic info about the cluster, indices and aliases
468
585
# bin/metacpan mapping --delete
469
586
# bin/metacpan mapping --list_types
470
587
# bin/metacpan mapping --delete_index xxx
@@ -481,3 +598,91 @@ __END__
481
598
This is the index mapping handling script.
482
599
Used rarely, but carries the most important task of setting
483
600
the index and mapping the types.
601
+
602
+ =head1 OPTIONS
603
+
604
+ This Script accepts the following options
605
+
606
+ =over 4
607
+
608
+ =item Option C<--show_cluster_info >
609
+
610
+ This option makes the Script show basic information about the I<ElasticSearch > Cluster
611
+ and its indices and aliases.
612
+ This information has to be collected with the C<MetaCPAN::Role::Script::check_health() > Method.
613
+ On Script start-up it is empty.
614
+
615
+ bin/metacpan mapping --show_cluster_info
616
+
617
+ See L<Method C<MetaCPAN::Role::Script::check_health()> >
618
+
619
+ =item Option C<--delete >
620
+
621
+ This option makes the Script delete all indices configured in the project and re-create them emtpy.
622
+ It verifies the index integrity of the indices and aliases calling the methods
623
+ C<MetaCPAN::Role::Script::check_health() > and C<verify_mapping() > .
624
+ If the C<verify_mapping() > Method fails it exits the Script
625
+ with B<Exit Code > C< 1 > .
626
+
627
+ bin/metacpan mapping --delete
628
+
629
+ See L<Method C<deploy_mapping()> >
630
+
631
+ See L<Method C<verify_mapping()> >
632
+
633
+ See L<Method C<MetaCPAN::Role::Script::check_health()> >
634
+
635
+ =back
636
+
637
+ =head1 METHODS
638
+
639
+ This Package provides the following methods
640
+
641
+ =over 4
642
+
643
+ =item C<deploy_mapping() >
644
+
645
+ Deletes and re-creates the indices and aliases defined in the Project.
646
+ The user will be requested for manual confirmation on the command line before the elemination.
647
+ The integrity of the indices and aliases will be checked with the C<verify_mapping() > Method.
648
+ On successful creation it returns C< 1 > , otherwise it returns C< 0 > .
649
+
650
+ B<Returns: > It returns C< 1 > when the indices and aliases are created and verified as correct.
651
+ Otherwise it returns C< 0 > .
652
+
653
+ B<Exceptions: > It can throw exceptions when the connection to I<ElasticSearch > fails
654
+ or there is any issue in any I<ElasticSearch > Request run by the Script.
655
+
656
+ See L<Option C<--delete> >
657
+
658
+ See L<Method C<verify_mapping()> >
659
+
660
+ See L<Method C<MetaCPAN::Role::Script::check_health()> >
661
+
662
+ =item C<verify_mapping( \%indices, \%aliases ) >
663
+
664
+ Checks the defined indices and aliases against the actually in the I<ElasticSearch > Cluster
665
+ existing indices and aliases which must have been requested with
666
+ the C<MetaCPAN::Role::Script::check_health() > Method.
667
+
668
+ B<Parameters: >
669
+
670
+ C<\%indices > - Reference to a hash that defines the indices required for the Project.
671
+
672
+ C<\%aliases > - Reference to a hash that defines the aliases required for the Project.
673
+
674
+ B<Returns: > It returns C< 1 > when the indices and aliases are created and verified as correct.
675
+ Otherwise it returns C< 0 > .
676
+
677
+ B<Exceptions: > It can throw exceptions when the connection to I<ElasticSearch > fails
678
+ or there is any issue in any I<ElasticSearch > Request run by the Script.
679
+
680
+ See L<Option C<--delete> >
681
+
682
+ See L<Method C<verify_mapping()> >
683
+
684
+ See L<Method C<MetaCPAN::Role::Script::check_health()> >
685
+
686
+ =back
687
+
688
+ =cut
0 commit comments