Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added support for installed softwares by Snap on linux
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
lib/FusionInventory/Agent/Task/Inventory/Generic/Softwares/Snap.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package FusionInventory::Agent::Task::Inventory::Generic::Softwares::Snap; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use parent 'FusionInventory::Agent::Task::Inventory::Module'; | ||
|
||
use File::stat; | ||
use YAML::Tiny; | ||
|
||
use FusionInventory::Agent::Tools; | ||
|
||
sub isEnabled { | ||
return canRun('snap'); | ||
} | ||
|
||
sub doInventory { | ||
my (%params) = @_; | ||
|
||
my $inventory = $params{inventory}; | ||
my $logger = $params{logger}; | ||
|
||
my $packages = _getPackagesList( | ||
logger => $logger, | ||
command => 'snap list --color never', | ||
); | ||
return unless $packages; | ||
|
||
foreach my $snap (@{$packages}) { | ||
_getPackagesInfo( | ||
logger => $logger, | ||
snap => $snap, | ||
command => 'snap info --color never --abs-time '.$snap->{NAME}, | ||
); | ||
$inventory->addEntry( | ||
section => 'SOFTWARES', | ||
entry => $snap | ||
); | ||
} | ||
} | ||
|
||
sub _getPackagesList { | ||
my (%params) = @_; | ||
|
||
my $handle = getFileHandle(%params); | ||
return unless $handle; | ||
|
||
my @packages; | ||
while (my $line = <$handle>) { | ||
chomp $line; | ||
my @infos = split(/\s+/, $line) | ||
or next; | ||
|
||
# Skip header | ||
next if $infos[0] eq 'Name' && $infos[1] eq 'Version'; | ||
|
||
# Skip base and snapd | ||
next if $infos[5] && $infos[5] =~ /^base|snapd$/; | ||
|
||
my $snap = { | ||
NAME => $infos[0], | ||
VERSION => $infos[1], | ||
FROM => 'snap' | ||
}; | ||
|
||
my $folder = "/snap/".$snap->{NAME}; | ||
if (-d $folder) { | ||
my $st = stat($folder); | ||
my ($year, $month, $day) = (localtime($st->mtime))[5, 4, 3]; | ||
$snap->{INSTALLDATE} = sprintf( | ||
"%02d/%02d/%04d", $day, $month + 1, $year + 1900 | ||
); | ||
} | ||
|
||
push @packages, $snap, | ||
} | ||
close $handle; | ||
|
||
return \@packages; | ||
} | ||
|
||
sub _getPackagesInfo { | ||
my (%params) = @_; | ||
|
||
my $snap = delete $params{snap}; | ||
my $lines = getAllLines(%params) | ||
or return; | ||
|
||
my $yaml = YAML::Tiny->read_string($lines); | ||
my $infos = $yaml->[0] | ||
or return; | ||
|
||
return unless $infos->{name}; | ||
|
||
$snap->{PUBLISHER} = $infos->{publisher}; | ||
# Cleanup publisher from 'starred' if verified | ||
$snap->{PUBLISHER} =~ s/[*]$//; | ||
$snap->{COMMENTS} = $infos->{summary}; | ||
$snap->{HELPLINK} = $infos->{contact}; | ||
|
||
# Find installed size | ||
my ($size) = $infos->{installed} =~ /\(.*\)\s+(\d+\S+)/; | ||
$snap->{FILESIZE} = getCanonicalSize($size, 1024) * 1048576 | ||
if $size; | ||
} | ||
|
||
1; |