1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Collector\Package\Dnf; |
4
|
|
|
|
5
|
|
|
use Startwind\Inventorio\Collector\Collector; |
6
|
|
|
use Startwind\Inventorio\Collector\OperatingSystem\OperatingSystemCollector; |
7
|
|
|
use Startwind\Inventorio\Exec\Runner; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This collector returns details about all installed DNF (RPM-based) packages. |
11
|
|
|
*/ |
12
|
|
|
class DnfPackageCollector implements Collector |
13
|
|
|
{ |
14
|
|
|
protected const COLLECTION_IDENTIFIER = 'DnfPackages'; |
15
|
|
|
|
16
|
|
|
public function getIdentifier(): string |
17
|
|
|
{ |
18
|
|
|
return self::COLLECTION_IDENTIFIER; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function collect(): array |
22
|
|
|
{ |
23
|
|
|
if (OperatingSystemCollector::getOsFamily() !== OperatingSystemCollector::OS_FAMILY_LINUX) { |
24
|
|
|
return []; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$result = [ |
28
|
|
|
'packages' => $this->collectPackages(), |
29
|
|
|
'updatable' => $this->collectUpdatablePackages() |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
return $result; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function collectPackages(): array |
36
|
|
|
{ |
37
|
|
|
if (!Runner::getInstance()->commandExists('rpm')) { |
38
|
|
|
return []; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$command = 'rpm -qa --qf "%{NAME} %{VERSION}-%{RELEASE}\n"'; |
42
|
|
|
|
43
|
|
|
$output = Runner::getInstance()->run($command)->getOutput(); |
44
|
|
|
|
45
|
|
|
$packages = (explode("\n", $output)); |
46
|
|
|
|
47
|
|
|
$packageList = []; |
48
|
|
|
|
49
|
|
|
foreach ($packages as $packageObject) { |
50
|
|
|
$parts = explode(' ', $packageObject); |
51
|
|
|
if (count($parts) > 0 && $parts[0]) { |
52
|
|
|
$packageList[$parts[0]] = $parts[1]; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $packageList; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function collectUpdatablePackages(): array |
60
|
|
|
{ |
61
|
|
|
if (!Runner::getInstance()->commandExists('dnf')) { |
62
|
|
|
return []; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$output = Runner::getInstance()->run('dnf check-update -q || true')->getOutput(); |
66
|
|
|
$lines = explode("\n", $output); |
67
|
|
|
|
68
|
|
|
$packages = []; |
69
|
|
|
|
70
|
|
|
foreach ($lines as $line) { |
71
|
|
|
if (preg_match('/^(\S+)\s+(\S+)\s+(\S+)/', $line, $matches)) { |
72
|
|
|
$packages[$matches[1]] = [ |
73
|
|
|
'currentVersion' => null, // Fedora does not show current version here |
74
|
|
|
'newVersion' => $matches[2] |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $packages; |
80
|
|
|
} |
81
|
|
|
} |