| 1 | package Beam::Make::Docker::Image; | ||||||||||||
| 2 | 3 | our $VERSION = '0.003'; | |||||||||||
| 3 | # ABSTRACT: A Beam::Make recipe to build/pull/update a Docker image | ||||||||||||
| 4 | |||||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||||
| 6 | #pod | ||||||||||||
| 7 | #pod ### Beamfile | ||||||||||||
| 8 | #pod nordaaker/convos: | ||||||||||||
| 9 | #pod $class: Beam::Make::Docker::Image | ||||||||||||
| 10 | #pod image: nordaaker/convos | ||||||||||||
| 11 | #pod | ||||||||||||
| 12 | #pod =head1 DESCRIPTION | ||||||||||||
| 13 | #pod | ||||||||||||
| 14 | #pod This L<Beam::Make> recipe class updates a Docker image, either by building it | ||||||||||||
| 15 | #pod or by checking a remote repository. | ||||||||||||
| 16 | #pod | ||||||||||||
| 17 | #pod B<NOTE:> This works for basic use-cases, but could use some | ||||||||||||
| 18 | #pod improvements. Improvements should attempt to match the C<docker-compose> | ||||||||||||
| 19 | #pod file syntax when possible. | ||||||||||||
| 20 | #pod | ||||||||||||
| 21 | #pod =head1 SEE ALSO | ||||||||||||
| 22 | #pod | ||||||||||||
| 23 | #pod L<Beam::Make::Docker::Container>, L<Beam::Make>, L<https://docker.com> | ||||||||||||
| 24 | #pod | ||||||||||||
| 25 | #pod =cut | ||||||||||||
| 26 | |||||||||||||
| 27 | 3 | ▶ | use v5.20; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 28 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 3 | ▶ | use autodie qw( :all ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | 3 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | 3 | ▶ | use Time::Piece; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | 3 | ▶ | use Log::Any qw( $LOG ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 33 | 3 | ▶ | use File::Which qw( which ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | 3 | ▶ | use JSON::PP qw( decode_json ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | 3 | ▶ | use Digest::SHA qw( sha1_base64 ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 36 | 3 | ▶ | use experimental qw( signatures postderef ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 37 | |||||||||||||
| 38 | 3 | extends 'Beam::Make::Recipe'; | |||||||||||
| 39 | |||||||||||||
| 40 | #pod =attr image | ||||||||||||
| 41 | #pod | ||||||||||||
| 42 | #pod The image to build or pull. If building, will tag the resulting image. | ||||||||||||
| 43 | #pod Required. | ||||||||||||
| 44 | #pod | ||||||||||||
| 45 | #pod =cut | ||||||||||||
| 46 | |||||||||||||
| 47 | 3 | has image => ( | |||||||||||
| 48 | is => 'ro', | ||||||||||||
| 49 | required => 1, | ||||||||||||
| 50 | ); | ||||||||||||
| 51 | |||||||||||||
| 52 | #pod =attr build | ||||||||||||
| 53 | #pod | ||||||||||||
| 54 | #pod The path to the build context. If set, will build an image instead of pulling | ||||||||||||
| 55 | #pod from a repository. | ||||||||||||
| 56 | #pod | ||||||||||||
| 57 | #pod =cut | ||||||||||||
| 58 | |||||||||||||
| 59 | 3 | has build => ( | |||||||||||
| 60 | is => 'ro', | ||||||||||||
| 61 | ); | ||||||||||||
| 62 | |||||||||||||
| 63 | #pod =attr args | ||||||||||||
| 64 | #pod | ||||||||||||
| 65 | #pod A mapping of build args (C<< --build-arg <KEY>=<VALUE> >>). | ||||||||||||
| 66 | #pod | ||||||||||||
| 67 | #pod =cut | ||||||||||||
| 68 | |||||||||||||
| 69 | has args => ( | ||||||||||||
| 70 | is => 'ro', | ||||||||||||
| 71 | 2 | ▶ | default => sub { {} }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 72 | 3 | ); | |||||||||||
| 73 | |||||||||||||
| 74 | #pod =attr tags | ||||||||||||
| 75 | #pod | ||||||||||||
| 76 | #pod A list of additional tags for the image. | ||||||||||||
| 77 | #pod | ||||||||||||
| 78 | #pod =cut | ||||||||||||
| 79 | |||||||||||||
| 80 | has tags => ( | ||||||||||||
| 81 | is => 'ro', | ||||||||||||
| 82 | 2 | ▶ | default => sub { [] }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 83 | 3 | ); | |||||||||||
| 84 | |||||||||||||
| 85 | #pod =attr dockerfile | ||||||||||||
| 86 | #pod | ||||||||||||
| 87 | #pod The name of the Dockerfile to use. If unset, Docker uses the default name: C<Dockerfile>. | ||||||||||||
| 88 | #pod | ||||||||||||
| 89 | #pod =cut | ||||||||||||
| 90 | |||||||||||||
| 91 | 3 | has dockerfile => ( | |||||||||||
| 92 | is => 'ro', | ||||||||||||
| 93 | ); | ||||||||||||
| 94 | |||||||||||||
| 95 | #pod =attr docker | ||||||||||||
| 96 | #pod | ||||||||||||
| 97 | #pod The path to the Docker executable to use. Defaults to looking up | ||||||||||||
| 98 | #pod C<docker> in C<PATH>. | ||||||||||||
| 99 | #pod | ||||||||||||
| 100 | #pod =cut | ||||||||||||
| 101 | |||||||||||||
| 102 | has docker => ( | ||||||||||||
| 103 | is => 'ro', | ||||||||||||
| 104 | 2 | ▶ | default => sub { which 'docker' }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 105 | 3 | ); | |||||||||||
| 106 | |||||||||||||
| 107 | 1 | ▶ | sub make( $self, %vars ) { | ||||||||||
|
Subroutine
make: called CC=6 SCAR=31.4
Pod
documented
| |||||||||||||
| 108 | 1 | my @cmd = ( $self->docker ); | |||||||||||
| 109 | 1 | ▶ | if ( my $context = $self->build ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 0 | push @cmd, 'build', '-t', $self->image; | |||||||||||
| 111 | 0 | ▶ | if ( my @tags = $self->tags->@* ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 0 | push @cmd, map {; '-t', $_ } @tags; | |||||||||||
| 113 | } | ||||||||||||
| 114 | 0 | ▶ | if ( my %args = $self->args->%* ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | 0 | push @cmd, map {; '--build-arg', join '=', $_, $args{$_} } keys %args; | |||||||||||
| 116 | } | ||||||||||||
| 117 | 0 | ▶ | if ( my $file = $self->dockerfile ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 0 | push @cmd, '-f', $file; | |||||||||||
| 119 | } | ||||||||||||
| 120 | 0 | push @cmd, $context; | |||||||||||
| 121 | } | ||||||||||||
| 122 | else { | ||||||||||||
| 123 | 1 | push @cmd, 'pull', $self->image; | |||||||||||
| 124 | } | ||||||||||||
| 125 | 1 | @cmd = $self->fill_env( @cmd ); | |||||||||||
| 126 | 1 | $LOG->debug( 'Running docker command: ', @cmd ); | |||||||||||
| 127 | 1 | system @cmd; | |||||||||||
| 128 | 0 | ▶ | delete $self->{_inspect_output} if exists $self->{_inspect_output}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | |||||||||||||
| 130 | # Update the cache | ||||||||||||
| 131 | 0 | my $info = $self->_image_info; | |||||||||||
| 132 | 0 | my $created = $info->{Created} =~ s/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).*$/$1/r; | |||||||||||
| 133 | 0 | my $iso8601 = '%Y-%m-%dT%H:%M:%S'; | |||||||||||
| 134 | 0 | $self->cache->set( $self->name, $self->_cache_hash, Time::Piece->strptime( $created, $iso8601 ) ); | |||||||||||
| 135 | |||||||||||||
| 136 | 0 | return 0; | |||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 1 | ▶ | sub _image_info( $self ) { | ||||||||||
|
Subroutine
_image_info: called CC=3 SCAR=11.1
| |||||||||||||
| 140 | 1 | state $json = JSON::PP->new->canonical->utf8; | |||||||||||
| 141 | 1 | my $output = $self->{_inspect_output}; | |||||||||||
| 142 | 1 | ▶ | if ( !$output ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 1 | my $cmd = join ' ', $self->docker, qw( image inspect ), $self->image; | |||||||||||
| 144 | 1 | $LOG->debug( 'Running docker command:', $cmd ); | |||||||||||
| 145 | 1 | $output = `$cmd`; | |||||||||||
| 146 | 1 | $self->{_inspect_output} = $output; | |||||||||||
| 147 | } | ||||||||||||
| 148 | 1 | my ( $image ) = $json->decode( $output )->@*; | |||||||||||
| 149 | 1 | ▶ | return $image || {}; | ||||||||||
|
Condition50%1/2
$image || {}
MC/DC0%0/1
$image || {}
$image
Truth table50%1/2
A
A= $image
| |||||||||||||
| 150 | } | ||||||||||||
| 151 | |||||||||||||
| 152 | 0 | ▶ | sub _config_hash( $self ) { | ||||||||||
|
Subroutine
_config_hash: not called CC=1 SCAR=6.9
| |||||||||||||
| 153 | 0 | my @keys = grep !/^_|^name$|^cache$/, keys %$self; | |||||||||||
| 154 | 0 | my $json = JSON::PP->new->canonical->utf8; | |||||||||||
| 155 | 0 | my $hash = sha1_base64( $json->encode( { $self->%{ @keys } } ) ); | |||||||||||
| 156 | 0 | return $hash; | |||||||||||
| 157 | } | ||||||||||||
| 158 | |||||||||||||
| 159 | 1 | ▶ | sub _cache_hash( $self ) { | ||||||||||
|
Subroutine
_cache_hash: called CC=2 SCAR=8.1
| |||||||||||||
| 160 | 1 | my $image = $self->_image_info; | |||||||||||
| 161 | 1 | ▶ | return '' unless keys %$image; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 162 | 0 | return sha1_base64( $image->{Id} . $self->_config_hash ); | |||||||||||
| 163 | } | ||||||||||||
| 164 | |||||||||||||
| 165 | 1 | ▶ | sub last_modified( $self ) { | ||||||||||
|
Subroutine
last_modified: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 166 | 1 | return $self->cache->last_modified( $self->name, $self->_cache_hash ); | |||||||||||
| 167 | } | ||||||||||||
| 168 | |||||||||||||
| 169 | 3 | 1; | |||||||||||
| 170 | |||||||||||||
| 171 | __END__ | ||||||||||||