| 1 | # -*- perl -*- | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Author: Slaven Rezic | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Copyright (C) 2017,2018,2020 Slaven Rezic. All rights reserved. | ||||||||||||||||||||||||||||
| 7 | # This package is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 8 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # Mail: slaven@rezic.de | ||||||||||||||||||||||||||||
| 11 | # WWW: http://www.rezic.de/eserte/ | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | package Doit::Deb; # Convention: all commands here should be prefixed with 'deb_' | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | our $VERSION = '0.024'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Doit::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Doit::Util 'get_sudo_cmd'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | sub new { bless {}, shift } | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | sub functions { qw(deb_install_packages deb_missing_packages deb_install_key) } | ||||||||||||||||||||||||||
|
Subroutine
functions: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub deb_install_packages { | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my($self, @packages) = @_; | ||||||||||||||||||||||||||
|
Subroutine
deb_install_packages: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 0 | my @missing_packages = $self->deb_missing_packages(@packages); # XXX cmd vs. info??? | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | if (@missing_packages) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | $self->system(get_sudo_cmd(), 'apt-get', '-y', 'install', @missing_packages); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 0 | @missing_packages; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub deb_missing_packages { | ||||||||||||||||||||||||||||
| 37 | 2 | ▶ | my($self, @packages) = @_; | ||||||||||||||||||||||||||
|
Subroutine
deb_missing_packages: called CC=12 SCAR=27.5
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | my @missing_packages; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | if (@packages) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 2 | require IPC::Open3; | |||||||||||||||||||||||||||
| 43 | 2 | require Symbol; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | my %seen_packages; | |||||||||||||||||||||||||||
| 46 | my %required_version; | ||||||||||||||||||||||||||||
| 47 | 2 | for my $package (@packages) { | |||||||||||||||||||||||||||
| 48 | 2 | ▶ | if (ref $package eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | my($package_name, $package_version) = @$package; | |||||||||||||||||||||||||||
| 50 | 0 | $required_version{$package_name} = $package_version; | |||||||||||||||||||||||||||
| 51 | 0 | $package = $package_name; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 2 | my @cmd = ('dpkg-query', '-W', '-f=${Package}\t${Status}\t${Version}\n', @packages); | |||||||||||||||||||||||||||
| 55 | 2 | my $err = Symbol::gensym(); | |||||||||||||||||||||||||||
| 56 | 2 | my $fh; | |||||||||||||||||||||||||||
| 57 | 2 | ▶ | my $pid = IPC::Open3::open3(undef, $fh, $err, @cmd) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | or error "Error running '@cmd': $!"; | ||||||||||||||||||||||||||||
| 59 | 2 | ▶ | while(<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 1 | chomp; | |||||||||||||||||||||||||||
| 61 | 1 | ▶ | if (m{^([^\t]+)\t([^\t]+)\t([^\t]*)$}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | if ($2 ne 'install ok installed') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | push @missing_packages, $1; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 1 | ▶ | if ($required_version{$1} && $required_version{$1} ne $3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$required_version{$1} and $required_version{$1} ne $3
MC/DC0%0/2
$required_version{$1} and $required_version{$1} ne $3
$required_version{$1}
$required_version{$1} ne $3
Truth table33%1/3
A and B
A= $required_version{$1}B= $required_version{$1} ne $3
| |||||||||||||||||||||||||||||
| 66 | 0 | push @missing_packages, $1; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 1 | $seen_packages{$1} = 1; | |||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||
| 70 | 0 | warning "cannot parse '$_', ignore line..."; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 2 | waitpid $pid, 0; | |||||||||||||||||||||||||||
| 74 | 2 | for my $package (@packages) { | |||||||||||||||||||||||||||
| 75 | 2 | ▶ | if (!$seen_packages{$package}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | push @missing_packages, $package; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 2 | @missing_packages; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub deb_install_key { | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my($self, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
deb_install_key: not called CC=23 SCAR=63.1
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 0 | my $url = delete $opts{url}; | |||||||||||||||||||||||||||
| 86 | 0 | my $keyserver = delete $opts{keyserver}; | |||||||||||||||||||||||||||
| 87 | 0 | my $key = delete $opts{key}; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | error "Unhandled options: " . join(" ", %opts) if %opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if (!$url) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if (!$keyserver) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | error "keyserver is missing"; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if (!$key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | error "key is missing"; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ($keyserver) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | error "Don't define both url and keyserver"; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | my $found_key; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | $key =~ s{\s}{}g; # convenience: strip spaces from key ('apt-key finger' returns them with spaces) | |||||||||||||||||||||||||||
| 106 | 0 | local $ENV{LC_ALL} = 'C'; | |||||||||||||||||||||||||||
| 107 | # XXX If run with $sudo, then this will emit warnings in the form | ||||||||||||||||||||||||||||
| 108 | # gpg: WARNING: unsafe ownership on configuration file `$HOME/.gnupg/gpg.conf' | ||||||||||||||||||||||||||||
| 109 | # Annoying, but harmless. Could be workarounded by specifying | ||||||||||||||||||||||||||||
| 110 | # '--homedir=/root/.gpg', but this would create gpg files under ~root. Similar | ||||||||||||||||||||||||||||
| 111 | # if using something like | ||||||||||||||||||||||||||||
| 112 | # local $ENV{HOME} = (getpwuid($<))[7]; | ||||||||||||||||||||||||||||
| 113 | # Probably better would be to work with privilege escalation and run | ||||||||||||||||||||||||||||
| 114 | # this command as normal user (to be implemented). | ||||||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||||||
| 116 | # Older Debian (jessie and older?) have only /etc/apt/trusted.gpg, | ||||||||||||||||||||||||||||
| 117 | # newer ones (stretch and newer?) have /etc/apt/trusted.gpg.d/*.gpg | ||||||||||||||||||||||||||||
| 118 | SEARCH_FOR_KEY: { | ||||||||||||||||||||||||||||
| 119 | 0 | require File::Glob; | |||||||||||||||||||||||||||
| 120 | 0 | for my $keyfile ('/etc/apt/trusted.gpg', File::Glob::bsd_glob('/etc/apt/trusted.gpg.d/*.gpg')) { | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (-r $keyfile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | my @cmd = ('gpg', '--keyring', $keyfile, '--list-keys', '--fingerprint', '--with-colons'); | |||||||||||||||||||||||||||
| 123 | 0 | ▶ | open my $fh, '-|', @cmd | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | or error "Running '@cmd' failed: $!"; | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | while(<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if (m{^fpr:::::::::\Q$key\E:$}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | $found_key = 1; | |||||||||||||||||||||||||||
| 128 | 0 | last SEARCH_FOR_KEY; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | close $fh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | or error "Running '@cmd' failed: $!"; | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | my $changed = 0; | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | if (!$found_key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($keyserver) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 141 | 0 | $self->system(get_sudo_cmd(), 'apt-key', 'adv', '--keyserver', $keyserver, '--recv-keys', $key); | |||||||||||||||||||||||||||
| 142 | } elsif ($url) { | ||||||||||||||||||||||||||||
| 143 | 0 | my @fetch_cmd; | |||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ($self->which('curl')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | @fetch_cmd = ('curl', '-fsSL', $url); | |||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||
| 147 | 0 | @fetch_cmd = ('wget', '-O-', $url); # other alternative would be lwp-request | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 0 | my @add_cmd = (get_sudo_cmd(), 'apt-key', 'add', '-'); | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if ($self->is_dry_run) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | info "Fetch key using '@fetch_cmd' and add using '@add_cmd' (dry-run)"; | |||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | open my $ifh, '-|', @fetch_cmd | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | or error "Failed to start '@fetch_cmd': $!"; | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | open my $ofh, '|-', @add_cmd | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | or error "Failed to start '@add_cmd': $!"; | ||||||||||||||||||||||||||||
| 157 | 0 | local $/ = \1024; | |||||||||||||||||||||||||||
| 158 | 0 | ▶ | while(<$ifh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | print $ofh $_; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | close $ofh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | or error "Running '@add_cmd' failed: $!"; | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | close $ifh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | or error "Running '@fetch_cmd' failed: $!"; | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } else { | ||||||||||||||||||||||||||||
| 167 | 0 | error "Shouldn't happen (either url or keyserver has to be specified)"; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 0 | $changed = 1; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 0 | $changed; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | 1; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||