/usr/share/perl5/Rex/CMDB/YAML.pm is in rex 1.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
package Rex::CMDB::YAML;
use strict;
use warnings;
our $VERSION = '1.4.1'; # VERSION
use base qw(Rex::CMDB::Base);
use Rex::Commands -no => [qw/get/];
use Rex::Logger;
use YAML;
use Data::Dumper;
use Hash::Merge qw/merge/;
require Rex::Commands::File;
sub new {
my $that = shift;
my $proto = ref($that) || $that;
my $self = {@_};
$self->{merger} = Hash::Merge->new();
if ( !defined $self->{merge_behavior} ) {
$self->{merger}->specify_behavior(
{
SCALAR => {
SCALAR => sub { $_[0] },
ARRAY => sub { $_[0] },
HASH => sub { $_[0] },
},
ARRAY => {
SCALAR => sub { $_[0] },
ARRAY => sub { $_[0] },
HASH => sub { $_[0] },
},
HASH => {
SCALAR => sub { $_[0] },
ARRAY => sub { $_[0] },
HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
},
},
'REX_DEFAULT',
); # first found value always wins
$self->{merger}->set_behavior('REX_DEFAULT');
}
else {
if ( ref $self->{merge_behavior} eq 'HASH' ) {
$self->{merger}
->specify_behavior( $self->{merge_behavior}, 'USER_DEFINED' );
$self->{merger}->set_behavior('USER_DEFINED');
}
else {
$self->{merger}->set_behavior( $self->{merge_behavior} );
}
}
bless( $self, $proto );
return $self;
}
sub get {
my ( $self, $item, $server ) = @_;
# first open $server.yml
# second open $environment/$server.yml
# third open $environment/default.yml
# forth open default.yml
my (@files);
if ( !ref $self->{path} ) {
my $env = environment;
my $yaml_path = $self->{path};
@files = (
"$yaml_path/$env/$server.yml", "$yaml_path/$env/default.yml",
"$yaml_path/$server.yml", "$yaml_path/default.yml"
);
}
elsif ( ref $self->{path} eq "CODE" ) {
@files = $self->{path}->( $self, $item, $server );
}
elsif ( ref $self->{path} eq "ARRAY" ) {
@files = @{ $self->{path} };
}
@files = map { $self->_parse_path($_) } @files;
my $all = {};
Rex::Logger::debug( Dumper( \@files ) );
# configuration variables
my $config_values = Rex::Config->get_all;
my %template_vars;
for my $key ( keys %{$config_values} ) {
if ( !exists $template_vars{$key} ) {
$template_vars{$key} = $config_values->{$key};
}
}
$template_vars{environment} = Rex::Commands::environment();
for my $file (@files) {
Rex::Logger::debug("CMDB - Opening $file");
if ( -f $file ) {
my $content = eval { local ( @ARGV, $/ ) = ($file); <>; };
my $t = Rex::Config->get_template_function();
$content .= "\n"; # for safety
$content = $t->( $content, \%template_vars );
my $ref = YAML::Load($content);
$all = $self->{merger}->merge( $all, $ref );
}
}
if ( !$item ) {
return $all;
}
else {
return $all->{$item};
}
Rex::Logger::debug("CMDB - no item ($item) found");
return;
}
1;
|