Nrepo.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 226 CC 15 SCAR 29.7
CC
15
Cov
0%
_validate_config
72.0
_get_plugin
12.0
_get_repo_dir
12.0
CRAP 19.4
1 #!/bin/false
2 package App::Nrepo;
3  
4 use Moo;
5 use strictures 2;
6 use namespace::clean;
7 use Carp;
8 use Cwd qw(getcwd);
9 use Data::Dumper;
10 use Params::Validate qw(:all);
11 use Module::Pluggable::Object;
12 use File::Spec;
13 use App::Nrepo::Logger;
14  
15 our $VERSION = '0.1'; # VERSION
16  
17 has config => ( is => 'ro' );
18 has logger => ( is => 'ro', default => sub { App::Nrepo::Logger->new() } );
19  
20 =head1 NAME
21  
22 App::Nrepo - An application to handle management of various software repositories
23 This module is purely to designed to be used with the accompanying script bin/nrepo
24 Please look at its pod for instantiating of this Module
25  
26 =head2 Methods
27  
28 =over 4
29  
30 =cut
31  
32 =item C<go>
33  
34 Entry point for this object
35 my $o = App::Nrepo->new(config => $hashref, logger => Log::Dispatch->new());
36 $o->go($action, %options);
37 Valid actions:
38  
39 =over 4
40  
41 =item C<add-file>
42  
43 =item C<del-file>
44  
45 =item C<clean>
46  
47 =item C<init>
48  
49 =item C<list>
50  
51 =item C<mirror>
52  
53 =item C<tag>
54  
55 =back
56  
57 For each actions required options see its appropriate method below
58  
59 =cut
60  
61 sub go {
62 my ( $self, $action, @args ) = @_;
63 $self->_validate_config();
64  
65 my $dispatch = {
66 'add-file' => \&add_file,
67 'del-file' => \&del_file,
68 'clean' => \&clean,
69 'init' => \&init,
70 'list' => \&list,
71 'mirror' => \&mirror,
72 'tag' => \&tag,
73 };
74  
75 exists $dispatch->{$action}
76 || $self->logger->log_and_croak(
77 level => 'error',
78 message => "ERROR: ${action} not supported."
79 );
80  
81 $dispatch->{$action}->( $self, @args );
82  
83 exit(0);
84 }
85  
86 sub _validate_config {
87 my $self = shift;
88  
89 # If data_dir is relative, lets expand it based on cwd
90 $self->config->{'data_dir'} =
91 File::Spec->rel2abs( $self->config->{data_dir} );
92  
93 $self->logger->log_and_croak(
94 level => 'error',
95 message => sprintf "datadir does not exist: %s",
96 $self->config->{data_dir},
97 ) unless -d $self->config->{data_dir};
98  
99 $self->logger->log_and_croak(
100 level => 'error',
101 message => sprintf "Unknown tag_style %s, must be topdir or bottomdir\n",
102 $self->config->{tag_style},
103 ) unless $self->config->{tag_style} =~ m/^(?:top|bottom)dir$/;
104  
105 # required params for reposrc-tag
106 for my $repo ( sort keys %{ $self->config->{'repo'} } ) {
107 for my $param (qw/type local arch/) {
108 $self->logger->log_and_croak(
109 level => 'error',
110 message => sprintf "repo: %s missing param: %s",
111 $repo, $param,
112 ) unless $self->config->{repo}->{$repo}->{$param};
113  
114 # Data validation for specific type
115 if ( $param eq 'arch' ) {
116  
117 # We allow identical options which we use for arch, lets end up with an array regardless
118 my $arch = $self->config->{'repo'}->{$repo}->{'arch'};
119 my $arches = ref($arch) eq 'ARRAY' ? $arch : [$arch];
120 $self->config->{'repo'}->{$repo}->{'arch'} = $arches;
121 }
122 elsif ( $param eq 'type' ) {
123 unless (
124 $self->config->{repo}->{$repo}->{$param} eq 'Yum'
125 || $self->config->{repo}->{$repo}->{$param} eq 'Apt'
126 || $self->config->{repo}->{$repo}->{$param} eq 'Plain',
127 )
128 {
129 $self->logger->log_and_croak(
130 level => 'error',
131 message => sprintf "repo: %s param: %s value: %s is not supported",
132 $repo, $param, $self->config->{repo}->{$repo}->{$param},
133 );
134 }
135 }
136 }
137 }
138 }
139  
140 sub _get_plugin {
141 my $self = shift;
142 my %o = validate(
143 @_,
144 {
145 type => { type => SCALAR, },
146 options => { options => HASHREF, },
147 }
148 );
149  
150 my $plugin;
151 for my $p (
152 Module::Pluggable::Object->new(
153 instantiate => 'new',
154 search_path => ['App::Nrepo::Plugin'],
155 except => ['App::Nrepo::Plugin::Base'],
156 )->plugins( %{ $o{'options'} } )
157 )
158 {
159 $plugin = $p if $p->type() eq $o{'type'};
160 }
161 $self->logger->log_and_croak(
162 level => 'error',
163 message => "Failed to find a plugin for type: $o{'type'}"
164 ) unless $plugin;
165 return $plugin;
166 }
167  
168 sub _get_repo_dir {
169 my $self = shift;
170 my %o = validate(
171 @_,
172 {
173 repo => { type => SCALAR },
174 tag => { type => SCALAR, default => 'head', },
175 }
176 );
177  
178 my $data_dir = $self->config->{data_dir};
179 my $tag_style = $self->config->{tag_style};
180 my $repo = $o{'repo'};
181 my $tag = $o{'tag'};
182 my $local = $self->config->{'repo'}->{$repo}->{'local'};
183  
184 if ( $tag_style eq 'topdir' ) {
185 return File::Spec->catdir( $data_dir, $tag, $local );
186 }
187 elsif ( $tag_style eq 'bottomdir' ) {
188 return File::Spec->catdir( $data_dir, $local, $tag );
189 }
190 else {
191 $self->logger->log_and_croak(
192 level => 'error',
193 message => '_get_repo_dir: Unknown tag_style: ' . $tag_style
194 );
195 }
196 }
197  
198 =item C<add_file>
199  
200 Action: add-file
201  
202 Description: Adds a file to a local repository and updates the related metadata
203  
204 Options:
205  
206 =over 4
207  
208 =item C<repo>
209  
210 The name of the repository as reflected in the config
211  
212 =item C<arch>
213  
214 The arch this package should be added to as reflected in the config
215  
216 =item C<file>
217  
218 The path of the file to be added to the repository
219  
220 =item C<force>
221  
222 Boolean to enable force overwriting an existing file in the repository
223  
224 =back
225  
226 =cut
227  
228 sub add_file {
229 my $self = shift;
230 my %o = validate(
231 @_,
232 {
233 'repo' => { type => SCALAR },
234 'arch' => { type => SCALAR },
235 'file' => { type => SCALAR | ARRAYREF },
236 'force' => { type => BOOLEAN, default => 0 },
237 },
238 );
239 my $options = {
240 repo => $o{'repo'},
241 arches => $self->config->{'repo'}->{ $o{'repo'} }->{'arch'},
242 backend => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
243 dir => $self->_get_repo_dir( repo => $o{'repo'} ),
244 force => $o{'force'},
245 };
246 my $plugin = $self->_get_plugin(
247 type => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
248 options => $options,
249 );
250  
251 $plugin->add_file( $o{'arch'}, $o{'file'} );
252 }
253  
254 =item C<del_file>
255  
256 Action: del-file
257  
258 Description: Removes a file to a local repository and updates the related metadata
259  
260 Options:
261  
262 =over 4
263  
264 =item C<repo>
265  
266 The name of the repository as reflected in the config
267  
268 =item C<arch>
269  
270 The arch this package should be removed from as reflected in the config
271  
272 =item C<file>
273  
274 The filename to be removed to the repository
275  
276 =back
277  
278 =cut
279  
280 sub del_file {
281 my $self = shift;
282 my %o = validate(
283 @_,
284 {
285 'repo' => { type => SCALAR },
286 'arch' => { type => SCALAR },
287 'file' => { type => SCALAR | ARRAYREF },
288 },
289 );
290 my $options = {
291 repo => $o{'repo'},
292 arches => $self->config->{'repo'}->{ $o{'repo'} }->{'arch'},
293 backend => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
294 dir => $self->_get_repo_dir( repo => $o{'repo'} ),
295 force => $o{'force'},
296 };
297 my $plugin = $self->_get_plugin(
298 type => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
299 options => $options,
300 );
301  
302 $plugin->del_file( $o{'arch'}, $o{'file'} );
303 }
304  
305 =item C<clean>
306  
307 Action: clean
308  
309 Description: Removes files from a repository that are not referenced in the metadata
310  
311 Options:
312  
313 =over 4
314  
315 =item C<repo>
316  
317 The name of the repository as reflected in the config
318 If 'all' is supplied it will perform this action on all repositories in config
319  
320 =back
321  
322 =cut
323  
324 sub clean {
325 my $self = shift;
326 my %o = validate(
327 @_,
328 {
329 repo => { type => SCALAR, },
330 arch => { type => SCALAR, optional => 1 },
331 force => { type => BOOLEAN, optional => 1, },
332 }
333 );
334  
335 if ( $o{'repo'} eq 'all' ) {
336 my %options = %o;
337 for my $repo ( keys %{ $self->config->{'repo'} } ) {
338 $options{'repo'} = $repo;
339 $self->_clean(%options);
340 }
341 }
342 else {
343 $self->_clean(%o);
344 }
345 }
346  
347 sub _clean {
348 my ( $self, %o ) = @_;
349  
350 my $options = {
351 repo => $o{'repo'},
352 arches => $self->config->{'repo'}->{ $o{'repo'} }->{'arch'},
353 backend => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
354 dir => $self->_get_repo_dir( repo => $o{'repo'} ),
355 force => $o{'force'},
356 };
357 my $plugin = $self->_get_plugin(
358 type => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
359 options => $options,
360 );
361  
362 $plugin->clean();
363 }
364  
365 =item C<init>
366  
367 Action: init
368  
369 Description: Initialises a custom repository by generating the appropriate metadata files
370  
371 Options:
372  
373 =over 4
374  
375 =item C<repo>
376  
377 The name of the repository as reflected in the config
378  
379 =back
380  
381 =cut
382  
383 sub init {
384 my $self = shift;
385 my %o = validate(
386 @_,
387 {
388 repo => { type => SCALAR, },
389 arch => { type => SCALAR, optional => 1 },
390 }
391 );
392  
393 my $options = {
394 repo => $o{'repo'},
395 arches => $self->config->{'repo'}->{ $o{'repo'} }->{'arch'},
396 backend => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
397 dir => $self->_get_repo_dir( repo => $o{'repo'} ),
398 };
399  
400 my $plugin = $self->_get_plugin(
401 type => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
402 options => $options,
403 );
404  
405 $plugin->init( $o{'arch'} );
406 }
407  
408 =item C<list>
409  
410 Action: list
411  
412 Description: Lists the repositories as reflected in the config
413  
414 =cut
415  
416 sub list {
417 my $self = shift;
418 print "Repository list:\n";
419 print sprintf "|%8s|%8s|%50s|\n", 'Type', 'Mirrored', 'Name';
420 for my $repo ( sort keys %{ $self->config->{repo} } ) {
421 my $type = $self->config->{repo}->{$repo}->{type};
422 my $mirrored = $self->config->{repo}->{$repo}->{url} ? 'Yes' : 'No';
423 print sprintf "|%8s|%8s|%50s|\n", $type, $mirrored, $repo;
424 }
425 }
426  
427 =item C<mirror>
428  
429 Action: mirror
430  
431 Description: Mirrors repository from upstream provider into the head tag
432  
433 Options:
434  
435 =over 4
436  
437 =item C<repo>
438  
439 The name of the repository as reflected in the config
440 If 'all' is supplied it will perform this action on all repositories in config
441  
442 =item C<checksums>
443  
444 By default we just use the manifests information about size of packages to determine if the local file
445 is valid. If you want to have checksums used enable this boolean flag.
446 With this enabled updating a mirror can take quite a long time
447  
448 =back
449  
450 =cut
451  
452 sub mirror {
453 my $self = shift;
454 my %o = validate(
455 @_,
456 {
457 'repo' => { type => SCALAR },
458 'force' => { type => BOOLEAN, default => 0 },
459 'arch' => { type => SCALAR, optional => 1 },
460 'checksums' => { type => SCALAR, optional => 1 },
461 }
462 );
463  
464 if ( $o{'repo'} eq 'all' ) {
465 my %options = %o;
466 for my $repo ( keys %{ $self->config->{'repo'} } ) {
467 $options{'repo'} = $repo;
468 $self->_mirror(%options);
469 }
470 }
471 else {
472 $self->_mirror(%o);
473 }
474 }
475  
476 sub _mirror {
477 my ( $self, %o ) = @_;
478  
479 my $options = {
480 repo => $o{'repo'},
481 arches => $self->config->{'repo'}->{ $o{'repo'} }->{'arch'},
482 url => $self->config->{'repo'}->{ $o{'repo'} }->{'url'},
483 checksums => $o{'checksums'},
484 backend => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
485 dir => $self->_get_repo_dir( repo => $o{'repo'} ),
486 ssl_ca => $self->config->{'repo'}->{ $o{'repo'} }->{'ca'} || undef,
487 ssl_cert => $self->config->{'repo'}->{ $o{'repo'} }->{'cert'} || undef,
488 ssl_key => $self->config->{'repo'}->{ $o{'repo'} }->{'key'} || undef,
489 force => $o{'force'},
490 };
491 my $plugin = $self->_get_plugin(
492 type => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
493 options => $options
494 );
495 $plugin->mirror();
496 }
497  
498 =item C<tag>
499  
500 Action: tag
501  
502 Description: Tags a repository at a particular state
503  
504 Options:
505  
506 =over 4
507  
508 =item C<repo>
509  
510 The name of the repository as reflected in the config
511  
512 =item C<src-tag>
513  
514 The source tag to use for this operation, by default this is 'head'
515 The source tag must pre exist.
516  
517 =item C<dest-tag>
518  
519 The destination tag to use for this operation.
520  
521 =item C<symlink>
522  
523 This will make the link operation use a symlink instead of hardlinking
524 For example you may tag every time you update from upstream but you move a production tag around...provides easy roll back
525 for your clients package configuration
526  
527 =item C<force>
528  
529 Force will overwrite a pre existing dest-tag location
530  
531 =back
532  
533 =cut
534  
535 sub tag {
536 my $self = shift;
537 my %o = validate(
538 @_,
539 {
540 'repo' => { type => SCALAR },
541 'tag' => { type => SCALAR },
542 'src-tag' => { type => SCALAR, default => 'head' },
543 'symlink' => { type => BOOLEAN, default => 0 },
544 'force' => { type => BOOLEAN, default => 0 },
545 },
546 );
547  
548 my $options = {
549 repo => $o{'repo'},
550 arches => $self->config->{'repo'}->{ $o{'repo'} }->{'arch'},
551 backend => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
552 dir => $self->_get_repo_dir( repo => $o{'repo'} ),
553 force => $o{'force'},
554 };
555  
556 my $plugin = $self->_get_plugin(
557 type => $self->config->{'repo'}->{ $o{'repo'} }->{'type'},
558 options => $options,
559 );
560  
561 $plugin->tag(
562 src_dir => $self->_get_repo_dir( repo => $o{'repo'}, tag => $o{'src-tag'} ),
563 src_tag => $o{'src-tag'},
564 dest_dir => $self->_get_repo_dir( repo => $o{'repo'}, tag => $o{'tag'} ),
565 dest_tag => $o{'tag'},
566 symlink => $o{'symlink'},
567 hard_tag_regex => $self->config->{'repo'}->{'hard_tag_regex'}
568 || $self->config->{'hard_tag_regex'},
569 );
570 }
571  
572 1;
573  
574 =back
575  
576 =cut
577  
578 __END__