File Coverage

blib/lib/Ixchel/Actions/sagan_include.pm
Criterion Covered Total %
statement 20 64 31.2
branch 0 16 0.0
condition n/a
subroutine 7 11 63.6
pod 2 4 50.0
total 29 95 30.5


line stmt bran cond sub pod time code
1             package Ixchel::Actions::sagan_include;
2              
3 1     1   88830 use 5.006;
  1         5  
4 1     1   6 use strict;
  1         2  
  1         35  
5 1     1   7 use warnings;
  1         2  
  1         68  
6 1     1   592 use File::Slurp;
  1         46248  
  1         94  
7 1     1   460 use YAML::XS qw(Dump);
  1         3555  
  1         74  
8 1     1   10 use File::Spec;
  1         1  
  1         63  
9 1     1   6 use base 'Ixchel::Actions::base';
  1         2  
  1         581  
10              
11             =head1 NAME
12              
13             Ixchel::Actions::sagan_include - Generates the instance specific include for a sagan instance.
14              
15             =head1 VERSION
16              
17             Version 0.2.0
18              
19             =cut
20              
21             our $VERSION = '0.2.0';
22              
23             =head1 CLI SYNOPSIS
24              
25             ixchel -a sagan_include [B<--np>] [B<-w>] [B<-i> ]
26              
27             =head1 CODE SYNOPSIS
28              
29             use Data::Dumper;
30              
31             my $results=$ixchel->action(action=>'sagan_include', opts=>{ w=>1, });
32              
33             print Dumper($results);
34              
35             =head1 DESCRIPTION
36              
37             Generates the Sagan include config.
38              
39             The base include used is .sagan.config. If .sagan.multi_instance is set to 1,
40             then .sagan.instances.$instance is merged on top of it using L
41             with RIGHT_PRECEDENT as below with arrays being replaced.
42              
43             ```
44             {
45             'SCALAR' => {
46             'SCALAR' => sub { $_[1] },
47             'ARRAY' => sub { [ $_[0], @{ $_[1] } ] },
48             'HASH' => sub { $_[1] },
49             },
50             'ARRAY' => {
51             'SCALAR' => sub { $_[1] },
52             'ARRAY' => sub { [ @{ $_[1] } ] },
53             'HASH' => sub { $_[1] },
54             },
55             'HASH' => {
56             'SCALAR' => sub { $_[1] },
57             'ARRAY' => sub { [ values %{ $_[0] }, @{ $_[1] } ] },
58             'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
59             },
60             }
61             ```
62              
63             If told to write it out, .sagan.config_base is used as the base directory to write
64             to with the file name being 'sagan-include.yaml' or in the case of multi instance
65             "sagan-include-$instance.yaml".
66              
67             .include is set to .sagan.config_base.'/sagan-rules.yaml' in the case of single
68             instance setups if .sagan.multi_instance is set to 1 then
69             .sagan.config_base."/sagan-rules-$instance.yaml"
70              
71             =head1 FLAGS
72              
73             =head2 -w
74              
75             Write out the configs.
76              
77             =head2 -i instance
78              
79             A instance to operate on.
80              
81             =head1 RESULT HASH REF
82              
83             .errors :: A array of errors encountered.
84             .status_text :: A string description of what was done and teh results.
85             .ok :: Set to zero if any of the above errored.
86              
87             =cut
88              
89       0 0   sub new_extra { }
90              
91             sub action_extra {
92 0     0 0   my $self = $_[0];
93              
94 0           my $config_base = $self->{config}{sagan}{config_base};
95              
96 0 0         if ( $self->{config}{sagan}{multi_instance} ) {
97 0           my @instances;
98              
99 0 0         if ( defined( $self->{opts}{i} ) ) {
100 0           @instances = ( $self->{opts}{i} );
101             } else {
102 0           @instances = keys( %{ $self->{config}{sagan}{instances} } );
  0            
103             }
104 0           foreach my $instance (@instances) {
105 0           my $filled_in;
106 0           eval {
107 0           my $base_config = $self->{config}{sagan}{config};
108              
109 0 0         if ( !defined( $self->{config}{sagan}{instances}{$instance} ) ) {
110 0           die( $instance . ' does not exist under .sagan.instances' );
111             }
112              
113 0           my $config = $self->{config}{sagan}{instances}{$instance};
114              
115 0           my $merger = Hash::Merge->new('RIGHT_PRECEDENT');
116             # # make sure arrays from the actual config replace any arrays in the defaultconfig
117             # $merger->add_behavior_spec(
118             # {
119             # 'SCALAR' => {
120             # 'SCALAR' => sub { $_[1] },
121             # 'ARRAY' => sub { [ $_[0], @{ $_[1] } ] },
122             # 'HASH' => sub { $_[1] },
123             # },
124             # 'ARRAY' => {
125             # 'SCALAR' => sub { $_[1] },
126             # 'ARRAY' => sub { [ @{ $_[1] } ] },
127             # 'HASH' => sub { $_[1] },
128             # },
129             # 'HASH' => {
130             # 'SCALAR' => sub { $_[1] },
131             # 'ARRAY' => sub { [ values %{ $_[0] }, @{ $_[1] } ] },
132             # 'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
133             # },
134             # },
135             # 'Ixchel',
136             # );
137 0           my %tmp_config = %{$config};
  0            
138 0           my %tmp_base_config = %{$base_config};
  0            
139 0           my $merged = $merger->merge( \%tmp_base_config, \%tmp_config );
140              
141 0           $merged->{include} = File::Spec->canonpath( $config_base . '/sagan-rules-' . $instance . '.yaml' );
142              
143 0           $filled_in = '%YAML 1.1' . "\n" . Dump($merged);
144              
145 0 0         if ( $self->{opts}{w} ) {
146 0           write_file( $config_base . '/sagan-include-' . $instance . '.yaml', $filled_in );
147             }
148             };
149 0 0         if ($@) {
150 0           $self->status_add(
151             error => 1,
152             status => '-----[ Errored: '
153             . $instance
154             . ' ]-------------------------------------' . "\n" . '# '
155             . $@ . "\n"
156             );
157             } else {
158 0           $self->status_add( status => '-----[ '
159             . $instance
160             . ' ]-------------------------------------' . "\n"
161             . $filled_in
162             . "\n" );
163             }
164              
165             } ## end foreach my $instance (@instances)
166             } else {
167 0 0         if ( defined( $self->{opts}{i} ) ) {
168 0           $self->status_add(
169             error => 1,
170             status => '-i may not be used in single instance mode, .sagan.multi_instance=0'
171             );
172 0           return undef;
173             }
174              
175 0           my $filled_in;
176 0           eval {
177 0           my $config = $self->{config}{sagan}{config};
178              
179 0           $config->{include} = File::Spec->canonpath( $config_base . '/sagan-rules.yaml' );
180              
181 0           $filled_in = '%YAML 1.1' . "\n" . Dump($config);
182              
183 0 0         if ( $self->{opts}{w} ) {
184 0           write_file( $config_base . '/include.yaml', $filled_in );
185             }
186             };
187 0 0         if ($@) {
188 0           $self->status_add(
189             error => 1,
190             status => 'Errored ... ' . $@
191             );
192             } else {
193 0           $self->status_add( status => "Filled in ... \n" . $filled_in );
194             }
195             } ## end else [ if ( $self->{config}{sagan}{multi_instance...})]
196              
197 0           return undef;
198             } ## end sub action_extra
199              
200             sub short {
201 0     0 1   return 'Generates the instance specific include for a sagan instance.';
202             }
203              
204             sub opts_data {
205 0     0 1   return 'i=s
206             w
207             ';
208             }
209              
210             1;