| 1 | package Util::Medley::Linux::Yum; | ||
| 2 | $Util::Medley::Linux::Yum::VERSION = '0.047'; | ||
| 3 | use Modern::Perl; | ||
| 4 | use Moose; | ||
| 5 | use namespace::autoclean; | ||
| 6 | use Data::Printer alias => 'pdump'; | ||
| 7 | use Kavorka 'method', 'multi'; | ||
| 8 | |||
| 9 | with | ||
| 10 | 'Util::Medley::Roles::Attributes::Spawn', | ||
| 11 | 'Util::Medley::Roles::Attributes::String'; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Util::Medley::Linux::Yum - Class for interacting with YUM | ||
| 16 | |||
| 17 | =head1 VERSION | ||
| 18 | |||
| 19 | version 0.047 | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | my $yum = Util::Medley::Yum->new; | ||
| 26 | |||
| 27 | # | ||
| 28 | # positional | ||
| 29 | # | ||
| 30 | $aref = $yum->repoList([$enabled], [$disabled]); | ||
| 31 | |||
| 32 | # | ||
| 33 | # named pair | ||
| 34 | # | ||
| 35 | $aref = $yum->repoList([enabled => 0|1], | ||
| 36 | [disabled => 0|1]); | ||
| 37 | |||
| 38 | $aref = $yum->repoQuery ([all => 0|1], | ||
| 39 | [installed => 0|1], | ||
| 40 | [repoId => $repoId]); | ||
| 41 | =cut | ||
| 42 | |||
| 43 | ######################################################## | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | A simple wrapper library for YUM on Linux. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | ######################################################## | ||
| 52 | |||
| 53 | =head1 ATTRIBUTES | ||
| 54 | |||
| 55 | none | ||
| 56 | |||
| 57 | =head1 METHODS | ||
| 58 | |||
| 59 | =head2 repoList | ||
| 60 | |||
| 61 | Generates a list of configured YUM repositories. | ||
| 62 | |||
| 63 | Returns: ArrayRef[HashRef] | ||
| 64 | |||
| 65 | Example HashRef: | ||
| 66 | |||
| 67 | { | ||
| 68 | repoBaseurl "http://centos3.zswap.net/7.8.2003/updates/x86_64/ (9 more)", | ||
| 69 | repoExpire "21,600 second(s) (last: Tue Oct 13 12:14:28 2020)", | ||
| 70 | repoId "updates/7/x86_64", | ||
| 71 | repoMirrors "http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=updates&infra=vag", | ||
| 72 | repoName "CentOS-7 - Updates", | ||
| 73 | repoPkgs "1,104", | ||
| 74 | repoSize "5.4 G", | ||
| 75 | repoStatus "enabled", | ||
| 76 | repoUpdated "Mon Sep 14 08:18:15 2020" | ||
| 77 | } | ||
| 78 | |||
| 79 | =over | ||
| 80 | |||
| 81 | =item usage: | ||
| 82 | |||
| 83 | $aref = $yum->repoList([$enabled], [$disabled]); | ||
| 84 | |||
| 85 | $aref = $yum->repoList([enabled => 1], | ||
| 86 | [disabled => 0]); | ||
| 87 | |||
| 88 | =item args: | ||
| 89 | |||
| 90 | =over | ||
| 91 | |||
| 92 | =item enabled [Bool] (optional) | ||
| 93 | |||
| 94 | Flag indicating the returned list should include enabled repos. | ||
| 95 | |||
| 96 | Default: 1 | ||
| 97 | |||
| 98 | =item disabled [Bool] (optional) | ||
| 99 | |||
| 100 | Flag indicating the returned list should include disabled repos. | ||
| 101 | |||
| 102 | Default: 0 | ||
| 103 | |||
| 104 | =back | ||
| 105 | |||
| 106 | =back | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | multi method repoList (Bool :$enabled = 1, | ||
| 111 | Bool :$disabled = 0) { | ||
| 112 | |||
| 113 | my @cmd = ( 'yum', 'repolist', '-v', 'all' ); | ||
| 114 | |||
| 115 | my ( $stdout, $stderr, $exit ) = | ||
| 116 | $self->Spawn->capture( cmd => \@cmd, wantArrayRef => 1 ); | ||
| 117 | if ($exit) { | ||
| 118 | confess $stderr; | ||
| 119 | } | ||
| 120 | |||
| 121 | my @repos; | ||
| 122 | my $repoHref; | ||
| 123 | |||
| 124 | foreach my $line (@$stdout) { | ||
| 125 | |||
| 126 | next if $self->String->isBlank($line); | ||
| 127 | |||
| 128 | if ( $line =~ /^Repo-\w+\s/ ) { | ||
| 129 | my ( $key, $value ) = $self->_repoListParseLine($line); | ||
| 130 | if ( $key eq 'repoId' ) { | ||
| 131 | if ($repoHref) { | ||
| 132 | push @repos, $repoHref; | ||
| 133 | } | ||
| 134 | |||
| 135 | $repoHref = {}; | ||
| 136 | } | ||
| 137 | |||
| 138 | $repoHref->{$key} = $value; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | if ($repoHref) { | ||
| 143 | push @repos, $repoHref; | ||
| 144 | } | ||
| 145 | |||
| 146 | my $reposAref = []; | ||
| 147 | foreach my $repo (@repos) { | ||
| 148 | my $status = $repo->{repoStatus}; | ||
| 149 | if ( $status eq 'enabled' ) { | ||
| 150 | if ($enabled) { | ||
| 151 | push @$reposAref, $repo; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | elsif ( $status eq 'disabled' ) { | ||
| 155 | if ($disabled) { | ||
| 156 | push @$reposAref, $repo; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | confess "unhandled repoStatus: $status"; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | return $reposAref; | ||
| 165 | } | ||
| 166 | |||
| 167 | multi method repoList (Bool $enabled, | ||
| 168 | Bool $disabled) { | ||
| 169 | |||
| 170 | my %a; | ||
| 171 | $a{enabled} = $enabled if defined $enabled; | ||
| 172 | $a{disabled} = $disabled if defined $disabled; | ||
| 173 | |||
| 174 | return $self->repoList(%a); | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | =head2 repoQuery | ||
| 179 | |||
| 180 | Captures the output from the 'repoquery' command. | ||
| 181 | |||
| 182 | Returns: ArrayRef[Str] | ||
| 183 | |||
| 184 | =over | ||
| 185 | |||
| 186 | =item usage: | ||
| 187 | |||
| 188 | $aref = $yum->repoQuery ([all => 0|1], | ||
| 189 | [installed => 0|1], | ||
| 190 | [repoId => $repoId]); | ||
| 191 | |||
| 192 | Positional params not supported for this method due to | ||
| 193 | the volume of options. | ||
| 194 | |||
| 195 | =item args: | ||
| 196 | |||
| 197 | =over | ||
| 198 | |||
| 199 | =item all [Bool] (optional) | ||
| 200 | |||
| 201 | Equivalent to the --all flag on the cli. | ||
| 202 | |||
| 203 | Default: 0 | ||
| 204 | |||
| 205 | =item installed [Bool] (optional) | ||
| 206 | |||
| 207 | Equivalent to the --installed flag on the cli. | ||
| 208 | |||
| 209 | Default: 0 | ||
| 210 | |||
| 211 | =item repoId [Str] (optional) | ||
| 212 | |||
| 213 | Equivalent to the --repoid option on the cli. | ||
| 214 | |||
| 215 | =back | ||
| 216 | |||
| 217 | =back | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | method repoQuery (Bool :$all, | ||
| 222 | Bool :$installed, | ||
| 223 | Str :$repoId) { | ||
| 224 | |||
| 225 | my @cmd = ( 'repoquery' ); | ||
| 226 | push @cmd, '--all' if $all; | ||
| 227 | push @cmd, '--installed' if $installed; | ||
| 228 | push @cmd, '--repoid', $repoId if $repoId; | ||
| 229 | |||
| 230 | my ( $stdout, $stderr, $exit ) = | ||
| 231 | $self->Spawn->capture( cmd => \@cmd, wantArrayRef => 1 ); | ||
| 232 | if ($exit) { | ||
| 233 | confess $stderr; | ||
| 234 | } | ||
| 235 | |||
| 236 | return $stdout; | ||
| 237 | } | ||
| 238 | |||
| 239 | #################################################################3 | ||
| 240 | |||
| 241 | method _repoListParseLine (Str $line) { | ||
| 242 | |||
| 243 | my ( $key, @value ) = split( /:/, $line ); | ||
| 244 | |||
| 245 | $key = $self->String->trim($key); | ||
| 246 | $key = $self->String->camelize($key); | ||
| 247 | |||
| 248 | my $value = join ':', @value; | ||
| 249 | $value = $self->String->trim($value); | ||
| 250 | |||
| 251 | return ( $key, $value ); | ||
| 252 | } | ||
| 253 | |||
| 254 | 1; |