-
Notifications
You must be signed in to change notification settings - Fork 37
/
Range.pm
729 lines (588 loc) · 19.5 KB
/
Range.pm
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
=head1 NAME
Range - deal with Seco ranges.
=head1 SYNOPSIS
use Range qw/:common/;
my @nodes = expand_range('%ks301');
my $nodes = compress_range(\@nodes);
my $same_nodes = compress_range(@nodes);
my @sorted_nodes = sorted_expand_range('@ALL');
=head1 DESCRIPTION
Do stuff with ranges.
Expand ranges and cluster definitions.
Compress (a ref to) an array of nodes into a compact string rep.
Copyright (c) 2011, Yahoo! Inc. All rights reserved.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
=cut
package Seco::Range;
use warnings;
use strict;
use Exporter;
use IO::File;
use Sys::Hostname;
use Storable;
use Carp;
use constant NODE_CLUSTER => '/home/seco/candy/whoismycluster/node_cluster.dat';
use vars qw(@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS $VERSION $recursion);
use vars qw($IGOR);
use Log::Log4perl qw(:easy);
@ISA = qw/Exporter/;
@EXPORT_OK = qw/expand_range get_cluster_nodes range_set_altpath
compress_range sorted_expand_range nodes_parser get_expanded_clusters/;
%EXPORT_TAGS = (
all => [@EXPORT_OK],
common => [qw/expand_range compress_range sorted_expand_range nodes_parser
get_expanded_clusters/]);
@EXPORT = (); # don't pollute the namespace - use Range qw/:common/
$VERSION = "1.4.1";
$recursion = 0;
#eval <<'MEMOIZE';
#use Memoize; # hmmm caching
#memoize('expand_range');
#memoize('_get_cluster_keys');
#MEMOIZE
my $node_regex = qr/
([-\w.]*?) # prefix
(\d+) # followed by the start of the range
(\.[-A-Za-z\d.]*[-A-Za-z]+[-A-Za-z\d.]*)? # optional domain
/x;
my ($balanced_parens, $balanced_braces);
if ($] > 5.006) {
$balanced_parens = qr/
\(
(?: (?> [^( )]+ ) # no backtracking here (normal chars)
| # or
(??{ $balanced_parens }) )* # recursive matching here
\)
/x;
$balanced_braces = qr/
{
(?: (?> [^{ }]+ ) # no backtracking here (normal chars)
|
(??{ $balanced_braces }) )* # recursive matching here
}
/x;
} else {
# F*ing solaris machines
$balanced_parens = qr/
\(
(?: (?> [^()]+ ) | \( (?> [^()]+) \)+)*
\)
/x;
$balanced_braces = qr/
{
(?: (?> [^{}]+ ) | { (?> [^{}]+) }+)*
}
/x;
}
my $range_re = qr@
(?:^|,) # beginning of the string or , is start of range
( # start capturing our range
[^(){}/,]* # normal characters (not {} or / or ())
(?:
$balanced_parens
|
(?:-|&) \/ [^\/]+ \/ # a reg.ex.
| # or
(?:-|&) \| [^\|]+ \|
|
$balanced_braces
[^{},]* # followed by optional normal chars
)+ # one or more times
| # or it can be a simple thing (no braces involved)
(?> # never backtrack over these
[^(){},/]+) # normal chars
)@x;
my $range_altpath;
sub range_set_altpath {
my $new_path = shift;
return $range_altpath = $new_path;
}
sub _sort_nodes {
my $ref_nodes = shift;
my @sorted =
map { $_->[0] }
sort { $a->[1] cmp $b->[1] ||
$a->[3] cmp $b->[3] ||
$a->[2] <=> $b->[2] ||
$a->[0] cmp $b->[0] }
map { if (/^$node_regex$/) {
[$_, defined $1 ? $1 : "", defined $2 ? $2 : 0,
defined $3 ? $3 : ""]
} else { [$_, "", 0, ""] }
} @$ref_nodes;
return @sorted;
}
# compress_range related stuff
sub _get_group {
my ($prefix, $digits, $count, $suffix) = @_;
$prefix = "" unless defined $prefix;
my $len_digits = length($digits);
my $node_fmt = "\%s\%0${len_digits}d";
my $group_fmt = "$node_fmt-" . substr($node_fmt, 2);
my $group = sprintf($group_fmt, $prefix, $digits,
$digits + $count);
$suffix = "" unless defined $suffix;
return $group . $suffix;
}
sub compress_range {
my @nodes = @_;
if (@nodes == 1 and ref $nodes[0]) {
@nodes = @{$nodes[0]};
}
unless (@nodes) {
_range_warn("No nodes specified.");
return;
}
@nodes = _sort_nodes(\@nodes);
my @result;
my ($prev_prefix, $prev_digits, $prev_suffix) = ("", undef, "");
my $prev_n;
my ($prefix, $digits, $suffix);
my $count = 0;
for my $n (@nodes) {
if ($n =~ /^$node_regex$/) {
($prefix, $digits, $suffix) = ($1, $2, $3);
$prefix = "" unless defined $prefix;
$suffix = "" unless defined $suffix;
#print defined $prefix ? $prefix : "(undef)", " - ", defined $digits ? $digits : "(undef)", " - ", defined $suffix ? $suffix : "(undef)", "\n";
} else {
($prefix, $digits, $suffix) = ($n, undef, undef);
}
if (defined $digits and
$prefix eq $prev_prefix and
$suffix eq $prev_suffix and
defined $prev_digits and
$digits == $prev_digits + $count + 1) {
$count++;
next;
}
if (defined $prev_n) {
if ($count > 0) {
push @result, _get_group($prev_prefix, $prev_digits, $count,
$prev_suffix);
} else {
push @result, $prev_n;
}
}
$prev_n = $n;
$prev_prefix = $prefix;
$prev_digits = $digits;
$prev_suffix = $suffix;
$count = 0;
}
if ($count > 0) {
push @result, _get_group($prev_prefix, $prev_digits, $count,
$prev_suffix);
} else {
push @result, $prev_n;
}
return join(",", @result);
}
sub nodes_parser {
my ($c, $r, $x) = @_;
my @range = ();
if (defined $r) {
push @range, $r;
}
if (defined $c) { push @range, '%' . $c; }
if (defined $x) { push @range, "-($x)" };
return sorted_expand_range(join(",", @range));
}
sub sorted_expand_range {
my @nodes = expand_range(@_);
return _sort_nodes(\@nodes);
}
my %current_clusters;
sub get_expanded_clusters {
return [keys %current_clusters];
}
sub expand_range {
my ($ranges) = @_; return () unless defined $ranges;
local $recursion = $recursion + 1;
confess "Max recursion hit in expand_range"
if $recursion > 20;
DEBUG("expand_range($ranges)");
if ($recursion == 1) {
DEBUG("resetting current_clusters");
%current_clusters = () ;
}
my %nodes;
for ($ranges) {
s/\s+//g; # Get rid of spaces
s/^"(.*)"$/$1/; # Dequote if quoted
# Igor has no notion of expanding "hosts" vs "groups" so
# we're only going to use a single syntax (%HOST:)
#s/@([A-Za-z0-9][^,]+)/%GROUPS:$1/g; # shorthand notation for groups
s/@([A-Za-z0-9][^,]+)/%HOSTS:$1/g; # shorthand notation for hosts
}
my @ranges = $ranges =~ m/$range_re/g;
for my $range (@ranges) {
# see if it's a special range
if (substr($range,0,1) eq "(") {
my $parens_text = $range;
my $content = ($parens_text =~ m/^($balanced_parens)$/)[0];
$content = substr($content, 1, -1);
my @nodes = expand_range($content);
@nodes{@nodes} = undef;
} elsif ($range =~ m{^&\|([^|]+)\|} ||
$range =~ m{^&\/([^\/]+)\/}) {
# filter
my $regex = qr/$1/;
while (my ($k, $v) = each(%nodes)) {
delete $nodes{$k} unless $k =~ /$regex/;
}
} elsif ($range =~ m{^-\|([^|]+)\|} ||
$range =~ m{^-\/([^\/]+)\/}) {
# filter not
my $regex = qr/$1/;
while (my ($k, $v) = each(%nodes)) {
delete $nodes{$k} if $k =~ /$regex/;
}
} elsif ($range =~ /^(%|-|&|\^|\*)(.+)$/) {
# special operators that modify the rest of the range
my ($special_op, $rest) = ($1, $2);
if ($special_op eq "%") {
# it's a cluster
my @nodes = get_cluster_nodes($rest);
@nodes{@nodes} = undef;
} elsif ($special_op eq "-") {
# delete nodes from nodes
my @nodes = expand_range($rest);
delete @nodes{@nodes};
} elsif ($special_op eq "&") {
# intersection
my @common_nodes = ();
my @nodes = expand_range($rest);
for my $node (@nodes) {
push @common_nodes, $node if exists $nodes{$node};
}
%nodes = (); @nodes{@common_nodes} = undef;
} elsif ($special_op eq "^") {
my @nodes = expand_range($rest);
my @admins = _get_admins_for(@nodes);
@nodes{@admins} = undef;
} elsif ($special_op eq "*") {
my @nodes = expand_range($rest);
my @clusters = _get_clusters_for(@nodes);
@nodes{@clusters} = undef;
}
} else {
# simple range
if ($range =~ /^
$node_regex
- # our separator is '-'
\1? # the prefix again, which is optional
(\d+) # and the end of the range
((?(3) \3 | # if the domain matched before, we want it here
(?:\.[-A-Za-z\d.]+)?)) # if it didn't then we can have a new
# one here, like foo1-3.search
$
/x)
{
my ($prefix, $start, $suf1, $end, $suf2) = ($1, $2, $3, $4, $5);
$prefix = "" unless defined $prefix;
my $suffix = "";
if (defined $suf1 and defined $suf2) {
if ($suf1 ne $suf2) {
warn "Different suffixes: $suf1 $suf2";
}
$suffix = $suf1;
} elsif (defined $suf2) {
$suffix = $suf2;
}
my $len = length($start);
# pad $end with leading characters from start so we can
# type 01-3 and expand that to 01,02,03 or maybe
# ks301000-9 for ks301000-301009
my $len_end = length($end);
$end = substr($start, 0, $len - $len_end) . $end
if $len_end < $len;
my @nodes = map {$_ = sprintf("$prefix%0${len}d$suffix", $_) }
($start .. $end);
@nodes{@nodes} = undef;
} elsif ($range =~ /{/) { # }
my @nodes = expand_range(join(",", _expand_braces($range)));
@nodes{@nodes} = undef;
} else {
# single machine
$nodes{$range} = undef;
}
}
}
return keys %nodes;
}
sub get_cluster_nodes {
my $cluster = shift;
$cluster =~ s/HOSTS://;
$current_clusters{$cluster} = 1;
DEBUG("expanding $cluster");
my $role = $IGOR->get_role(split(/\./,$cluster,2));
if ($IGOR->{'use_expand_cache'}) {
my $members = $role->get_members();
my $hosts = $members->expand();
my $base = $members->based_on();
for (@$base) {
$current_clusters{$_} = 1;
}
return @$hosts;
}
my $members = $role->get_members()->get_range();
DEBUG(" $members");
my $nodes = [expand_range($members)];
return @$nodes;
=for ignoring
my $cluster = shift;
my @clusters = expand_range($cluster);
my @result;
for $cluster (@clusters) {
die "Malformed cluster name $cluster"
unless $cluster =~ /^([-\w.]+):?([-\w.]+)?$/;
$cluster = $1;
my $part = defined($2) ? $2 : "CLUSTER";
my %keys = _get_cluster_keys($cluster);
push @result, expand_range($keys{$part});
}
return @result;
=cut
}
{
my %nodes_admin;
sub _populate_nodes_admin {
my @admins = expand_range('%HOSTS:KEYS');
for my $admin (@admins) {
my @admin_nodes = expand_range("\%HOSTS:$admin");
for my $node (@admin_nodes) {
$nodes_admin{$node} = $admin;
}
}
}
sub _get_admins_for {
my @nodes = @_;
_populate_nodes_admin() unless %nodes_admin;
my %results;
for my $node (@nodes) {
my $admin = $nodes_admin{$node};
if ($admin) {
$results{$admin}++;
} else {
_range_warn("$node: admin not found");
}
}
return keys %results;
}
}
{
my $nodes_cluster;
sub _populate_nodes_cluster {
$nodes_cluster = retrieve(NODE_CLUSTER);
unless ($nodes_cluster) {
_range_warn("Can't read node_cluster.dat");
return;
}
}
sub _get_clusters_for {
my @nodes = @_;
_populate_nodes_cluster() unless $nodes_cluster;
my %results;
for my $node (@nodes) {
my $cluster = $nodes_cluster->{$node};
if ($cluster) {
$results{$cluster}++;
} else {
_range_warn("$node: cluster not found");
}
}
return keys %results;
}
}
sub _expand_braces {
my $range = shift;
my @todo = ($range);
my @results;
local $_;
while (@todo) {
$_ = shift(@todo);
if (/^(.*)($balanced_braces)(.*)$/) {
my ($pre, $braces, $post) = ($1, $2, $3);
my @braces = expand_range(substr($braces, 1, -1));
for my $elt (@braces) {
if ($pre =~ /{/) {
push @todo, "$pre$elt$post";
} else {
push @results, "$pre$elt$post";
}
}
} else {
push @results, $_;
}
}
return @results;
}
sub _get_cluster_keys {
my $cluster = shift;
my @lines = _read_cluster_file($cluster);
return unless @lines;
my (%keys, $current_key, @current_range);
for (@lines) {
s/#.*$//; s/\s+$//;
s/\$(\w+)/\%$cluster:$1/g; # Turn $MACRO into %cluster:MACRO
next unless /\S/;
my $joinsep;
if (/^\s/ && $current_key) {
if (/^\s+INCLUDE/) {
s/^\s+INCLUDE\s+//;
} elsif (/^\s+EXCLUDE/) {
s/^\s+EXCLUDE\s+(.*)/-($1)/;
} else {
die "RangeError: $_: don't know how to parse that";
}
s/\s+//; # strip white space
push @current_range, $_;
} else { # New Key
# save old key info if it exists
$keys{$current_key} = join(",", @current_range) if $current_key;
$current_key = $_;
@current_range = ();
}
}
$keys{$current_key} = join(",", @current_range) if $current_key;
$keys{KEYS} = join(",", keys %keys);
$keys{UP} = "\%${cluster}";
$keys{DOWN} = "\%${cluster}:ALL,-\%${cluster}:CLUSTER";
$keys{VIPS} = _get_cluster_vips($cluster);
return %keys;
}
sub _get_cluster_file {
my $cluster = shift;
my ($fh, $filename);
my $alt = $range_altpath || "";
($filename) = grep( -e $_ ,
"$alt/$cluster/tools/conf/nodes.cf",
"$alt/$cluster/nodes.cf",
"/home/seco/tools/conf/$cluster/nodes.cf",
"/usr/local/gemclient/$cluster/nodes.cf");
$filename or _range_warn("$cluster: missing on this machine");
return $filename;
}
sub _read_big_file {
my $filename = shift;
open my $big_fh, '<', $filename or do {
_range_warn("$filename: $!");
return;
};
my @result;
while (<$big_fh>) {
if (/^\$INCLUDE\s+"([^"]+)"/) {
my $include = $1;
my $relative_dir = "./";
if ($include !~ m{^/}) {
# it's a relative PATH, prepend the dir for the cur file
if ($filename =~ m{^(.*/)}) {
$relative_dir = $1;
}
}
push @result, _read_big_file("$relative_dir$filename");
} else {
push @result, $_;
}
}
close $big_fh;
return wantarray() ? @result : join("", @result);
}
sub _read_cluster_file {
my $cluster = shift;
my $filename = _get_cluster_file($cluster);
my @lines = _read_big_file($filename);
# TODO: parse $INCLUDE
return @lines;
}
sub _range_warn {
my $warn = shift;
warn "$warn\n" if -t STDIN && -t STDOUT;
}
sub _open_cluster_vips {
my $cluster = shift;
my ($fh, $filename);
my $alt = $range_altpath || "";
($filename) = grep ( -e $_,
"$alt/$cluster/tools/conf/vips.cf",
"$alt/$cluster/vips.cf",
"/home/seco/tools/conf/$cluster/vips.cf");
$filename ||= "/dev/null";
$fh = new IO::File "$filename", "r";
return $fh;
}
sub _get_cluster_vips {
my $cluster = shift;
my $fh = _open_cluster_vips($cluster);
my (@vips);
while (<$fh>) {
s/#.*$//;
s/\s+$//;
next unless /\S/;
my($vip) = split(/\s+/);
push(@vips,$vip) if ($vip =~ m/./);
}
close($fh);
return join(",",@vips);
}
1;
__END__
=head1 FUNCTIONS
=head2 compress_range
$string = compress_range(\@nodes)
$string = compress_range(@nodes)
=head2 expand_range
@nodes = expand_range($range) # @nodes in random order, faster
=head2 sorted_expand_range
@nodes = sorted_expand_range($range) # @nodes in a nice order
=head2 get_cluster_nodes
@nodes = get_cluster_nodes("ks301"); # random order
=head2 range_set_altpath
range_set_altpath("/usr/local/gemclient")
# look for cluster definitions in the directory specified
=head1 RANGE SYNTAX
=head2 SIMPLE RANGES
node1,node2,node3,node4 == node1-node4 == node1-4
node1000-1099 == node1000-99 # auto pads digits to the end of the range
1-100 # numeric only ranges
foo1-2.search.scd.yahoo.com ==
foo1.search.scd.yahoo.com-foo2.search.scd.yahoo.com # domain support
209.131.40.1-209.131.40.255 == 209.131.40.1-255 # IP ranges
=head2 CLUSTERS
%ks301 == nodes defined in ks301/nodes.cf - Default Section CLUSTER
%ks301:ALL == nodes defined in a specific section of ks301/nodes.cf
%ks301:VIPS == IPs in ks301/vips.cf
=head2 SPECIAL CLUSTERS
%HOSTS and %GROUPS
%HOSTS:haides has all the hosts that haides is responsible for.
@haides is a shortcut for the above.
%GROUPS:ADMIN has all the machines in the group ADMIN
@ADMIN is a shortcut.
=head2 OPERATIONS
range1,range2 == union
range1,-range2 == set difference
range1,&range2 == intersection
^range1 == admins for the nodes in range1
range1,-(range2,range3) == () can be used for grouping
range1,&|regex| # all nodes in range1 that match regex
range1,-|regex| # all nodes in range1 that do not match regex
/regex/ == all nodes that match regex (it does matching against @ALL)
The difference between |regex| and /regex/ is that |regex| does the
matching against the left side of the expression, while /regex/ does
the matching against all nodes. Therefore
fornode.pl -r /ks30/ -l # makes sense
fornode.pl -r |ks30| -l # doesn't make sense since there's nothing to the left
=head2 MORE ADVANCED RANGES
foo{1,3,5} == foo1,foo3,foo5
%ks30{1,3} == %ks301,%ks303
%ks301-7 == nodes in clusters ks301 to ks307
%all:KEYS == all defined sections in cluster all
%{%all} == expands all clusters in %all
%all:sc5,-({f,k}s301-7) == names for clusters in sc5 except ks301-7,fs301-7
%all:sc5,-|ks| == clusters in sc5, except those matching ks
=head1 BUGS
Need more docs
=head1 AUTHOR
Daniel Muino <[email protected]>
=cut