From 1e010a25b9db505db83d53e186545dd70738a035 Mon Sep 17 00:00:00 2001 From: bovem Date: Wed, 5 Apr 2023 15:19:09 +0000 Subject: [PATCH] Added settings_path flag for swach CLI --- cloudwash/cli.py | 25 +++++++++++++++---------- cloudwash/client.py | 4 +++- cloudwash/config.py | 31 +++++++++++++++++++------------ cloudwash/providers/aws.py | 8 +++++--- cloudwash/providers/azure.py | 10 ++++++---- cloudwash/providers/gce.py | 5 +++-- 6 files changed, 51 insertions(+), 32 deletions(-) diff --git a/cloudwash/cli.py b/cloudwash/cli.py index 654d24391..a62e75209 100644 --- a/cloudwash/cli.py +++ b/cloudwash/cli.py @@ -1,6 +1,6 @@ import click -from cloudwash.config import settings +from cloudwash.config import generate_settings from cloudwash.config import validate_provider from cloudwash.logger import logger from cloudwash.providers.aws import cleanup as awsCleanup @@ -14,6 +14,7 @@ # Common Click utils _common_options = [ + click.option("-s", "--settings-path", type=str, help="Path to settings file", default=None), click.option("--vms", is_flag=True, help="Remove only unused VMs from the provider"), click.option("--discs", is_flag=True, help="Remove only unused DISCs from the provider"), click.option("--nics", is_flag=True, help="Remove only unused NICs from the provider"), @@ -48,7 +49,7 @@ def cleanup_providers(ctx, dry, version): cloudwash_version = pkg_resources.get_distribution("cloudwash").version click.echo(f"Version: {cloudwash_version}") - click.echo(f"Settings File: {settings.settings_file}") + # click.echo(f"Settings File: {settings.settings_file}") if ctx.invoked_subcommand: logger.info( f"\n<<<<<<< Running the cleanup script in {'DRY' if dry else 'ACTION'} RUN mode >>>>>>>" @@ -58,11 +59,12 @@ def cleanup_providers(ctx, dry, version): @cleanup_providers.command(help="Cleanup GCE provider") @common_options @click.pass_context -def gce(ctx, vms, discs, nics, _all): +def gce(ctx, settings_path, vms, discs, nics, _all): # Validate GCE Settings - validate_provider(ctx.command.name) + settings = generate_settings(settings_path) + validate_provider(ctx.command.name, settings) is_dry_run = ctx.parent.params["dry"] - gceCleanup(vms=vms, discs=discs, nics=nics, _all=_all, dry_run=is_dry_run) + gceCleanup(vms=vms, discs=discs, nics=nics, _all=_all, dry_run=is_dry_run, settings=settings) @cleanup_providers.command(help="Cleanup Azure provider") @@ -75,9 +77,10 @@ def gce(ctx, vms, discs, nics, _all): help="Remove resource group only if all resources are older than SLA", ) @click.pass_context -def azure(ctx, vms, discs, nics, pips, _all, _all_rg): +def azure(ctx, settings_path, vms, discs, nics, pips, _all, _all_rg): # Validate Azure Settings - validate_provider(ctx.command.name) + settings = generate_settings(settings_path) + validate_provider(ctx.command.name, settings) is_dry_run = ctx.parent.params["dry"] azureCleanup( vms=vms, @@ -87,6 +90,7 @@ def azure(ctx, vms, discs, nics, pips, _all, _all_rg): _all=_all, _all_rg=_all_rg, dry_run=is_dry_run, + settings=settings ) @@ -95,12 +99,13 @@ def azure(ctx, vms, discs, nics, pips, _all, _all_rg): @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 aws(ctx, vms, discs, nics, pips, stacks, _all): +def aws(ctx, settings_path, vms, discs, nics, pips, stacks, _all): # Validate Amazon Settings - validate_provider(ctx.command.name) + settings = generate_settings(settings_path) + validate_provider(ctx.command.name, settings) is_dry_run = ctx.parent.params["dry"] awsCleanup( - vms=vms, discs=discs, nics=nics, pips=pips, stacks=stacks, _all=_all, dry_run=is_dry_run + vms=vms, discs=discs, nics=nics, pips=pips, stacks=stacks, _all=_all, dry_run=is_dry_run, settings=settings ) diff --git a/cloudwash/client.py b/cloudwash/client.py index 3db7f46d9..4322fc33c 100644 --- a/cloudwash/client.py +++ b/cloudwash/client.py @@ -3,7 +3,7 @@ import wrapanapi -from cloudwash.config import settings +from cloudwash.config import generate_settings @contextmanager @@ -11,6 +11,8 @@ def compute_client(compute_resource, **kwargs): """The context manager for compute resource client to initiate and disconnect :param str compute_resource: The compute resource name """ + settings = kwargs["settings"] + if compute_resource == "azure": client = wrapanapi.AzureSystem( username=settings.azure.auth.client_id, diff --git a/cloudwash/config.py b/cloudwash/config.py index aa6ad0e2d..68308b842 100644 --- a/cloudwash/config.py +++ b/cloudwash/config.py @@ -6,20 +6,27 @@ from cloudwash.logger import logger -CURRENT_DIRECTORY = Path().resolve() -settings_file = PurePath(CURRENT_DIRECTORY, 'settings.yaml') -# Initialize and Configure Settings -settings = Dynaconf( - core_loaders=["YAML"], - envvar_prefix="CLEANUP", - settings_file=settings_file, - preload=["conf/*.yaml"], - envless_mode=True, - lowercase_read=True, -) +def generate_settings(settings_path): + if settings_path==None: + print("No settings file path specified using current directory as default.") + CURRENT_DIRECTORY = Path().resolve() + settings_file = PurePath(CURRENT_DIRECTORY, 'settings.yaml') + else: + settings_file = PurePath(settings_path) + # Initialize and Configure Settings + settings = Dynaconf( + core_loaders=["YAML"], + envvar_prefix="CLEANUP", + settings_file=settings_file, + preload=["conf/*.yaml"], + envless_mode=True, + lowercase_read=True, + ) + return settings -def validate_provider(provider_name): + +def validate_provider(provider_name, settings): provider = provider_name.upper() provider_settings = [ f"{provider}.{setting_key}" for setting_key in settings.to_dict().get(provider) diff --git a/cloudwash/providers/aws.py b/cloudwash/providers/aws.py index ef7f0b557..cf0a57726 100644 --- a/cloudwash/providers/aws.py +++ b/cloudwash/providers/aws.py @@ -1,6 +1,6 @@ """ec2 CR Cleanup Utilities""" from cloudwash.client import compute_client -from cloudwash.config import settings +from cloudwash.config import generate_settings from cloudwash.logger import logger from cloudwash.utils import dry_data from cloudwash.utils import echo_dry @@ -10,17 +10,19 @@ def cleanup(**kwargs): is_dry_run = kwargs["dry_run"] + settings = generate_settings(kwargs["settings_path"]) + data = ['VMS', 'NICS', 'DISCS', 'PIPS', 'RESOURCES', 'STACKS'] regions = settings.aws.auth.regions if "all" in regions: - with compute_client("aws", aws_region="us-west-2") as client: + with compute_client("aws", aws_region="us-west-2", settings=settings) as client: regions = client.list_regions() for region in regions: dry_data['VMS']['stop'] = [] dry_data['VMS']['skip'] = [] for items in data: dry_data[items]['delete'] = [] - with compute_client("aws", aws_region=region) as aws_client: + with compute_client("aws", aws_region=region, settings=settings) as aws_client: # Dry Data Collection Defs def dry_vms(): all_vms = aws_client.list_vms() diff --git a/cloudwash/providers/azure.py b/cloudwash/providers/azure.py index 6bb5b1db3..0ac975e91 100644 --- a/cloudwash/providers/azure.py +++ b/cloudwash/providers/azure.py @@ -1,6 +1,6 @@ """Azure CR Cleanup Utilities""" from cloudwash.client import compute_client -from cloudwash.config import settings +from cloudwash.config import generate_settings from cloudwash.logger import logger from cloudwash.utils import dry_data from cloudwash.utils import echo_dry @@ -10,6 +10,8 @@ def _dry_vms(all_vms): """Filters and returns running VMs to be deleted from all VMs""" _vms = {"stop": [], "delete": [], "skip": []} + settings = generate_settings(kwargs["settings_path"]) + for vm in all_vms: # Remove the VM that's in Failed state and cant perform in assessments if not vm.exists: @@ -46,14 +48,14 @@ def cleanup(**kwargs): if "all" in regions: # non-existent RG can be chosen for query # as it's never accessed and is only stored within wrapper - with compute_client("azure", azure_region="us-west", resource_group="foo") as azure_client: + with compute_client("azure", azure_region="us-west", resource_group="foo", settings=settings) as azure_client: regions = list(zip(*azure_client.list_region()))[0] for region in regions: if "all" in groups: # non-existent RG can be chosen for query # as it's never accessed and is only stored within wrapper - with compute_client("azure", azure_region=region, resource_group="foo") as azure_client: + with compute_client("azure", azure_region=region, resource_group="foo", settings=settings) as azure_client: groups = azure_client.list_resource_groups() for group in groups: @@ -62,7 +64,7 @@ def cleanup(**kwargs): for items in data: dry_data[items]['delete'] = [] - with compute_client("azure", azure_region=region, resource_group=group) as azure_client: + with compute_client("azure", azure_region=region, resource_group=group, settings=settings) as azure_client: # Dry Data Collection Defs def dry_vms(): all_vms = azure_client.list_vms() diff --git a/cloudwash/providers/gce.py b/cloudwash/providers/gce.py index 7e3f257f3..cf57ca10b 100644 --- a/cloudwash/providers/gce.py +++ b/cloudwash/providers/gce.py @@ -1,6 +1,6 @@ """GCE CR Cleanup Utilities""" from cloudwash.client import compute_client -from cloudwash.config import settings +from cloudwash.config import generate_settings from cloudwash.logger import logger from cloudwash.utils import dry_data from cloudwash.utils import echo_dry @@ -10,8 +10,9 @@ def cleanup(**kwargs): is_dry_run = kwargs["dry_run"] + settings = kwargs["settings"] - with compute_client("gce") as gce_client: + with compute_client("gce", settings=settings) as gce_client: if kwargs["vms"] or kwargs["_all"]: allvms = gce_client.list_vms(zones=gce_zones()) for vm in allvms: