| 1 | package App::ECR; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use English qw(-no_match_vars); | ||
| 8 | |||
| 9 | use Role::Tiny::With; | ||
| 10 | with 'App::AWS'; | ||
| 11 | |||
| 12 | __PACKAGE__->follow_best_practice; | ||
| 13 | __PACKAGE__->mk_accessors(qw(profile region)); | ||
| 14 | |||
| 15 | use parent qw(App::Command); | ||
| 16 | |||
| 17 | ######################################################################## | ||
| 18 | sub describe_images { | ||
| 19 | ######################################################################## | ||
| 20 | my ( $self, $repository_name, $query ) = @_; | ||
| 21 | |||
| 22 | return $self->command( | ||
| 23 | 'describe-images' => [ | ||
| 24 | '--repository-name' => $repository_name, | ||
| 25 | $query ? ( '--query' => $query ) : () | ||
| 26 | ] | ||
| 27 | ); | ||
| 28 | } | ||
| 29 | |||
| 30 | ######################################################################## | ||
| 31 | sub get_latest_image { | ||
| 32 | ######################################################################## | ||
| 33 | my ( $self, $repository_name ) = @_; | ||
| 34 | |||
| 35 | if ( $repository_name =~ /\//xsm ) { | ||
| 36 | $repository_name = ( split /\//xsm, $repository_name )[-1]; | ||
| 37 | } | ||
| 38 | |||
| 39 | ($repository_name) = split /:/xsm, $repository_name; | ||
| 40 | |||
| 41 | my $query = 'imageDetails[?imageTags != null && contains(imageTags, `latest`)]'; | ||
| 42 | |||
| 43 | my $result = $self->describe_images( $repository_name, $query ); | ||
| 44 | |||
| 45 | return @{ $result || [] }; | ||
| 46 | } | ||
| 47 | |||
| 48 | ######################################################################## | ||
| 49 | sub validate_images { | ||
| 50 | ######################################################################## | ||
| 51 | my ( $self, @images ) = @_; | ||
| 52 | |||
| 53 | foreach my $image (@images) { | ||
| 54 | warn sprintf "WARN: image not found in ECR: [%s]\n", $image | ||
| 55 | if !$self->get_latest_image($image); | ||
| 56 | } | ||
| 57 | |||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | 1; |