Skip to content

Commit

Permalink
change -all flag to remove whole group
Browse files Browse the repository at this point in the history
delete azure resource group if newest resource is older than SLA
  • Loading branch information
laDok8 committed Jun 16, 2022
1 parent cc40621 commit a7ee6a2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ Usage: swach azure [OPTIONS]
Cleanup Azure provider
Options:
--all Remove all unused Resources from the provider
--nics Remove only unused NICs from the provider
--discs Remove only unused DISCs from the provider
--vms Remove only unused VMs from the provider
--pips Remove only PiPs from the provider
--help Show this message and exit.
--all Remove all unused Resources from the provider
--all_if_older Remove provider group if all resources are older than SLA
--nics Remove only unused NICs from the provider
--discs Remove only unused DISCs from the provider
--vms Remove only unused VMs from the provider
--pips Remove only PiPs from the provider
--help Show this message and exit.
```

Expand All @@ -122,11 +123,11 @@ VMs:
Deletable: ['test-bvhoduliam']
Stoppable: ['foremanqe-nightly2']
DISCs:
Deletable: ['test-axodawttrw-nic0']
Deletable: ['test-bvhoduliam-osdisk']
NICs:
Deletable: ['test-axodawttrw-nic0']
PIPs:
Deletable: ['test-axodawttrw-nic0']
Deletable: ['test-axodawttrw-pip0']
====================================
```

Expand Down
5 changes: 3 additions & 2 deletions cloudwash/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ def gce(ctx, vms, discs, nics, _all):
@cleanup_providers.command(help='Cleanup Azure provider')
@common_options
@click.option('--pips', is_flag=True, help='Remove only PiPs from the provider')
@click.option('--all_if_older', '_all_if_older', is_flag=True, help='Remove provider group if all resources are older than SLA')
@click.pass_context
def azure(ctx, vms, discs, nics, pips, _all):
def azure(ctx, vms, discs, nics, pips, _all, _all_if_older):
# Validate Azure Settings
validate_provider(ctx.command.name)
is_dry_run = ctx.parent.params['dry']
azureCleanup(
vms=vms, discs=discs, nics=nics, pips=pips, _all=_all, dry_run=is_dry_run)
vms=vms, discs=discs, nics=nics, pips=pips, _all=_all, _all_if_older=_all_if_older, dry_run=is_dry_run)


@cleanup_providers.command(help='Cleanup Amazon provider')
Expand Down
9 changes: 9 additions & 0 deletions cloudwash/providers/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def dry_pips():
[dry_data['PIPS']['delete'].append(dpip) for dpip in rpips]
return rpips

def dry_resources(hours_old=None):
dry_data['RESOURCES']['delete'] = azure_client.list_resources_from_hours_old(hours_old=hours_old or (settings.sla_minutes / 60))
return dry_data['RESOURCES']['delete']

# Remove / Stop VMs
def remove_vms(avms):
# Remove VMs
Expand Down Expand Up @@ -83,5 +87,10 @@ def remove_vms(avms):
if not is_dry_run:
azure_client.remove_pips_by_search()
logger.info(f'Removed following and all unused pips from Azure Cloud. \n{rpips}')
if kwargs['_all_if_older']:
rres = dry_resources()
if not is_dry_run:
azure_client.remove_resource_group_of_old_resources(hours_old=(settings.sla_minutes / 60))
logger.info(f'Removed following and all unused resources from Azure Cloud. \n{rres}')
if is_dry_run:
echo_dry(dry_data)
7 changes: 5 additions & 2 deletions cloudwash/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytz

_vms_dict = {'VMS': {'delete': [], 'stop': []}}
dry_data = {'NICS': {'delete': []}, 'DISCS': {'delete': []}, 'PIPS': {'delete': []}}
dry_data = {'NICS': {'delete': []}, 'DISCS': {'delete': []}, 'PIPS': {'delete': []}, 'RESOURCES': {'delete': []}}
dry_data.update(_vms_dict)


Expand All @@ -22,6 +22,7 @@ def echo_dry(dry_data=None) -> None:
deletable_discs = dry_data["DISCS"]['delete']
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']

if deletable_vms or stopable_vms:
logger.info(f'VMs:\n\tDeletable: {deletable_vms}\n\tStoppable: {stopable_vms}')
Expand All @@ -31,7 +32,9 @@ def echo_dry(dry_data=None) -> None:
logger.info(f'NICs:\n\tDeletable: {deletable_nics}')
if deletable_pips:
logger.info(f'PIPs:\n\tDeletable: {deletable_pips}')
if not any([deletable_vms, stopable_vms, deletable_discs, deletable_nics, deletable_pips]):
if deletable_resources:
logger.info(f'RESOURCEs:\n\tDeletable: {deletable_resources}')
if not any([deletable_vms, stopable_vms, deletable_discs, deletable_nics, deletable_pips, deletable_resources]):
logger.info('No resources are eligible for cleanup!')
logger.info('\n====================================\n')

Expand Down

0 comments on commit a7ee6a2

Please sign in to comment.