| 1 | package Beam::Make::Docker::Container; | ||||||||||||||
| 2 | 2 | our $VERSION = '0.003'; | |||||||||||||
| 3 | # ABSTRACT: A Beam::Make recipe to build a Docker container | ||||||||||||||
| 4 | |||||||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||||||
| 6 | #pod | ||||||||||||||
| 7 | #pod ### Beamfile | ||||||||||||||
| 8 | #pod convos: | ||||||||||||||
| 9 | #pod $class: Beam::Make::Docker::Container | ||||||||||||||
| 10 | #pod image: nordaaker/convos:latest | ||||||||||||||
| 11 | #pod restart: unless-stopped | ||||||||||||||
| 12 | #pod ports: | ||||||||||||||
| 13 | #pod - 8080:3000 | ||||||||||||||
| 14 | #pod volumes: | ||||||||||||||
| 15 | #pod - $HOME/convos/data:/data | ||||||||||||||
| 16 | #pod | ||||||||||||||
| 17 | #pod =head1 DESCRIPTION | ||||||||||||||
| 18 | #pod | ||||||||||||||
| 19 | #pod This L<Beam::Make> recipe class creates a Docker container, re-creating | ||||||||||||||
| 20 | #pod it if the underlying image has changed. | ||||||||||||||
| 21 | #pod | ||||||||||||||
| 22 | #pod B<NOTE:> This works for basic use-cases, but could use some | ||||||||||||||
| 23 | #pod improvements. Improvements should attempt to match the C<docker-compose> | ||||||||||||||
| 24 | #pod file syntax when possible. | ||||||||||||||
| 25 | #pod | ||||||||||||||
| 26 | #pod =head1 SEE ALSO | ||||||||||||||
| 27 | #pod | ||||||||||||||
| 28 | #pod L<Beam::Make::Docker::Image>, L<Beam::Make>, L<https://docker.com> | ||||||||||||||
| 29 | #pod | ||||||||||||||
| 30 | #pod =cut | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 2 | ▶ | use v5.20; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 34 | 2 | ▶ | use autodie qw( :all ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | 2 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 2 | ▶ | use Time::Piece; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | 2 | ▶ | use Log::Any qw( $LOG ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | 2 | ▶ | use File::Which qw( which ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 39 | 2 | ▶ | use JSON::PP qw( decode_json ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | 2 | ▶ | use Digest::SHA qw( sha1_base64 ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | 2 | ▶ | use experimental qw( signatures postderef ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | 2 | extends 'Beam::Make::Recipe'; | |||||||||||||
| 44 | |||||||||||||||
| 45 | #pod =attr image | ||||||||||||||
| 46 | #pod | ||||||||||||||
| 47 | #pod The image to use for this container. Required. | ||||||||||||||
| 48 | #pod | ||||||||||||||
| 49 | #pod =cut | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 2 | has image => ( | |||||||||||||
| 52 | is => 'ro', | ||||||||||||||
| 53 | required => 1, | ||||||||||||||
| 54 | ); | ||||||||||||||
| 55 | |||||||||||||||
| 56 | #pod =attr command | ||||||||||||||
| 57 | #pod | ||||||||||||||
| 58 | #pod The command to run, overriding the image's default command. Can be a list or a string. | ||||||||||||||
| 59 | #pod | ||||||||||||||
| 60 | #pod =cut | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 2 | has command => ( | |||||||||||||
| 63 | is => 'ro', | ||||||||||||||
| 64 | ); | ||||||||||||||
| 65 | |||||||||||||||
| 66 | #pod =attr volumes | ||||||||||||||
| 67 | #pod | ||||||||||||||
| 68 | #pod A list of volumes to map. Volumes are strings with the local volume or | ||||||||||||||
| 69 | #pod Docker named volume and the destination mount point separated by a C<:>. | ||||||||||||||
| 70 | #pod | ||||||||||||||
| 71 | #pod =cut | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 2 | has volumes => ( | |||||||||||||
| 74 | is => 'ro', | ||||||||||||||
| 75 | ); | ||||||||||||||
| 76 | |||||||||||||||
| 77 | #pod =attr ports | ||||||||||||||
| 78 | #pod | ||||||||||||||
| 79 | #pod A list of ports to map. Ports are strings with the local port and the | ||||||||||||||
| 80 | #pod destination port separated by a C<:>. | ||||||||||||||
| 81 | #pod | ||||||||||||||
| 82 | #pod =cut | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 2 | has ports => ( | |||||||||||||
| 85 | is => 'ro', | ||||||||||||||
| 86 | ); | ||||||||||||||
| 87 | |||||||||||||||
| 88 | #pod =attr environment | ||||||||||||||
| 89 | #pod | ||||||||||||||
| 90 | #pod The environment variables to set in this container. A list of C<< NAME=VALUE >> strings. | ||||||||||||||
| 91 | #pod | ||||||||||||||
| 92 | #pod =cut | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 2 | has environment => ( | |||||||||||||
| 95 | is => 'ro', | ||||||||||||||
| 96 | ); | ||||||||||||||
| 97 | |||||||||||||||
| 98 | #pod =attr restart | ||||||||||||||
| 99 | #pod | ||||||||||||||
| 100 | #pod The restart policy for this container. One of C<no>, C<on-failure>, | ||||||||||||||
| 101 | #pod C<always>, C<unless-stopped>. | ||||||||||||||
| 102 | #pod | ||||||||||||||
| 103 | #pod =cut | ||||||||||||||
| 104 | |||||||||||||||
| 105 | 2 | has restart => ( | |||||||||||||
| 106 | is => 'ro', | ||||||||||||||
| 107 | ); | ||||||||||||||
| 108 | |||||||||||||||
| 109 | #pod =attr docker | ||||||||||||||
| 110 | #pod | ||||||||||||||
| 111 | #pod The path to the Docker executable to use. Defaults to looking up | ||||||||||||||
| 112 | #pod C<docker> in C<PATH>. | ||||||||||||||
| 113 | #pod | ||||||||||||||
| 114 | #pod =cut | ||||||||||||||
| 115 | |||||||||||||||
| 116 | has docker => ( | ||||||||||||||
| 117 | is => 'ro', | ||||||||||||||
| 118 | 1 | ▶ | default => sub { which 'docker' }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 119 | 2 | ); | |||||||||||||
| 120 | |||||||||||||||
| 121 | 0 | ▶ | sub make( $self, %vars ) { | ||||||||||||
|
Subroutine
make: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||
| 122 | # Always clean if we're making: Docker can only have one container | ||||||||||||||
| 123 | # with a given name. | ||||||||||||||
| 124 | 0 | $self->clean; | |||||||||||||
| 125 | |||||||||||||||
| 126 | my @cmd = ( | ||||||||||||||
| 127 | $self->docker, qw( container create ), | ||||||||||||||
| 128 | '--name' => $self->name, | ||||||||||||||
| 129 | ( $self->restart ? ( '--restart' => $self->restart ) : () ), | ||||||||||||||
| 130 | 0 | ▶ | ( $self->environment ? map {; "-e", $_ } $self->environment->@* : () ), | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 0 | ▶ | ( $self->ports ? map {; "-p", $_ } $self->ports->@* : () ), | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 0 | ▶ | ( $self->volumes ? map {; "-v", $_ } $self->volumes->@* : () ), | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 133 | $self->image, | ||||||||||||||
| 134 | ); | ||||||||||||||
| 135 | 0 | ▶ | if ( my $cmd = $self->command ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | 0 | ▶ | push @cmd, ref $cmd eq 'ARRAY' ? @$cmd : $cmd; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 137 | } | ||||||||||||||
| 138 | 0 | @cmd = $self->fill_env( @cmd ); | |||||||||||||
| 139 | 0 | $LOG->debug( 'Running docker command:', @cmd ); | |||||||||||||
| 140 | 0 | system @cmd; | |||||||||||||
| 141 | 0 | ▶ | delete $self->{_inspect_output} if exists $self->{_inspect_output}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | |||||||||||||||
| 143 | # Update the cache | ||||||||||||||
| 144 | 0 | my $info = $self->_container_info; | |||||||||||||
| 145 | 0 | my $created = $info->{Created} =~ s/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).*$/$1/r; | |||||||||||||
| 146 | 0 | my $iso8601 = '%Y-%m-%dT%H:%M:%S'; | |||||||||||||
| 147 | 0 | $self->cache->set( $self->name, $self->_cache_hash, Time::Piece->strptime( $created, $iso8601 ) ); | |||||||||||||
| 148 | |||||||||||||||
| 149 | 0 | $self->start; | |||||||||||||
| 150 | 0 | return 0; | |||||||||||||
| 151 | } | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 0 | ▶ | sub clean( $self ) { | ||||||||||||
|
Subroutine
clean: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 154 | 0 | my $info = $self->_container_info; | |||||||||||||
| 155 | 0 | ▶ | if ( keys %$info ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 156 | 0 | ▶ | if ( $info->{State}{Running} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 157 | 0 | system qw( docker stop ), $self->name; | |||||||||||||
| 158 | } | ||||||||||||||
| 159 | 0 | system qw( docker rm ), $self->name; | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | 0 | ▶ | sub start( $self ) { | ||||||||||||
|
Subroutine
start: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 164 | 0 | system qw( docker start ), $self->name; | |||||||||||||
| 165 | } | ||||||||||||||
| 166 | |||||||||||||||
| 167 | 0 | ▶ | sub _container_info( $self ) { | ||||||||||||
|
Subroutine
_container_info: not called CC=3 SCAR=24.8
| |||||||||||||||
| 168 | 0 | state $json = JSON::PP->new->canonical->utf8; | |||||||||||||
| 169 | 0 | my $output = $self->{_inspect_output}; | |||||||||||||
| 170 | 0 | ▶ | if ( !$output ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | 0 | my $cmd = join ' ', $self->docker, qw( container inspect ), $self->name; | |||||||||||||
| 172 | 0 | $LOG->debug( 'Running docker command:', $cmd ); | |||||||||||||
| 173 | 0 | $output = `$cmd`; | |||||||||||||
| 174 | 0 | $self->{_inspect_output} = $output; | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | 0 | my ( $container ) = $json->decode( $output )->@*; | |||||||||||||
| 177 | 0 | ▶ | return $container || {}; | ||||||||||||
|
Condition0%0/2
$container || {}
MC/DC0%0/1
$container || {}
$container
Truth table0%0/2
A
A= $container
| |||||||||||||||
| 178 | } | ||||||||||||||
| 179 | |||||||||||||||
| 180 | 0 | ▶ | sub _config_hash( $self ) { | ||||||||||||
|
Subroutine
_config_hash: not called CC=1 SCAR=6.9
| |||||||||||||||
| 181 | 0 | my @keys = grep !/^_|^name$|^cache$/, keys %$self; | |||||||||||||
| 182 | 0 | my $json = JSON::PP->new->canonical->utf8; | |||||||||||||
| 183 | 0 | my $hash = sha1_base64( $json->encode( { $self->%{ @keys } } ) ); | |||||||||||||
| 184 | 0 | return $hash; | |||||||||||||
| 185 | } | ||||||||||||||
| 186 | |||||||||||||||
| 187 | 0 | ▶ | sub _cache_hash( $self ) { | ||||||||||||
|
Subroutine
_cache_hash: not called CC=2 SCAR=17.9
| |||||||||||||||
| 188 | 0 | my $container = $self->_container_info; | |||||||||||||
| 189 | 0 | ▶ | return '' unless keys %$container; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 190 | 0 | return sha1_base64( $container->{Id} . $self->_config_hash ); | |||||||||||||
| 191 | } | ||||||||||||||
| 192 | |||||||||||||||
| 193 | 0 | ▶ | sub last_modified( $self ) { | ||||||||||||
|
Subroutine
last_modified: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 194 | 0 | return $self->cache->last_modified( $self->name, $self->_cache_hash ); | |||||||||||||
| 195 | } | ||||||||||||||
| 196 | |||||||||||||||
| 197 | 2 | 1; | |||||||||||||
| 198 | |||||||||||||||
| 199 | __END__ | ||||||||||||||