| 1 | package Util::Medley::PkgManager::YUM; | ||
| 2 | $Util::Medley::PkgManager::YUM::VERSION = '0.049'; | ||
| 3 | use Modern::Perl; | ||
| 4 | use Moose; | ||
| 5 | use namespace::autoclean; | ||
| 6 | use Data::Printer alias => 'pdump'; | ||
| 7 | use Kavorka 'method', 'multi'; | ||
| 8 | use English; | ||
| 9 | |||
| 10 | with | ||
| 11 | 'Util::Medley::Roles::Attributes::Spawn', | ||
| 12 | 'Util::Medley::Roles::Attributes::String'; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Util::Medley::PkgManager::YUM - Class for interacting with YUM | ||
| 17 | |||
| 18 | =head1 VERSION | ||
| 19 | |||
| 20 | version 0.049 | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | my $yum = Util::Medley::Yum->new; | ||
| 27 | |||
| 28 | # | ||
| 29 | # positional | ||
| 30 | # | ||
| 31 | $aref = $yum->repoList([$enabled], [$disabled]); | ||
| 32 | |||
| 33 | # | ||
| 34 | # named pair | ||
| 35 | # | ||
| 36 | $aref = $yum->repoList([enabled => 0|1], | ||
| 37 | [disabled => 0|1]); | ||
| 38 | |||
| 39 | $aref = $yum->repoQuery ([all => 0|1], | ||
| 40 | [installed => 0|1], | ||
| 41 | [repoId => $repoId]); | ||
| 42 | =cut | ||
| 43 | |||
| 44 | ######################################################## | ||
| 45 | |||
| 46 | =head1 DESCRIPTION | ||
| 47 | |||
| 48 | A simple wrapper library for YUM on Linux. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | ######################################################## | ||
| 53 | |||
| 54 | =head1 ATTRIBUTES | ||
| 55 | |||
| 56 | none | ||
| 57 | |||
| 58 | =head1 METHODS | ||
| 59 | |||
| 60 | =head2 repoList | ||
| 61 | |||
| 62 | Generates a list of configured YUM repositories. | ||
| 63 | |||
| 64 | Returns: ArrayRef[HashRef] | ||
| 65 | |||
| 66 | Example HashRef: | ||
| 67 | |||
| 68 | { | ||
| 69 | repoBaseurl "http://centos3.zswap.net/7.8.2003/updates/x86_64/ (9 more)", | ||
| 70 | repoExpire "21,600 second(s) (last: Tue Oct 13 12:14:28 2020)", | ||
| 71 | repoId "updates/7/x86_64", | ||
| 72 | repoMirrors "http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=updates&infra=vag", | ||
| 73 | repoName "CentOS-7 - Updates", | ||
| 74 | repoPkgs "1,104", | ||
| 75 | repoSize "5.4 G", | ||
| 76 | repoStatus "enabled", | ||
| 77 | repoUpdated "Mon Sep 14 08:18:15 2020" | ||
| 78 | } | ||
| 79 | |||
| 80 | =over | ||
| 81 | |||
| 82 | =item usage: | ||
| 83 | |||
| 84 | $aref = $yum->repoList([$enabled], [$disabled]); | ||
| 85 | |||
| 86 | $aref = $yum->repoList([enabled => 1], | ||
| 87 | [disabled => 0]); | ||
| 88 | |||
| 89 | =item args: | ||
| 90 | |||
| 91 | =over | ||
| 92 | |||
| 93 | =item enabled [Bool] (optional) | ||
| 94 | |||
| 95 | Flag indicating the returned list should include enabled repos. | ||
| 96 | |||
| 97 | Default: 1 | ||
| 98 | |||
| 99 | =item disabled [Bool] (optional) | ||
| 100 | |||
| 101 | Flag indicating the returned list should include disabled repos. | ||
| 102 | |||
| 103 | Default: 0 | ||
| 104 | |||
| 105 | =back | ||
| 106 | |||
| 107 | =back | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | multi method repoList( Bool :$enabled = 1, 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 | my $cnt = 0; | ||
| 124 | |||
| 125 | foreach my $line (@$stdout) { | ||
| 126 | |||
| 127 | next if $self->String->isBlank($line); | ||
| 128 | |||
| 129 | if ( $line =~ /^Repo-\w+\s/ ) { | ||
| 130 | |||
| 131 | my ( $key, $value ) = $self->_repoListParseLine($line); | ||
| 132 | |||
| 133 | if ( $key eq 'repoId' ) { | ||
| 134 | if ($cnt > 0) { | ||
| 135 | push @repos, $repoHref; | ||
| 136 | } | ||
| 137 | |||
| 138 | $value = $self->_parseRepoId($value); | ||
| 139 | $repoHref = {}; | ||
| 140 | $cnt++; | ||
| 141 | } | ||
| 142 | |||
| 143 | $repoHref->{$key} = $value; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | if ( $cnt > 0 ) {#$repoHref->{repoId} ) { | ||
| 148 | push @repos, $repoHref; | ||
| 149 | } | ||
| 150 | |||
| 151 | ### | ||
| 152 | |||
| 153 | my $reposAref = []; | ||
| 154 | |||
| 155 | foreach my $repo (@repos) { | ||
| 156 | my $status = $repo->{repoStatus}; | ||
| 157 | if ( $status eq 'enabled' ) { | ||
| 158 | push @$reposAref, $repo if $enabled; | ||
| 159 | } | ||
| 160 | elsif ( $status eq 'disabled' ) { | ||
| 161 | push @$reposAref, $repo if $disabled; | ||
| 162 | } | ||
| 163 | else { | ||
| 164 | confess "unhandled repoStatus: $status"; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | return $reposAref; | ||
| 169 | } | ||
| 170 | |||
| 171 | multi method repoList( Bool $enabled, Bool $disabled) { | ||
| 172 | |||
| 173 | my %a; | ||
| 174 | $a{enabled} = $enabled if defined $enabled; | ||
| 175 | $a{disabled} = $disabled if defined $disabled; | ||
| 176 | |||
| 177 | return $self->repoList(%a); | ||
| 178 | } | ||
| 179 | |||
| 180 | =head2 repoQuery | ||
| 181 | |||
| 182 | Captures the output from the 'repoquery' command. | ||
| 183 | |||
| 184 | Returns: ArrayRef[Str] | ||
| 185 | |||
| 186 | =over | ||
| 187 | |||
| 188 | =item usage: | ||
| 189 | |||
| 190 | $aref = $yum->repoQuery ([all => 0|1], | ||
| 191 | [installed => 0|1], | ||
| 192 | [repoId => $repoId]); | ||
| 193 | |||
| 194 | Positional params not supported for this method due to | ||
| 195 | the volume of options. | ||
| 196 | |||
| 197 | =item args: | ||
| 198 | |||
| 199 | =over | ||
| 200 | |||
| 201 | =item all [Bool] (optional) | ||
| 202 | |||
| 203 | Equivalent to the --all flag on the cli. | ||
| 204 | |||
| 205 | Default: 0 | ||
| 206 | |||
| 207 | =item installed [Bool] (optional) | ||
| 208 | |||
| 209 | Equivalent to the --installed flag on the cli. | ||
| 210 | |||
| 211 | Default: 0 | ||
| 212 | |||
| 213 | =item repoId [Str] (optional) | ||
| 214 | |||
| 215 | Equivalent to the --repoid option on the cli. | ||
| 216 | |||
| 217 | =back | ||
| 218 | |||
| 219 | =back | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | method repoQuery (Bool :$all, | ||
| 224 | Bool :$installed, | ||
| 225 | Str :$repoId) { | ||
| 226 | |||
| 227 | my @cmd = ('repoquery'); | ||
| 228 | push @cmd, '--all' if $all; | ||
| 229 | push @cmd, '--installed' if $installed; | ||
| 230 | push @cmd, '--repoid', $repoId if $repoId; | ||
| 231 | |||
| 232 | my ( $stdout, $stderr, $exit ) = | ||
| 233 | $self->Spawn->capture( cmd => \@cmd, wantArrayRef => 1 ); | ||
| 234 | if ($exit) { | ||
| 235 | confess $stderr; | ||
| 236 | } | ||
| 237 | |||
| 238 | return $stdout; | ||
| 239 | } | ||
| 240 | |||
| 241 | =head2 list | ||
| 242 | |||
| 243 | Captures the output from the 'yum list' command. | ||
| 244 | |||
| 245 | Returns: ArrayRef[Str] or ArrayRef[HashRef] | ||
| 246 | |||
| 247 | =over | ||
| 248 | |||
| 249 | =item usage: | ||
| 250 | |||
| 251 | $aref = $yum->list ([installed => 0|1], | ||
| 252 | [repoId => $repoId]); | ||
| 253 | |||
| 254 | Positional params not supported for this method due to | ||
| 255 | the volume of options. | ||
| 256 | |||
| 257 | =item args: | ||
| 258 | |||
| 259 | =over | ||
| 260 | |||
| 261 | =item installed [Bool] (optional) | ||
| 262 | |||
| 263 | List only installed packages. | ||
| 264 | |||
| 265 | Default: 0 | ||
| 266 | |||
| 267 | =item repoId [Str] (optional) | ||
| 268 | |||
| 269 | Limit the listing to this repo. This leverages 'repository-packages' under the | ||
| 270 | hood. | ||
| 271 | |||
| 272 | =item useSudo [Bool] (optional) | ||
| 273 | |||
| 274 | If a repoId is provided and your running under a user other than root, | ||
| 275 | you must use sudo privileges in order to run "yum repo-pkgs". Must be due to | ||
| 276 | some "feature" I am not privy to. | ||
| 277 | |||
| 278 | =item parseOutput [Bool] (optional) | ||
| 279 | |||
| 280 | Indicates the caller wants the output parsed and put into a hashref. | ||
| 281 | |||
| 282 | =back | ||
| 283 | |||
| 284 | =back | ||
| 285 | |||
| 286 | =cut | ||
| 287 | |||
| 288 | method list (Bool :$installed, | ||
| 289 | Str :$repoId, | ||
| 290 | Bool :$useSudo, | ||
| 291 | Bool :$parseOutput) { | ||
| 292 | |||
| 293 | my @cmd; | ||
| 294 | push @cmd, 'sudo' if $useSudo; | ||
| 295 | push @cmd, 'yum'; | ||
| 296 | push @cmd, '--quiet'; | ||
| 297 | push @cmd, '--color', 'never'; | ||
| 298 | push @cmd, 'repo-pkgs', $repoId | ||
| 299 | if $repoId; # this requires root/sudo for some reason | ||
| 300 | push @cmd, 'list'; | ||
| 301 | push @cmd, 'installed' if $installed; | ||
| 302 | |||
| 303 | my ( $stdout, $stderr, $exit ) = | ||
| 304 | $self->Spawn->capture( cmd => \@cmd, wantArrayRef => 1 ); | ||
| 305 | if ($exit) { | ||
| 306 | confess $stderr; | ||
| 307 | } | ||
| 308 | |||
| 309 | my $cleansed = $self->_cleanseListOutput($stdout); | ||
| 310 | |||
| 311 | if ($parseOutput) { | ||
| 312 | |||
| 313 | my @parsed; | ||
| 314 | foreach my $line (@$cleansed) { | ||
| 315 | |||
| 316 | my ($rpmName, $version, $repo) = split /\s+/, $line; | ||
| 317 | my $href = { | ||
| 318 | rpmName => $rpmName, | ||
| 319 | version => $version, | ||
| 320 | repo => $repo | ||
| 321 | }; | ||
| 322 | |||
| 323 | push @parsed, $line; | ||
| 324 | } | ||
| 325 | |||
| 326 | return \@parsed; | ||
| 327 | } | ||
| 328 | |||
| 329 | return $cleansed; | ||
| 330 | } | ||
| 331 | |||
| 332 | #################################################################3 | ||
| 333 | |||
| 334 | method _parseRepoId (Str $value) { | ||
| 335 | |||
| 336 | # | ||
| 337 | # from 'man yum' | ||
| 338 | # | ||
| 339 | # In non-verbose mode the first column will start with a ´*´ if the | ||
| 340 | # repo. has metalink data and the latest metadata is not local and | ||
| 341 | # will start with a ´!´ if the repo. has metadata that is expired. For | ||
| 342 | # non-verbose mode the last column will also display the number of | ||
| 343 | # packages in the repo. and (if there are any user specified excludes) | ||
| 344 | # the number of packages excluded. | ||
| 345 | # | ||
| 346 | $value =~ s/^[!\*]//; | ||
| 347 | |||
| 348 | # | ||
| 349 | # from 'man yum.conf' | ||
| 350 | # | ||
| 351 | # ui_repoid_vars: When a repository id is displayed, append these yum | ||
| 352 | # variables to the string if they are used in the | ||
| 353 | # baseurl/etc. Variables are appended in the order | ||
| 354 | # listed (and found). Default is 'releasever basearch'. | ||
| 355 | # | ||
| 356 | my ($repoId) = split( /\//, $value ); | ||
| 357 | |||
| 358 | return $repoId; | ||
| 359 | } | ||
| 360 | |||
| 361 | method _repoListParseLine (Str $line) { | ||
| 362 | |||
| 363 | my ( $key, @value ) = split( /:/, $line ); | ||
| 364 | |||
| 365 | $key = $self->String->trim($key); | ||
| 366 | $key = $self->String->camelize($key); | ||
| 367 | |||
| 368 | my $value = join ':', @value; | ||
| 369 | $value = $self->String->trim($value); | ||
| 370 | |||
| 371 | return ( $key, $value ); | ||
| 372 | } | ||
| 373 | |||
| 374 | # | ||
| 375 | # this method removes the header line ("...... packages") and joins | ||
| 376 | # broken lines. | ||
| 377 | # | ||
| 378 | method _cleanseListOutput (ArrayRef $list) { | ||
| 379 | |||
| 380 | my @clean; | ||
| 381 | my $prev; | ||
| 382 | |||
| 383 | foreach my $line (@$list) { | ||
| 384 | |||
| 385 | next if $line =~ /^\w+ packages$/i; | ||
| 386 | |||
| 387 | my ($rpmName, $version, $repo) = split /\s+/, $line; | ||
| 388 | if ($rpmName) { | ||
| 389 | if (defined $version) { | ||
| 390 | if ($repo) { | ||
| 391 | # happy path | ||
| 392 | push @clean, $line; | ||
| 393 | next; | ||
| 394 | } | ||
| 395 | } | ||
| 396 | } | ||
| 397 | else { | ||
| 398 | # join with prev line | ||
| 399 | my $joined = join ' ', $prev, $self->String->trim($line); | ||
| 400 | $joined =~ s/\s\s+/ /g; | ||
| 401 | |||
| 402 | my ($rpmName, $version, $repo) = split /\s+/, $joined; | ||
| 403 | if ($rpmName and defined $version and $repo) { | ||
| 404 | # happy path | ||
| 405 | push @clean, $joined; | ||
| 406 | } | ||
| 407 | else { | ||
| 408 | confess "unable to join output lines: $joined"; | ||
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | $prev = $line; | ||
| 413 | } | ||
| 414 | |||
| 415 | return \@clean; | ||
| 416 | } | ||
| 417 | |||
| 418 | 1; |