-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from Gauravtalreja1/vmware-provider
Add support for VM cleanup in VMware provider
- Loading branch information
Showing
4 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |