File Coverage

blib/lib/Armadito/Agent/Config.pm
Criterion Covered Total %
statement 15 78 19.2
branch 0 42 0.0
condition 0 15 0.0
subroutine 5 10 50.0
pod 5 5 100.0
total 25 150 16.6


line stmt bran cond sub pod time code
1             package Armadito::Agent::Config;
2              
3 24     24   1141267 use strict;
  24         28  
  24         578  
4 24     24   74 use warnings;
  24         31  
  24         572  
5              
6 24     24   83 use English qw(-no_match_vars);
  24         57  
  24         106  
7 24     24   7588 use File::Spec;
  24         31  
  24         306  
8 24     24   507 use UNIVERSAL::require;
  24         25  
  24         98  
9              
10             my $deprecated = {};
11              
12             sub new {
13 0     0 1   my ( $class, %params ) = @_;
14              
15 0           my $self = {};
16 0           bless $self, $class;
17              
18 0           return $self;
19             }
20              
21             sub loadDefaults {
22 0     0 1   my ( $self, $default ) = @_;
23              
24 0           foreach my $key ( keys %$default ) {
25 0           $self->{$key} = $default->{$key};
26             }
27             }
28              
29             sub loadFromFile {
30 0     0 1   my ( $self, $file ) = @_;
31              
32 0 0         if ($file) {
33 0 0         die "non-existing file $file" unless -f $file;
34 0 0         die "non-readable file $file" unless -r $file;
35             }
36             else {
37 0           die "no configuration file";
38             }
39              
40 0           my $handle;
41 0 0         die "Config: Failed to open $file: $ERRNO" if ( !open $handle, '<', $file );
42              
43 0           while ( my $line = <$handle> ) {
44 0           $line =~ s/#.+//;
45 0 0         if ( $line =~ /([\w-]+)\s*=\s*(.+)/ ) {
46 0           my $key = $1;
47 0           my $val = $2;
48              
49             # Remove the quotes
50 0           $val =~ s/\s+$//;
51 0           $val =~ s/^'(.*)'$/$1/;
52 0           $val =~ s/^"(.*)"$/$1/;
53              
54 0 0         if ( exists $self->{$key} ) {
55 0           $self->{$key} = $val;
56             }
57             else {
58 0           warn "unknown configuration directive $key";
59             }
60             }
61             }
62 0           close $handle;
63             }
64              
65             sub overrideWithArgs {
66 0     0 1   my ( $self, %params ) = @_;
67              
68 0           foreach my $key ( keys %{$self} ) {
  0            
69 0 0 0       if ( defined( $params{options}->{$key} ) && $params{options}->{$key} ne "" ) {
70 0           $self->{$key} = $params{options}->{$key};
71             }
72             }
73             }
74              
75             sub checkContent {
76 0     0 1   my ($self) = @_;
77              
78             # check for deprecated options
79 0           foreach my $old ( keys %$deprecated ) {
80 0 0         next unless defined $self->{$old};
81              
82 0 0 0       next if $old =~ /^no-/ && !$self->{$old};
83              
84 0           my $handler = $deprecated->{$old};
85              
86             # notify user of deprecation
87 0           warn "the '$old' option is deprecated, $handler->{message}\n";
88              
89             # transfer the value to the new option, if possible
90 0 0         if ( $handler->{new} ) {
91 0 0         if ( ref $handler->{new} eq 'HASH' ) {
    0          
92              
93             # old boolean option replaced by new non-boolean options
94 0           foreach my $key ( keys %{ $handler->{new} } ) {
  0            
95 0           my $value = $handler->{new}->{$key};
96 0 0         if ( $value =~ /^\+(\S+)/ ) {
97              
98             # multiple values: add it to exiting one
99 0 0         $self->{$key} = $self->{$key} ? $self->{$key} . ',' . $1 : $1;
100             }
101             else {
102             # unique value: replace exiting value
103 0           $self->{$key} = $value;
104             }
105             }
106             }
107             elsif ( ref $handler->{new} eq 'ARRAY' ) {
108              
109             # old boolean option replaced by new boolean options
110 0           foreach my $new ( @{ $handler->{new} } ) {
  0            
111 0           $self->{$new} = $self->{$old};
112             }
113             }
114             else {
115             # old non-boolean option replaced by new option
116 0           $self->{ $handler->{new} } = $self->{$old};
117             }
118             }
119              
120             # avoid cluttering configuration
121 0           delete $self->{$old};
122             }
123              
124             # a logfile options implies a file logger backend
125 0 0         if ( $self->{logfile} ) {
126 0           $self->{logger} .= ',File';
127             }
128              
129             # ca-cert-file and ca-cert-dir are antagonists
130 0 0 0       if ( $self->{'ca-cert-file'} && $self->{'ca-cert-dir'} ) {
131 0           die "use either 'ca-cert-file' or 'ca-cert-dir' option, not both\n";
132             }
133              
134             # logger backend without a logfile isn't enoguh
135 0 0 0       if ( $self->{'logger'} =~ /file/i && !$self->{'logfile'} ) {
136 0           die "usage of 'file' logger backend makes 'logfile' option mandatory\n";
137             }
138              
139             # multi-values options, the default separator is a ','
140 0           foreach my $option (
141             qw/
142             logger
143             local
144             server
145             httpd-trust
146             no-task
147             no-category
148             tasks
149             /
150             )
151             {
152              
153             # Check if defined AND SCALAR
154             # to avoid split a ARRAY ref or HASH ref...
155 0 0 0       if ( $self->{$option} && ref( $self->{$option} ) eq '' ) {
156 0           $self->{$option} = [ split( /,/, $self->{$option} ) ];
157             }
158             else {
159 0           $self->{$option} = [];
160             }
161             }
162              
163             # files location
164             $self->{'ca-cert-file'} = File::Spec->rel2abs( $self->{'ca-cert-file'} )
165 0 0         if $self->{'ca-cert-file'};
166             $self->{'ca-cert-dir'} = File::Spec->rel2abs( $self->{'ca-cert-dir'} )
167 0 0         if $self->{'ca-cert-dir'};
168             $self->{'logfile'} = File::Spec->rel2abs( $self->{'logfile'} )
169 0 0         if $self->{'logfile'};
170             }
171              
172             1;
173             __END__
174              
175             =head1 NAME
176              
177             Armadito::Agent::Config - Armadito Agent configuration
178              
179             =head1 DESCRIPTION
180              
181             This is the object used by the agent to store its configuration.
182              
183             =head1 METHODS
184              
185             =head2 new(%params)
186              
187             The constructor. The following parameters are allowed, as keys of the %params
188             hash:
189              
190             =over
191              
192             =item I<confdir>
193              
194             the configuration directory.
195              
196             =item I<options>
197              
198             additional options override.
199              
200             =back
201              
202             =head2 loadDefaults()
203              
204             Load default configuration from in-code predefined variable $default.
205              
206             =head2 loadFromFile()
207              
208             Load configuration from given file (i.e. agent.cfg or scheduler.cfg file path)
209              
210             =head2 overrideWithArgs()
211              
212             Override loaded configuration by given command line arguments.
213              
214             =head2 checkContent()
215              
216             Check if loaded configuration is valid.
217              
218