| 1 | package Ubic::Multiservice::Dir; | ||||||||||||||||||||||||||||
| 2 | 154 | $Ubic::Multiservice::Dir::VERSION = '1.59'; | |||||||||||||||||||||||||||
| 3 | 154 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 154 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: multiservice which uses directory with configs to instantiate services | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 154 | ▶ | use parent qw(Ubic::Multiservice); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 154 | ▶ | use Params::Validate qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 154 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 154 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 154 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 154 | ▶ | use Ubic::ServiceLoader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub new { | ||||||||||||||||||||||||||||
| 16 | 374 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 374 | my ($dir, @options) = validate_pos(@_, 1, 0); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 374 | my $options = {}; | |||||||||||||||||||||||||||
| 20 | 374 | ▶ | if (@options) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 141 | $options = validate(@options, { | |||||||||||||||||||||||||||
| 22 | protected => 0, | ||||||||||||||||||||||||||||
| 23 | }); | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 374 | return bless { service_dir => $dir, %$options } => $class; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub has_simple_service { | ||||||||||||||||||||||||||||
| 29 | 187 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
has_simple_service: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 187 | my ($name) = validate_pos(@_, {type => SCALAR, regex => qr/^[\w.-]+$/}); | |||||||||||||||||||||||||||
| 31 | 187 | ▶ | if ($self->_name2file($name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 186 | return 1; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | else { | ||||||||||||||||||||||||||||
| 35 | 1 | return; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub _filter_files { | ||||||||||||||||||||||||||||
| 40 | 1594 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_filter_files: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 41 | 1594 | my @files = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1594 | my @filtered; | |||||||||||||||||||||||||||
| 44 | 1594 | for my $name (@files) { | |||||||||||||||||||||||||||
| 45 | # list of taboo extensions is stolen from logrotate(8) | ||||||||||||||||||||||||||||
| 46 | 2439 | ▶ | if ($name =~ /( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | next; # skip silently | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 2439 | push @filtered, $name; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 1594 | return @filtered; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub _name2file { | ||||||||||||||||||||||||||||
| 69 | 1395 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_name2file: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 70 | 1395 | my ($name) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1395 | my $base = "$self->{service_dir}/$name"; | |||||||||||||||||||||||||||
| 73 | 1395 | my @files = glob "$base.*"; | |||||||||||||||||||||||||||
| 74 | 1395 | ▶ | unshift @files, $base if -e $base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1395 | @files = $self->_filter_files(@files); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1395 | ▶ | unless (@files) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 3 | return; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1392 | ▶ | if (@files > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 1 | for my $file (@files[1 .. $#files]) { | |||||||||||||||||||||||||||
| 84 | 1 | warn "Ignoring duplicate service config '$file', using '$files[0]' instead"; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 1392 | return shift @files; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub simple_service { | ||||||||||||||||||||||||||||
| 91 | 1208 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
simple_service: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 1208 | my ($name) = validate_pos(@_, {type => SCALAR, regex => qr/^[\w.-]+$/}); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1208 | my $file = $self->_name2file($name); | |||||||||||||||||||||||||||
| 95 | 1208 | ▶ | unless (defined $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 2 | croak "Service '$name' not found"; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1206 | ▶ | if (-d $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | # directory => multiservice | ||||||||||||||||||||||||||||
| 101 | 233 | my $service = Ubic::Multiservice::Dir->new($file); | |||||||||||||||||||||||||||
| 102 | 233 | $service->name($name); | |||||||||||||||||||||||||||
| 103 | 233 | return $service; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 973 | my $service = Ubic::ServiceLoader->load($file); | |||||||||||||||||||||||||||
| 107 | 970 | $service->name($name); | |||||||||||||||||||||||||||
| 108 | 970 | return $service; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub service_names { | ||||||||||||||||||||||||||||
| 112 | 199 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
service_names: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 199 | my %names; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 199 | my @files = glob("$self->{service_dir}/*"); | |||||||||||||||||||||||||||
| 117 | 199 | @files = $self->_filter_files(@files); | |||||||||||||||||||||||||||
| 118 | 199 | for my $file (@files) { | |||||||||||||||||||||||||||
| 119 | 1046 | ▶ | next unless -f $file or -d $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-f $file or -d $file
MC/DC0%0/2
-f $file or -d $file
-f $file
-d $file
Truth table66%2/3
A or B
A= -f $fileB= -d $file
| |||||||||||||||||||||||||||||
| 120 | 1046 | my $name = basename($file); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1046 | my ($service_name, $ext) = Ubic::ServiceLoader->split_service_filename($name); | |||||||||||||||||||||||||||
| 123 | 1046 | ▶ | unless (defined $service_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | warn "Invalid file $file - only alphanumerics, underscores and hyphens are allowed\n"; | |||||||||||||||||||||||||||
| 125 | 0 | next; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 1046 | $names{ $service_name }++; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 199 | return sort keys %names; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub multiop { | ||||||||||||||||||||||||||||
| 134 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
multiop: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 5 | ▶ | $self->{protected} ? 'protected' : 'allowed'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 154 | 1; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||