-
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.
- Loading branch information
Showing
6 changed files
with
61 additions
and
47 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,6 @@ repos: | |
hooks: | ||
- id: black | ||
- repo: https://github.com/pycqa/flake8 | ||
rev: 3.9.2 | ||
rev: 7.1.0 | ||
hooks: | ||
- id: flake8 |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
aws_data = ['VMS', 'NICS', 'DISCS', 'PIPS', 'RESOURCES', 'STACKS'] | ||
azure_data = ['VMS', 'NICS', 'DISCS', 'IMAGES', 'PIPS', 'RESOURCES'] | ||
gce_data = ['VMS', 'NICS', 'DISCS'] |
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 |
---|---|---|
@@ -1,53 +1,31 @@ | ||
"""GCE CR Cleanup Utilities""" | ||
from cloudwash.client import compute_client | ||
from cloudwash.config import settings | ||
from cloudwash.constants import gce_data as data | ||
from cloudwash.entities.providers import GCECleanup | ||
from cloudwash.logger import logger | ||
from cloudwash.utils import dry_data | ||
from cloudwash.utils import echo_dry | ||
from cloudwash.utils import gce_zones | ||
from cloudwash.utils import total_running_time | ||
|
||
|
||
def cleanup(**kwargs): | ||
is_dry_run = kwargs["dry_run"] | ||
|
||
is_dry_run = kwargs.get("dry_run", False) | ||
zones = settings.gce.auth.get('zones', ['all']) | ||
if "all" in zones: | ||
zones = gce_zones() | ||
if kwargs["nics"] or kwargs["_all"]: | ||
logger.warning("Cloudwash does not supports NICs operation for GCE yet!") | ||
if kwargs["discs"] or kwargs["_all"]: | ||
logger.warning("Cloudwash does not supports DISCs operation for GCE yet!") | ||
with compute_client("gce") as gce_client: | ||
if kwargs["vms"] or kwargs["_all"]: | ||
allvms = gce_client.list_vms(zones=gce_zones()) | ||
for vm in allvms: | ||
if vm.name in settings.gce.exceptions.vm.vm_list: | ||
dry_data["VMS"]["skip"].append(vm.name) | ||
continue | ||
elif total_running_time(vm).minutes >= settings.gce.criteria.vm.sla_minutes: | ||
if vm.name in settings.gce.exceptions.vm.stop_list: | ||
dry_data["VMS"]["stop"].append(vm.name) | ||
if not is_dry_run: | ||
try: | ||
vm.stop() | ||
except TypeError: | ||
logger.info(f"Stopped VM: {vm.name}") | ||
except Exception: | ||
logger.exception(f"Error stopping VM {vm.name}") | ||
continue | ||
elif vm.name.startswith(settings.gce.criteria.vm.delete_vm): | ||
dry_data["VMS"]["delete"].append(vm.name) | ||
if not is_dry_run: | ||
try: | ||
vm.delete() | ||
# There as an issue with GCE API while deleting/stopping the VM | ||
# That it throws TypeError, hence catching that error here | ||
# Remove it once its fixed | ||
except TypeError: | ||
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 GCE yet!" | ||
) | ||
if kwargs["discs"] or kwargs["_all"]: | ||
logger.warning( | ||
"Cloudwash dependency 'WrapanAPI' does not supports DISCs operation for GCE yet!" | ||
) | ||
if is_dry_run: | ||
echo_dry(dry_data) | ||
for zone in zones: | ||
for items in data: | ||
dry_data[items]['delete'] = [] | ||
gce_client.cleaning_zone = zone | ||
gcecleanup = GCECleanup(client=gce_client) | ||
logger.info(f"\nResources from the zone: {zone}") | ||
if kwargs["vms"] or kwargs["_all"]: | ||
gcecleanup.vms.cleanup() | ||
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