| 1 | package Configd::Unit; | ||||||||||||||||||||||||||||
| 2 | 2 | $Configd::Unit::VERSION = '0.005'; | |||||||||||||||||||||||||||
| 3 | #ABSTRACT: The systemd drop-in that rebuilds a language's files before the daemon reads them. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use 5.034; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use re '/aa'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use File::Path qw{make_path}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Configd::Language(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | our $DROPIN = '10-configd.conf'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 0 | my ( $class, %opts ) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | die "A Configd::Unit needs a language\n" unless $opts{language}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | $opts{configd} //= '/usr/bin/configd'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts{"configd"} //= "/usr/bin/configd"
MC/DC0%0/1
$opts{"configd"} //= "/usr/bin/configd"
$opts{"configd"}
Truth table0%0/2
A
A= $opts{"configd"}
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | $opts{root} //= $opts{language}->root() // q{}; | ||||||||||||||||||||||||||
|
Condition0%0/4
$opts{"root"} //= $opts{"language"}->root // ""
$opts{"language"}->root // ""
MC/DC0%0/1
$opts{"root"} //= $opts{"language"}->root // ""
$opts{"root"}
Truth table0%0/2
A
A= $opts{"root"}
| |||||||||||||||||||||||||||||
| 27 | 0 | $opts{root} =~ s{/\z}{}; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | return bless {%opts}, $class; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub dropin_dir { | ||||||||||||||||||||||||||
|
Subroutine
dropin_dir: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 0 | my ( $self, $name ) = @_; | |||||||||||||||||||||||||||
| 35 | 0 | return "$self->{root}/etc/systemd/system/$name.d"; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | my $language = $self->{language}->name(); | |||||||||||||||||||||||||||
| 43 | 0 | my $configd = $self->{configd}; | |||||||||||||||||||||||||||
| 44 | 0 | my @files = map { $_->{path} } $self->{language}->files(); | |||||||||||||||||||||||||||
| 45 | 0 | my $list = join( "\n", map { "# $_" } @files ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # A daemon with no reload of its own gets ExecStartPre only: see | ||||||||||||||||||||||||||||
| 48 | # Configd::Language::reloads. | ||||||||||||||||||||||||||||
| 49 | 0 | my @exec = ("ExecStartPre=+$configd build $language"); | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | push @exec, "ExecReload=+$configd build $language" if $self->{language}->reloads(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | my $preamble = <<"UNIT"; | |||||||||||||||||||||||||||
| 53 | # Installed by configd. Removing this file and running `systemctl daemon-reload` | ||||||||||||||||||||||||||||
| 54 | # is all it takes to stop configd having anything to do with this service; the | ||||||||||||||||||||||||||||
| 55 | # files below stay exactly as they were last generated. | ||||||||||||||||||||||||||||
| 56 | # | ||||||||||||||||||||||||||||
| 57 | $list | ||||||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||||||
| 59 | # Each of those is built from the directory of the same name with .d on the end. | ||||||||||||||||||||||||||||
| 60 | # Edit the fragments, not the file: this rebuilds it on every start and reload. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | [Service] | ||||||||||||||||||||||||||||
| 63 | UNIT | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | return $preamble . join( "\n", @exec ) . "\n"; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub install { | ||||||||||||||||||||||||||
|
Subroutine
install: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | my $wanted = $self->render(); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my @written; | |||||||||||||||||||||||||||
| 75 | 0 | foreach my $name ( $self->{language}->units() ) { | |||||||||||||||||||||||||||
| 76 | 0 | my $dir = $self->dropin_dir($name); | |||||||||||||||||||||||||||
| 77 | 0 | my $path = "$dir/$DROPIN"; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | make_path($dir) unless -d $dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $current = -f $path ? Configd::Language::slurp($path) : undef; ## no critic (ValuesAndExpressions::ProhibitFiletest_f) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | next if defined $current && $current eq $wanted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $current and $current eq $wanted
MC/DC0%0/2
defined $current and $current eq $wanted
defined $current
$current eq $wanted
Truth table0%0/3
A and B
A= defined $currentB= $current eq $wanted
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | Configd::Language::spew( $path, $wanted ); | |||||||||||||||||||||||||||
| 85 | 0 | push @written, $path; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | return @written; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub uninstall { | ||||||||||||||||||||||||||
|
Subroutine
uninstall: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my @removed; | |||||||||||||||||||||||||||
| 96 | 0 | foreach my $name ( $self->{language}->units() ) { | |||||||||||||||||||||||||||
| 97 | 0 | my $dir = $self->dropin_dir($name); | |||||||||||||||||||||||||||
| 98 | 0 | my $path = "$dir/$DROPIN"; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | next unless -f $path; ## no critic (ValuesAndExpressions::ProhibitFiletest_f) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | unlink $path or die "Could not remove $path: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | push @removed, $path; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # An empty .d directory is litter, but one holding somebody else's | ||||||||||||||||||||||||||||
| 105 | # drop-in is theirs. | ||||||||||||||||||||||||||||
| 106 | 0 | rmdir $dir; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | return @removed; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub installed { | ||||||||||||||||||||||||||
|
Subroutine
installed: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | my @units = $self->{language}->units(); | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | return 0 unless @units; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | foreach my $name (@units) { | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | return 0 unless -f $self->dropin_dir($name) . "/$DROPIN"; ## no critic (ValuesAndExpressions::ProhibitFiletest_f) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | return 1; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | 1; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||||||||||