Repositorio/Repository/Yum.pm

filter: Statementstmt 20.4% 37 / 181 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 42.3% 11 / 26 Podpod 0.0% 0 / 4 totaltotal 18.1% 48 / 264 CC 24 SCAR 33.0
CC
24
Cov
17%
_run_createrepo
90.0
mirror
42.0
_download_packages
30.0
CRAP 27.2
« Repositorio/Server/Helper/Proxy.pm ↑ Summary Repositorio/Server/Docker/Image.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4 # vim: set ts=2 sw=2 tw=0:
5 # vim: set expandtab:
6  
7 package Rex::Repositorio::Repository::Yum;
8  
9 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Basename qw'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Path 'make_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Expect;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our $VERSION = '1.2.1'; # VERSION
22  
23 1 extends "Rex::Repositorio::Repository::Base";
24  
25 0 ▶sub mirror {
Subroutine mirror: not called CC=6 SCAR=37.4
Pod undocumented
26 0 my ( $self, %option ) = @_;
27  
28 0 ▶ unless ($self->repo->{url}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->repo->{"url"}) 0 0 0
29 0 $self->app->logger->notice('No URL, skipping mirror');
30 return
31 0 }
32  
33 0 $self->repo->{url} =~ s/\/$//;
34 0 $self->repo->{local} =~ s/\/$//;
35 0 my $name = $self->repo->{name};
36  
37 0 $self->app->logger->notice("Downloading metadata...");
38  
39 0 my ( $packages_ref, $repomd_ref );
40 0 ( $packages_ref, $repomd_ref ) = $self->_get_repomd_xml( $self->repo->{url} );
41 0 my @packages = @{$packages_ref};
42  
43 0 my $url = $self->repo->{url} . "/repodata/repomd.xml";
44 0 my $destbase = $self->app->get_repo_dir(repo => $self->repo->{name});
45 0 my $repodatabase = File::Spec->catfile( 'repodata' );
46  
47 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 $self->download_metadata(
49 url => $url,
50 dest => File::Spec->catfile($repodatabase,'repomd.xml'),
51 force => $option{update_metadata},
52 0 );
53  
54 0 $self->app->logger->info("2/3 ${url}");
55  
56 $self->download_metadata(
57 url => $url,
58 dest => File::Spec->catfile($repodatabase,'repomd.xml'),
59 force => $option{update_metadata},
60 0 );
61  
62 0 $self->app->logger->info("3/3 ${url}");
63 }
64 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 $self->app->logger->info("3/3 ${url}");
66 0 $self->app->logger->error($_);
67 0 };
68  
69 0 $self->app->logger->notice('Downloading packages...');
70 0 $self->_download_packages( \%option, @packages );
71  
72 0 $self->app->logger->notice('Downloading rest of metadata...');
73  
74 0 my $m_count = 0;
75 0 my $m_total = scalar(@{$repomd_ref->{data}});
76  
77 0 for my $file_data ( @{ $repomd_ref->{data} } ) {
78  
79 my $file_url =
80 0 $self->{repo}->{url} . "/" . $file_data->{location}->[0]->{href};
81 0 my $file = basename $file_data->{location}->[0]->{href};
82  
83 0 $m_count++;
84 0 $self->app->logger->info("${m_count}/$m_total ${file_url}");
85  
86 $self->download_metadata(
87 url => $file_url,
88 dest => File::Spec->catfile($repodatabase,$file),
89 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 $self->_checksum(
91 @_,
92 $file_data->{checksum}->[0]->{type},
93 $file_data->{checksum}->[0]->{content}
94 0 );
95 },
96 force => $option{update_metadata},
97 0 );
98 }
99  
100 0 ▶ if ( exists $self->repo->{images} && $self->repo->{images} eq "true" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->repo->{"images"} and $self->repo->{"images"} eq "true") 0 0 0
Condition0%0/3
exists $self->repo->{"images"} and $self->repo->{"images"} eq "true"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->repo->{"images"} and $self->repo->{"images"} eq "true"
exists $self->repo->{"images"} $self->repo->{"images"} eq "true"
Truth table0%0/3
A and B
A= exists $self->repo->{"images"}B= $self->repo->{"images"} eq "true"
ABresult
0X0
100
111
101  
102 0 $self->app->logger->notice('Downloading images...');
103 0 my @files = (
104 "images/boot.iso", "images/efiboot.img",
105 "images/efidisk.img", "images/install.img",
106 "images/pxeboot/initrd.img", "images/pxeboot/vmlinuz",
107 "images/upgrade.img", "LiveOS/squashfs.img",
108 );
109 0 my $file_count = 0;
110 0 my $file_total = scalar @files;
111  
112 0 for my $file (@files) {
113 0 my $file_url = $self->repo->{url} . "/" . $file;
114 0 my $local_file = File::Spec->catfile($file);
115  
116 0 $file_count++;
117 0 $self->app->logger->info("${file_count}/$file_total ${file_url}");
118  
119 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 $self->download_package(
121 url => $file_url,
122 name => $file,
123 dest => $local_file,
124 );
125 0 1;
126 }
127 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 $self->app->logger->error("Error downloading ${file_url}.");
129 0 };
130 }
131 }
132 }
133  
134 0 ▶sub _download_packages {
Subroutine _download_packages: not called CC=5 SCAR=34.0
135 0 my ( $self, $_option, @packages ) = @_;
136  
137 0 ▶ if ( !$_option->{update_files} && !$_option->{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_option->{"update_files"} or $_option->{"force"}) 0 0 0
Condition0%0/3
$_option->{"update_files"} or $_option->{"force"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_option->{"update_files"} or $_option->{"force"}
$_option->{"update_files"} $_option->{"force"}
Truth table0%0/3
A or B
A= $_option->{"update_files"}B= $_option->{"force"}
ABresult
1X1
011
000
138 0 return;
139 }
140  
141 0 my %option = %{$_option};
142  
143 0 my $p_count = 0;
144 0 my $p_total = scalar @packages;
145  
146 0 my $destbase = $self->app->get_repo_dir(repo => $self->repo->{name});
147  
148 0 for my $package (@packages) {
149 0 my $package_url = $self->repo->{url} . "/" . $package->{location};
150 0 my $package_name = $package->{name};
151  
152 0 $p_count++;
153 0 $self->app->logger->info("${p_count}/$p_total ${package_url}");
154 0 my $local_file = File::Spec->catfile($package->{location});
155  
156 0 my ($type, $value);
157 0 ▶ if ($option{'checksums'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{'checksums'}) { } 0 0 0
158 0 $type = $package->{checksum}->{type};
159 0 $value = $package->{checksum}->{data};
160 }
161 else {
162 0 $type = 'size';
163 0 $value = $package->{size};
164 }
165  
166 $self->download_package(
167 url => $package_url,
168 name => $package_name,
169 dest => $local_file,
170 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 0 $self->_checksum(
172 @_,
173 $type,
174 $value,
175 );
176 },
177 update_file => $option{update_files},
178 force => $option{force},
179 0 );
180 }
181 }
182  
183 0 ▶sub _get_repomd_xml {
Subroutine _get_repomd_xml: not called CC=2 SCAR=17.9
184 0 my ( $self, $url ) = @_;
185  
186 0 my $repomd_ref =
187 $self->decode_xml( $self->download("${url}/repodata/repomd.xml") );
188  
189 my ($primary_file) =
190 0 grep { $_->{type} eq "primary" } @{ $repomd_ref->{data} };
191 0 $primary_file = $primary_file->{location}->[0]->{href};
192  
193 0 $url = $url . "/" . $primary_file;
194 0 my $xml = $self->get_xml( $self->download_gzip($url) );
195  
196 0 my @packages;
197 0 my @xml_packages = $xml->getElementsByTagName('package');
198 0 for my $xml_package (@xml_packages) {
199 0 my ($name_node) = $xml_package->getChildrenByTagName("name");
200 0 my ($checksum_node) = $xml_package->getChildrenByTagName("checksum");
201 0 my ($size_node) = $xml_package->getChildrenByTagName("size");
202 0 my ($location_node) = $xml_package->getChildrenByTagName("location");
203 0 push @packages, {
204 location => $location_node->getAttribute("href"),
205 name => $name_node->textContent,
206 checksum => {
207 type => $checksum_node->getAttribute("type"),
208 data => $checksum_node->textContent,
209 },
210 size => $size_node->getAttribute('package'),
211 };
212 }
213  
214 0 return ( \@packages, $repomd_ref );
215 }
216  
217 0 ▶sub init {
Subroutine init: not called CC=3 SCAR=24.8
Pod undocumented
218 0 my $self = shift;
219  
220 0 my $repo_dir = $self->app->get_repo_dir(repo => $self->repo->{name});
221 0 my $repodata_path = File::Spec->catdir($repo_dir, 'repodata');
222 0 $self->app->logger->debug("init: repodata_path: ${repodata_path}");
223 0 ▶ unless (-d $repodata_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $repodata_path) 0 0 0
224 0 $self->app->logger->debug("init: make_path: ${repodata_path}");
225 0 my $make_path_error;
226 #my $dirs = File::Path->make_path($repodata_path, { error => \$make_path_error },);
227 #my $dirs = File::Path->make_path($repodata_path);
228 0 ▶ unless (make_path($repodata_path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (make_path($repodata_path)) 0 0 0
229 0 $self->app->logger->log_and_croak(level => 'error', message => "init: unable to create path: ${repodata_path}");
230 }
231 }
232  
233 0 $self->_run_createrepo();
234 }
235  
236 0 ▶sub add_file {
Subroutine add_file: not called CC=3 SCAR=24.8
Pod undocumented
237 0 my $self = shift;
238 0 my %option = validate(
239 @_,
240 {
241 file => {
242 type => SCALAR
243 },
244 }
245 );
246  
247 0 my $dest = File::Spec->catfile($self->app->get_repo_dir(repo => $self->repo->{name}), basename( $option{file} ));
248  
249 0 ▶ if ( exists $self->repo->{gpg} && $self->repo->{gpg}->{key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->repo->{"gpg"} and $self->repo->{"gpg"}{"key"}) 0 0 0
Condition0%0/3
exists $self->repo->{"gpg"} and $self->repo->{"gpg"}{"key"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->repo->{"gpg"} and $self->repo->{"gpg"}{"key"}
exists $self->repo->{"gpg"} $self->repo->{"gpg"}{"key"}
Truth table0%0/3
A and B
A= exists $self->repo->{"gpg"}B= $self->repo->{"gpg"}{"key"}
ABresult
0X0
100
111
250 0 my $key_id = $self->repo->{gpg}->{key};
251 0 my $exp = Expect->spawn("/bin/rpmsign", "--addsign", "--key-id=$key_id", $option{file});
252 $exp->expect(60, [
253 0 ▶ qr/Enter pass phrase:/ => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
254 0 my $exp = shift;
255 0 $exp->send($self->repo->{gpg}->{password} . "\n");
256 0 exp_continue;
257 },
258 0 ]);
259 0 $exp->soft_close();
260 }
261  
262 0 $self->add_file_to_repo( source => $option{file}, dest => $dest );
263  
264 0 $self->_run_createrepo();
265 }
266  
267 0 ▶sub remove_file {
Subroutine remove_file: not called CC=1 SCAR=6.9
Pod undocumented
268 0 my $self = shift;
269  
270 0 my %option = validate(
271 @_,
272 {
273 file => {
274 type => SCALAR
275 },
276 }
277 );
278  
279 0 my $file = File::Spec->catfile($self->app->get_repo_dir(repo => $self->repo->{name}), basename( $option{file} ));
280  
281 0 $self->remove_file_from_repo( file => $file );
282  
283 0 $self->_run_createrepo();
284 }
285  
286 0 ▶sub _run_createrepo {
Subroutine _run_createrepo: not called CC=9 SCAR=45.0
287 0 my $self = shift;
288  
289 0 my $repo_dir = $self->app->get_repo_dir(repo => $self->repo->{name});
290  
291 0 ▶ if ( exists $self->repo->{gpg} && $self->repo->{gpg}->{key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->repo->{"gpg"} and $self->repo->{"gpg"}{"key"}) 0 0 0
Condition0%0/3
exists $self->repo->{"gpg"} and $self->repo->{"gpg"}{"key"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->repo->{"gpg"} and $self->repo->{"gpg"}{"key"}
exists $self->repo->{"gpg"} $self->repo->{"gpg"}{"key"}
Truth table0%0/3
A and B
A= exists $self->repo->{"gpg"}B= $self->repo->{"gpg"}{"key"}
ABresult
0X0
100
111
292 0 unlink File::Spec->catfile($repo_dir, qw/ repodata repomd.xml.asc /);
293 }
294  
295 0 system "cd $repo_dir ; createrepo .";
296 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
297 0 confess "Error running createrepo.";
298 }
299  
300 0 ▶ if ( exists $self->repo->{gpg} && $self->repo->{gpg}->{key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->repo->{"gpg"} and $self->repo->{"gpg"}{"key"}) 0 0 0
Condition0%0/3
exists $self->repo->{"gpg"} and $self->repo->{"gpg"}{"key"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->repo->{"gpg"} and $self->repo->{"gpg"}{"key"}
exists $self->repo->{"gpg"} $self->repo->{"gpg"}{"key"}
Truth table0%0/3
A and B
A= exists $self->repo->{"gpg"}B= $self->repo->{"gpg"}{"key"}
ABresult
0X0
100
111
301 0 my $key = $self->repo->{gpg}->{key};
302 0 my $pass = $self->repo->{gpg}->{password};
303 0 ▶ if ( !$pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 0 0 0
304 0 $pass = $self->read_password("GPG key passphrase: ");
305 }
306  
307 0 my $cmd =
308 "cd $repo_dir ; gpg --default-key $key -a --batch --passphrase '"
309 . $pass
310 . "' --detach-sign repodata/repomd.xml";
311 0 system $cmd;
312  
313 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
314 0 $cmd =~ s/\Q$pass\E/\*\*\*\*\*\*\*/;
315 0 confess "Error running: $cmd";
316 }
317  
318 0 my $pub_file = $self->repo->{name} . ".asc";
319 0 unlink File::Spec->catfile($repo_dir, $pub_file);
320 0 $cmd = "cd $repo_dir ; gpg -a --output $pub_file --export $key";
321 0 system $cmd;
322  
323 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
324 0 confess "Error running gpg export: $cmd";
325 }
326 }
327 }
328  
329 # test if all necessary parameters are available
330 0 ▶override verify_options => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
331 0 my $self = shift;
332 0 super();
333  
334 0 ▶ if ( !exists $self->repo->{local} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->repo->{"local"}) 0 0 0
335 0 confess "No local path (local) given for: " . $self->repo->{name};
336 }
337 1 };
338  
339 1 1;
« Repositorio/Server/Helper/Proxy.pm ↑ Summary Repositorio/Server/Docker/Image.pm »