S3.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« S3/Role/Delete.pm ↑ Summary
1 package OrePAN2::S3;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Amazon::Credentials;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Amazon::S3::Lite;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Carp;
9 use CLI::Simple::Utils qw(choose);
10 use CLI::Simple::Constants qw(:booleans);
11 use Data::Dumper;
12 use English qw(-no_match_vars);
13 use Encode qw(encode_utf8);
14 use File::Basename qw(basename);
15 use File::Temp qw(tempfile);
16 use JSON;
17 use List::Util qw(pairs);
18 use Scalar::Util qw(openhandle);
19 use YAML;
20  
21 use Readonly;
22  
23 Readonly::Scalar our $PACKAGE_INDEX => '02packages.details.txt.gz';
24 Readonly::Scalar our $DEFAULT_CONFIG => $ENV{HOME} . '/.orepan2-s3.json';
25 Readonly::Scalar our $METACPAN_URL => 'https://metacpan.org/pod';
26 Readonly::Scalar our $AUTHOR_PATH => 'D/DU/DUMMY';
27  
28 use Role::Tiny::With;
29  
30 with 'OrePAN2::S3::Role::Inject';
31 with 'OrePAN2::S3::Role::Delete';
32 with 'OrePAN2::S3::Role::UploadArtifacts';
33  
34 use parent qw(CLI::Simple);
35  
36 our $VERSION = '1.2.0';
37  
38 __PACKAGE__->use_log4perl( level => 'info' );
39  
40 caller or __PACKAGE__->main();
41  
42 ########################################################################
43 sub slurp_file {
44 ########################################################################
45 my ( $file, $json ) = @_;
46  
47 local $RS = undef;
48  
49 my $content;
50  
51 if ( openhandle $file ) {
52 $content = <$file>;
53 }
54 else {
55 open my $fh, '<', $file
56 or die "could not open $file for reading: $OS_ERROR";
57  
58 $content = <$fh>;
59  
60 close $fh;
61 }
62  
63 return JSON->new->decode($content)
64 if $json;
65  
66 return wantarray ? split /\n/xsm, $content : $content;
67 }
68  
69 ########################################################################
70 sub write_config {
71 ########################################################################
72 my ( $self, $config ) = @_;
73  
74 my $file = $self->get_config_file;
75  
76 croak "ERROR: no config file set or config not found\n"
77 if !$file || !-e $file;
78  
79 croak "ERROR: $file is not writable\n"
80 if !-w $file;
81  
82 my $full_config = slurp_file( $file, $TRUE ); # slurp as JSON
83 my $profile_name //= $self->get_profile_name // 'default';
84  
85 $full_config->{$profile_name} = $config;
86  
87 open my $fh, '>', $file
88 or croak "ERROR: Could not open $file for writing: $OS_ERROR\n";
89  
90 print {$fh} JSON->new->pretty->encode($full_config);
91  
92 close $fh;
93  
94 return;
95 }
96  
97 ########################################################################
98 sub fetch_config {
99 ########################################################################
100 my ( $self, $profile_name ) = @_;
101  
102 $profile_name //= $self->get_profile_name // 'default';
103  
104 my $file = $self->get_config_file;
105  
106 die "no config file specified\n"
107 if !$file;
108  
109 die "$file not found\n"
110 if !-e $file;
111  
112 my $config = eval { return JSON->new->decode( scalar slurp_file($file) ); };
113  
114 die "could not read config file ($file)\n$EVAL_ERROR"
115 if !$config || $EVAL_ERROR;
116  
117 if ( $config->{$profile_name} && ref $config->{$profile_name} ) {
118 $config = $config->{$profile_name};
119 }
120 elsif ( $config->{$profile_name} ) {
121 $config = $config->{ $config->{$profile_name} };
122 }
123  
124 croak sprintf "ERROR: %s not a valid profile name\n", $profile_name
125 if !ref $config;
126  
127 $self->set_config($config);
128  
129 return $config;
130 }
131  
132 ########################################################################
133 sub init_s3 {
134 ########################################################################
135 my ($self) = @_;
136  
137 my $config = $self->get_config;
138 my $credentials = $self->get_credentials;
139  
140 my $region = $config->{AWS}{region} // $credentials->get_region // $ENV{AWS_REGION} // $ENV{AWS_DEFAULT_REGION}
141 // 'us-east-1';
142  
143 my $s3 = Amazon::S3::Lite->new(
144 { region => $region,
145 credentials => $credentials,
146 }
147 );
148  
149 $self->set_s3($s3);
150  
151 return;
152 }
153  
154 ########################################################################
155 sub fetch_orepan_index {
156 ########################################################################
157 my ($self) = @_;
158  
159 my ( $fh, $filename ) = tempfile(
160 'XXXXXX',
161 SUFFIX => '.gz',
162 UNLINK => $FALSE,
163 );
164  
165 my $config = $self->get_config;
166  
167 my $key = sprintf '%s/modules/%s', $config->{AWS}{prefix}, $PACKAGE_INDEX;
168 $self->get_s3->get_object( $self->get_bucket_name, $key, filename => $filename );
169  
170 return $filename;
171 }
172  
173 ########################################################################
174 sub cmd_invalidate_index {
175 ########################################################################
176 my ($self) = @_;
177  
178 warn "Not implemented yet\n";
179  
180 return $SUCCESS;
181 }
182  
183 ########################################################################
184 sub create_invalidation_batch {
185 ########################################################################
186 my (%args) = @_;
187  
188 local $LIST_SEPARATOR = q{};
189  
190 my @now = localtime;
191  
192 my $caller_reference = $args{CallerReference} || "@now";
193  
194 my $invalidation_batch = {
195 DistributionId => $args{DistributionId},
196 InvalidationBatch => {
197 CallerReference => $caller_reference,
198 Paths => {
199 Items => $args{Items},
200 Quantity => scalar @{ $args{Items} },
201 }
202 }
203 };
204  
205 return $invalidation_batch;
206 }
207  
208 ########################################################################
209 sub _upload_html {
210 ########################################################################
211 my ( $self, $file, $key ) = @_;
212  
213 croak sprintf '%s not found', $file
214 if !ref $file && !-e $file;
215  
216 my $content = ref $file ? ${$file} : slurp_file($file);
217 my $encoded = encode_utf8($content);
218 $self->get_s3->put_object( $self->get_bucket_name, $key, $encoded, content_type => 'text/html', );
219  
220 return;
221 }
222  
223 # should be a method of OrePAN2::Index (someday)
224 ########################################################################
225 sub _packages_for_archive {
226 ########################################################################
227 my ( $self, $index, $archive_path ) = @_;
228  
229 return grep {
230 my ( undef, $path ) = $index->lookup($_);
231 $path eq $archive_path;
232 } $index->packages;
233 }
234  
235 ########################################################################
236 sub update_index {
237 ########################################################################
238 my ( $self, $code ) = @_;
239  
240 require IO::Compress::Gzip;
241 require OrePAN2::Index;
242  
243 my $config = $self->get_config;
244 my $prefix = $config->{AWS}{prefix};
245  
246 my $index_file = $self->fetch_orepan_index;
247 my $index = OrePAN2::Index->new;
248 $index->load($index_file);
249 unlink $index_file;
250  
251 $code->($index);
252  
253 my $gz_content;
254 my $gz = IO::Compress::Gzip->new( \$gz_content ) or die "gzip failed\n";
255 $gz->print( $index->as_string );
256 $gz->close;
257  
258 my $index_key = sprintf '%s/modules/02packages.details.txt.gz', $prefix;
259 $self->get_s3->put_object( $self->get_bucket_name, $index_key, $gz_content, content_type => 'application/gzip', );
260  
261 $self->get_logger->info( sprintf 'updated index at %s', $index_key );
262  
263 return;
264 }
265  
266 ########################################################################
267 sub scan_provides {
268 ########################################################################
269 my ( $self, $file ) = @_;
270  
271 require Archive::Tar;
272 require CPAN::Meta;
273  
274 my $tar = Archive::Tar->new;
275 $tar->read($file);
276  
277 # find the top-level prefix, e.g. "CPAN-Maker-1.8.2"
278 my ($entry) = grep { $_->name =~ m{/META\.(?:json|yml|yaml)$}xsm } $tar->get_files;
279  
280 if ( !$entry ) {
281 $self->get_logger->warn("no META file found in $file");
282 return {};
283 }
284  
285 my ($prefix) = ( split m{/}xsm, $entry->name )[0];
286  
287 for my $metafile (qw(META.json META.yml META.yaml)) {
288 my $content = eval { $tar->get_content("$prefix/$metafile") };
289 next if !$content;
290  
291 my $meta = eval { CPAN::Meta->load_string($content) };
292 next if !$meta || $EVAL_ERROR;
293  
294 return $meta->{provides} if $meta->{provides};
295 }
296  
297 # Should not happen - injecting tarballs we create with CPAN::Maker
298 $self->get_logger->warn("META found but no provides in $file");
299  
300 return {};
301 }
302  
303 ########################################################################
304 # COMMANDS
305 ########################################################################
306  
307 ########################################################################
308 sub cmd_upload_index {
309 ########################################################################
310 my ( $self, $index ) = @_;
311  
312 if ( !$index ) {
313 ($index) = $self->get_args();
314 }
315  
316 $index //= 'index.html';
317  
318 return $self->_upload_html( $index, 'index.html' );
319 }
320  
321 ########################################################################
322 sub cmd_show_orepan_index {
323 ########################################################################
324 my ($self) = @_;
325  
326 my $file = $self->fetch_orepan_index;
327  
328 require OrePAN2::Index;
329  
330 my $index = OrePAN2::Index->new();
331  
332 $index->load($file);
333  
334 my $listing = $index->as_string;
335  
336 unlink $file;
337  
338 return $self->send_output($listing);
339 }
340  
341 ########################################################################
342 sub cmd_create_docs {
343 ########################################################################
344 my ($self) = @_;
345  
346 require DarkPAN::Utils;
347  
348 my ($distribution) = $self->get_args();
349 $distribution //= $self->get_distribution();
350  
351 die "use -d or pass distribution name as an argument\n"
352 if !$distribution;
353  
354 my ( $key_prefix, $version ) = DarkPAN::Utils::parse_distribution_path($distribution);
355  
356 my $dpu = choose {
357 if ( -e $distribution && $distribution !~ /^(?:http|D\/DU)/xsm ) {
358 require Archive::Tar;
359  
360 my $tar = Archive::Tar->new;
361 $tar->read($distribution);
362 return DarkPAN::Utils->new( package => $tar );
363 }
364  
365 $distribution = basename($distribution);
366  
367 if ( $distribution !~ /^http/xsm ) {
368 $distribution = sprintf 'D/DU/DUMMY/%s', $distribution;
369 }
370  
371 my $orepan_url = $self->get_url // $self->get_config->{url};
372  
373 die "use --url or set url in config\n"
374 if !$orepan_url;
375  
376 my $dpu = DarkPAN::Utils->new( base_url => $orepan_url );
377  
378 $dpu->fetch_package($distribution);
379  
380 return $dpu;
381 };
382  
383 my $module_name = $key_prefix;
384 $module_name =~ s/\-/::/gxsm;
385  
386 my $file = $dpu->extract_module( $distribution, $module_name );
387  
388 if ( $self->get_upload ) {
389 if ($file) {
390 $self->upload_html(
391 name => "$key_prefix.html",
392 content => $file,
393 prefix => $key_prefix,
394 wrap => $TRUE,
395 distribution => basename($distribution),
396 );
397 }
398 }
399 else {
400 open my $fh, '>', "$key_prefix.html"
401 or die "could not open $key_prefix.html for writing\n";
402  
403 print {$fh} $file;
404  
405 close $fh;
406 }
407  
408 my $readme = $dpu->extract_file( sprintf '%s-%s/README.md', $key_prefix, $version );
409  
410 if ( $self->get_upload && $readme ) {
411 $self->upload_html(
412 name => 'README.html',
413 markdown => $readme,
414 prefix => $key_prefix,
415 wrap => $TRUE,
416 );
417 }
418 elsif ($readme) {
419 open my $fh, '>', 'README.html'
420 or die "could not open README.html for writing\n";
421  
422 print {$fh} $readme;
423  
424 close $fh;
425 }
426  
427 return 0;
428 }
429  
430 ########################################################################
431 sub upload_html {
432 ########################################################################
433 my ( $self, %args ) = @_;
434  
435 my ( $content, $prefix, $name, $markdown, $distribution ) = @args{qw(content prefix name markdown distribution)};
436  
437 my $perldoc_url_distros = $self->get_config->{perldoc_url_distros} // [];
438 my $perldoc_url_prefix;
439  
440 if ($distribution) {
441 foreach my $p ( pairs @{$perldoc_url_distros} ) {
442  
443 my ( $pattern, $flags ) = @{$p};
444 my $qr = qr/(?$flags:$pattern)/;
445 next if $distribution !~ $qr;
446  
447 $perldoc_url_prefix = $self->get_config->{perldoc_url_prefix};
448 last;
449 }
450 }
451  
452 $perldoc_url_prefix //= $METACPAN_URL;
453  
454 my $html = choose {
455 if ($markdown) {
456 require Text::Markdown::Discount;
457  
458 Text::Markdown::Discount::markdown($markdown);
459 }
460  
461 require DarkPAN::Utils::Docs;
462  
463 my $docs = DarkPAN::Utils::Docs->new(
464 text => $content,
465 url_prefix => $perldoc_url_prefix,
466 );
467  
468 return $docs->get_html;
469 };
470  
471 if ( $args{wrap} ) {
472 $html = <<"END_OF_HTML";
473 <!DOCTYPE HTML>
474 <html>
475 <head>
476 <title>README</title>
477 <link rel="stylesheet" href="/css/pod.css">
478 </head>
479 <body>
480 $html
481 </body>
482 </html>
483 END_OF_HTML
484 }
485  
486 my $key = sprintf 'docs/%s/%s', $prefix, $name;
487  
488 $self->_upload_html( \$html, $key );
489  
490 return;
491 }
492  
493 ########################################################################
494 sub look_for_object {
495 ########################################################################
496 my ( $self, $prefix, $name ) = @_;
497  
498 my $key = sprintf 'docs/%s/%s', $prefix, $name;
499  
500 return sprintf '/docs/%s/%s', $prefix, $name
501 if $self->get_s3->head_object( $self->get_bucket_name, $key );
502  
503 return;
504 }
505  
506 ########################################################################
507 sub cmd_create_index {
508 ########################################################################
509 my ($self) = @_;
510  
511 require DarkPAN::Utils;
512  
513 my $config = $self->get_config;
514  
515 my $file = $self->fetch_orepan_index;
516  
517 my $repo = $self->parse_index($file);
518  
519 unlink $file;
520  
521 $self->get_logger->debug( Dumper( [ repo => $repo ] ) );
522  
523 no strict 'refs'; ## no critic
524  
525 *{'utils::module_name'} = sub {
526 my ( $self, $distribution ) = @_;
527  
528 my $module_name = basename("/$distribution");
529 $module_name =~ s/-\d+[.].*$//xsm;
530  
531 return $module_name;
532 };
533  
534 my $utils = bless {}, 'utils';
535  
536 my $sections = $config->{custom_sections} // {};
537  
538 foreach my $var ( keys %{$sections} ) {
539 my ( $pattern, $flags ) = @{ $sections->{$var} };
540  
541 my $qr = qr/(?$flags:$pattern)/;
542 $sections->{$var} = [ $pattern, $flags, $qr ];
543 }
544  
545 my %readme_links;
546 my %pod_links;
547  
548 foreach my $distribution ( keys %{$repo} ) {
549  
550 my ($distribution_name) = DarkPAN::Utils::parse_distribution_path($distribution);
551  
552 my $readme = $self->look_for_object( $distribution_name, 'README.html' );
553  
554 my $pod = $self->look_for_object( $distribution_name, "${distribution_name}.html" );
555  
556 my $old_name = $distribution;
557 $distribution =~ s/^.*\/([^\/]+)/$1/xsm;
558 $repo->{$distribution} = delete $repo->{$old_name};
559  
560 if ($readme) {
561 $readme_links{$distribution} = $readme;
562 }
563  
564 if ($pod) {
565 $pod_links{$distribution} = $pod;
566 }
567  
568 if ($sections) {
569 foreach my $var ( keys %{$sections} ) {
570 if ( $distribution =~ $sections->{$var}->[2] ) {
571 $sections->{$var}->[3] //= {};
572 $sections->{$var}->[3]->{$distribution} = delete $repo->{$distribution};
573 }
574 }
575 }
576 }
577  
578 my $params = {
579 utils => $utils,
580 repo => $repo,
581 readme_links => \%readme_links,
582 pod_links => \%pod_links,
583 localtime => scalar localtime,
584 map { ( $_ => $sections->{$_}->[3] ) } keys %{$sections},
585  
586 };
587  
588 $self->get_logger->trace( Dumper( [ params => $params ] ) );
589  
590 my $text = $self->get_template;
591  
592 require Template;
593  
594 my $template = Template->new();
595  
596 my $output = q{};
597  
598 $template->process( \$text, $params, \$output )
599 or die $template->error();
600  
601 if ( $self->get_upload ) {
602 $self->_upload_html( \$output, 'index.html' );
603 $self->get_logger->debug($output);
604 }
605 else {
606 $self->send_output($output);
607 }
608  
609 return 0;
610 }
611  
612 ########################################################################
613 sub send_output {
614 ########################################################################
615 my ( $self, $content ) = @_;
616  
617 my $outfile = $self->get_output;
618  
619 my $fh = eval {
620  
621 return *STDOUT
622 if !$outfile;
623  
624 open my $fh, '>', $outfile;
625  
626 return $fh;
627 };
628  
629 die "could not open file for output\n$EVAL_ERROR"
630 if !$fh;
631  
632 print {$fh} $content;
633  
634 $outfile && close $fh;
635  
636 return 0;
637 }
638  
639 ########################################################################
640 sub parse_index {
641 ########################################################################
642 my ( $self, $file ) = @_;
643  
644 require OrePAN2::Index;
645  
646 my $index = OrePAN2::Index->new();
647  
648 $index->load($file);
649  
650 my $listing = $index->as_string;
651 $listing =~ s/^(.*)\n\n//xsm;
652  
653 my %repo;
654  
655 foreach ( split /\n/xsm, $listing ) {
656 my ( $module, $version, $package ) = split /\s+/xsm;
657 $repo{$package} //= [];
658 push @{ $repo{$package} }, [ $module, $version ];
659 }
660  
661 return \%repo;
662 }
663  
664 ########################################################################
665 sub cmd_download_orepan_index {
666 ########################################################################
667 my ($self) = @_;
668  
669 my $filename = eval { return $self->fetch_orepan_index(); };
670  
671 die "ERROR: Could not download $PACKAGE_INDEX\n$EVAL_ERROR"
672 if !$filename || !-s "$filename";
673  
674 rename $filename, $PACKAGE_INDEX;
675  
676 print {*STDOUT} $PACKAGE_INDEX . "\n";
677  
678 return;
679 }
680  
681 ########################################################################
682 sub init {
683 ########################################################################
684 my ($self) = @_;
685  
686 my $config = $self->fetch_config;
687  
688 my $profile = $config->{AWS}->{profile};
689  
690 my $credentials = Amazon::Credentials->new( profile => $profile );
691 $self->set_credentials($credentials);
692  
693 my $bucket_name = $self->get_bucket_name // $config->{AWS}->{bucket};
694  
695 $self->set_bucket_name($bucket_name);
696  
697 $self->init_s3;
698  
699 my $author_path = $config->{author_path} // $AUTHOR_PATH;
700 $self->set_author_path($author_path);
701  
702 $self->fetch_template;
703  
704 return;
705 }
706  
707 ########################################################################
708 sub fetch_template {
709 ########################################################################
710 my ($self) = @_;
711  
712 my $template = $self->get_template;
713  
714 my $index = $self->get_config->{index} // {};
715  
716 # see if the index is set in the config file...
717 if ( !$template && $index->{template} ) {
718 $template = $index->{template};
719 }
720  
721 my $index_template = $template eq 'default' ? slurp_file(*DATA) : slurp_file($template);
722  
723 $index_template =~ s/\n\n=pod.*$/\n/xsm;
724  
725 $self->set_template($index_template);
726  
727 return;
728 }
729  
730 ########################################################################
731 sub extract_from_tarball {
732 ########################################################################
733 my ( $tarball, $file ) = @_;
734  
735 require Archive::Tar;
736  
737 my $t = Archive::Tar->new;
738  
739 $t->read( $tarball, 1 )
740 or croak "failed to read tarball: $tarball";
741  
742 my $prefix = basename($tarball);
743 $prefix =~ s/[.]tar.*$//xsm;
744  
745 croak "file not found ($prefix/$file)"
746 if !$t->contains_file("$prefix/$file");
747  
748 return $t->get_content("$prefix/$file");
749 }
750  
751 ########################################################################
752 sub main {
753 ########################################################################
754 my $cli = OrePAN2::S3->new(
755 option_specs => [
756 qw(
757 help|h
758 bucket-name|b=s
759 config-file|c=s
760 output|o=s
761 profile|p=s
762 profile-name|n=s
763 template|t=s
764 url|U=s
765 distribution|d=s
766 upload|u
767 )
768 ],
769 default_options => {
770 config_file => $DEFAULT_CONFIG,
771 profile_name => 'default',
772 profile => $ENV{AWS_PROFILE}
773 },
774 extra_options => [qw(config credentials template author_path s3)],
775 commands => {
776 'create-docs' => \&cmd_create_docs,
777 create => \&cmd_create_index,
778 delete => \&cmd_delete,
779 download => \&cmd_download_orepan_index,
780 'dump-template' => sub {
781 print {*STDOUT} shift->get_template;
782 return 0;
783 },
784 inject => \&cmd_inject,
785 'invalidate-index' => \&cmd_invalidate_index,
786 show => \&cmd_show_orepan_index,
787 upload => \&cmd_upload_index,
788 'upload-artifacts' => \&cmd_upload_artifacts,
789 },
790 );
791  
792 return $cli->run();
793 }
794  
795 1;
796  
797 __DATA__
798 <!DOCTYPE HTML>
799 <html>
800 <head>
801 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
802 <title>CPAN Repository</title>
803 <script src="https://code.jquery.com/jquery-3.7.1.min.js"
804 integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo="
805 crossorigin="anonymous"></script>
806  
807 <script>
808 $(function() {
809 $('.collapsable').hide();
810  
811 $('h2').on('click', function() {
812 $(this).next().toggle();
813 });
814 });
815  
816 </script>
817 <style>
818 h2 {
819 cursor: pointer;
820 color: purple;
821 }
822 h2:hover {
823 color: green;
824 }
825 body {
826 font-family: monospace;
827 }
828 </style>
829 </head>
830
831 <body>
832 <h1>CPAN Repository</h1>
833
834 [% FOREACH distribution = repo.keys %]
835 <h2>[% distribution %]</h2>
836  
837 <ul class="collapsable" id="[% utils.module_name(distribution) %]">
838 [% FOREACH module IN repo.$distribution %]
839 <li>[% module.0 %]</li>
840 [% END %]
841 </ul>
842 [% END %]
843
844 <hr>
845 <address>Generated on [% localtime %] by Template::Toolkit</address>
846 </body>
847 </html>
848  
849 =pod
850  
851 =head1 NAME
852  
853 OrePAN2::S3 - Manage a DarkPAN CPAN mirror on Amazon S3
854  
855 =head1 SYNOPSIS
856  
857 # via the bash wrapper (recommended)
858 orepan2-s3 add My-Dist-1.0.tar.gz
859 orepan2-s3 index
860  
861 # or directly via the modulino
862 orepan2-s3-index create --upload
863  
864 # add and index a new distribution
865 orepan2-s3-index inject My-App-1.0.0.tar.gz
866  
867 # upload artifacts listed in config file
868 orepan2-s3-index upload-artifacts
869  
870 =head1 DESCRIPTION
871  
872 This class is used to add distributions to your own DarkPAN
873 repository housed on Amazon's S3 storage. It leverages L<OrePAN2> to
874 create and maintain your own DarkPAN repository. You can read more
875 about setting up a DarkPAN on Amazon using S3 + Cloudfront
876 L<here|https://github.com/rlauer6/OrePAN2-S3/blob/master/README.md>.
877  
878 You can read more about creating a secure static website using Amazon
879 S3 L<here|https://blog.tbcdevelopmentgroup.com/2025-02-18-post.html>.
880  
881 =head1 USAGE
882  
883 orepan2-s3 options command
884  
885 Perl script for maintaining a DarkPAN mirror using S3 + CloudFront.
886  
887 I<NOTE: C<orepan2-s3> is the bash script that calls this Perl class
888 that doubles as a modulino. The documentation here refers to the
889 script (not the class)>.
890  
891 =head2 Options
892  
893 -h, --help Display this help message
894 -b, --bucket-name Overrides bucket in configuration
895 -c, --config-file Name of the configuration file (default: ~/.orepan2-s3.json)
896 -o, --output Name of the output file
897 -p, --profile Your AWS profile if not provided in configuration
898 -n, --profile-name Name of a profile inside the config file
899 -t, --template Name of a template that will be used as the index.html page
900 -d, --distribution Path to distribution tarball
901 -u, --upload Upload files after processing (for create-index, create-docs)
902 -U, --url Cloudfront URL
903  
904 =head2 Commands
905  
906 =over 5
907  
908 =item * create - Create a new F<index.html> from the mirror's manifest file.
909  
910 =item * delete - Delete a distribution from repo and reindex
911  
912 =item * download - Download the mirror's manifest file (F<02packages.details.txt.gz>).
913  
914 =item * inject - Uploads a tarball and adds to the DarkPAN index
915  
916 =item * show - Print the manifest file to STDOUT or a file.
917  
918 =item * dump-template - Outputs the default index.html template.
919  
920 =item * create-docs - parse distribution looking for a README.md and/or pod
921  
922 =item * invalidate-index - I<not currently implemented>
923  
924 =item * upload - Upload the index.html file to the mirror's root.
925  
926 =item * upload-artifacts - Uploads the files listed in the C<index: files:> section of the config file.
927  
928 =back
929  
930 =head2 Notes
931  
932 =over 5
933  
934 =item The preferred way of using this utility is through the bash wrapper.
935  
936 I<The following commands are available only through the C<orepan2-s3>
937 bash wrapper, not the modulino directly:>
938  
939 =over 5
940  
941 =item * add {file} - inject a tarball into the repository and re-index
942  
943 =item * delete {file} - remove a distribution and re-index
944  
945 =item * invalidate - invalidate the CloudFront cache
946  
947 =back
948  
949 =back
950  
951 =head2 Configuration File
952  
953 The configuration file for C<orepan2-s3> is a JSON file that can
954 contain multiple profiles (or none). Each profile represents a DarkPAN
955 S3 repository. The format should look something like this:
956  
957 {
958 "default" : "bedrock",
959 "tbc" : {
960 "author_path": "D/DU/DUMMY",
961 "AWS": {
962 "profile" : "prod",
963 "region" : "us-east-1",
964 "bucket" : "tbc-cpan-mirror",
965 "prefix" : "orepan2"
966 },
967 "CloudFront" : {
968 "DistributionId" : "E2ABCDEFGHIJK"
969 }
970 },
971 "bedrock" : {
972 "author_path": "D/DU/DUMMY",
973 "index" : {
974 "template" : "/path/to/template",
975 "files": {
976 "src" : "dest"
977 }
978 },
979 "AWS": {
980 "profile" : "prod",
981 "region" : "us-east-1",
982 "bucket" : "cpan.openbedrock.net",
983 "prefix" : "orepan2"
984 },
985 "CloudFront" : {
986 "DistributionId" : "E2JKLMNOPQRXYZ",
987 "InvalidationPaths" : [],
988 "url" : "https://cpan.openbedrock.net/orepan2"
989 }
990 }
991 }
992  
993 Each profile can contain the keys described below. If you only have one
994 profile you don't need to place it in a 'default' section.
995  
996 The value for the 'default' key can be the name of a profile or a hash
997 of the profile.
998  
999 =over 5
1000  
1001 =item author_path
1002  
1003 Overrides default C<D/DU/DUMMY>. For a personal DarkPAN you should
1004 place all modules in one path.
1005  
1006 =item index
1007  
1008 This section allows you to create custom template for the DarkPAN home page.
1009  
1010 =over 10
1011  
1012 =item template
1013  
1014 The name of a template file that will be parsed and uploaded as
1015 F</index.html>. If you do not provide a template file a default
1016 template is used. The default template is a L<Template::Toolkit> style
1017 template. To see the default template use the C<dump-template> command
1018 to the F<orepan2-s3-index> script.
1019  
1020 orepan2-s3-index dump-template
1021  
1022 The templating process is provided with these variables:
1023  
1024 =over 15
1025  
1026 =item utils
1027  
1028 A blessed reference to an object with one method (C<module_name>) that
1029 returns a version of the module name suitable for use as unique CSS id.
1030  
1031 =item repo
1032  
1033 A hash of key value pairs where the key is the name of a DarkPAN
1034 distribution and value is an array or arrays. Each array is of the
1035 form:
1036  
1037 [0] => Perl module name
1038 [1] => Module version
1039  
1040 =item localtime
1041  
1042 The current time and date as a string.
1043  
1044 =item pod_links
1045  
1046 A hash where the keys are distribution names and the values are links
1047 to the POD for a module.
1048  
1049 =item readme_links
1050  
1051 A hash where the keys are distribution names and the values are links
1052 to a README for a module.
1053  
1054 I<NOTE: Sometimes the README and the POD will contain the same information.>
1055  
1056 =back
1057  
1058 =item files
1059  
1060 A hash of source/destination pairs that specify additional files you
1061 want uploaded to your S3 bucket.
1062  
1063 Example:
1064  
1065 "files": {
1066 "/home/rlauer/git/some-project/foo.css" : "/css/foo.css",
1067 "/home/rlauer/git/some-project/foo.js" : "/javascript/foo.js"
1068 }
1069  
1070 =back
1071  
1072 =item AWS
1073  
1074 =over 10
1075  
1076 =item profile
1077  
1078 The IAM profile that allows access to the S3 bucket and CloudFront.
1079  
1080 =item region
1081  
1082 AWS region. Default: us-east-1
1083  
1084 =item bucket
1085  
1086 S3 bucket name
1087  
1088 =item prefix
1089  
1090 The prefix where the CPAN distribution files will be stored. Default: orepan2.
1091  
1092 =back
1093  
1094 =item CloudFront
1095  
1096 I<NOTE: Your profile must have the ability to invalidate the CloudFront cache!>
1097  
1098 =over 10
1099  
1100 =item DistributionId
1101  
1102 CloudFront distribution id
1103  
1104 =item InvalidationPaths
1105  
1106 C<OrePAN2::S3> is designed to work with CloudFront + Amazon
1107 S3. CloudFront is a CDN and will read content from your S3 bucket when
1108 clients make HTTP requests. CloudFront will cache content to avoid
1109 costly reads to the S3 bucket. If you change some of the static assets
1110 (like the index page) you may want to invalidate the cache to see
1111 your new assets. You could wait until the cache is updated (the
1112 default time is 24 hours)...but why? The script will automatically
1113 invalidate the cache for you if you tell it what assets to invalidate
1114 when you add a new distribution.
1115  
1116 An array of additional paths to invalidate when adding new distributions.
1117  
1118 I<Note: There is no additional charge for adding additional
1119 paths. Each invalidation batch is considered as one billing unit by
1120 AWS. However, keep in mind you get 1000 invalidation paths for free
1121 each month. Thereafter each path costs $0.005 per path.>
1122  
1123 =back
1124  
1125 =item custom_sections
1126  
1127 This section contains key/value pairs where the key is the name of a
1128 variable that will be exposed to your template and the values
1129 are a two element array that contains a regular expression and
1130 possible regexp flags. The script will use the regexp to filter your
1131 distributions and add them to a hash whose name is the key you
1132 provided.
1133  
1134 The purpose of this section is to allow you to possibly organize your
1135 distributions under possible HTML headings.
1136  
1137 Example:
1138  
1139 "custom_sections" : {
1140 "plugins" : ["^BLM\-(?!Startup)", "xsm"],
1141 "app_plugins" : ["^BLM\-Startup", "xsm"],
1142 }
1143  
1144 ...then in your template:
1145  
1146 <h1>Application Plugin Index</h1>
1147
1148 [% FOREACH distribution = app_plugins.sort %]
1149 <h2>
1150 <span class="collapse-section-icon">&#9660;</span>
1151 [% distribution %]
1152 [% IF readme_links.$distribution %]
1153 <a title="README" class='doc-link' href="[% readme_links.$distribution %]"><span class="material-symbols-outlined">docs</span></a>
1154 [% END %]
1155 [% IF pod_links.$distribution %]
1156 <a title="pod" class='doc-link' href="[% pod_links.$distribution %]"><span class="material-symbols-outlined">docs</span></a>
1157 [% END %]
1158 </h2>
1159
1160 <ul class="collapsable" id="[% utils.module_name(distribution) %]">
1161 [% FOREACH module IN app_plugins.$distribution %]
1162 <li>[% module.0 %]</li>
1163 [% END %]
1164 </ul>
1165 [% END %]
1166 <hr>
1167  
1168 =back
1169  
1170 =head1 AUTHOR
1171  
1172 Rob Lauer - <rlauer@treasurersbriefcase.com>
1173  
1174 =head1 SEE ALSO
1175  
1176 L<OrePAN2>, L<Amazon::S3::Lite>, L<DarkPAN::Utils>, L<CLI::Simple>, L<Template>
1177  
1178 =head1 LICENSE
1179  
1180 This program is free software; you can redistribute it and/or modify
1181 it under the same terms as Perl itself.
1182  
1183 =cut
« S3/Role/Delete.pm ↑ Summary