Skip to content

Commit

Permalink
Merge pull request #16 from bonclay7/async_snap_delete
Browse files Browse the repository at this point in the history
[WIP] Async snaphots delete
  • Loading branch information
bonclay7 committed Aug 5, 2016
2 parents 6518a50 + c1c5285 commit b601f85
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
49 changes: 37 additions & 12 deletions amicleaner/cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env python
import boto3
import argparse
import sys

import boto3
from botocore.exceptions import ClientError
from prettytable import PrettyTable

from resources.config import MAPPING_KEY, MAPPING_VALUES, KEEP_PREVIOUS
from resources.config import TERM
from resources.models import AMI, AWSEC2Instance
Expand All @@ -28,14 +30,24 @@ def remove_amis(self, amis):
:param amis: array of AMI objects
"""

failed_snapshots = list()

amis = amis or []
for ami in amis:
self.ec2.deregister_image(ImageId=ami.id)
print "{0} deregistered".format(ami.id)
for block_device in ami.block_device_mappings:
self.ec2.delete_snapshot(SnapshotId=block_device.snapshot_id)
try:
self.ec2.delete_snapshot(
SnapshotId=block_device.snapshot_id
)
except ClientError:
failed_snapshots.append(block_device.snapshot_id)
print "{0} deleted\n".format(block_device.snapshot_id)

if failed_snapshots:
print_failed_snapshots(failed_snapshots)

return True

def remove_amis_from_ids(self, ami_ids):
Expand All @@ -57,9 +69,7 @@ def remove_amis_from_ids(self, ami_ids):
ami = AMI.object_with_json(image_json)
amis.append(ami)

self.remove_amis(amis)

return True
return self.remove_amis(amis)

def fetch_available_amis(self):

Expand Down Expand Up @@ -232,6 +242,7 @@ def parse_args(args):
parser.add_argument("--keep-previous",
dest='keep_previous',
type=int,
default=KEEP_PREVIOUS,
help="Number of previous AMI to keep excluding those"
"currently being running")

Expand Down Expand Up @@ -281,7 +292,8 @@ def fetch_and_prepare(mapping_strategy, keep_previous, full_report=False):


def print_report(candidates, full_report=False):
# print results

""" Print results """

if not candidates:
return
Expand All @@ -307,9 +319,22 @@ def print_report(candidates, full_report=False):
print groups_table.get_string(sortby="Group name")


def delete_amis(candidates, from_ids=False):
def print_failed_snapshots(failed_snapshots):

""" Print failed snapshots """

snap_table = PrettyTable()
snap_table.field_names = ["Failed Snapshots"]

for failed_snap in failed_snapshots:
snap_table.add_row(failed_snap)
print "\nFailed Snapshots:"
print snap_table


def prepare_delete_amis(candidates, from_ids=False):

""" delete candidates AMIs and related snapshots """
""" prepare deletion of candidates AMIs"""

if from_ids:
print TERM.bold("\nCleaning from {} AMI id(s) ...".format(
Expand All @@ -333,10 +358,10 @@ def main():
# defaults
mapping_key = args.mapping_key or MAPPING_KEY
mapping_values = args.mapping_values or MAPPING_VALUES
keep_previous = args.keep_previous or KEEP_PREVIOUS
keep_previous = args.keep_previous

if args.from_ids:
delete_amis(args.from_ids, True)
prepare_delete_amis(args.from_ids, True)
else:
# print defaults
print TERM.bold("Default values : ==>")
Expand All @@ -348,7 +373,7 @@ def main():

candidates = fetch_and_prepare(
mapping_strategy,
keep_previous,
args.keep_previous,
args.full_report
)

Expand All @@ -365,7 +390,7 @@ def main():
delete = True

if delete:
delete_amis(candidates)
prepare_delete_amis(candidates)


if __name__ == "__main__":
Expand Down
24 changes: 10 additions & 14 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,23 +247,19 @@ def test_reduce():
assert len(left) == 0


@mock_ec2
def test_remove_ami():
cleaner = AMICleaner()

with open("tests/mocks/ami.json") as mock_file:
json_to_parse = json.load(mock_file)
ami = AMI.object_with_json(json_to_parse)

assert cleaner.remove_amis(None) is True
# assert cleaner.remove_amis([ami]) is True
def test_remove_ami_from_none():
assert AMICleaner().remove_amis(None) is True


@mock_ec2
def test_remove_ami_from_ids():
cleaner = AMICleaner()
assert cleaner.remove_amis_from_ids(None) is False
# assert cleaner.remove_amis_from_ids(["ami-02197662"]) is True
pass
"""
ami_backend = AmiBackend()
ami_backend.create_image("instance-id", "linux")
print ami_backend.amis
assert AMICleaner().remove_amis_from_ids(["ami-02197662"]) is True
"""


def test_parse_args_no_args():
Expand All @@ -274,7 +270,7 @@ def test_parse_args_no_args():
assert parser.full_report is False
assert parser.mapping_key is None
assert parser.mapping_values is None
assert parser.keep_previous is None
assert parser.keep_previous is 4


def test_parse_args():
Expand Down

0 comments on commit b601f85

Please sign in to comment.