bin/cfengine-tags

filter: Statementstmt 28.0% 39 / 139 Branchbran 1.9% 1 / 52 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 73.3% 11 / 15 Podpod n/a totaltotal 19.6% 51 / 260 CC 44 SCAR 65.7
CC
44
Cov
18%
process
1190.0
run
90.0
__ANON__
12.0
CRAP 712.4
« bin/racktables-check ↑ Summary blib/lib/RackMan/Utils.pm »
1 #!/usr/bin/env perl
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::MoreUtils qw< uniq >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use RackMan;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use RackMan::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use RackMan::Tasks;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 1 $::PROGRAM = "Cfengine tags generator";
16 1 $::VERSION = "1.07";
17 1 $::COMMAND = basename($0);
18  
19  
20 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 CONFIG_SECTION => "cfengine-tags",
22 1 };
23  
24  
25 #
26 # main
27 #
28 MAIN: {
29 1 ▶ if (not caller()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller()) 0 1 1
30 0 run();
31 0 exit RackMan->status;
32 }
33 }
34  
35  
36 #
37 # run()
38 # ---
39 0 ▶sub run {
Subroutine run: not called CC=9 SCAR=45.0
40 # detect if stdout is connected to a terminal
41 0 ▶ (-t STDOUT ? $Term::ANSIColor::AUTORESET : $ENV{ANSI_COLORS_DISABLED}) = 1;
Branch: 1 branch
BranchTrueFalseTotal
-t STDOUT ? : 0 0 0
42 0 $|++;
43  
44 # default options
45 0 my %options = (
46 config => "/usr/local/etc/rack.conf",
47 );
48  
49 # parse options
50 0 Getopt::Long::Configure(qw< no_auto_abbrev no_ignore_case >);
51 0 ▶ GetOptions(\%options, qw{
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%options, ("help|usage|h!", "man!", "version|V!", "verbose|v!", "config|c=s", "output-path|path|O=s", "attrs|A=s", "tags|T=s", "filter|F=s", "class_names|class-names|N")) or pod2usage(0) 0 0 0
52 help|usage|h! man! version|V!
53 verbose|v! config|c=s output-path|path|O=s
54 attrs|A=s tags|T=s filter|F=s class_names|class-names|N
55 }) or pod2usage(0);
56  
57 # handle --version, --help and --man
58 0 ▶ $options{man} and pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
$options{"man"} and pod2usage(2) 0 0 0
59 0 ▶ $options{help} and pod2usage(1);
Branch: 1 branch
BranchTrueFalseTotal
$options{"help"} and pod2usage(1) 0 0 0
60 0 ▶ $options{version} and print "$::PROGRAM v$::VERSION\n" and exit;
Branch: 1 branch
BranchTrueFalseTotal
$options{"version"} and print "$main::PROGRAM v$main::VERSION\n" and exit 0 0 0
Condition0%0/3
$options{"version"} and print "$main::PROGRAM v$main::VERSION\n"
!ll&&!rl&&r
000
MC/DC0%0/2
$options{"version"} and print "$main::PROGRAM v$main::VERSION\n"
$options{"version"} print "$main::PROGRAM v$main::VERSION\n"
Truth table0%0/3
A and B
A= $options{"version"}B= print "$main::PROGRAM v$main::VERSION\n"
ABresult
0X0
100
111
61  
62 # read configuration file
63 0 my $config = RackMan::Config->new(-file => $options{config});
64  
65 # check for mandatory parameters
66 0 ▶ RackMan->error("missing config parameter [".CONFIG_SECTION."]/path")
Branch: 1 branch
BranchTrueFalseTotal
unless $config->val("cfengine-tags", "path") 0 0 0
67 if not $config->val(CONFIG_SECTION, "path");
68  
69 # instanciate the backend object
70 0 my $rackman = RackMan->new({ options => \%options, config => $config });
71  
72 # do the actual work
73 0 ▶ push @ARGV, $config->val(CONFIG_SECTION, "type", "server") if not @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 0 0
74 0 process($rackman, @ARGV);
75 }
76  
77  
78 #
79 # pod2usage()
80 # ---------
81 0 ▶sub pod2usage {
Subroutine pod2usage: not called CC=1 SCAR=6.9
82 0 my ($n) = @_;
83 0 require Pod::Usage;
84 0 Pod::Usage::pod2usage({ -exitval => 0, -verbose => $n, -noperldoc => 1 });
85 }
86  
87  
88 #
89 # process()
90 # -------
91 0 ▶sub process {
Subroutine process: not called CC=34 SCAR=70.8
92 0 my ($rackman, @args) = @_;
93  
94 # construct the list of wanted tags, if any
95 my $tags_list = $rackman->options->{tags}
96 0 ▶ || $rackman->config->val(CONFIG_SECTION, "tags", "");
Condition0%0/3
$rackman->options->{'tags'} || $rackman->config->val('cfengine-tags', 'tags', '')
l!l&&r!l&&!r
000
MC/DC0%0/2
$rackman->options->{'tags'} || $rackman->config->val('cfengine-tags', 'tags', '')
$rackman->options->{'tags'} $rackman->config->val('cfengine-tags', 'tags', '')
Truth table0%0/3
A || B
A= $rackman->options->{'tags'}B= $rackman->config->val('cfengine-tags', 'tags', '')
ABresult
1X1
011
000
97 0 $tags_list =~ s/^ +| +$//g;
98 0 my %wanted_tag = map { $_ => 1 } split / *, */, $tags_list;
99  
100 # construct the list of wanted attributes, if any
101 my @attrs = split / *, */, $rackman->options->{attrs}
102 0 ▶ || $rackman->config->val(CONFIG_SECTION, "attrs", "");
Condition0%0/3
$rackman->options->{'attrs'} || $rackman->config->val('cfengine-tags', 'attrs', '')
l!l&&r!l&&!r
000
MC/DC0%0/2
$rackman->options->{'attrs'} || $rackman->config->val('cfengine-tags', 'attrs', '')
$rackman->options->{'attrs'} $rackman->config->val('cfengine-tags', 'attrs', '')
Truth table0%0/3
A || B
A= $rackman->options->{'attrs'}B= $rackman->config->val('cfengine-tags', 'attrs', '')
ABresult
1X1
011
000
103  
104 # construct the filter
105 0 my %filter;
106 my $filter = $rackman->options->{filter}
107 0 ▶ || $rackman->config->val(CONFIG_SECTION, "filter", "");
Condition0%0/3
$rackman->options->{'filter'} || $rackman->config->val('cfengine-tags', 'filter', '')
l!l&&r!l&&!r
000
MC/DC0%0/2
$rackman->options->{'filter'} || $rackman->config->val('cfengine-tags', 'filter', '')
$rackman->options->{'filter'} $rackman->config->val('cfengine-tags', 'filter', '')
Truth table0%0/3
A || B
A= $rackman->options->{'filter'}B= $rackman->config->val('cfengine-tags', 'filter', '')
ABresult
1X1
011
000
108 0 ▶ if ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 0 0 0
109 0 for my $token (split / *, */, $filter) {
110 0 ▶ if (index($token, "tag:") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($token, "tag:") == 0) 0 0 0
111 0 $filter{tag}{ lc substr($token, 4) } = 1;
112 next
113 0 }
114  
115 0 ▶ if (index($token, "=") > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($token, "=") > 0) 0 0 0
116 0 my ($name, $value) = split /=/, $token;
117 0 push @{ $filter{attr}{$name} }, $value;
118 next
119 0 }
120 }
121 }
122  
123 # NOTE: I know that the way to first list the devices, then find their
124 # tags, then construct the reverse hash by tags, is pretty inefficient
125 # but it's also pretty easy to code, way easier for me than trying to
126 # find the correct SQL requests (or the DBIx::Class equivalents) :p
127  
128 # fetch the list of devices of the given type(s)
129 0 my @types = map { split /[, ]+/ } @args;
130 my @devices = map {
131 0 RackMan::Tasks->task_list({ rackman => $rackman, type => $_ })
132 } @types;
133  
134 # construct the lists of devices for each tag
135 0 my %devices_by_tag;
136  
137 0 for my $device (@devices) {
138 0 ▶ next if $device->{name} eq "(null)";
Branch: 1 branch
BranchTrueFalseTotal
if $device->{"name"} eq "(null)" 0 0 0
139 0 my $rackdev = $rackman->device_by_id($device->{id});
140  
141 # fetch the tags for this RackObject, filter them if needed
142 0 my @tags = map { s/\W+/_/g; $_ } map lc,
143 0 @{ $rackdev->explicit_tags },
144 0 @{ $rackdev->implicit_tags };
145 0 ▶ @tags = grep $wanted_tag{$_}, @tags if $tags_list;
Branch: 1 branch
BranchTrueFalseTotal
if $tags_list 0 0 0
146 0 ▶ next unless @tags;
Branch: 1 branch
BranchTrueFalseTotal
unless @tags 0 0 0
147  
148 # fetch the attributes of this RackObject
149 0 my %attr = %{ $rackdev->attributes };
150  
151 # filter out devices without the requested tags or attributes
152 0 ▶ if ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 0 0 0
153 # filter on tags
154 0 ▶ if (exists $filter{tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $filter{"tag"}) 0 0 0
155 0 ▶ next unless grep $filter{tag}{$_}, @tags;
Branch: 1 branch
BranchTrueFalseTotal
unless grep $filter{"tag"}{$_}, @tags 0 0 0
156 }
157  
158 # filter on attributes
159 0 ▶ if (exists $filter{attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $filter{"attr"}) 0 0 0
160 0 my $keep = 0;
161  
162 ATTR:
163 0 for my $name (keys %{ $filter{attr} }) {
164 0 for my $value (@{ $filter{attr}{$name} }) {
165 0 ▶ next unless exists $attr{$name} and defined $attr{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr{$name} and defined $attr{$name} 0 0 0
Condition0%0/3
exists $attr{$name} and defined $attr{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $attr{$name} and defined $attr{$name}
exists $attr{$name} defined $attr{$name}
Truth table0%0/3
A and B
A= exists $attr{$name}B= defined $attr{$name}
ABresult
0X0
100
111
166 0 ▶ $keep ||= $value eq $attr{$name};
Condition0%0/3
$keep ||= $value eq $attr{$name}
l!l&&r!l&&!r
000
MC/DC0%0/2
$keep ||= $value eq $attr{$name}
$keep $value eq $attr{$name}
Truth table0%0/3
A ||= B
A= $keepB= $value eq $attr{$name}
ABresult
1X1
011
000
167 0 ▶ last ATTR if $keep;
Branch: 1 branch
BranchTrueFalseTotal
if $keep 0 0 0
168 }
169 }
170  
171 0 ▶ next unless $keep;
Branch: 1 branch
BranchTrueFalseTotal
unless $keep 0 0 0
172 }
173 }
174  
175 # fetch the FQDN of this RackObject
176 0 my $fqdn = $attr{FQDN};
177 0 ▶ RackMan->warning("RackObject '$device->{name}' lacks a FQDN")
Branch: 1 branch
BranchTrueFalseTotal
unless $fqdn 0 0 0
Condition0%0/5
$fqdn || 'RackMan'->warning("RackObject '$device->{'name'}' lacks a FQDN") && (next)
l!l&&r!l&&!r
000
"RackMan"->warning(qq[RackObject '$device->{"name"}' lacks a FQDN]) and next
!ll
00
MC/DC0%0/2
$fqdn || 'RackMan'->warning("RackObject '$device->{'name'}' lacks a FQDN") && (next)
$fqdn "RackMan"->warning(qq[RackObject '$device->{"name"}' lacks a FQDN])
Truth table0%0/3
A || B
A= $fqdnB= "RackMan"->warning(qq[RackObject '$device->{"name"}' lacks a FQDN])
ABresult
1X1
011
000
178 and next unless $fqdn;
179  
180 # check if it correctly resolves
181 0 my (undef, undef, $addrtype, $length, @addrs) = gethostbyname($fqdn);
182 0 ▶ RackMan->warning("RackObject '$device->{name}' has a non resolvable FQDN")
Branch: 1 branch
BranchTrueFalseTotal
unless @addrs 0 0 0
Condition0%0/5
@addrs || 'RackMan'->warning("RackObject '$device->{'name'}' has a non resolvable FQDN") && (next)
l!l&&r!l&&!r
000
"RackMan"->warning(qq[RackObject '$device->{"name"}' has a non resolvable FQDN]) and next
!ll
00
MC/DC0%0/2
@addrs || 'RackMan'->warning("RackObject '$device->{'name'}' has a non resolvable FQDN") && (next)
@addrs "RackMan"->warning(qq[RackObject '$device->{"name"}' has a non resolvable FQDN])
Truth table0%0/3
A || B
A= @addrsB= "RackMan"->warning(qq[RackObject '$device->{"name"}' has a non resolvable FQDN])
ABresult
1X1
011
000
183 and next unless @addrs;
184  
185 # mogrify it for Cfengine
186 0 ▶ $fqdn =~ s/[-.]/_/g if $rackman->options->{class_names};
Branch: 1 branch
BranchTrueFalseTotal
if $rackman->options->{"class_names"} 0 0 0
187  
188  
189 # to avoid conflicts, construct a mapping of each tag to its
190 # "fully qualified name", which includes the parent tags
191 0 my (%tag_path, @comps);
192  
193 # (private function to linearize the tag tree)
194 0 ▶ my $walk; $walk = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
195 0 my ($node) = @_;
196  
197 0 ▶ if (keys %$node) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$node) { } 0 0 0
198 0 for my $tag (keys %$node) {
199 0 push @comps, $tag;
200 0 $walk->($node->{$tag});
201 0 pop @comps;
202 }
203 }
204 else {
205 0 $tag_path{$comps[0]} = join "_", reverse @comps;
206 }
207 0 };
208  
209 0 $walk->($rackdev->tag_tree);
210  
211 # post-process: lowercase, remove non-word chars
212 0 %tag_path = map { s/\W+/_/g; $_ } map lc, %tag_path;
213  
214  
215 # associate the FQDN to each corresponding tag
216 0 for my $tag (@tags) {
217 0 ▶ push @{ $devices_by_tag{$tag_path{$tag} || $tag} }, $fqdn;
Condition0%0/3
$tag_path{$tag} || $tag
l!l&&r!l&&!r
000
MC/DC0%0/2
$tag_path{$tag} || $tag
$tag_path{$tag} $tag
Truth table0%0/3
A || B
A= $tag_path{$tag}B= $tag
ABresult
1X1
011
000
218 }
219  
220 # also associate the FQDN to each requested attribute
221 0 for my $attr_name (@attrs) {
222 0 push @{ $devices_by_tag{ $attr{$attr_name} } }, $fqdn;
223 }
224 }
225  
226 # write the tags files
227 0 ▶ my $dir = $rackman->options->{"output-path"}
Condition0%0/3
$rackman->options->{'output-path'} || $rackman->config->val('cfengine-tags', 'path')
l!l&&r!l&&!r
000
MC/DC0%0/2
$rackman->options->{'output-path'} || $rackman->config->val('cfengine-tags', 'path')
$rackman->options->{'output-path'} $rackman->config->val('cfengine-tags', 'path')
Truth table0%0/3
A || B
A= $rackman->options->{'output-path'}B= $rackman->config->val('cfengine-tags', 'path')
ABresult
1X1
011
000
228 || $rackman->config->val(CONFIG_SECTION, "path");
229 0 mkpath $dir;
230  
231 0 for my $tag (keys %devices_by_tag) {
232 0 my $file = catfile($dir, $tag);
233 0 ▶ open my $fh, ">", $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or "RackMan"->error("can't write file '${file}': $!") 0 0 0
234 or RackMan->error("can't write file '$file': $!");
235 0 print {$fh} map "$_\n", uniq sort @{ $devices_by_tag{$tag} };
236 0 close $fh;
237 }
238 }
239  
240  
241 1 1
242  
243 __END__
« bin/racktables-check ↑ Summary blib/lib/RackMan/Utils.pm »