Skip to content

Commit

Permalink
add AWS CloudFormation resource deletion option
Browse files Browse the repository at this point in the history
  • Loading branch information
laDok8 committed Sep 8, 2022
1 parent 8a85f38 commit 6acd4b3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cloudwash/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ def azure(ctx, vms, discs, nics, pips, _all, _all_rg):
@cleanup_providers.command(help="Cleanup Amazon provider")
@common_options
@click.option("--pips", is_flag=True, help="Remove only Public IPs from the provider")
@click.option("--stacks", is_flag=True, help="Remove only CloudFormations from the provider")
@click.pass_context
def ec2(ctx, vms, discs, nics, pips, _all):
def ec2(ctx, vms, discs, nics, pips, stacks, _all):
# Validate Amazon Settings
validate_provider(ctx.command.name)
is_dry_run = ctx.parent.params["dry"]
ec2Cleanup(vms=vms, discs=discs, nics=nics, pips=pips, _all=_all, dry_run=is_dry_run)
ec2Cleanup(
vms=vms, discs=discs, nics=nics, pips=pips, stacks=stacks, _all=_all, dry_run=is_dry_run
)


@cleanup_providers.command(help="Cleanup VMWare provider")
Expand Down
21 changes: 21 additions & 0 deletions cloudwash/providers/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,27 @@ def dry_pips():
[dry_data["PIPS"]["delete"].append(dpip["AllocationId"]) for dpip in rpips]
return dry_data["PIPS"]["delete"]

def dry_stacks():
all_stacks = ec2_client.list_stacks()
[
dry_data['STACKS']['delete'].append(stack.name)
for stack in all_stacks
if total_running_time(stack).minutes >= settings.sla_minutes
and stack.name not in settings.providers.ec2.except_stack_list
]
return dry_data['STACKS']['delete']

# Remove / Stop VMs
def remove_vms(avms):
# Remove VMs
[ec2_client.get_vm(vm_name).delete() for vm_name in avms["delete"]]
# Stop VMs
[ec2_client.get_vm(vm_name).stop() for vm_name in avms["stop"]]

# Delete CloudFormations
def remove_stacks(stacks):
[ec2_client.get_stack(stack).delete() for stack in stacks]

# Actual Cleaning and dry execution
logger.info(f"\nResources from the region: {region}")
if kwargs["vms"] or kwargs["_all"]:
Expand All @@ -82,5 +96,12 @@ def remove_vms(avms):
if not is_dry_run:
ec2_client.remove_all_unused_ips()
logger.info(f"Removed PIPs: \n{rpips}")
if kwargs["stacks"] or kwargs["_all"]:
rstacks = dry_stacks()
if not is_dry_run:
remove_stacks(stacks=rstacks)
logger.info(
f"Removed following and all unused stacks from ec2 Cloud. \n{rstacks}"
)
if is_dry_run:
echo_dry(dry_data)
5 changes: 5 additions & 0 deletions cloudwash/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"DISCS": {"delete": []},
"PIPS": {"delete": []},
"RESOURCES": {"delete": []},
"STACKS": {"delete": []},
}
dry_data.update(_vms_dict)

Expand All @@ -30,6 +31,7 @@ def echo_dry(dry_data=None) -> None:
deletable_nics = dry_data["NICS"]["delete"]
deletable_pips = dry_data["PIPS"]["delete"] if "PIPS" in dry_data else None
deletable_resources = dry_data["RESOURCES"]["delete"]
deletable_stacks = dry_data["STACKS"]["delete"] if "STACKS" in dry_data else None
if deletable_vms or stopable_vms or skipped_vms:
logger.info(
f"VMs:\n\tDeletable: {deletable_vms}\n\tStoppable: {stopable_vms}\n\t"
Expand All @@ -43,6 +45,8 @@ def echo_dry(dry_data=None) -> None:
logger.info(f"PIPs:\n\tDeletable: {deletable_pips}")
if deletable_resources:
logger.info(f"RESOURCEs:\n\tDeletable: {deletable_resources}")
if deletable_stacks:
logger.info(f"STACKs:\n\tDeletable: {deletable_stacks}")
if not any(
[
deletable_vms,
Expand All @@ -51,6 +55,7 @@ def echo_dry(dry_data=None) -> None:
deletable_nics,
deletable_pips,
deletable_resources,
deletable_stacks,
]
):
logger.info("\nNo resources are eligible for cleanup!")
Expand Down
2 changes: 2 additions & 0 deletions settings.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ PROVIDERS:
EXCEPT_VM_LIST: []
# VMs that would be stopped from running states
EXCEPT_VM_STOP_LIST: []
# CloudFormations that would be skipped from cleanup
EXCEPT_STACK_LIST: []

0 comments on commit 6acd4b3

Please sign in to comment.