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::ShareDir qw(dist_dir);
16 use File::Temp qw(tempfile);
17 use JSON;
18 use List::Util qw(pairs);
19 use Scalar::Util qw(openhandle);
20  
21 use Readonly;
22  
23 Readonly::Scalar our $PACKAGE_INDEX => '02packages.details.txt.gz';
24 Readonly::Scalar our $DEFAULT_CONFIG => sprintf '%s/%s', $ENV{HOME} // q{}, '/.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.3';
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 # COMMANDS
268 ########################################################################
269  
270 ########################################################################
271 sub cmd_upload_index {
272 ########################################################################
273 my ( $self, $index ) = @_;
274  
275 if ( !$index ) {
276 ($index) = $self->get_args();
277 }
278  
279 $index //= 'index.html';
280  
281 return $self->_upload_html( $index, 'index.html' );
282 }
283  
284 ########################################################################
285 sub cmd_show_orepan_index {
286 ########################################################################
287 my ($self) = @_;
288  
289 my $file = $self->fetch_orepan_index;
290  
291 require OrePAN2::Index;
292  
293 my $index = OrePAN2::Index->new();
294  
295 $index->load($file);
296  
297 my $listing = $index->as_string;
298  
299 unlink $file;
300  
301 return $self->send_output($listing);
302 }
303  
304 ########################################################################
305 sub cmd_create_docs {
306 ########################################################################
307 my ($self) = @_;
308  
309 require DarkPAN::Utils;
310  
311 my ($distribution) = $self->get_args();
312 $distribution //= $self->get_distribution();
313  
314 die "use -d or pass distribution name as an argument\n"
315 if !$distribution;
316  
317 my ( $key_prefix, $version ) = DarkPAN::Utils::parse_distribution_path($distribution);
318  
319 my $dpu = choose {
320 if ( -e $distribution && $distribution !~ /^(?:http|D\/DU)/xsm ) {
321 require Archive::Tar;
322  
323 my $tar = Archive::Tar->new;
324 $tar->read($distribution);
325 return DarkPAN::Utils->new( package => $tar );
326 }
327  
328 $distribution = basename($distribution);
329  
330 if ( $distribution !~ /^http/xsm ) {
331 $distribution = sprintf 'D/DU/DUMMY/%s', $distribution;
332 }
333  
334 my $orepan_url = $self->get_url // $self->get_config->{url};
335  
336 die "use --url or set url in config\n"
337 if !$orepan_url;
338  
339 my $dpu = DarkPAN::Utils->new( base_url => $orepan_url );
340  
341 $dpu->fetch_package($distribution);
342  
343 return $dpu;
344 };
345  
346 my $module_name = $key_prefix;
347 $module_name =~ s/\-/::/gxsm;
348  
349 my $file = $dpu->extract_module( $distribution, $module_name );
350  
351 if ( $self->get_upload ) {
352 if ($file) {
353 $self->upload_html(
354 name => "$key_prefix.html",
355 content => $file,
356 prefix => $key_prefix,
357 wrap => $TRUE,
358 distribution => basename($distribution),
359 );
360 }
361 }
362 else {
363 open my $fh, '>', "$key_prefix.html"
364 or die "could not open $key_prefix.html for writing\n";
365  
366 print {$fh} $file;
367  
368 close $fh;
369 }
370  
371 my $readme = $dpu->extract_file( sprintf '%s-%s/README.md', $key_prefix, $version );
372  
373 if ( $self->get_upload && $readme ) {
374 $self->upload_html(
375 name => 'README.html',
376 markdown => $readme,
377 prefix => $key_prefix,
378 wrap => $TRUE,
379 );
380 }
381 elsif ($readme) {
382 open my $fh, '>', 'README.html'
383 or die "could not open README.html for writing\n";
384  
385 print {$fh} $readme;
386  
387 close $fh;
388 }
389  
390 return 0;
391 }
392  
393 ########################################################################
394 sub upload_html {
395 ########################################################################
396 my ( $self, %args ) = @_;
397  
398 my ( $content, $prefix, $name, $markdown, $distribution ) = @args{qw(content prefix name markdown distribution)};
399  
400 my $perldoc_url_distros = $self->get_config->{perldoc_url_distros} // [];
401 my $perldoc_url_prefix;
402  
403 if ($distribution) {
404 foreach my $p ( pairs @{$perldoc_url_distros} ) {
405  
406 my ( $pattern, $flags ) = @{$p};
407 my $qr = qr/(?$flags:$pattern)/;
408 next if $distribution !~ $qr;
409  
410 $perldoc_url_prefix = $self->get_config->{perldoc_url_prefix};
411 last;
412 }
413 }
414  
415 $perldoc_url_prefix //= $METACPAN_URL;
416  
417 my $html = choose {
418 if ($markdown) {
419 require Text::Markdown::Discount;
420  
421 Text::Markdown::Discount::markdown($markdown);
422 }
423  
424 require DarkPAN::Utils::Docs;
425  
426 my $docs = DarkPAN::Utils::Docs->new(
427 text => $content,
428 url_prefix => $perldoc_url_prefix,
429 );
430  
431 return $docs->get_html;
432 };
433  
434 if ( $args{wrap} ) {
435 $html = <<"END_OF_HTML";
436 <!DOCTYPE HTML>
437 <html>
438 <head>
439 <title>README</title>
440 <link rel="stylesheet" href="/css/pod.css">
441 </head>
442 <body>
443 $html
444 </body>
445 </html>
446 END_OF_HTML
447 }
448  
449 my $key = sprintf 'docs/%s/%s', $prefix, $name;
450  
451 $self->_upload_html( \$html, $key );
452  
453 return;
454 }
455  
456 ########################################################################
457 sub look_for_object {
458 ########################################################################
459 my ( $self, $prefix, $name ) = @_;
460  
461 my $key = sprintf 'docs/%s/%s', $prefix, $name;
462  
463 return sprintf '/docs/%s/%s', $prefix, $name
464 if $self->get_s3->head_object( $self->get_bucket_name, $key );
465  
466 return;
467 }
468  
469 ########################################################################
470 sub cmd_create_index {
471 ########################################################################
472 my ($self) = @_;
473  
474 require DarkPAN::Utils;
475  
476 my $config = $self->get_config;
477  
478 my $file = $self->fetch_orepan_index;
479  
480 my $repo = $self->parse_index($file);
481  
482 unlink $file;
483  
484 $self->get_logger->debug( Dumper( [ repo => $repo ] ) );
485  
486 no strict 'refs'; ## no critic
487  
488 *{'utils::module_name'} = sub {
489 my ( $self, $distribution ) = @_;
490  
491 my $module_name = basename("/$distribution");
492 $module_name =~ s/-\d+[.].*$//xsm;
493  
494 return $module_name;
495 };
496  
497 my $utils = bless {}, 'utils';
498  
499 my $sections = $config->{custom_sections} // {};
500  
501 foreach my $var ( keys %{$sections} ) {
502 my ( $pattern, $flags ) = @{ $sections->{$var} };
503  
504 my $qr = qr/(?$flags:$pattern)/;
505 $sections->{$var} = [ $pattern, $flags, $qr ];
506 }
507  
508 my %readme_links;
509 my %pod_links;
510  
511 foreach my $distribution ( keys %{$repo} ) {
512 $self->get_logger->info( 'distribution: ' . $distribution );
513  
514 my ($distribution_name) = DarkPAN::Utils::parse_distribution_path($distribution);
515  
516 if ( !$distribution_name ) {
517 warn "WARN: could not get distribution name from $distribution\n";
518 next;
519 }
520  
521 my $readme = $self->look_for_object( $distribution_name, 'README.html' );
522  
523 my $pod = $self->look_for_object( $distribution_name, "${distribution_name}.html" );
524  
525 my $old_name = $distribution;
526 $distribution =~ s/^.*\/([^\/]+)/$1/xsm;
527 $repo->{$distribution} = delete $repo->{$old_name};
528  
529 if ($readme) {
530 $readme_links{$distribution} = $readme;
531 }
532  
533 if ($pod) {
534 $pod_links{$distribution} = $pod;
535 }
536  
537 if ($sections) {
538 foreach my $var ( keys %{$sections} ) {
539 if ( $distribution =~ $sections->{$var}->[2] ) {
540 $sections->{$var}->[3] //= {};
541 $sections->{$var}->[3]->{$distribution} = delete $repo->{$distribution};
542 }
543 }
544 }
545 }
546  
547 my $params = {
548 utils => $utils,
549 repo => $repo,
550 readme_links => \%readme_links,
551 pod_links => \%pod_links,
552 localtime => scalar localtime,
553 map { ( $_ => $sections->{$_}->[3] ) } keys %{$sections},
554  
555 };
556  
557 $self->get_logger->trace( Dumper( [ params => $params ] ) );
558  
559 my $text = $self->get_template;
560  
561 require Template;
562  
563 my $template = Template->new();
564  
565 my $output = q{};
566  
567 $template->process( \$text, $params, \$output )
568 or die $template->error();
569  
570 if ( $self->get_upload ) {
571 $self->_upload_html( \$output, 'index.html' );
572 $self->get_logger->debug($output);
573 }
574 else {
575 $self->send_output($output);
576 }
577  
578 return 0;
579 }
580  
581 ########################################################################
582 sub send_output {
583 ########################################################################
584 my ( $self, $content ) = @_;
585  
586 my $outfile = $self->get_output;
587  
588 my $fh = eval {
589  
590 return *STDOUT
591 if !$outfile;
592  
593 open my $fh, '>', $outfile;
594  
595 return $fh;
596 };
597  
598 die "could not open file for output\n$EVAL_ERROR"
599 if !$fh;
600  
601 print {$fh} $content;
602  
603 $outfile && close $fh;
604  
605 return 0;
606 }
607  
608 ########################################################################
609 sub parse_index {
610 ########################################################################
611 my ( $self, $file ) = @_;
612  
613 require OrePAN2::Index;
614  
615 my $index = OrePAN2::Index->new();
616  
617 $index->load($file);
618  
619 my $listing = $index->as_string;
620 $listing =~ s/^(.*)\n\n//xsm;
621  
622 my %repo;
623  
624 foreach ( split /\n/xsm, $listing ) {
625 my ( $module, $version, $package ) = split /\s+/xsm;
626 $repo{$package} //= [];
627 push @{ $repo{$package} }, [ $module, $version ];
628 }
629  
630 return \%repo;
631 }
632  
633 ########################################################################
634 sub cmd_download_orepan_index {
635 ########################################################################
636 my ($self) = @_;
637  
638 my $filename = eval { return $self->fetch_orepan_index(); };
639  
640 die "ERROR: Could not download $PACKAGE_INDEX\n$EVAL_ERROR"
641 if !$filename || !-s "$filename";
642  
643 rename $filename, $PACKAGE_INDEX;
644  
645 print {*STDOUT} $PACKAGE_INDEX . "\n";
646  
647 return;
648 }
649  
650 ########################################################################
651 sub init {
652 ########################################################################
653 my ($self) = @_;
654  
655 my $config = $self->fetch_config;
656  
657 my $profile = $config->{AWS}->{profile};
658  
659 my $dist = __PACKAGE__;
660 $dist =~ s/::/-/xsmg;
661 my $dist_dir = $self->set_dist_dir( dist_dir($dist) );
662  
663 my $credentials = Amazon::Credentials->new( profile => $profile );
664 $self->set_credentials($credentials);
665  
666 my $bucket_name = $self->get_bucket_name // $config->{AWS}->{bucket};
667  
668 $self->set_bucket_name($bucket_name);
669  
670 $self->init_s3;
671  
672 my $author_path = $config->{author_path} // $AUTHOR_PATH;
673 $self->set_author_path($author_path);
674  
675 $self->fetch_template;
676  
677 return;
678 }
679  
680 ########################################################################
681 sub fetch_template {
682 ########################################################################
683 my ($self) = @_;
684  
685 my $template = $self->get_template;
686  
687 my $index = $self->get_config->{index} // {};
688  
689 # see if the index is set in the config file...
690 if ( !$template && $index->{template} ) {
691 $template = $index->{template};
692 $template = $template =~ /^\//xsm ? $template : sprintf '%s/%s', $self->get_dist_dir, $template;
693 }
694  
695 my $index_template = $template eq 'default' ? slurp_file(*DATA) : slurp_file($template);
696  
697 $index_template =~ s/\n\n=pod.*$/\n/xsm;
698  
699 $self->set_template($index_template);
700  
701 return;
702 }
703  
704 ########################################################################
705 sub extract_from_tarball {
706 ########################################################################
707 my ( $tarball, $file ) = @_;
708  
709 require Archive::Tar;
710  
711 my $t = Archive::Tar->new;
712  
713 $t->read( $tarball, 1 )
714 or croak "failed to read tarball: $tarball";
715  
716 my $prefix = basename($tarball);
717 $prefix =~ s/[.]tar.*$//xsm;
718  
719 croak "file not found ($prefix/$file)"
720 if !$t->contains_file("$prefix/$file");
721  
722 return $t->get_content("$prefix/$file");
723 }
724  
725 ########################################################################
726 sub main {
727 ########################################################################
728 my $cli = OrePAN2::S3->new(
729 option_specs => [
730 qw(
731 help|h
732 bucket-name|b=s
733 config-file|c=s
734 output|o=s
735 profile|p=s
736 profile-name|n=s
737 template|t=s
738 url|U=s
739 distribution|d=s
740 upload|u
741 upload-only|U
742 )
743 ],
744 default_options => {
745 config_file => $DEFAULT_CONFIG,
746 profile_name => 'default',
747 profile => $ENV{AWS_PROFILE}
748 },
749 extra_options => [qw(config credentials template author_path s3 dist_dir)],
750 commands => {
751 'create' => \&cmd_create_index,
752 'create-docs' => \&cmd_create_docs,
753 'create-index' => \&cmd_create_index,
754 'delete' => \&cmd_delete,
755 'download' => \&cmd_download_orepan_index,
756 'dump-template' => sub {
757 print {*STDOUT} shift->get_template;
758 return 0;
759 },
760 'inject' => \&cmd_inject,
761 'invalidate-index' => \&cmd_invalidate_index,
762 'show' => \&cmd_show_orepan_index,
763 'upload' => \&cmd_upload_index,
764 'upload-artifacts' => \&cmd_upload_artifacts,
765 },
766 );
767  
768 return $cli->run();
769 }
770  
771 1;
772  
773 __DATA__
774 <!DOCTYPE HTML>
775 <html>
776 <head>
777 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
778 <title>CPAN Repository</title>
779 <script src="https://code.jquery.com/jquery-3.7.1.min.js"
780 integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo="
781 crossorigin="anonymous"></script>
782  
783 <script>
784 $(function() {
785 $('.collapsable').hide();
786  
787 $('h2').on('click', function() {
788 $(this).next().toggle();
789 });
790 });
791  
792 </script>
793 <style>
794 h2 {
795 cursor: pointer;
796 color: purple;
797 }
798 h2:hover {
799 color: green;
800 }
801 body {
802 font-family: monospace;
803 }
804 </style>
805 </head>
806
807 <body>
808 <h1>CPAN Repository</h1>
809
810 [% FOREACH distribution = repo.keys %]
811 <h2>[% distribution %]</h2>
812  
813 <ul class="collapsable" id="[% utils.module_name(distribution) %]">
814 [% FOREACH module IN repo.$distribution %]
815 <li>[% module.0 %]</li>
816 [% END %]
817 </ul>
818 [% END %]
819
820 <hr>
821 <address>Generated on [% localtime %] by Template::Toolkit</address>
822 </body>
823 </html>
824  
825 =pod
826  
827 =head1 NAME
828  
829 OrePAN2::S3 - Manage a DarkPAN CPAN mirror on Amazon S3
830  
831 =head1 SYNOPSIS
832  
833 # via the bash wrapper (recommended)
834 orepan2-s3 add My-Dist-1.0.tar.gz
835 orepan2-s3 index
836  
837 # or directly via the modulino
838 orepan2-s3-index create --upload
839  
840 # add and index a new distribution
841 orepan2-s3-index inject My-App-1.0.0.tar.gz
842  
843 # upload artifacts listed in config file
844 orepan2-s3-index upload-artifacts
845  
846 =head1 DESCRIPTION
847  
848 This class is used to add distributions to your own DarkPAN
849 repository housed on Amazon's S3 storage. It leverages L<OrePAN2> to
850 create and maintain your own DarkPAN repository. You can read more
851 about setting up a DarkPAN on Amazon using S3 + Cloudfront
852 L<here|https://github.com/rlauer6/OrePAN2-S3/blob/master/README.md>.
853  
854 You can read more about creating a secure static website using Amazon
855 S3 L<here|https://blog.tbcdevelopmentgroup.com/2025-02-18-post.html>.
856  
857 =head1 USAGE
858  
859 orepan2-s3 options command
860  
861 Perl script for maintaining a DarkPAN mirror using S3 + CloudFront.
862  
863 I<NOTE: C<orepan2-s3> is the bash script that calls this Perl class
864 that doubles as a modulino. The documentation here refers to the
865 script (not the class)>.
866  
867 =head2 Options
868  
869 -h, --help Display this help message
870 -b, --bucket-name Overrides bucket in configuration
871 -c, --config-file Name of the configuration file (default: ~/.orepan2-s3.json)
872 -o, --output Name of the output file
873 -p, --profile Your AWS profile if not provided in configuration
874 -n, --profile-name Name of a profile inside the config file
875 -t, --template Name of a template that will be used as the index.html page
876 -d, --distribution Path to distribution tarball
877 -u, --upload Upload files after processing (for create-index, create-docs)
878 -U, --url Cloudfront URL
879  
880 =head2 Commands
881  
882 =over 5
883  
884 =item * create - Create a new F<index.html> from the mirror's manifest file.
885  
886 =item * delete - Delete a distribution from repo and reindex
887  
888 =item * download - Download the mirror's manifest file (F<02packages.details.txt.gz>).
889  
890 =item * inject - Uploads a tarball and adds to the DarkPAN index
891  
892 =item * show - Print the manifest file to STDOUT or a file.
893  
894 =item * dump-template - Outputs the default index.html template.
895  
896 =item * create-docs - parse distribution looking for a README.md and/or pod
897  
898 =item * invalidate-index - I<not currently implemented>
899  
900 =item * upload - Upload the index.html file to the mirror's root.
901  
902 =item * upload-artifacts - Uploads the files listed in the C<index: files:> section of the config file.
903  
904 =back
905  
906 =head2 Notes
907  
908 =over 5
909  
910 =item The preferred way of using this utility is through the bash wrapper.
911  
912 I<The following commands are available only through the C<orepan2-s3>
913 bash wrapper, not the modulino directly:>
914  
915 =over 5
916  
917 =item * add {file} - inject a tarball into the repository and re-index
918  
919 =item * delete {file} - remove a distribution and re-index
920  
921 =item * invalidate - invalidate the CloudFront cache
922  
923 =back
924  
925 =back
926  
927 =head2 Configuration File
928  
929 The configuration file for C<orepan2-s3> is a JSON file that can
930 contain multiple profiles (or none). Each profile represents a DarkPAN
931 S3 repository. The format should look something like this:
932  
933 {
934 "default" : "bedrock",
935 "tbc" : {
936 "author_path": "D/DU/DUMMY",
937 "AWS": {
938 "profile" : "prod",
939 "region" : "us-east-1",
940 "bucket" : "tbc-cpan-mirror",
941 "prefix" : "orepan2"
942 },
943 "CloudFront" : {
944 "DistributionId" : "E2ABCDEFGHIJK"
945 }
946 },
947 "bedrock" : {
948 "author_path": "D/DU/DUMMY",
949 "index" : {
950 "template" : "/path/to/template",
951 "files": {
952 "src" : "dest"
953 }
954 },
955 "AWS": {
956 "profile" : "prod",
957 "region" : "us-east-1",
958 "bucket" : "cpan.openbedrock.net",
959 "prefix" : "orepan2"
960 },
961 "CloudFront" : {
962 "DistributionId" : "E2JKLMNOPQRXYZ",
963 "InvalidationPaths" : [],
964 "url" : "https://cpan.openbedrock.net/orepan2"
965 }
966 }
967 }
968  
969 Each profile can contain the keys described below. If you only have one
970 profile you don't need to place it in a 'default' section.
971  
972 The value for the 'default' key can be the name of a profile or a hash
973 of the profile.
974  
975 =over 5
976  
977 =item author_path
978  
979 Overrides default C<D/DU/DUMMY>. For a personal DarkPAN you should
980 place all modules in one path.
981  
982 =item index
983  
984 This section allows you to create custom template for the DarkPAN home page.
985  
986 =over 10
987  
988 =item template
989  
990 The name of a template file that will be parsed and uploaded as
991 F</index.html>. If you do not provide a template file a default
992 template is used. The default template is a L<Template::Toolkit> style
993 template. To see the default template use the C<dump-template> command
994 to the F<orepan2-s3-index> script.
995  
996 orepan2-s3-index dump-template
997  
998 The templating process is provided with these variables:
999  
1000 =over 15
1001  
1002 =item utils
1003  
1004 A blessed reference to an object with one method (C<module_name>) that
1005 returns a version of the module name suitable for use as unique CSS id.
1006  
1007 =item repo
1008  
1009 A hash of key value pairs where the key is the name of a DarkPAN
1010 distribution and value is an array or arrays. Each array is of the
1011 form:
1012  
1013 [0] => Perl module name
1014 [1] => Module version
1015  
1016 =item localtime
1017  
1018 The current time and date as a string.
1019  
1020 =item pod_links
1021  
1022 A hash where the keys are distribution names and the values are links
1023 to the POD for a module.
1024  
1025 =item readme_links
1026  
1027 A hash where the keys are distribution names and the values are links
1028 to a README for a module.
1029  
1030 I<NOTE: Sometimes the README and the POD will contain the same information.>
1031  
1032 =back
1033  
1034 =item files
1035  
1036 A hash of source/destination pairs that specify additional files you
1037 want uploaded to your S3 bucket.
1038  
1039 Example:
1040  
1041 "files": {
1042 "/home/rlauer/git/some-project/foo.css" : "/css/foo.css",
1043 "/home/rlauer/git/some-project/foo.js" : "/javascript/foo.js"
1044 }
1045  
1046 =back
1047  
1048 =item AWS
1049  
1050 =over 10
1051  
1052 =item profile
1053  
1054 The IAM profile that allows access to the S3 bucket and CloudFront.
1055  
1056 =item region
1057  
1058 AWS region. Default: us-east-1
1059  
1060 =item bucket
1061  
1062 S3 bucket name
1063  
1064 =item prefix
1065  
1066 The prefix where the CPAN distribution files will be stored. Default: orepan2.
1067  
1068 =back
1069  
1070 =item CloudFront
1071  
1072 I<NOTE: Your profile must have the ability to invalidate the CloudFront cache!>
1073  
1074 =over 10
1075  
1076 =item DistributionId
1077  
1078 CloudFront distribution id
1079  
1080 =item InvalidationPaths
1081  
1082 C<OrePAN2::S3> is designed to work with CloudFront + Amazon
1083 S3. CloudFront is a CDN and will read content from your S3 bucket when
1084 clients make HTTP requests. CloudFront will cache content to avoid
1085 costly reads to the S3 bucket. If you change some of the static assets
1086 (like the index page) you may want to invalidate the cache to see
1087 your new assets. You could wait until the cache is updated (the
1088 default time is 24 hours)...but why? The script will automatically
1089 invalidate the cache for you if you tell it what assets to invalidate
1090 when you add a new distribution.
1091  
1092 An array of additional paths to invalidate when adding new distributions.
1093  
1094 I<Note: There is no additional charge for adding additional
1095 paths. Each invalidation batch is considered as one billing unit by
1096 AWS. However, keep in mind you get 1000 invalidation paths for free
1097 each month. Thereafter each path costs $0.005 per path.>
1098  
1099 =back
1100  
1101 =item custom_sections
1102  
1103 This section contains key/value pairs where the key is the name of a
1104 variable that will be exposed to your template and the values
1105 are a two element array that contains a regular expression and
1106 possible regexp flags. The script will use the regexp to filter your
1107 distributions and add them to a hash whose name is the key you
1108 provided.
1109  
1110 The purpose of this section is to allow you to possibly organize your
1111 distributions under possible HTML headings.
1112  
1113 Example:
1114  
1115 "custom_sections" : {
1116 "plugins" : ["^BLM\-(?!Startup)", "xsm"],
1117 "app_plugins" : ["^BLM\-Startup", "xsm"],
1118 }
1119  
1120 ...then in your template:
1121  
1122 <h1>Application Plugin Index</h1>
1123
1124 [% FOREACH distribution = app_plugins.sort %]
1125 <h2>
1126 <span class="collapse-section-icon">&#9660;</span>
1127 [% distribution %]
1128 [% IF readme_links.$distribution %]
1129 <a title="README" class='doc-link' href="[% readme_links.$distribution %]"><span class="material-symbols-outlined">docs</span></a>
1130 [% END %]
1131 [% IF pod_links.$distribution %]
1132 <a title="pod" class='doc-link' href="[% pod_links.$distribution %]"><span class="material-symbols-outlined">docs</span></a>
1133 [% END %]
1134 </h2>
1135
1136 <ul class="collapsable" id="[% utils.module_name(distribution) %]">
1137 [% FOREACH module IN app_plugins.$distribution %]
1138 <li>[% module.0 %]</li>
1139 [% END %]
1140 </ul>
1141 [% END %]
1142 <hr>
1143  
1144 =back
1145  
1146 =head1 AUTHOR
1147  
1148 Rob Lauer - <rlauer@treasurersbriefcase.com>
1149  
1150 =head1 SEE ALSO
1151  
1152 L<OrePAN2>, L<Amazon::S3::Lite>, L<DarkPAN::Utils>, L<CLI::Simple>, L<Template>
1153  
1154 =head1 LICENSE
1155  
1156 This program is free software; you can redistribute it and/or modify
1157 it under the same terms as Perl itself.
1158  
1159 =cut
« S3/Role/Delete.pm ↑ Summary