File Coverage

lib/Ubic/Multiservice/Dir.pm
Criterion Covered Total %
statement 79 82 96.3
branch 19 22 86.3
condition 2 3 66.6
subroutine 15 15 100.0
pod 5 5 100.0
total 120 127 94.4


line stmt bran cond sub pod time code
1             package Ubic::Multiservice::Dir;
2             $Ubic::Multiservice::Dir::VERSION = '1.58_01'; # TRIAL
3 25     25   113 use strict;
  25         38  
  25         853  
4 25     25   120 use warnings;
  25         37  
  25         938  
5              
6             # ABSTRACT: multiservice which uses directory with configs to instantiate services
7              
8 25     25   102 use parent qw(Ubic::Multiservice);
  25         43  
  25         177  
9 25     25   1442 use Params::Validate qw(:all);
  25         42  
  25         4985  
10 25     25   135 use Carp;
  25         44  
  25         1491  
11 25     25   332 use File::Basename;
  25         51  
  25         1974  
12 25     25   128 use Scalar::Util qw(blessed);
  25         47  
  25         1323  
13 25     25   10370 use Ubic::ServiceLoader;
  25         52  
  25         29489  
14              
15             sub new {
16 23     23 1 54 my $class = shift;
17 23         220 my ($dir, @options) = validate_pos(@_, 1, 0);
18              
19 23         70 my $options = {};
20 23 100       77 if (@options) {
21 14         171 $options = validate(@options, {
22             protected => 0,
23             });
24             }
25 23         291 return bless { service_dir => $dir, %$options } => $class;
26             }
27              
28             sub has_simple_service {
29 17     17 1 32 my $self = shift;
30 17         318 my ($name) = validate_pos(@_, {type => SCALAR, regex => qr/^[\w.-]+$/});
31 17 100       286 if ($self->_name2file($name)) {
32 16         109 return 1;
33             }
34             else {
35 1         6 return;
36             }
37             }
38              
39             sub _filter_files {
40 61     61   100 my $self = shift;
41 61         138 my @files = @_;
42              
43 61         92 my @filtered;
44 61         161 for my $name (@files) {
45             # list of taboo extensions is stolen from logrotate(8)
46 71 50       434 if ($name =~ /(
47             \.rpmorig |
48             \.rpmsave |
49             ,v |
50             \.swp |
51             \.rpmnew |
52             ~ |
53             \.cfsaved |
54             \.rhn-cfg-tmp-.* |
55             \.dpkg-dist |
56             \.dpkg-old |
57             \.dpkg-new |
58             \.disabled
59             )$/x
60             ) {
61 0         0 next; # skip silently
62             }
63 71         162 push @filtered, $name;
64             }
65 61         202 return @filtered;
66             }
67              
68             sub _name2file {
69 58     58   120 my $self = shift;
70 58         96 my ($name) = @_;
71              
72 58         196 my $base = "$self->{service_dir}/$name";
73 58         3923 my @files = glob "$base.*";
74 58 100       1030 unshift @files, $base if -e $base;
75              
76 58         252 @files = $self->_filter_files(@files);
77              
78 58 100       178 unless (@files) {
79 5         17 return;
80             }
81              
82 53 100       163 if (@files > 1) {
83 1         7 for my $file (@files[1 .. $#files]) {
84 1         44 warn "Ignoring duplicate service config '$file', using '$files[0]' instead";
85             }
86             }
87 53         298 return shift @files;
88             }
89              
90             sub simple_service {
91 41     41 1 80 my $self = shift;
92 41         680 my ($name) = validate_pos(@_, {type => SCALAR, regex => qr/^[\w.-]+$/});
93              
94 41         606 my $file = $self->_name2file($name);
95 41 100       135 unless (defined $file) {
96 4         1648 croak "Service '$name' not found";
97             }
98              
99 37 100       464 if (-d $file) {
100             # directory => multiservice
101 9         73 my $service = Ubic::Multiservice::Dir->new($file);
102 9         50 $service->name($name);
103 9         35 return $service;
104             }
105              
106 28         292 my $service = Ubic::ServiceLoader->load($file);
107 28         189 $service->name($name);
108 28         106 return $service;
109             }
110              
111             sub service_names {
112 3     3 1 594 my $self = shift;
113              
114 3         7 my %names;
115              
116 3         424 my @files = glob("$self->{service_dir}/*");
117 3         25 @files = $self->_filter_files(@files);
118 3         14 for my $file (@files) {
119 17 50 66     315 next unless -f $file or -d $file;
120 17         594 my $name = basename($file);
121              
122 17         2367 my ($service_name, $ext) = Ubic::ServiceLoader->split_service_filename($name);
123 17 50       44 unless (defined $service_name) {
124 0         0 warn "Invalid file $file - only alphanumerics, underscores and hyphens are allowed\n";
125 0         0 next;
126             }
127              
128 17         56 $names{ $service_name }++;
129             }
130 3         57 return sort keys %names;
131             }
132              
133             sub multiop {
134 2     2 1 431 my $self = shift;
135 2 100       25 $self->{protected} ? 'protected' : 'allowed';
136             }
137              
138              
139             1;
140              
141             __END__