File Coverage

blib/lib/Tapper/Installer/Precondition/Package.pm
Criterion Covered Total %
statement 52 65 80.0
branch 14 30 46.6
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 74 103 71.8


line stmt bran cond sub pod time code
1             our $AUTHORITY = 'cpan:TAPPER';
2             $Tapper::Installer::Precondition::Package::VERSION = '5.0.1';
3             use strict;
4 4     4   175334 use warnings;
  4         20  
  4         110  
5 4     4   18 use 5.010;
  4         11  
  4         96  
6 4     4   68  
  4         16  
7             use Tapper::Installer::Precondition::Exec;
8 4     4   415 use File::Basename;
  4         8  
  4         144  
9 4     4   27 use Moose;
  4         9  
  4         260  
10 4     4   24 extends 'Tapper::Installer::Precondition';
  4         7  
  4         27  
11              
12              
13              
14             {
15             my ($self, $package) = @_;
16             if ($package->{url}) {
17 4     4 1 3437 my ($proto, $fullpath) = $package->{url} =~ m|^(\w+)://(.+)$|;
18 4 100       24 if ($proto eq 'nfs') {
19 1         158 my $nfs_dir='/mnt/nfs';
20 1 50       14 $self->makedir($nfs_dir);
21 1         9 my $path = dirname $fullpath;
22 1         13 my $filename = basename $fullpath;
23 1         159 my ($error, $retval) = $self->log_and_exec("mount $path $nfs_dir");
24 1         50 return ("Can't mount nfs share $path to $nfs_dir: $retval") if $error;
25 1         19 delete $package->{url};
26 1 50       85 $package->{filename} = "$nfs_dir/$filename";
27 1         7 $self->install($package);
28 1         16 ($error, $retval) = $self->log_and_exec("umount $nfs_dir");
29 1         69 return 0;
30 1         5 }
31 1         11 else { return ("Procol'$proto' is not supported") }
32             }
33 0         0  
34              
35             my $filename = $package->{filename};
36             $self->log->debug("installing $filename");
37 3         15  
38 3         47 my $basedir = $self->cfg->{paths}{base_dir};
39              
40 3         1263 # install into subdir
41             if ($package->{target_directory}) {
42             $basedir .= $package->{target_directory};
43 3 50       22 $self->makedir($basedir) if not -d $basedir;
44 0         0 }
45 0 0       0  
46             my $package_dir = '';
47             $package_dir = $self->cfg->{paths}{package_dir};
48 3         22 my $pkg = $filename;
49 3         85 $pkg = "$package_dir/$filename" unless $filename =~ m(^/);
50 3         8  
51 3 100       30 my ($error, $type) = $self->get_file_type("$pkg");
52             return("Can't get file type of $filename: $type") if $error;
53 3         52  
54 3 50       15  
55             my $output;
56             $self->log->debug("type is $type");
57 3         6 if ($type eq "gzip") {
58 3         16 ($error, $output) = $self->log_and_exec("tar --no-same-owner -C $basedir -xzf $pkg");
59 3 100       86 return("can't unpack package $filename: $output\n") if $error;
    50          
    50          
    50          
    0          
60 2         18 }
61 2 50       21 elsif ($type eq "tar") {
62             ($error, $output) = $self->log_and_exec("tar --no-same-owner -C $basedir -xf $pkg");
63             return("can't unpack package $filename: $output\n") if $error;
64 0         0 }
65 0 0       0 elsif ($type eq "bz2") {
66             ($error, $output) = $self->log_and_exec("tar --no-same-owner -C $basedir -xjf $pkg");
67             return("can't unpack package $filename: $output\n") if $error;
68 0         0 }
69 0 0       0 elsif ($type eq "deb") {
70             system("cp $pkg $basedir/");
71             $pkg = basename $pkg;
72 1         8115 my $exec = Tapper::Installer::Precondition::Exec->new($self->cfg);
73 1         189 return $exec->install({command => "dpkg -i $pkg"});
74 1         126 }
75 1         35 elsif ($type eq "rpm") {
76             system("cp $pkg $basedir/");
77             $pkg = basename $pkg;
78 0         0 my $exec = Tapper::Installer::Precondition::Exec->new($self->cfg);
79 0         0 # use -U to overwrite possibly existing older package
80 0         0 return $exec->install({command => "rpm -U $pkg"});
81             }
82 0         0 else {
83             $self->log->warn(qq($pkg is of unrecognised file type "$type"));
84             return(qq($pkg is of unrecognised file type "$type"));
85 0         0 }
86 0         0 return(0);
87             }
88 2         8  
89             1;
90              
91              
92             =pod
93              
94             =encoding UTF-8
95              
96             =head1 NAME
97              
98             Tapper::Installer::Precondition::Package
99              
100             =head1 SYNOPSIS
101              
102             use Tapper::Installer::Precondition::Package;
103              
104             =head1 NAME
105              
106             Tapper::Installer::Precondition::Package - Install a package to a given location
107              
108             =head1 FUNCTIONS
109              
110             =head2 install
111              
112             This function encapsulates installing one single package. At the moment, .tar,
113             .tar.gz, .tar.bz2, rpm and deb are recognised.
114             Recognised options for package preconditions are:
115             * filename - absolute or relative path of the package file (relativ to package_dir in config)
116             * target_directory - directory where to unpack package
117             *
118              
119             @param hash reference - contains all information about the package
120              
121             @return success - 0
122             @return error - error string
123              
124             =head1 AUTHORS
125              
126             =over 4
127              
128             =item *
129              
130             AMD OSRC Tapper Team <tapper@amd64.org>
131              
132             =item *
133              
134             Tapper Team <tapper-ops@amazon.com>
135              
136             =back
137              
138             =head1 COPYRIGHT AND LICENSE
139              
140             This software is Copyright (c) 2022 by Advanced Micro Devices, Inc.
141              
142             This is free software, licensed under:
143              
144             The (two-clause) FreeBSD License
145              
146             =cut