Skip to content

Commit

Permalink
Merge pull request #114 from Gauravtalreja1/vmware-provider
Browse files Browse the repository at this point in the history
Add support for VM cleanup in VMware provider
  • Loading branch information
jyejare authored Nov 24, 2023
2 parents e452a29 + bfb6dd1 commit 7bf451a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cloudwash/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from cloudwash.providers.aws import cleanup as awsCleanup
from cloudwash.providers.azure import cleanup as azureCleanup
from cloudwash.providers.gce import cleanup as gceCleanup
from cloudwash.providers.vmware import cleanup as vmwareCleanup

# Adding the pythonpath for importing modules from cloudwash packages
# sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
Expand Down Expand Up @@ -119,7 +120,8 @@ def aws(ctx, vms, discs, nics, images, pips, stacks, _all):
@click.pass_context
def vmware(ctx, vms, discs, nics, _all):
validate_provider(ctx.command.name)
# TODO: Further TO_BE_IMPLEMENTED
is_dry_run = ctx.parent.params["dry"]
vmwareCleanup(vms=vms, discs=discs, nics=nics, _all=_all, dry_run=is_dry_run)


@cleanup_providers.command(help="Cleanup RHEV provider")
Expand Down
6 changes: 6 additions & 0 deletions cloudwash/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def compute_client(compute_resource, **kwargs):
password=settings.aws.auth.secret_key,
region=kwargs['aws_region'],
)
elif compute_resource == "vmware":
client = wrapanapi.VMWareSystem(
hostname=settings.vmware.auth.vcenter,
username=settings.vmware.auth.username,
password=settings.vmware.auth.password,
)
else:
raise ValueError(
f"{compute_resource} is an incorrect value. It should be one of azure or gce or ec2"
Expand Down
47 changes: 47 additions & 0 deletions cloudwash/providers/vmware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""VMware CR Cleanup Utilities"""
from cloudwash.client import compute_client
from cloudwash.config import settings
from cloudwash.logger import logger
from cloudwash.utils import dry_data
from cloudwash.utils import echo_dry
from cloudwash.utils import total_running_time


def cleanup(**kwargs):
is_dry_run = kwargs["dry_run"]

with compute_client("vmware") as client:
if kwargs["vms"] or kwargs["_all"]:
allvms = client.list_vms()
for vm in allvms:
if vm.name in settings.vmware.exceptions.vm.vm_list:
dry_data["VMS"]["skip"].append(vm.name)
continue
elif total_running_time(vm).minutes >= settings.vmware.criteria.vm.sla_minutes:
if vm.name in settings.vmware.exceptions.vm.stop_list:
dry_data["VMS"]["stop"].append(vm.name)
if not is_dry_run:
try:
vm.stop()
logger.info(f"Stopped VM: {vm.name}")
except Exception:
logger.exception(f"Error stopping VM {vm.name}")
continue
elif vm.name.startswith(settings.vmware.criteria.vm.delete_vm):
dry_data["VMS"]["delete"].append(vm.name)
if not is_dry_run:
try:
vm.delete()
logger.info(f"Deleted VM: {vm.name}")
except Exception:
logger.exception(f"Error deleting VM {vm.name}")
if kwargs["nics"] or kwargs["_all"]:
logger.warning(
"Cloudwash dependency 'WrapanAPI' does not supports NICs operation for VMware yet!"
)
if kwargs["discs"] or kwargs["_all"]:
logger.warning(
"Cloudwash dependency 'WrapanAPI' does not supports DISCs operation for VMware yet!"
)
if is_dry_run:
echo_dry(dry_data)
21 changes: 21 additions & 0 deletions conf/vmware.yaml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
VMWARE:
AUTH:
VCENTER: <VCENTER>
USERNAME: <USERNAME>
PASSWORD: <PASSWORD>!
CRITERIA:
VM:
# The VM to be deleted with prepend string, e.g VM name that starts with 'test'
DELETE_VM: 'test'
# Number of minutes the deletable VM should be allowed to live, e.g 120 minutes = 2 Hours
SLA_MINUTES: 120
DISC:
UNASSIGNED: True
NIC:
UNASSIGNED: True
EXCEPTIONS:
VM:
# VM names that would be skipped from cleanup
VM_LIST: ['VMware vCenter Server 7']
# VMs that would be stopped from current running state
STOP_LIST: []

0 comments on commit 7bf451a

Please sign in to comment.