Skip to content

Commit

Permalink
run save sandbox and delete saved sandbox in threads
Browse files Browse the repository at this point in the history
  • Loading branch information
saklar13 committed Aug 28, 2023
1 parent 2dde275 commit bee50f3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
50 changes: 21 additions & 29 deletions cloudshell/cp/vcenter/flows/save_restore_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
from collections.abc import Iterable
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager, suppress

import attr
Expand Down Expand Up @@ -68,17 +69,17 @@ def __attrs_post_init__(self):
self._on_task_progress = on_task_progress_check_if_cancelled(
self._cancellation_manager
)
self._dc = DcHandler.get_dc(self._resource_conf.default_datacenter, self._si)

def save_apps(self, save_actions: Iterable[SaveApp]) -> str:
dc = DcHandler.get_dc(self._resource_conf.default_datacenter, self._si)
results = [self._save_app(action, dc) for action in save_actions]
with ThreadPoolExecutor() as executor:
results = list(executor.map(self._save_app, save_actions))
return DriverResponse(results).to_driver_response_json()

def delete_saved_apps(self, delete_saved_app_actions: list[DeleteSavedApp]) -> str:
dc = DcHandler.get_dc(self._resource_conf.default_datacenter, self._si)
for action in delete_saved_app_actions:
self._delete_saved_app(action, dc)
self._delete_folders(delete_saved_app_actions, dc)
with ThreadPoolExecutor() as executor:
list(executor.map(self._delete_saved_app, delete_saved_app_actions))
self._delete_folders(delete_saved_app_actions)
results = [
DeleteSavedAppResult(action.actionId) for action in delete_saved_app_actions
]
Expand Down Expand Up @@ -123,41 +124,34 @@ def _validate_app_attrs(self, attrs: dict[str, str]):
VMFromVMDeployApp.ATTR_NAMES.behavior_during_save
)

@staticmethod
def _prepare_folders(
vm_location: str, dc: DcHandler, sandbox_id: str
) -> FolderHandler:
def _prepare_folders(self, vm_location: str, sandbox_id: str) -> FolderHandler:
folder_path = VcenterPath(vm_location)
folder_path.append(SAVED_SANDBOXES_FOLDER)
folder_path.append(sandbox_id)
return self._dc.get_or_create_vm_folder(folder_path)

return dc.get_or_create_vm_folder(folder_path)

@staticmethod
def _get_vm_resource_pool(
app_attrs: dict[str, str], dc: DcHandler
) -> ResourcePoolHandler:
def _get_vm_resource_pool(self, app_attrs: dict[str, str]) -> ResourcePoolHandler:
r_pool_name = app_attrs.get(VMFromVMDeployApp.ATTR_NAMES.vm_resource_pool)
cluster_name = app_attrs.get(VMFromVMDeployApp.ATTR_NAMES.vm_cluster)
compute_entity = dc.get_compute_entity(cluster_name)
compute_entity = self._dc.get_compute_entity(cluster_name)
return compute_entity.get_resource_pool(r_pool_name)

def _save_app(self, save_action: SaveApp, dc: DcHandler) -> SaveAppResult:
def _save_app(self, save_action: SaveApp) -> SaveAppResult:
logger.info(f"Starting save app {save_action.actionParams.sourceAppName}")
logger.debug(f"Save action model: {save_action}")
with self._cancellation_manager:
vm_uuid = save_action.actionParams.sourceVmUuid
sandbox_id = save_action.actionParams.savedSandboxId
vm = dc.get_vm_by_uuid(vm_uuid)
vm = self._dc.get_vm_by_uuid(vm_uuid)
app_attrs = self._get_app_attrs(save_action, str(vm.path))
vm_resource_pool = self._get_vm_resource_pool(app_attrs, dc)
vm_storage = dc.get_datastore(
vm_resource_pool = self._get_vm_resource_pool(app_attrs)
vm_storage = self._dc.get_datastore(
app_attrs[VMFromVMDeployApp.ATTR_NAMES.vm_storage]
)

with self._cancellation_manager:
vm_folder = self._prepare_folders(
app_attrs[VMFromVMDeployApp.ATTR_NAMES.vm_location], dc, sandbox_id
app_attrs[VMFromVMDeployApp.ATTR_NAMES.vm_location], sandbox_id
)

with self._behavior_during_save(vm, app_attrs):
Expand All @@ -173,7 +167,7 @@ def _save_app(self, save_action: SaveApp, dc: DcHandler) -> SaveAppResult:
net_actions = VMNetworkActions(
self._resource_conf, self._cancellation_manager
)
default_network = dc.get_network(self._resource_conf.holding_network)
default_network = self._dc.get_network(self._resource_conf.holding_network)
for vnic in cloned_vm.vnics:
network = vnic.network

Expand Down Expand Up @@ -227,23 +221,21 @@ def _clone_vm(
config_spec=None,
).execute()

def _delete_saved_app(self, action: DeleteSavedApp, dc: DcHandler):
def _delete_saved_app(self, action: DeleteSavedApp) -> None:
for artifact in action.actionParams.artifacts:
vm_uuid = artifact.artifactRef
with self._cancellation_manager:
try:
vm = dc.get_vm_by_uuid(vm_uuid)
vm = self._dc.get_vm_by_uuid(vm_uuid)
except VmNotFound:
continue
vm.power_off(soft=False, on_task_progress=self._on_task_progress)
vm.delete(self._on_task_progress)

def _delete_folders(
self, delete_saved_app_actions: list[DeleteSavedApp], dc: DcHandler
):
def _delete_folders(self, delete_saved_app_actions: list[DeleteSavedApp]) -> None:
path = VcenterPath(self._resource_conf.vm_location) + SAVED_SANDBOXES_FOLDER
try:
sandbox_folder = dc.get_vm_folder(path)
sandbox_folder = self._dc.get_vm_folder(path)
except FolderNotFound:
return

Expand Down
4 changes: 4 additions & 0 deletions cloudshell/cp/vcenter/handlers/cluster_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def ram_usage(self) -> UsageInfo:
def get_v_switch(self, name: str) -> VSwitchHandler:
...

@abstractmethod
def get_resource_pool(self, path: str | None) -> ResourcePoolHandler:
...


class ClusterHandler(BasicComputeEntityHandler):
_vc_obj: vim.ComputeResource | vim.ClusterComputeResource
Expand Down

0 comments on commit bee50f3

Please sign in to comment.