Skip to content

Commit

Permalink
Added settings_path flag for swach CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
bovem committed Apr 5, 2023
1 parent fb78112 commit 1e010a2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 32 deletions.
25 changes: 15 additions & 10 deletions cloudwash/cli.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"),
Expand Down Expand Up @@ -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 >>>>>>>"
Expand All @@ -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")
Expand All @@ -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,
Expand All @@ -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
)


Expand All @@ -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
)


Expand Down
4 changes: 3 additions & 1 deletion cloudwash/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

import wrapanapi

from cloudwash.config import settings
from cloudwash.config import generate_settings


@contextmanager
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,
Expand Down
31 changes: 19 additions & 12 deletions cloudwash/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions cloudwash/providers/aws.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
10 changes: 6 additions & 4 deletions cloudwash/providers/azure.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions cloudwash/providers/gce.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 1e010a2

Please sign in to comment.