| 1 | # | ||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | package Rex::CMDB::YAML; | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 6 | ▶ | use v5.14.4; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 6 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 6 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 6 | ▶ | use base qw(Rex::CMDB::Base); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 6 | ▶ | use Rex::Commands -no => [qw/get/]; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 6 | ▶ | use Rex::Logger; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 6 | ▶ | use YAML; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 6 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 6 | ▶ | use Hash::Merge qw/merge/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 6 | require Rex::Commands::File; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 10 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=15.8
Pod
undocumented
| |||||||||||||||||||||
| 23 | 10 | my $that = shift; | |||||||||||||||||||
| 24 | 10 | ▶ | my $proto = ref($that) || $that; | ||||||||||||||||||
|
Condition33%1/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table33%1/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||
| 25 | 10 | my $self = {@_}; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 10 | $self->{merger} = Hash::Merge->new(); | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 10 | ▶ | if ( !defined $self->{merge_behavior} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | $self->{merger}->specify_behavior( | ||||||||||||||||||||
| 31 | { | ||||||||||||||||||||
| 32 | SCALAR => { | ||||||||||||||||||||
| 33 | 22 | ▶ | SCALAR => sub { $_[0] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 0 | ▶ | ARRAY => sub { $_[0] }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 35 | 0 | ▶ | HASH => sub { $_[0] }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||
| 37 | ARRAY => { | ||||||||||||||||||||
| 38 | 0 | ▶ | SCALAR => sub { $_[0] }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 39 | 11 | ▶ | ARRAY => sub { $_[0] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 0 | ▶ | HASH => sub { $_[0] }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||
| 42 | HASH => { | ||||||||||||||||||||
| 43 | 0 | ▶ | SCALAR => sub { $_[0] }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 44 | 0 | ▶ | ARRAY => sub { $_[0] }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 45 | 123 | ▶ | HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||
| 48 | 8 | 'REX_DEFAULT', | |||||||||||||||||||
| 49 | ); # first found value always wins | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 8 | $self->{merger}->set_behavior('REX_DEFAULT'); | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | else { | ||||||||||||||||||||
| 54 | 2 | ▶ | if ( ref $self->{merge_behavior} eq 'HASH' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | $self->{merger} | ||||||||||||||||||||
| 56 | 0 | ->specify_behavior( $self->{merge_behavior}, 'USER_DEFINED' ); | |||||||||||||||||||
| 57 | 0 | $self->{merger}->set_behavior('USER_DEFINED'); | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||
| 60 | 2 | $self->{merger}->set_behavior( $self->{merge_behavior} ); | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 10 | bless( $self, $proto ); | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 10 | return $self; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 43 | ▶ | sub get { | ||||||||||||||||||
|
Subroutine
get: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||
| 70 | 43 | my ( $self, $item, $server ) = @_; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 43 | $server = $self->__get_hostname_for($server); | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 43 | my $result = {}; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 43 | ▶ | if ( $self->__cache->valid( $self->__cache_key() ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 10 | $result = $self->__cache->get( $self->__cache_key() ); | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | else { | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 33 | my @files = $self->_get_cmdb_files( $item, $server ); | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 33 | Rex::Logger::debug( Dumper( \@files ) ); | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | # configuration variables | ||||||||||||||||||||
| 86 | 33 | my $config_values = Rex::Config->get_all; | |||||||||||||||||||
| 87 | 33 | my %template_vars; | |||||||||||||||||||
| 88 | 33 | for my $key ( keys %{$config_values} ) { | |||||||||||||||||||
| 89 | 37 | ▶ | if ( !exists $template_vars{$key} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 37 | $template_vars{$key} = $config_values->{$key}; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 33 | $template_vars{environment} = Rex::Commands::environment(); | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 33 | for my $file (@files) { | |||||||||||||||||||
| 96 | 132 | Rex::Logger::debug("CMDB - Opening $file"); | |||||||||||||||||||
| 97 | 132 | ▶ | if ( -f $file ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 94 | my $content = eval { local ( @ARGV, $/ ) = ($file); <>; }; | |||||||||||||||||||
| 100 | 94 | my $t = Rex::Config->get_template_function(); | |||||||||||||||||||
| 101 | 94 | $content .= "\n"; # for safety | |||||||||||||||||||
| 102 | 94 | $content = $t->( $content, \%template_vars ); | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 94 | my $ref = YAML::Load($content); | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 94 | $result = $self->{merger}->merge( $result, $ref ); | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 43 | ▶ | if ( defined $item ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 17 | return $result->{$item}; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||
| 115 | 26 | return $result; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 39 | ▶ | sub _get_cmdb_files { | ||||||||||||||||||
|
Subroutine
_get_cmdb_files: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 121 | 39 | my ( $self, $item, $server ) = @_; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 39 | $server = $self->__get_hostname_for($server); | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 39 | my @files; | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 39 | ▶ | if ( !ref $self->{path} ) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 128 | 35 | my $env = Rex::Commands::environment(); | |||||||||||||||||||
| 129 | 35 | my $server_file = "$server.yml"; | |||||||||||||||||||
| 130 | 35 | my $default_file = 'default.yml'; | |||||||||||||||||||
| 131 | @files = ( | ||||||||||||||||||||
| 132 | File::Spec->join( $self->{path}, $env, $server_file ), | ||||||||||||||||||||
| 133 | File::Spec->join( $self->{path}, $env, $default_file ), | ||||||||||||||||||||
| 134 | File::Spec->join( $self->{path}, $server_file ), | ||||||||||||||||||||
| 135 | 35 | File::Spec->join( $self->{path}, $default_file ), | |||||||||||||||||||
| 136 | ); | ||||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | elsif ( ref $self->{path} eq "CODE" ) { | ||||||||||||||||||||
| 139 | 2 | @files = $self->{path}->( $self, $item, $server ); | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | elsif ( ref $self->{path} eq "ARRAY" ) { | ||||||||||||||||||||
| 142 | 2 | @files = @{ $self->{path} }; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 39 | my $os = Rex::Hardware::Host->get_operating_system(); | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | @files = map { | ||||||||||||||||||||
| 148 | 39 | $self->_parse_path( $_, { hostname => $server, operatingsystem => $os, } ) | |||||||||||||||||||
| 149 | } @files; | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 39 | return @files; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 6 | 1; | |||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||