lib/Rex/CMDB.pm

filter: Statementstmt 83.6% 46 / 55 Branchbran 55.5% 10 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 50.0% 1 / 2 totaltotal 78.8% 67 / 85 CC 11 SCAR 18.5
CC
11
Cov
77%
__ANON__
15.3
cmdb
3.0
cmdb_active
2.0
CRAP 6.3
« lib/Rex/Service/Ubuntu.pm ↑ Summary lib/Rex/Interface/File/HTTP.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::CMDB - Function to access the CMDB (configuration management database)
8  
9 =head1 DESCRIPTION
10  
11 This module exports a function to access a CMDB via a common interface. When the L<0.51 feature flag|Rex#0.51> or later is used, the CMDB is enabled by default with L<Rex::CMDB::YAML> as the default provider.
12  
13 =head1 SYNOPSIS
14  
15 use Rex::CMDB;
16  
17 set cmdb => {
18 type => 'YAML',
19 path => [ 'cmdb/{hostname}.yml', 'cmdb/default.yml', ],
20 merge_behavior => 'LEFT_PRECEDENT',
21 };
22  
23 task 'prepare', 'server1', sub {
24 my %all_information = get cmdb;
25 my $specific_item = get cmdb('item');
26 my $specific_item_for_server = get cmdb( 'item', 'server' );
27 };
28  
29 =head1 EXPORTED FUNCTIONS
30  
31 =cut
32  
33 package Rex::CMDB;
34  
35 51 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 49 our $VERSION = '1.16.1'; # VERSION
39  
40 51 ▶use Rex::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 50 ▶use Rex::Value;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 49 require Rex::Exporter;
44 49 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 49 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 49 @EXPORT = qw(cmdb);
47  
48 49 my $CMDB_PROVIDER;
49  
50 =head2 set cmdb
51  
52 set cmdb => {
53 type => 'YAML',
54 %provider_options,
55 };
56  
57 Instantiate a specific C<type> of CMDB provider with the given options. Returns the provider instance.
58  
59 Please consult the documentation of the given provider for their supported options.
60  
61 =cut
62  
63 Rex::Config->register_set_handler(
64 10 ▶ "cmdb" => sub {
Subroutine __ANON__: called CC=8 SCAR=27.3
65 10 my ($option) = @_;
66  
67 10 my %args = Rex::Args->getopts;
68  
69 10 ▶ if ( exists $args{O} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"O"}) 0 10 10
70 0 for my $itm ( split( /;/, $args{O} ) ) {
71 0 my ( $key, $val ) = split( /=/, $itm );
72 0 ▶ if ( $key eq "cmdb_path" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "cmdb_path") 0 0 0
73 0 ▶ if ( ref $option->{path} eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $option->{'path'} eq 'ARRAY') { } 0 0 0
74 0 unshift @{ $option->{path} }, $val;
75 }
76 else {
77 0 $option->{path} = [$val];
78 }
79 }
80 }
81 }
82  
83 10 my $klass = $option->{type};
84  
85 10 ▶ if ( !$klass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($klass) 0 10 10
86  
87 # no cmdb set
88 0 return;
89 }
90  
91 10 ▶ if ( $klass !~ m/::/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($klass =~ /::/u) 10 0 10
92 10 $klass = "Rex::CMDB::$klass";
93 }
94  
95 10 ▶ eval "use $klass";
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 10 10
98 0 die("CMDB provider ($klass) not found: $@");
99 }
100  
101 10 $CMDB_PROVIDER = $klass->new( %{$option} );
102 }
103 49 );
104  
105 =head2 cmdb([$item, $server])
106  
107 Function to query a CMDB.
108  
109 If called without arguments, it returns the full CMDB data structure for the current connection.
110  
111 If only a defined C<$item> is passed, it returns only the value for the given CMDB item, for the current connection.
112  
113 If only a defined C<$server> is passed, it returns the whole CMDB data structure for the given server.
114  
115 If both C<$item> and C<$server> are defined, it returns the given CMDB item for the given server.
116  
117 The value returned is a L<Rex::Value>, so you may need to use the C<get cmdb(...)> form if you'd like to assign the result to a Perl variable:
118  
119 task 'prepare', 'server1', sub {
120 my %all_information = get cmdb;
121 my $specific_item = get cmdb('item');
122 my $specific_item_for_server = get cmdb( 'item', 'server' );
123 };
124  
125 If caching is enabled, this function caches the full data structure for the given server under the C<cmdb/$CMDB_PROVIDER/$server> cache key after the first query.
126  
127 =cut
128  
129 35 ▶sub cmdb {
Subroutine cmdb: called CC=3 SCAR=11.0
Pod documented
130 35 my ( $item, $server ) = @_;
131  
132 35 ▶ return if !cmdb_active();
Branch: 1 branch
BranchTrueFalseTotal
unless cmdb_active() 10 25 35
133  
134 25 $CMDB_PROVIDER->__warm_up_cache_for($server);
135  
136 25 my $value = $CMDB_PROVIDER->get( $item, $server );
137  
138 25 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 22 3 25
139 22 return Rex::Value->new( value => $value );
140 }
141 else {
142 3 Rex::Logger::debug("CMDB - no item ($item) found");
143 3 return;
144 }
145 }
146  
147 54 ▶sub cmdb_active {
Subroutine cmdb_active: called CC=2 SCAR=6.9
Pod undocumented
148 54 ▶ return ( $CMDB_PROVIDER ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$CMDB_PROVIDER ? : 29 25 54
149 }
150  
151 49 1;
« lib/Rex/Service/Ubuntu.pm ↑ Summary lib/Rex/Interface/File/HTTP.pm »