Backend/Systemd.pm

filter: Statementstmt 84.3% 97 / 115 Branchbran 61.7% 21 / 34 Conditioncond 55.5% 10 / 18 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 92.8% 13 / 14 Podpod 40.0% 2 / 5 totaltotal 73.7% 146 / 198 CC 29 SCAR 29.5
CC
29
Cov
77%
read_systemd_units
29.2
read_systemd_files
20.0
read
2.1
CRAP 19.0
↑ Summary Backend/Systemd/Unit.pm »
1 #
2 # This file is part of Config-Model-Systemd
3 #
4 # This software is Copyright (c) 2008-2026 by Dominique Dumont.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package Config::Model::Backend::Systemd ;
11 2 $Config::Model::Backend::Systemd::VERSION = '0.261.3';
12 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Mouse ;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Path::Tiny 0.086;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 extends 'Config::Model::Backend::Any';
20 2 with 'Config::Model::Backend::Systemd::Layers';
21  
22 2 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 my $logger = get_logger("Backend::Systemd");
26 2 my $user_logger = get_logger("User");
27  
28 2 has config_dir => (
29 is => 'rw',
30 isa => 'Path::Tiny'
31 );
32  
33 2 has 'annotation' => ( is => 'ro', isa => 'Bool', default => 1 );
34  
35 # TODO: accepts other systemd suffixes
36 2 my @service_types = qw/service socket timer/;
37 2 my $joined_types = join('|', @service_types);
38 2 my $filter = qr/\.($joined_types)(\.d)?$/;
39  
40 25 ▶sub get_backend_arg {
Subroutine get_backend_arg: called CC=2 SCAR=7.4
Pod undocumented
41 25 my $self = shift ;
42  
43 25 my $ba = $self->instance->backend_arg;
44 25 ▶ if (not $ba) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ba) 0 25 25
45 0 Config::Model::Exception::User->throw(
46 objet => $self->node,
47 error => "Missing systemd unit to work on. This may be passed as 3rd argument to cme",
48 );
49 }
50 25 return $ba;
51 }
52  
53 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
54 25 ▶sub read ($self, @args) {
Subroutine read: called CC=2 SCAR=7.6
Pod documented
55 25 my $app = $self->instance->application;
56  
57 25 ▶ if ($app =~ /file/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app =~ /file/u) { } 0 25 25
58 0 return $self->read_systemd_files(@args);
59 }
60 else {
61 25 return $self->read_systemd_units(@args);
62 }
63 }
64  
65 0 ▶sub read_systemd_files ($self, %args) {
Subroutine read_systemd_files: not called CC=4 SCAR=30.0
Pod undocumented
66 # args are:
67 # root => './my_test', # fake root directory, used for tests
68 # config_dir => /etc/foo', # absolute path
69 # config_file => 'foo.conf', # file name
70 # file_path => './my_test/etc/foo/foo.conf'
71 # check => yes|no|skip
72  
73 #use Tk::ObjScanner; Tk::ObjScanner::scan_object(\%args) ;
74 0 my $file = $args{file_path};
75 0 ▶ if (not $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 0 0
76 0 Config::Model::Exception::User->throw(
77 objet => $self->node,
78 error => "Missing systemd file to work on. This may be passed as 3rd argument to cme",
79 );
80 }
81  
82 0 $user_logger->info( "Loading unit file '$file'");
83 0 my ($service_name, $unit_type) = split /\./, path($file)->basename;
84  
85 0 ▶ my @to_create = $unit_type ? ($unit_type) : @service_types;
Branch: 1 branch
BranchTrueFalseTotal
$unit_type ? : 0 0 0
86 0 foreach my $unit_type (@to_create) {
87 0 $logger->debug("registering unit $unit_type name $service_name from file name");
88 0 $self->node->load(step => qq!$unit_type:"$service_name"!, check => $args{check} ) ;
89 }
90 0 return 1;
91 }
92  
93 25 ▶sub read_systemd_units ($self, %args) {
Subroutine read_systemd_units: called CC=24 SCAR=33.7
Pod undocumented
94 # args are:
95 # root => './my_test', # fake root directory, used for tests
96 # config_dir => /etc/foo', # absolute path
97 # config_file => 'foo.conf', # file name
98 # file_path => './my_test/etc/foo/foo.conf'
99 # check => yes|no|skip
100  
101 25 my $app = $self->instance->application;
102  
103 25 my $select_unit = $self->get_backend_arg;
104 25 ▶ if (not $select_unit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($select_unit) 0 25 25
105 0 Config::Model::Exception::User->throw(
106 objet => $self->node,
107 error => "Missing systemd unit to work on. This must be passed as 3rd argument to cme",
108 );
109 }
110  
111 25 ▶ if ($app ne 'systemd-user' and $select_unit =~ $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app ne "systemd-user" and $select_unit =~ /$filter/u) 0 25 25
Condition66%2/3
$app ne "systemd-user" and $select_unit =~ /$filter/u
!ll&&!rl&&r
14110
MC/DC0%0/2
$app ne "systemd-user" and $select_unit =~ /$filter/u
$app ne "systemd-user" $select_unit =~ /$filter/u
Truth table66%2/3
A and B
A= $app ne "systemd-user"B= $select_unit =~ /$filter/u
ABresult
0X0
100
111
112 0 my $unit_type = $1;
113 0 ▶ if ($app eq 'systemd') {
Branch: 2 branches
BranchTrueFalseTotal
if ($app eq 'systemd') { } 0 0 0
elsif ($app ne 'systemd-$unit_type') { } 0 0 0
114 0 Config::Model::Exception::User->throw(
115 objet => $self->node,
116 error => "With 'systemd' app, unit name should not specify '$unit_type'. "
117 ." Use 'systemd-$unit_type' app if you want to act only on $select_unit",
118 );
119 }
120 elsif ($app ne 'systemd-$unit_type') {
121 0 Config::Model::Exception::User->throw(
122 objet => $self->node,
123 error => "Unit name $select_unit does not match app $app"
124 );
125 }
126 }
127  
128 25 ▶ if ($select_unit ne '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_unit ne '*') { } 25 0 25
129 25 $user_logger->info( "Loading unit matching '$select_unit'");
130 } else {
131 0 $user_logger->info("Loading all units...")
132 }
133  
134 25 ▶ my $root_path = $args{root} || path('/');
Condition33%1/3
$args{'root'} || path('/')
l!l&&r!l&&!r
2500
MC/DC0%0/2
$args{'root'} || path('/')
$args{'root'} path('/')
Truth table33%1/3
A || B
A= $args{'root'}B= path('/')
ABresult
1X1
011
000
135  
136 # load layers. layered mode is handled by Unit backend. Only a hash
137 # key is created here, so layered mode does not matter
138 25 foreach my $layer ($self->default_directories) {
139 111 my $dir = $root_path->child($layer);
140 111 ▶ next unless $dir->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->is_dir 89 22 111
141 22 $self->config_dir($dir);
142  
143 22 foreach my $file ($dir->children($filter) ) {
144 30 my $file_name = $file->basename();
145 30 my $unit_name = $file->basename($filter);
146 30 $logger->trace( "checking unit $file_name from $file (layered mode) against $select_unit");
147 30 ▶ if ($select_unit ne '*' and $file_name !~ /$select_unit/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_unit ne "*" and not $file_name =~ /$select_unit/u) 8 22 30
Condition66%2/3
$select_unit ne "*" and not $file_name =~ /$select_unit/u
!ll&&!rl&&r
0228
MC/DC50%1/2
$select_unit ne "*" and not $file_name =~ /$select_unit/u
$select_unit ne "*" not $file_name =~ /$select_unit/u
Truth table66%2/3
A and not(B)
A= $select_unit ne "*"B= not $file_name =~ /$select_unit/u
ABresult
0X0
100
111
148 8 $logger->trace( "unit $file_name from $file (layered mode) does not match $select_unit");
149 8 next;
150 }
151 22 my ($unit_type) = ($file =~ $filter);
152 22 $logger->debug( "registering unit $unit_type name $unit_name from $file (layered mode))");
153 # force config_dir during init
154 22 $self->node->load(step => qq!$unit_type:"$unit_name"!, check => $args{check} ) ;
155 }
156 }
157  
158 25 my $dir = $root_path->child($args{config_dir});
159  
160 25 ▶ if (not $dir->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir->is_dir) 4 21 25
161 4 $logger->debug("skipping missing directory $dir");
162 4 return 1 ;
163 }
164  
165 21 $self->config_dir($dir);
166 21 my $found = 0;
167 21 foreach my $file ($dir->children($filter) ) {
168 28 $logger->trace("Scanning unit dir $dir with filter $filter");
169 28 my ($unit_type,$dot_d) = ($file =~ $filter);
170 28 my $file_name = $file->basename();
171 28 my $unit_name = $file->basename($filter);
172  
173 28 ▶ next if ($select_unit ne '*' and $file_name !~ /$select_unit/);
Branch: 1 branch
BranchTrueFalseTotal
if $select_unit ne "*" and not $file_name =~ /$select_unit/u 4 24 28
Condition66%2/3
$select_unit ne "*" and not $file_name =~ /$select_unit/u
!ll&&!rl&&r
0244
MC/DC50%1/2
$select_unit ne "*" and not $file_name =~ /$select_unit/u
$select_unit ne "*" not $file_name =~ /$select_unit/u
Truth table66%2/3
A and not(B)
A= $select_unit ne "*"B= not $file_name =~ /$select_unit/u
ABresult
0X0
100
111
174 24 $logger->trace( "checking $file against $select_unit");
175  
176 24 ▶ if ($file->realpath eq '/dev/null') {
Branch: 2 branches
BranchTrueFalseTotal
if ($file->realpath eq '/dev/null') { } 1 23 24
elsif ($dot_d and $file->child('override.conf')->exists) { } 12 11 23
177 1 $logger->info("unit $unit_type name $unit_name from $file is disabled");
178 1 $self->node->load(step => qq!$unit_type:"$unit_name" disable=1!, check => $args{check} ) ;
179 }
180 ▶ elsif ($dot_d and $file->child('override.conf')->exists) {
Condition66%2/3
$dot_d and $file->child("override.conf")->exists
!ll&&!rl&&r
11012
MC/DC50%1/2
$dot_d and $file->child("override.conf")->exists
$dot_d $file->child("override.conf")->exists
Truth table66%2/3
A and B
A= $dot_dB= $file->child("override.conf")->exists
ABresult
0X0
100
111
181 12 $logger->info("registering unit $unit_type name $unit_name from override file");
182 12 $self->node->load(step => qq!$unit_type:"$unit_name"!, check => $args{check} ) ;
183 }
184 else {
185 11 $logger->info("registering unit $unit_type name $unit_name from $file");
186 11 $self->node->load(step => qq!$unit_type:"$unit_name"!, check => $args{check} ) ;
187 }
188 24 $found++;
189 }
190  
191 21 ▶ if (not $found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 2 19 21
192 # no service exists, let's create them.
193 2 $user_logger->info( "No unit '$select_unit' found in $dir, creating one...");
194 2 my ($service_name, $unit_type) = split /\./, $select_unit;
195 2 ▶ my @to_create = $unit_type ? ($unit_type) : @service_types;
Branch: 1 branch
BranchTrueFalseTotal
$unit_type ? : 1 1 2
196 2 ▶ $service_name //= $select_unit;
Condition33%1/3
$service_name //= $select_unit
l!l&&r!l&&!r
200
MC/DC0%0/2
$service_name //= $select_unit
$service_name $select_unit
Truth table33%1/3
A //= B
A= $service_nameB= $select_unit
ABresult
1X1
011
000
197 2 foreach my $unit_type (@to_create) {
198 4 $logger->debug("registering unit $unit_type name $service_name from scratch");
199 4 $self->node->load(step => qq!$unit_type:"$service_name"!, check => $args{check} ) ;
200 }
201 }
202 21 return 1 ;
203 }
204  
205 14 ▶sub write ($self, %args) {
Subroutine write: called CC=1 SCAR=0.0
Pod documented
206 # args are:
207 # root => './my_test', # fake root directory, userd for tests
208 # config_dir => /etc/foo', # absolute path
209 # file => 'foo.conf', # file name
210 # file_path => './my_test/etc/foo/foo.conf'
211 # check => yes|no|skip
212  
213 # file write is handled by Unit backend
214 14 return 1;
215 }
216  
217 2 ▶no Mouse ;
Subroutine BEGIN: called CC=1 SCAR=0.0
218 2 __PACKAGE__->meta->make_immutable ;
219  
220 2 1;
221  
222 # ABSTRACT: R/W backend for systemd configurations files
223  
224 __END__
↑ Summary Backend/Systemd/Unit.pm »