RDS cleanup snapshots command

parent f7c5611a
Pipeline #2097 failed with stages
in 1 minute and 14 seconds
......@@ -12,6 +12,7 @@ At the moment it only does three things; blue/green deploys for plugging into Gi
- [Cleanups](#cleanups)
- [AMIs](#amis)
- [EBS Volumes](#ebs-volumes)
- [RDS Snapshots](#rds-snapshots)
- [RDS](#rds)
- [Copy](#copy)
- [Container](#container)
......@@ -161,7 +162,7 @@ you think you are.
## Cleanups
Currently AMI and EBS cleanups are supported.
Currently AMI, EBS, and RDS snapshot cleanups are supported.
Common option:
......@@ -205,6 +206,16 @@ Delete all EBS volumes that are not attached to an instance (stopped or not):
--role-arns "arn:aws:iam::198765432100:role/management_assumable arn:aws:iam::123456789100:role/management_assumable" \
ebs
### RDS Snapshots
This will delete all snapshots tagged "akinaka-made":
akinaka.py cleanup \
--not-dry-run \
--region eu-west-1 \
--role-arns "arn:aws:iam::876521782800:role/OlinDataAssumedAdministrator" \
rds --tags "akinaka-made"
## RDS
Perform often necessary but complex tasks with RDS.
......
......@@ -60,3 +60,21 @@ def ebs(ctx):
except Exception as e:
logging.error(e)
exit(1)
@cleanup.command()
@click.option("--search-tags", required=True, help="Comma separated list of tags attached to snapshots to be deleted")
@click.pass_context
def rds(ctx, search_tags):
from .rds import cleanup_snapshots
region = ctx.obj.get('region')
not_dry_run = ctx.obj.get('not_dry_run')
role_arns = ctx.obj.get('role_arns')
role_arns = role_arns.split(" ")
try:
snapshots = cleanup_snapshots.CleanupSnapshots(region, role_arns, search_tags, not_dry_run)
snapshots.cleanup()
exit(0)
except Exception as e:
logging.error(e)
exit(1)
......@@ -42,7 +42,7 @@ class CleanupVolumes():
def cleanup(self):
for role in self.role_arns:
logging.error("\nProcessing account: {}".format(role))
logging.error("Processing account: {}".format(role))
volumes_to_delete = self.list_available_volumes(role)
if self.not_dry_run:
......@@ -52,6 +52,3 @@ class CleanupVolumes():
logging.info("These are the volumes I would have deleted if you gave me --not-dry-run:\n")
for volume in volumes_to_delete:
logging.info("Volume: {}\n".format(volume['VolumeId']))
#!/usr/bin/env python3
import boto3
from akinaka_client.aws_client import AWS_Client
from akinaka_libs import helpers
import logging
helpers.set_logger()
aws_client = AWS_Client()
class CleanupSnapshots():
def __init__(self, region, role_arns, search_tags, not_dry_run):
self.region = region
self.role_arns = role_arns
self.search_tags = search_tags
self.not_dry_run = not_dry_run
def list_tagged_snapshots(self, role_arn, search_tags):
rds_client = aws_client.create_client('rds', self.region, role_arn)
snapshots = rds_client.describe_db_snapshots()['DBSnapshots']
found_list = []
for snapshot in snapshots:
snapshot_arn = snapshot['DBSnapshotArn']
snapshot_id = snapshot['DBSnapshotIdentifier']
tags = rds_client.list_tags_for_resource(
ResourceName=snapshot_arn
)['TagList']
for tag in tags:
if [ snapshot_id for search_tag in search_tags if tag['Key'] == search_tag ] != []:
found_list.append(snapshot_id)
return found_list
def delete_snapshots(self, role_arn, snapshots_to_delete):
rds_client = aws_client.create_client('rds', self.region, role_arn)
for snapshot in snapshots_to_delete:
try:
logging.info(rds_client.delete_db_snapshot(DBSnapshotIdentifier=snapshot))
except rds_client.exceptions.InvalidDBSnapshotStateFault as e:
logging.error(
"Snapshot is not deletable, probably an automated snapshot:\n"
"{}".format(e)
)
def cleanup(self):
for role in self.role_arns:
logging.info("Processing account: {}".format(role))
snapshots_to_delete = self.list_tagged_snapshots(role, self.search_tags)
if self.not_dry_run:
logging.info("Deleting the following snapshots: {}".format(snapshots_to_delete))
self.delete_snapshots(role, snapshots_to_delete)
else:
logging.info("These are the snapshots I would have deleted if you gave me --not-dry-run: {}".format(snapshots_to_delete))
import click
from akinaka_libs import helpers
import logging
from akinaka_cleanup.rds import cleanup_snapshots
helpers.set_logger()
@click.group()
......@@ -30,8 +30,21 @@ def rds(ctx, source_role_arn, target_role_arn, snapshot_style, source_instance_n
region = ctx.obj.get('region')
try:
rds_copy = copy_rds.CopyRDS(region, source_role_arn, target_role_arn, snapshot_style, source_instance_name, overwrite_target, target_security_group, target_db_subnet, target_instance_name)
rds_copy = copy_rds.CopyRDS(region,
source_role_arn,
target_role_arn,
snapshot_style,
source_instance_name,
overwrite_target,
target_security_group,
target_db_subnet,
target_instance_name)
rds_copy.copy_instance()
logging.info("Will now delete useless snapshots")
snapshots = cleanup_snapshots.CleanupSnapshots(region, [source_role_arn], ["akinaka-made"], True)
snapshots.cleanup()
exit(0)
except Exception as e:
logging.error(e)
......
......@@ -6,7 +6,7 @@ with open("README.md", "r") as fh:
setuptools.setup(
name="akinaka",
python_requires='>=3.3.0',
version="0.2.24",
version="0.2.25",
author="Afraz",
author_email="afraz@olindata.com",
description="OlinData's aws cli Extras",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment