PITA/Host.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 131 CC 10 SCAR 29.6
CC
10
Cov
0%
new
56.0
image_extract
12.0
image_cache_apply_quota
6.0
CRAP 19.3
1 package PITA::Host;
2  
3 # Implements a single PITA Testing Host.
4 # Responsible for managing images and processing Requests
5  
6 use 5.008;
7 use strict;
8 use Carp ();
9 use File::Spec ();
10 use File::Remove ();
11 use File::Find::Rule ();
12 use Archive::Extract ();
13  
14 use constant FFR => 'File::Find::Rule';
15  
16 our $VERSION = '0.60';
17  
18  
19  
20  
21  
22 #####################################################################
23 # Constructor
24  
25 sub new {
26 my $class = shift;
27 my $self = bless { @_ }, $class;
28  
29 # Check we have a long term image storage
30 my $image_store = $self->image_store;
31 unless ( $image_store ) {
32 Carp::croak("No image_store provided for PITA::Host");
33 }
34 unless ( -d $image_store and -w _ ) {
35 Carp::croak("No image_store '$image_store' found, or insufficient permissions");
36 }
37  
38 # Check we have a short term expanded image cache.
39 # This is also where the live images are.
40 my $image_cache = $self->image_cache;
41 unless ( $image_cache ) {
42 Carp::croak("No image_cache provided for PITA::Host");
43 }
44 unless ( -d $image_cache and -w _ ) {
45 Carp::croak("No image_cache '$image_cache' found, or insufficient permissions");
46 }
47  
48 # Check the quote for the cache
49 $self->{image_cache_quota} = 2048 unless $self->image_cache_quota; # 2 gig default
50 unless ( _POSINT($self->image_cache_quota) ) {
51 Carp::croak("Invalid image_cache_quota. Not a positive integer");
52 }
53  
54 $self;
55 }
56  
57 sub image_store {
58 $_[0]->{image_store};
59 }
60  
61 sub image_cache {
62 $_[0]->{image_cache};
63 }
64  
65 sub image_cache_quota {
66 $_[0]->{image_cache_quota};
67 }
68  
69  
70  
71  
72  
73 #####################################################################
74 # Image and Cache Management
75  
76 sub image_extract {
77 my $self = shift;
78 my $name = shift;
79  
80 # What are we extracting to where
81 my $from = File::Spec->catfile(
82 $self->image_store, "$name.img.gz",
83 );
84 my $to = File::Spec->catfile(
85 $self->image_cache, "$name.img",
86 );
87  
88 # Extract the compressed image
89 local $Archive::Extract::PREFER_BIN = 1;
90 my $archive = Archive::Extract->new( archive => $from, type => 'gz' );
91 unless ( $archive ) {
92 Carp::croak("Failed to create Archive::Extract for $from");
93 }
94 unless ( $archive->extract( to => $to ) ) {
95 Carp::croak("Failed to extract archive to $to");
96 }
97  
98 1;
99 }
100  
101 sub image_cache_clear {
102 my $self = shift;
103  
104 # Find all image files
105 my @files = FFR->file
106 ->name( '*.img' )
107 ->in( $self->image_cache );
108  
109 # Delete all image files
110 foreach my $file ( @files ) {
111 File::Remove::remove( $file )
112 or Carp::croak("Failed to delete $file");
113 }
114  
115 1;
116 }
117  
118 sub image_cache_apply_quota {
119 my $self = shift;
120  
121 # Find the list of expanded files (recent to oldest)
122 my @files = FFR->file
123 ->name( '*.img' )
124 ->in( $self->image_cache );
125 @files = map { [ stat($_), $_ ] } @files;
126 @files = sort { $a->[8] <=> $b->[8] } @files;
127  
128 # Keep files from youngest to oldest, and then
129 # kill the rest.
130 my $quota = $self->image_cache_quota;
131 foreach my $file ( @files ) {
132 $quota -= $file->[7];
133 next if $quota >= 0;
134 File::Remove::remove($file->[-1])
135 or Carp::croak("Failed to delete $file->[-1]");
136 }
137  
138 1;
139 }
140  
141 1;