Skip to content

Commit

Permalink
Add support for deleting resources from all regions in AWS
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamsg199 committed Jun 15, 2022
1 parent cc40621 commit 01405e3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 56 deletions.
4 changes: 2 additions & 2 deletions cloudwash/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@contextmanager
def compute_client(compute_resource):
def compute_client(compute_resource, ec2_region):
"""The context manager for compute resource client to initiate and disconnect
:param str compute_resource: The compute resource name
Expand All @@ -29,7 +29,7 @@ def compute_client(compute_resource):
client = wrapanapi.EC2System(
username=settings.providers.ec2.username,
password=settings.providers.ec2.password,
region=settings.providers.ec2.region
region= ec2_region
)
else:
raise ValueError(
Expand Down
108 changes: 55 additions & 53 deletions cloudwash/providers/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,63 @@ def cleanup(**kwargs):

is_dry_run = kwargs['dry_run']

with compute_client('ec2') as ec2_client:
# Dry Data Collection Defs
def dry_vms():
all_vms = ec2_client.list_vms()
for vm in all_vms:
if vm.name.startswith(settings.delete_vm) and total_running_time(vm).minutes >= settings.sla_minutes:
if vm.name in settings.providers.ec2.except_vm_stop_list:
dry_data['VMS']['stop'].append(vm.name)
continue
dry_data['VMS']['delete'].append(vm.name)
return dry_data['VMS']
for region in settings.providers.ec2.region:
with compute_client('ec2',ec2_region=region) as ec2_client:
# Dry Data Collection Defs
def dry_vms():
all_vms = ec2_client.list_vms()
for vm in all_vms:
if vm.name.startswith(settings.delete_vm) and total_running_time(vm).minutes >= settings.sla_minutes:
if vm.name in settings.providers.ec2.except_vm_stop_list:
dry_data['VMS']['stop'].append(vm.name)
continue
dry_data['VMS']['delete'].append(vm.name)
return dry_data['VMS']

def dry_nics():
rnics = ec2_client.get_all_unused_network_interfaces()
[dry_data['NICS']['delete'].append(dnic['NetworkInterfaceId']) for dnic in rnics]
return dry_data['NICS']['delete']
def dry_nics():
rnics = ec2_client.get_all_unused_network_interfaces()
[dry_data['NICS']['delete'].append(dnic['NetworkInterfaceId']) for dnic in rnics]
return dry_data['NICS']['delete']

def dry_discs():
rdiscs = ec2_client.get_all_unattached_volumes()
[dry_data['DISCS']['delete'].append(ddisc['VolumeId']) for ddisc in rdiscs]
return dry_data['DISCS']['delete']
def dry_discs():
rdiscs = ec2_client.get_all_unattached_volumes()
[dry_data['DISCS']['delete'].append(ddisc['VolumeId']) for ddisc in rdiscs]
return dry_data['DISCS']['delete']

def dry_pips():
rpips = ec2_client.get_all_disassociated_addresses()
[dry_data['PIPS']['delete'].append(dpip['AllocationId']) for dpip in rpips]
return dry_data['PIPS']['delete']
def dry_pips():
rpips = ec2_client.get_all_disassociated_addresses()
[dry_data['PIPS']['delete'].append(dpip['AllocationId']) for dpip in rpips]
return dry_data['PIPS']['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']]
# 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']]

# Actual Cleaning and dry execution
if kwargs['vms'] or kwargs['_all']:
avms = dry_vms()
if not is_dry_run:
remove_vms(avms=avms)
logger.info(
f"Stopped {avms['stop']} and removed {avms['delete']} VMs from ec2 Cloud.")
if kwargs['nics'] or kwargs['_all']:
rnics = dry_nics()
if not is_dry_run:
ec2_client.remove_all_unused_nics()
logger.info(f'Removed following and all unused nics from ec2 Cloud. \n{rnics}')
if kwargs['discs'] or kwargs['_all']:
rdiscs = dry_discs()
if not is_dry_run:
ec2_client.remove_all_unused_volumes()
logger.info(f'Removed following and all unused discs from ec2 Cloud. \n{rdiscs}')
if kwargs['pips'] or kwargs['_all']:
rpips = dry_pips()
if not is_dry_run:
ec2_client.remove_all_unused_ips()
logger.info(f'Removed following and all unused pips from ec2 Cloud. \n{rpips}')
if is_dry_run:
echo_dry(dry_data)
# Actual Cleaning and dry execution
logger.info(f"\nCleaning resources from the region: {region}")
if kwargs['vms'] or kwargs['_all']:
avms = dry_vms()
if not is_dry_run:
remove_vms(avms=avms)
logger.info(
f"Stopped {avms['stop']} and removed {avms['delete']} VMs from ec2 Cloud.")
if kwargs['nics'] or kwargs['_all']:
rnics = dry_nics()
if not is_dry_run:
ec2_client.remove_all_unused_nics()
logger.info(f'Removed following and all unused nics from ec2 Cloud. \n{rnics}')
if kwargs['discs'] or kwargs['_all']:
rdiscs = dry_discs()
if not is_dry_run:
ec2_client.remove_all_unused_volumes()
logger.info(f'Removed following and all unused discs from ec2 Cloud. \n{rdiscs}')
if kwargs['pips'] or kwargs['_all']:
rpips = dry_pips()
if not is_dry_run:
ec2_client.remove_all_unused_ips()
logger.info(f'Removed following and all unused pips from ec2 Cloud. \n{rpips}')
if is_dry_run:
echo_dry(dry_data)
2 changes: 1 addition & 1 deletion settings.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ PROVIDERS:
EC2:
USERNAME:
PASSWORD:
REGION:
REGION: []
EXCEPT_VM_STOP_LIST: []

0 comments on commit 01405e3

Please sign in to comment.