Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change -all flag to remove whole group #13

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_rg Remove resource group only 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_rg', '_all_rg', is_flag=True, help='Remove resource group only 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_rg):
# 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_rg=_all_rg, 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_rg']:
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:
laDok8 marked this conversation as resolved.
Show resolved Hide resolved
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