-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpandocker
executable file
·597 lines (473 loc) · 17.6 KB
/
cpandocker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#!/usr/bin/perl
use strict; use warnings;
use FindBin;
use JSON;
use File::Copy;
our $cfg = {};
our $opts = cmdline_opts();
our $what = shift @ARGV;
print_usage() if !$what;
read_cfg();
{
no strict 'refs';
$what = "cpandocker_$what";
&$what(@ARGV);
}
exit 0;
#------------------------------------------------------------------------------
# cpandocker action subs
#
sub cpandocker_cfgsync {
my @new_cpans;
foreach my $cpn (@{$cfg->{cpans}//[]}){
push @new_cpans, scalar get_cpan_information($cpn->{name});
}
$cfg->{cpans} = [sort {$a->{name} cmp $b->{name}} @new_cpans];
$cfg->{version} = 1;
return;
}
sub cpandocker_list {
foreach my $cpn (@{$cfg->{cpans}//[]}){
print pretty_string_cpan($cpn);
}
return;
}
sub cpandocker_info {
foreach my $cpn (@{$cfg->{cpans}//[]}){
print pretty_string_cpan(scalar get_cpan_information($cpn->{name}));
}
return;
}
sub cpandocker_add {
my (@cpans) = @_;
my %cpans_to_add;
foreach my $cpn (@cpans){
my ($cpan_version, $tarball_version, $docker_cpan_tag) = do_cpan_docker_build($cpn);
$cpans_to_add{$cpn} = {
name => $cpn,
version => $cpan_version,
tarball => $tarball_version,
tag => [
$docker_cpan_tag,
"$docker_cpan_tag-$cpan_version",
]
};
}
if(keys %cpans_to_add){
print STDERR "adding: ".join(',',sort keys %cpans_to_add)."\n";
foreach my $cpn (@{$cfg->{cpans}//[]}){
$cpn = undef if defined $cpans_to_add{$cpn->{name}};
}
$cfg->{cpans} = [sort {$a->{name} cmp $b->{name}} grep {defined} @{$cfg->{cpans}//[]}, values %cpans_to_add];
save_cfg();
}
return;
}
sub cpandocker_build {
my (@cpans) = @_;
foreach my $cpan_module (@cpans){
do_cpan_docker_build($cpan_module);
}
return;
}
sub cpandocker_rebuildall {
foreach my $cpan_module (@{$cfg->{cpans}//[]}){
do_cpan_docker_build($cpan_module->{name});
cmd("docker image prune -f");
}
return;
}
sub cpandocker_versionall {
foreach my $cpan_module (@{$cfg->{cpans}//[]}){
do_cpan_version_check($cpan_module);
}
return;
}
sub cpandocker_version {
my (@cpans) = @_;
foreach my $cpan_module (@cpans){
do_cpan_version_check(get_cfg($cpan_module));
}
return;
}
sub cpandocker_pushall {
foreach my $cpan_module (@{$cfg->{cpans}//[]}){
do_cpan_docker_push($cpan_module);
}
return;
}
sub cpandocker_push {
my (@cpans) = @_;
foreach my $cpan_module (@{$cfg->{cpans}}){
next unless grep {$cpan_module->{name} eq $_} @cpans;
do_cpan_docker_push($cpan_module);
}
return;
}
sub cpandocker_buildimage {
return;
}
#------------------------------------------------------------------------------
# cpandocker action helper subs
#
sub do_cpan_docker_push {
my ($cpn) = @_;
foreach my $tag (@{$cpn->{tag}//[]}){
my $tgt_tag = ($ENV{REMOTE_DOCKER_REGISTRY}?"$ENV{REMOTE_DOCKER_REGISTRY}/":"")."$ENV{DOCKER_REGISTRY}/perl:cpan-$tag";
cmd("docker tag $ENV{DOCKER_REPOSITORY}/perl:cpan-$tag $tgt_tag");
cmd("docker push $tgt_tag");
}
return;
}
sub pretty_string_cpan {
my ($cpn) = @_;
return sprintf "%-25s %6s %-50s %-50s\n", "[$cpn->{name}]", $cpn->{version}, $cpn->{tarball}, join(',', @{$cpn->{tag}//[]});
}
sub get_cpan_information {
my ($cpan_to_add) = @_;
my @cpan_details = `cpan -D $cpan_to_add`;
my ($tarball_version, $cpan_version);
while(defined(my $l = shift @cpan_details)){
print STDERR $l;
if($l =~ /CPAN:\s+(\S+)\s+/){
$cpan_version = $1;
next
}
if($l =~ /(\S+\.(?:tar\.gz|tgz))/){
$tarball_version = $1;
next
}
}
die "No cpan $cpan_to_add found\n"
unless $tarball_version and $cpan_version;
my $docker_cpan_tag = lc($cpan_to_add =~ s/::/-/gr);
return wantarray?($cpan_to_add, $cpan_version, $tarball_version, $docker_cpan_tag):{
name => $cpan_to_add,
version => $cpan_version,
tarball => $tarball_version,
tag => [
$docker_cpan_tag,
"$docker_cpan_tag-$cpan_version",
]
}
}
sub do_cpan_version_check {
my ($cpn) = @_;
my $tdir = "/tmp/cpan_test_${$}_$ENV{USER}.".time();
mkdir($tdir) or die "Error making dir $tdir: $!\n";
print STDERR "using $tdir/Dockerfile\n";
$! = 0;
# Dockerfile
my $docker_cpan_ref = "$ENV{DOCKER_REPOSITORY}/perl:cpan-$cpn->{tag}[1]";
my $perl_version = $ENV{PERL_VERSION};
my $tmpdockername = "tmpperlcpanapp-$$-$ENV{USER}-".lc($cpn->{name} =~ s/::/-/gr);
my $docker_fn = "$tdir/Dockerfile";
open(my $d_fh, '>', $docker_fn)
or die "Error opening $docker_fn: $!\n";
print {$d_fh} <<EOdockerfile;
FROM $ENV{DOCKER_REGISTRY}/perl:$ENV{DOCKER_PERL_TAG} as tmpapp-01
FROM $ENV{DOCKER_REGISTRY}/perl:cpan-$cpn->{tag}[0] as tmpapp-02
FROM scratch
COPY --from=tmpapp-01 / /
COPY --from=tmpapp-02 / /
ENTRYPOINT ["/opt/bin/perl"]
EOdockerfile
cmd("docker build $tdir -f $tdir/Dockerfile --tag $tmpdockername");
cmd("docker run --rm -it $tmpdockername -M$cpn->{name} -we 'print \"[$cpn->{name}] VERSION: \$".$cpn->{name}."::VERSION\\n\"'", 0)
or die pretty_string_cpan($cpn)." FAILED: $!\n";
cmd("docker image rm -f $tmpdockername", 0)
or die "problem removing temp $tmpdockername: $!\n";
unlink $docker_fn;
rmdir $tdir;
return;
}
sub do_cpan_docker_build {
my ($cpan_to_add) = @_;
# get some details
my ($_d, $cpan_version, $tarball_version, $docker_cpan_tag) = get_cpan_information($cpan_to_add);
print STDERR "will tag the docker image for $cpan_to_add with $docker_cpan_tag-$cpan_version\n";
# build docker with an existing Dockerfile when the directory is already there?
my $d_dir = "$FindBin::Bin/dockers/perl:cpan-$docker_cpan_tag";
print STDERR "checking for $d_dir\n";
if(-d $d_dir){
cmd("docker build $d_dir -f $d_dir/Dockerfile "
." --build-arg docker_registry=$ENV{DOCKER_REGISTRY}"
." --build-arg remote_docker_registry=$ENV{REMOTE_DOCKER_REGISTRY}"
." --cache-from $ENV{DOCKER_REPOSITORY}/perl:cpan-$docker_cpan_tag-$cpan_version"
." --tag $ENV{DOCKER_REPOSITORY}/perl:cpan-$docker_cpan_tag-$cpan_version", 0)
or die "problem building the $d_dir/Dockerfile, quitting: $!\n";
} else {
build_cpan_docker($cpan_to_add, $docker_cpan_tag, $tarball_version, $cpan_version);
}
# put extra tags
cmd("docker tag $ENV{DOCKER_REPOSITORY}/perl:cpan-$docker_cpan_tag-$cpan_version ".
"$ENV{DOCKER_REPOSITORY}/perl:cpan-$docker_cpan_tag");
return ($cpan_version, $tarball_version, $docker_cpan_tag);
}
sub build_cpan_docker {
my ($cpan_to_add, $docker_cpan_tag, $tarball_version, $cpan_version) = @_;
# make a temp dir + Dockerfile
my $tdir = "/tmp/dockerize_cpan_${$}_$ENV{USER}.".time();
mkdir($tdir) or die "Error making dir $tdir: $!\n";
print STDERR "using $tdir/Dockerfile\n";
# cpan_config.pl
File::Copy::copy("$FindBin::Bin/dockers/perl-dev/cpan_config.pl", "$tdir/cpan_config.pl")
or die "Error copy cpan_config.pl: $!\n";
# Dockerfile
my $perl_version = $ENV{PERL_VERSION};
my $docker_fn = "$tdir/Dockerfile";
open(my $d_fh, '>', $docker_fn)
or die "Error opening $docker_fn: $!\n";
print {$d_fh} <<EOdockerfile;
# start from the perl-dev docker
FROM $ENV{DOCKER_REGISTRY}/perl:$ENV{DOCKER_PERL_DEV_TAG} as my-perl-d
# start clean, or else we mis dependencies (sadly enough)
# FIXME: split build_requires from requires?!
RUN rm -rf /opt/cpan/home
RUN rm -rf /opt/cpan/install
COPY cpan_config.pl /tmp/
# set some ENV vars, to be sure. Also, we will build in /newopt
# so PERL5LIB has to contain that
ENV LD_LIBRARY_PATH=/opt/lib64:/opt/lib:/opt/lib/perl5/$perl_version/x86_64/CORE
# see cpan_config.pl for CPAN_HOME/DESTDIR/INSTALL_BASE/PERL5LIB!!
# Add trailing slash to DESTDIR (/)!!
ENV DESTDIR=/newopt/
ENV CPAN_HOME=/opt/cpan/home
ENV CPAN_LOCAL=/opt/cpan/install
ENV PERL5LIB=\\
\$CPAN_LOCAL/opt/site_perl/lib/perl5:\\
\$CPAN_LOCAL/opt/site_perl/lib/perl5/site_perl/auto:\\
\$CPAN_LOCAL/opt/site_perl/lib/perl5/site_perl/$perl_version:\\
\$CPAN_LOCAL/opt/site_perl/lib/perl5/site_perl/$perl_version/auto:\\
\$CPAN_LOCAL/opt/site_perl/lib/perl5/site_perl/$perl_version/x86_64-linux:\\
\$CPAN_LOCAL/opt/site_perl/lib/perl5/site_perl/$perl_version/x86_64-linux/auto:\\
\$DESTDIR/opt/site_perl/lib/perl5:\\
\$DESTDIR/opt/site_perl/lib/perl5/site_perl/auto:\\
\$DESTDIR/opt/site_perl/lib/perl5/site_perl/$perl_version:\\
\$DESTDIR/opt/site_perl/lib/perl5/site_perl/$perl_version/auto:\\
\$DESTDIR/opt/site_perl/lib/perl5/site_perl/$perl_version/x86_64-linux:\\
\$DESTDIR/opt/site_perl/lib/perl5/site_perl/$perl_version/x86_64-linux/auto:\\
/opt/lib/perl5:\\
/opt/lib/perl5/site_perl:\\
/opt/lib/perl5/site_perl/auto:\\
/opt/lib/perl5/site_perl/$perl_version/:\\
/opt/lib/perl5/site_perl/$perl_version/auto:\\
/opt/lib/perl5/site_perl/$perl_version/x86_64-linux:\\
/opt/lib/perl5/site_perl/$perl_version/x86_64-linux/auto:\\
/opt/lib/perl5/auto:\\
/opt/lib/perl5/$perl_version:\\
/opt/lib/perl5/$perl_version/auto:\\
/opt/lib/perl5/$perl_version/x86_64-linux:\\
/opt/lib/perl5/$perl_version/x86_64-linux/auto
RUN echo \$PERL5LIB
# actually run cpan install
RUN /opt/bin/perl /opt/bin/cpan -j /tmp/cpan_config.pl -T -i \\
$tarball_version \\
; rm -rf \$DESTDIR/opt/site_perl/share \\
; rm -rf \$DESTDIR/opt/site_perl/man \\
; rm -rf \$DESTDIR/opt/lib \\
; find \$DESTDIR/ -type f -name '.packlist' |xargs rm -f \\
; find \$DESTDIR/ -type f -name '.pc' |xargs rm -f \\
; find \$DESTDIR/ -type f -name '.h' |xargs rm -f \\
; find \$DESTDIR/ -type f -name '.pod' |xargs rm -f \\
; exit 0
# and a simple test
RUN /opt/bin/perl -M$cpan_to_add -we 'print "[OK] CPAN TEST 1 ${cpan_to_add} built, version: \$${cpan_to_add}::VERSION\\n"'
# start a new scratch with runtime perl and move to / just to test (without PERL5LIB set!)
FROM $ENV{DOCKER_REGISTRY}/perl:$ENV{DOCKER_PERL_TAG} as test-perl-cpan
FROM scratch
COPY --from=my-perl-d /newopt/opt/ /
COPY --from=test-perl-cpan / /
ENV DESTDIR=
ENV INSTALL_BASE=
ENV LD_LIBRARY_PATH=
ENV PERL5LIB=
RUN /opt/bin/perl -M$cpan_to_add -we 'print "[OK] CPAN TEST 2 ${cpan_to_add} built, version: \$${cpan_to_add}::VERSION\\n"'
# test ok, so squash for push
FROM scratch
COPY --from=my-perl-d /newopt/opt/ /
EOdockerfile
close($d_fh)
or die "Error closing $docker_fn: $!\n";
# run docker build
cmd( "docker build $tdir -f $tdir/Dockerfile"
." --cache-from $ENV{DOCKER_REPOSITORY}/perl:cpan-$docker_cpan_tag-$cpan_version"
." --tag $ENV{DOCKER_REPOSITORY}/perl:cpan-$docker_cpan_tag-$cpan_version"
,0) or die "problem building the docker, quitting: $!\n";
unlink "$tdir/$_" for glob($tdir);
rmdir $tdir;
return;
}
#------------------------------------------------------------------------------
# cfg/options related subs
#
sub read_cfg {
# our config file
my $cpan_docker_home = $ENV{CPAN_DOCKER_HOME} ||= "$FindBin::Bin/";
my $cpan_docker_cfg_fn = $ENV{CPAN_DOCKER_CONFIG} ||= "$cpan_docker_home/.cpandocker.json";
$cfg = decode_json(do {local(@ARGV,$/)=$cpan_docker_cfg_fn;<>});
if(($cfg->{version}//'') ne '1'){
cpan_cfgsync();
save_cfg();
}
# config/defaults
my $perl_version =
$ENV{PERL_VERSION} //= $cfg->{perl_version} // "5.32.0";
$ENV{DOCKER_PERL_TAG} //= $cfg->{docker_perl_tag} // "$perl_version";
$ENV{DOCKER_PERL_DEV_TAG} //= $cfg->{docker_perl_dev_tag} // "$perl_version-dev-latest";
$ENV{DOCKER_LOCAL} //= $cfg->{docker_local} // "docker_build_$ENV{USER}";
$ENV{DOCKER_REGISTRY} //= $cfg->{docker_registry} // "aardbeiplantje";
$ENV{REMOTE_DOCKER_REGISTRY} //= $cfg->{remote_docker_registry} // '';
$ENV{DOCKER_REPOSITORY} //= $cfg->{docker_repository} // $ENV{DOCKER_LOCAL}.'/'.$ENV{DOCKER_REGISTRY};
return;
}
sub get_cfg {
my ($cpan_module) = @_;
foreach my $cpn (@{$cfg->{cpans}//[]}){
return $cpn if $cpn->{name} eq $cpan_module;
}
return
}
our $_json_enc;
sub save_cfg {
my $cpan_docker_home = $ENV{CPAN_DOCKER_HOME} ||= "$FindBin::Bin/";
my $cpan_docker_cfg_fn = $ENV{CPAN_DOCKER_CONFIG} ||= "$cpan_docker_home/.cpandocker.json";
$_json_enc //= JSON->new->utf8->allow_blessed->allow_unknown->allow_nonref->convert_blessed->pretty->canonical;
open(my $cfg_h, ">", $cpan_docker_cfg_fn)
or die "Error opening $cpan_docker_cfg_fn for write: $!\n";
print {$cfg_h} $_json_enc->encode($cfg);
close($cfg_h)
or die "Error opening $cpan_docker_cfg_fn for write: $!\n";
return;
}
sub show_usage {
my ($opts) = @_;
print_usage() if $opts->{help};
if($opts->{man}){
local $ENV{PAGER} = $ENV{PAGER}||'less';
load_cpan('FindBin')->again();
load_cpan('Pod::Usage');
Pod::Usage::pod2usage(-exitval => 1, -verbose => 2, -input => "$FindBin::Bin/$FindBin::Script");
}
return;
}
sub print_usage {
local $ENV{PAGER} = $ENV{PAGER}||'less';
load_cpan('FindBin')->again();
load_cpan('Pod::Usage');
Pod::Usage::pod2usage(-exitval => 0, -verbose => 1, -input => "$FindBin::Bin/$FindBin::Script");
return;
}
sub load_cpan {
eval "require $_[0]";
die $@ if $@;
return $_[0];
}
sub cmdline_opts {
load_cpan('Getopt::Long');
Getopt::Long::Configure('pass_through', 'no_ignore_case', 'bundling', 'no_auto_abbrev');
Getopt::Long::GetOptions(my $opts = {}, "cfg=s", "help|h|?!", "man|m!") or pod2usage(1);
show_usage($opts) if $opts->{man} or $opts->{help};
return $opts;
}
sub cmd {
my ($cmd, $exception) = @_;
$? = 0;
$! = 0;
my $r = system($cmd);
$r == 0 or ($exception//1) and die $!;
return $r == 0;
}
__END__
=pod
=head1 NAME
cpandocker - easy cpan docker image maintain utility
=head1 SYNOPSIS
B<cpandocker> B<[OPTIONS]> B<action> B<[cpan]> B<[cpan, ...]>
=head1 DESCRIPTION
This is the cpandocker tool that allows to make a perl sandbox and maintain
what cpan modules are in there via a configuration file. The configuration file
location and/or cpandocker home can be specified on command line.
cpandocker acts as a wrapper around docker. Building is done with the
aardbeiplantje/perl and aardbeiplantje/perl:5.32.0-dev-latest docker images.
=head1 OPTIONS
=over 4
=item B<help|h|?>
Show the usage information.
=item B<man|m>
Show the manpage.
=back
=head1 ARGUMENTS
=over 4
=item B<action>
What to do for the list of cpan modules on the command line. Note that there
doesn't have to be a cpan module specified, as there are actions that just list
or manage the F<.cpandocker.json> config file.
=over 2
=item * list
list what cpan modules are dockerized as configuration.
=item * cfgsync
fetch the latest information from cpan and sync this with the config.
=item * info
fetch the cpan information from cpan for the modules that are added to the
configuration
=item * add
add a cpan module, note that this also allows upgrading
=item * build
just build a module, don't add it to the configuration
=item * rebuildall
rebuild all cpans that are configured
=item * version
show the version of a configured cpan, this also acts as a perl module load check
=item * versionall
show the version for all configured cpans, this also acts as a perl module load
check for all modules that are configured.
=item * push
push a configured and build module to the remote docker repository
=item * pushall
push all cpans that are configured to the remote docker repository
=item * buildimage
builds the end docker image
=back
=item B<cpan>
The cpan module to execute the action for
=back
=head1 CPANDOCKER CONFIG
TODO! Document the .cpandocker.json config
=head1 ENVIRONMENT VARIABLES
=over 4
=item * PERL_VERSION
The perl version. For now this shouldn't be set, or always be set to 5.32.0.
=item * DOCKER_PERL_TAG
The docker tag to to use for checks. Note that for the moment this is only used
as a check whether the built cpan module would work with the runtime
aardbeiplantje/perl docker.
=item * DOCKER_PERL_DEV_TAG
This is the docker tag to use when making a cpan module build
=item * DOCKER_LOCAL
The local docker image prefix part for a default of DOCKER_REPOSITORY if
DOCKER_REGISTRY nor docker_registry isn't set.
The DOCKER_LOCAL is defaulted to docker_build_$USER.
=item * DOCKER_REGISTRY
This is the docker registry, for the moment "aardbeiplantje". It's used for
local tagging, and pushing to the remote docker repository.
This can be used to build cpans to the locally built perl-dev docker image,
e.g.:
$ make perl_docker
$ DOCKER_REGISTRY=docker_build_$USER/aardbeiplantje ./cpandocker add Redis
=item * REMOTE_DOCKER_REGISTRY
This overrides the remote docker registry part when doing a push. Default this
is not set, which is a default to pushing to docker.io.
=item * DOCKER_REPOSITORY
This is the registry/repository to use when building and making local tags.
This is defaulted to a concatenation of the $DOCKER_LOCAL/$DOCKER_REGISTRY
which makes for a good default:
docker_build_$USER/aardbeiplantje
=back
=head1 SEE ALSO
L<docker(1)>, L<perl(1)>, L<cpan(1)>.
=head1 AUTHORS
CowboyTim <[email protected]>
=head1 REPORT BUGS
github L<https://github.com/CowboyTim/perl-docker/issues>
=cut