| 1 | package Imager::CommandLine::DeleteAMI { | ||
| 2 | use MooseX::App; | ||
| 3 | use CloudDeploy::AMIDB; | ||
| 4 | use ARGV::Struct; | ||
| 5 | use Paws; | ||
| 6 | use v5.10; | ||
| 7 | |||
| 8 | has find_args => ( | ||
| 9 | is => 'ro', | ||
| 10 | isa => 'HashRef[Str]', | ||
| 11 | lazy => 1, | ||
| 12 | default => sub { | ||
| 13 | my $self = shift; | ||
| 14 | die "Must specify a criterion to find AMIs" if (scalar(@{ $self->extra_argv }) == 0); | ||
| 15 | return ARGV::Struct->new(argv => [ '{', @{ $self->extra_argv }, '}' ])->parse; | ||
| 16 | } | ||
| 17 | ); | ||
| 18 | |||
| 19 | option dryrun => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => 'Bool', | ||
| 22 | default => sub { 0 }, | ||
| 23 | documentation => 'Do a dry run (don\'t actually delete anything', | ||
| 24 | ); | ||
| 25 | |||
| 26 | option ami => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => 'Str', | ||
| 29 | documentation => 'This option ignores finding images in the AMI Database. It will just deregister the specified AMI id', | ||
| 30 | ); | ||
| 31 | |||
| 32 | option region => ( | ||
| 33 | is => 'ro', | ||
| 34 | isa => 'Str|Undef', | ||
| 35 | documentation => 'This option is needed if you specify --ami AND the image is not registered in the AMIDB (where the region can be auto-detected)', | ||
| 36 | ); | ||
| 37 | |||
| 38 | sub run { | ||
| 39 | my ($self) = @_; | ||
| 40 | |||
| 41 | if ($self->ami) { | ||
| 42 | $self->delete_ami($self->ami); | ||
| 43 | } else { | ||
| 44 | my $search = CloudDeploy::AMIDB->new; | ||
| 45 | my %criterion = %{ $self->find_args }; | ||
| 46 | |||
| 47 | if (not defined $criterion{Account}){ | ||
| 48 | $criterion{Account} = CloudDeploy::Config->new->account; | ||
| 49 | } | ||
| 50 | my @res = $search->search(%criterion); | ||
| 51 | |||
| 52 | die "No Image found in the DB" if (@res == 0); | ||
| 53 | die "You were going to delete more than one AMI. Aborting" if (@res > 1); | ||
| 54 | $self->delete_ami($res[0]->prop('ImageId')); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | sub delete_ami { | ||
| 59 | my ($self, $image_id) = @_; | ||
| 60 | say "Deleting " . $image_id; | ||
| 61 | |||
| 62 | my $region; | ||
| 63 | my $search = CloudDeploy::AMIDB->new; | ||
| 64 | my @res = $search->search(ImageId => $image_id); | ||
| 65 | |||
| 66 | if (@res == 0) { | ||
| 67 | say "Didn't find the AMI in the AMIDB. Not unregistering"; | ||
| 68 | # Pick up the region from the command line | ||
| 69 | die "Must specify --region where the AMI is because we can't auto-detect it" if (not defined $self->region); | ||
| 70 | $region = $self->region; | ||
| 71 | } elsif (@res > 1) { | ||
| 72 | die "Something went Wildly wrong! I found more than one $image_id in the AMIDB, and I'm aborting"; | ||
| 73 | } else { | ||
| 74 | print "Unregistering from AMIDB\n"; | ||
| 75 | $region = $res[0]->prop('Region'); | ||
| 76 | $search->delete({ ImageId => $image_id }) if (not $self->dryrun); | ||
| 77 | } | ||
| 78 | |||
| 79 | my $ec2 = Paws->service('EC2', region => $region); | ||
| 80 | my $images = $ec2->DescribeImages(Owners => [ 'self' ] , ImageIds => [ $image_id ]); | ||
| 81 | die "Found an abnormal (" . scalar(@{ $images->Images }) . ") amount of images with id $image_id" if (@{ $images->Images } != 1); | ||
| 82 | |||
| 83 | my @snap_ids = map { $_->Ebs->SnapshotId } grep { defined $_->Ebs } @{ $images->Images->[0]->BlockDeviceMappings }; | ||
| 84 | say "Deregistering $image_id"; | ||
| 85 | |||
| 86 | if ($self->dryrun){ | ||
| 87 | map { say "Deleting snapshot $_" } @snap_ids; | ||
| 88 | say "WARNING!: Running in dryrun mode. I haven't done anything."; | ||
| 89 | } else { | ||
| 90 | $ec2->DeregisterImage(ImageId => $image_id); | ||
| 91 | sleep(1); | ||
| 92 | foreach my $snap_id (@snap_ids) { | ||
| 93 | say "Deleting snapshot $snap_id"; | ||
| 94 | $ec2->DeleteSnapshot(SnapshotId => $snap_id); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | } | ||
| 100 | |||
| 101 | 1; | ||
| 102 |