File Coverage

blib/lib/FusionInventory/Agent/Task/Deploy/Datastore.pm
Criterion Covered Total %
statement 33 70 47.1
branch 0 20 0.0
condition 0 9 0.0
subroutine 11 16 68.7
pod 0 5 0.0
total 44 120 36.6


line stmt bran cond sub pod time code
1             package FusionInventory::Agent::Task::Deploy::Datastore;
2              
3 2     2   27695974 use strict;
  2         7  
  2         106  
4 2     2   15 use warnings;
  2         4  
  2         97  
5              
6 2     2   7 use English qw(-no_match_vars);
  2         32  
  2         26  
7 2     2   1182 use File::Glob;
  2         2  
  2         139  
8 2     2   9 use File::Spec;
  2         2  
  2         34  
9 2     2   41 use File::Path qw(mkpath rmtree);
  2         2  
  2         273  
10 2     2   11 use UNIVERSAL::require;
  2         3  
  2         16  
11              
12 2     2   570 use FusionInventory::Agent::Tools;
  2         4  
  2         284  
13 2     2   507 use FusionInventory::Agent::Logger;
  2         3  
  2         133  
14 2     2   869 use FusionInventory::Agent::Task::Deploy::Datastore::WorkDir;
  2         7  
  2         29  
15 2     2   777 use FusionInventory::Agent::Task::Deploy::DiskFree;
  2         4  
  2         1074  
16              
17             sub new {
18 0     0 0   my ($class, %params) = @_;
19              
20 0 0         die "no path parameter" unless $params{path};
21              
22 0   0       my $self = {
23             path => File::Spec->rel2abs($params{path}),
24             logger => $params{logger} ||
25             FusionInventory::Agent::Logger->new(),
26             };
27              
28 0 0         if (!$self->{path}) {
29 0           die("No datastore path");
30             }
31              
32 0           bless $self, $class;
33              
34 0           return $self;
35             }
36              
37             sub cleanUp {
38 0     0 0   my ($self) = @_;
39              
40 0 0         return unless -d $self->{path};
41              
42 0           my @storageDirs;
43 0           push @storageDirs, File::Glob::glob($self->{path}.'/fileparts/private/*');
44 0           push @storageDirs, File::Glob::glob($self->{path}.'/fileparts/shared/*');
45              
46 0           my $diskFull=$self->diskIsFull();
47 0 0         if (-d $self->{path}.'/workdir/') {
48 0           remove_tree( $self->{path}.'/workdir/', {error => \my $err} );
49             }
50              
51 0           foreach my $dir (@storageDirs) {
52              
53 0 0         if (!-d $dir) {
54 0           unlink $dir;
55 0           next;
56             }
57              
58 0 0         next unless $dir =~ /(\d+)$/;
59              
60 0 0 0       if (time > $1 || $diskFull) {
61 0           remove_tree( $dir, {error => \my $err} );
62             }
63             }
64              
65             }
66              
67             sub createWorkDir {
68 0     0 0   my ($self, $uuid) = @_;
69              
70             # mkpath($filePath);
71              
72 0           my $path = $self->{path}.'/workdir/'.$uuid;
73              
74 0           mkpath($path);
75 0 0         return unless -d $path;
76              
77 0           return FusionInventory::Agent::Task::Deploy::Datastore::WorkDir->new(
78             path => $path,
79             logger => $self->{logger}
80             );
81             }
82              
83             sub diskIsFull {
84 0     0 0   my ($self) = @_;
85              
86 0           my $logger = $self->{logger};
87              
88 0           my $freeSpace = getFreeSpace(
89             path => $self->{path},
90             logger => $logger
91             );
92              
93 0 0         if (!$freeSpace) {
94 0           $logger->debug('$spaceFree is undef!');
95 0           $freeSpace = 0;
96             }
97              
98 0           $logger->debug("Free space on $self->{path}: $freeSpace");
99             # 400MB Free, should be set by a config option
100 0           return ($freeSpace < 2000);
101             }
102              
103             # imported from File-Path-2.09
104             sub remove_tree {
105 0 0 0 0 0   push @_, {} unless @_ and UNIVERSAL::isa($_[-1],'HASH');
106 0           goto &rmtree;
107             }
108              
109             1;