-
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 #116 from jyejare/jyejare-abstract-refactor
[Develop] Code Abstraction/Standardisation of Resource Types
- Loading branch information
Showing
5 changed files
with
121 additions
and
28 deletions.
There are no files selected for viewing
Empty file.
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,61 @@ | ||
from abc import ABC | ||
from abc import abstractmethod | ||
|
||
|
||
class ResourceCleanup(ABC): | ||
@abstractmethod | ||
def cleanup(self): | ||
pass | ||
|
||
@abstractmethod | ||
def list(self): | ||
pass | ||
|
||
@abstractmethod | ||
def _set_dry(self): | ||
pass | ||
|
||
|
||
class VMsCleanup(ResourceCleanup): | ||
@abstractmethod | ||
def list(self): | ||
pass | ||
|
||
@abstractmethod | ||
def cleanup(self): | ||
pass | ||
|
||
@abstractmethod | ||
def stop(self): | ||
pass | ||
|
||
@abstractmethod | ||
def remove(self): | ||
pass | ||
|
||
@abstractmethod | ||
def skip(self): | ||
pass | ||
|
||
@abstractmethod | ||
def _set_dry(self): | ||
pass | ||
|
||
|
||
class ResourceCleanupManager: | ||
def __init__(self): | ||
self.resources = [] | ||
|
||
def add(self, resource): | ||
self.resources.append(resource) | ||
|
||
def remove(self): | ||
for resource in self.resources: | ||
resource.remove() | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.remove() | ||
return False |
Empty file.
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,56 @@ | ||
from cloudwash.config import settings | ||
from cloudwash.entities.base import VMsCleanup | ||
from cloudwash.logger import logger | ||
from cloudwash.utils import dry_data | ||
from cloudwash.utils import total_running_time | ||
|
||
|
||
class CleanVMs(VMsCleanup): | ||
def __init__(self, awsclient): | ||
self.client = awsclient | ||
self._delete = [] | ||
self._stop = [] | ||
self._skip = [] | ||
self.list() | ||
|
||
def _set_dry(self): | ||
# VMsContainer = namedtuple('VMsCotainer', ['delete', 'stop', 'skip']) | ||
# return VMsContainer(self._delete, self._stop, self._skip) | ||
dry_data['VMS']['delete'] = self._delete | ||
dry_data['VMS']['stop'] = self._stop | ||
dry_data['VMS']['skip'] = self._skip | ||
|
||
def list(self): | ||
all_vms = self.client.list_vms() | ||
|
||
for vm in all_vms: | ||
if vm.name in settings.aws.exceptions.vm.vm_list: | ||
self._delete.append(vm.name) | ||
continue | ||
|
||
elif total_running_time(vm).minutes >= settings.aws.criteria.vm.sla_minutes: | ||
if vm.name in settings.aws.exceptions.vm.stop_list: | ||
self._stop.append(vm.name) | ||
continue | ||
|
||
elif vm.name.startswith(settings.aws.criteria.vm.delete_vm): | ||
self._delete.append(vm.name) | ||
self._set_dry() | ||
|
||
def remove(self): | ||
for vm_name in self._delete: | ||
self.client.get_vm(vm_name).delete() | ||
logger.info(f"Removed VMs: \n{self._delete}") | ||
|
||
def stop(self): | ||
for vm_name in self._stop: | ||
self.client.get_vm(vm_name).stop() | ||
logger.info(f"Stopped VMs: \n{self._stop}") | ||
|
||
def skip(self): | ||
logger.info(f"Skipped VMs: \n{self._skip}") | ||
|
||
def cleanup(self): | ||
self.remove() | ||
self.stop() | ||
self.skip() |
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