| 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
| |||||||||||
| 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
| |||||||||||
| 73 | 0 | ▶ | if ( ref $option->{path} eq "ARRAY" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 86 | |||||||||||
| 87 | # no cmdb set | ||||||||||
| 88 | 0 | return; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 10 | ▶ | if ( $klass !~ m/::/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 149 | } | ||||||||||
| 150 | |||||||||||
| 151 | 49 | 1; | |||||||||